MCPcopy Index your code
hub / github.com/docker/cli / startCobraCommandTimer

Function startCobraCommandTimer

cli/command/telemetry_utils.go:76–103  ·  view source on GitHub ↗
(mp metric.MeterProvider, attrs []attribute.KeyValue)

Source from the content-addressed store, hash-verified

74}
75
76func 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
105func stdioAttributes(streams Streams) []attribute.KeyValue {
106 return []attribute.KeyValue{

Callers 1

StartInstrumentationMethod · 0.85

Calls 3

getDefaultMeterFunction · 0.85
attributesFromErrorFunction · 0.85
ForceFlushMethod · 0.65

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…