| 1 |
// Copyright The OpenTelemetry Authors |
| 2 |
// SPDX-License-Identifier: Apache-2.0 |
| 3 |
|
| 4 |
package runtime // import "go.opentelemetry.io/contrib/instrumentation/runtime" |
| 5 |
|
| 6 |
import ( |
| 7 |
"context" |
| 8 |
"errors" |
| 9 |
"math" |
| 10 |
"runtime/metrics" |
| 11 |
"sync" |
| 12 |
"time" |
| 13 |
|
| 14 |
"go.opentelemetry.io/otel/attribute" |
| 15 |
"go.opentelemetry.io/otel/sdk/instrumentation" |
| 16 |
"go.opentelemetry.io/otel/sdk/metric" |
| 17 |
"go.opentelemetry.io/otel/sdk/metric/metricdata" |
| 18 |
) |
| 19 |
|
| 20 |
var startTime time.Time |
| 21 |
|
| 22 |
func init() { |
| 23 |
startTime = time.Now() |
| 24 |
} |
| 25 |
|
| 26 |
var histogramMetrics = []string{goSchedLatencies} |
| 27 |
|
| 28 |
// Producer is a metric.Producer, which provides precomputed histogram metrics from the go runtime. |
| 29 |
type Producer struct { |
| 30 |
lock sync.Mutex |
| 31 |
collector *goCollector |
| 32 |
} |
| 33 |
|
| 34 |
var _ metric.Producer = (*Producer)(nil) |
| 35 |
|
| 36 |
// NewProducer creates a Producer which provides precomputed histogram metrics from the go runtime. |
| 37 |
// |
| 38 |
// Metrics emitted by NewProducer include: |
| 39 |
// |
| 40 |
// go.schedule.duration s The time goroutines have spent in the scheduler in a runnable state before actually running. |
| 41 |
func NewProducer(opts ...ProducerOption) *Producer { |
| 42 |
c := newProducerConfig(opts...) |
| 43 |
return &Producer{ |
| 44 |
collector: newCollector(c.MinimumReadMemStatsInterval, histogramMetrics), |
| 45 |
} |
| 46 |
} |
| 47 |
|
| 48 |
// Produce returns precomputed histogram metrics from the go runtime, or an error if unsuccessful. |
| 49 |
func (p *Producer) Produce(context.Context) ([]metricdata.ScopeMetrics, error) { |
| 50 |
p.lock.Lock() |
| 51 |
p.collector.refresh() |
| 52 |
schedHist := p.collector.getHistogram(goSchedLatencies) |
| 53 |
p.lock.Unlock() |
| 54 |
// Use the last collection time (which may or may not be now) for the timestamp. |
| 55 |
histDp := convertRuntimeHistogram(schedHist, p.collector.lastCollect) |
| 56 |
if len(histDp) == 0 { |
| 57 |
return nil, errors.New("unable to obtain go.schedule.duration metric from the runtime") |
| 58 |
} |
| 59 |
return []metricdata.ScopeMetrics{ |
| 60 |
{ |
| 61 |
Scope: instrumentation.Scope{ |
| 62 |
Name: ScopeName, |
| 63 |
Version: Version, |
| 64 |
}, |
| 65 |
Metrics: []metricdata.Metrics{ |
| 66 |
{ |
| 67 |
Name: "go.schedule.duration", |
| 68 |
Description: "The time goroutines have spent in the scheduler in a runnable state before actually running.", |
| 69 |
Unit: "s", |
| 70 |
Data: metricdata.Histogram[float64]{ |
| 71 |
Temporality: metricdata.CumulativeTemporality, |
| 72 |
DataPoints: histDp, |
| 73 |
}, |
| 74 |
}, |
| 75 |
}, |
| 76 |
}, |
| 77 |
}, nil |
| 78 |
} |
| 79 |
|
| 80 |
var emptySet = attribute.EmptySet() |
| 81 |
|
| 82 |
func convertRuntimeHistogram(runtimeHist *metrics.Float64Histogram, ts time.Time) []metricdata.HistogramDataPoint[float64] { |
| 83 |
if runtimeHist == nil { |
| 84 |
return nil |
| 85 |
} |
| 86 |
bounds := runtimeHist.Buckets |
| 87 |
counts := runtimeHist.Counts |
| 88 |
if len(bounds) < 2 { |
| 89 |
// runtime histograms are guaranteed to have at least two bucket boundaries. |
| 90 |
return nil |
| 91 |
} |
| 92 |
// trim the first bucket since it is a lower bound. OTel histogram boundaries only have an upper bound. |
| 93 |
bounds = bounds[1:] |
| 94 |
if bounds[len(bounds)-1] == math.Inf(1) { |
| 95 |
// trim the last bucket if it is +Inf, since the +Inf boundary is implicit in OTel. |
| 96 |
bounds = bounds[:len(bounds)-1] |
| 97 |
} else { |
| 98 |
// if the last bucket is not +Inf, append an extra zero count since |
| 99 |
// the implicit +Inf bucket won't have any observations. |
| 100 |
counts = append(counts, 0) |
| 101 |
} |
| 102 |
count := uint64(0) |
| 103 |
sum := float64(0) |
| 104 |
for i, c := range counts { |
| 105 |
count += c |
| 106 |
// This computed sum is an underestimate, since it assumes each |
| 107 |
// observation happens at the bucket's lower bound. |
| 108 |
if i > 0 && count != 0 { |
| 109 |
sum += bounds[i-1] * float64(count) |
| 110 |
} |
| 111 |
} |
| 112 |
|
| 113 |
return []metricdata.HistogramDataPoint[float64]{ |
| 114 |
{ |
| 115 |
StartTime: startTime, |
| 116 |
Count: count, |
| 117 |
Sum: sum, |
| 118 |
Time: ts, |
| 119 |
Bounds: bounds, |
| 120 |
BucketCounts: counts, |
| 121 |
Attributes: *emptySet, |
| 122 |
}, |
| 123 |
} |
| 124 |
} |