(mp metric.MeterProvider, attrs []attribute.KeyValue)
| 74 | } |
| 75 | |
| 76 | func startCobraCommandTimer(mp metric.MeterProvider, attrs []attribute.KeyValue) func(err error) { |
| 77 | meter := getDefaultMeter(mp) |
| 78 | durationCounter, _ := meter.Float64Counter( |
| 79 | "command.time", |
| 80 | metric.WithDescription("Measures the duration of the cobra command"), |
| 81 | metric.WithUnit("ms"), |
| 82 | ) |
| 83 | start := time.Now() |
| 84 | |
| 85 | return func(err error) { |
| 86 | // Use a new context for the export so that the command being cancelled |
| 87 | // doesn't affect the metrics, and we get metrics for cancelled commands. |
| 88 | ctx, cancel := context.WithTimeout(context.Background(), exportTimeout) |
| 89 | defer cancel() |
| 90 | |
| 91 | duration := float64(time.Since(start)) / float64(time.Millisecond) |
| 92 | cmdStatusAttrs := attributesFromError(err) |
| 93 | durationCounter.Add(ctx, duration, |
| 94 | metric.WithAttributes(attrs...), |
| 95 | metric.WithAttributes(cmdStatusAttrs...), |
| 96 | ) |
| 97 | if mp, ok := mp.(MeterProvider); ok { |
| 98 | if err := mp.ForceFlush(ctx); err != nil { |
| 99 | otel.Handle(err) |
| 100 | } |
| 101 | } |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | func stdioAttributes(streams Streams) []attribute.KeyValue { |
| 106 | return []attribute.KeyValue{ |
no test coverage detected
searching dependent graphs…