mirror of
https://github.com/superseriousbusiness/gotosocial.git
synced 2025-11-09 13:37:30 -06:00
[chore] Add Go runtime and host metrics (#4137)
Daenney is a dummy and forgot to add these when he revamped the OTEL stuff. Reviewed-on: https://codeberg.org/superseriousbusiness/gotosocial/pulls/4137 Co-authored-by: Daenney <daenney@noreply.codeberg.org> Co-committed-by: Daenney <daenney@noreply.codeberg.org>
This commit is contained in:
parent
4a6b357501
commit
90a5425fe9
14 changed files with 1124 additions and 0 deletions
120
vendor/go.opentelemetry.io/contrib/instrumentation/runtime/producer.go
generated
vendored
Normal file
120
vendor/go.opentelemetry.io/contrib/instrumentation/runtime/producer.go
generated
vendored
Normal file
|
|
@ -0,0 +1,120 @@
|
|||
// Copyright The OpenTelemetry Authors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
package runtime // import "go.opentelemetry.io/contrib/instrumentation/runtime"
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"math"
|
||||
"runtime/metrics"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"go.opentelemetry.io/otel/attribute"
|
||||
"go.opentelemetry.io/otel/sdk/instrumentation"
|
||||
"go.opentelemetry.io/otel/sdk/metric"
|
||||
"go.opentelemetry.io/otel/sdk/metric/metricdata"
|
||||
)
|
||||
|
||||
var startTime time.Time
|
||||
|
||||
func init() {
|
||||
startTime = time.Now()
|
||||
}
|
||||
|
||||
var histogramMetrics = []string{goSchedLatencies}
|
||||
|
||||
// Producer is a metric.Producer, which provides precomputed histogram metrics from the go runtime.
|
||||
type Producer struct {
|
||||
lock sync.Mutex
|
||||
collector *goCollector
|
||||
}
|
||||
|
||||
var _ metric.Producer = (*Producer)(nil)
|
||||
|
||||
// NewProducer creates a Producer which provides precomputed histogram metrics from the go runtime.
|
||||
func NewProducer(opts ...ProducerOption) *Producer {
|
||||
c := newProducerConfig(opts...)
|
||||
return &Producer{
|
||||
collector: newCollector(c.MinimumReadMemStatsInterval, histogramMetrics),
|
||||
}
|
||||
}
|
||||
|
||||
// Produce returns precomputed histogram metrics from the go runtime, or an error if unsuccessful.
|
||||
func (p *Producer) Produce(context.Context) ([]metricdata.ScopeMetrics, error) {
|
||||
p.lock.Lock()
|
||||
p.collector.refresh()
|
||||
schedHist := p.collector.getHistogram(goSchedLatencies)
|
||||
p.lock.Unlock()
|
||||
// Use the last collection time (which may or may not be now) for the timestamp.
|
||||
histDp := convertRuntimeHistogram(schedHist, p.collector.lastCollect)
|
||||
if len(histDp) == 0 {
|
||||
return nil, errors.New("unable to obtain go.schedule.duration metric from the runtime")
|
||||
}
|
||||
return []metricdata.ScopeMetrics{
|
||||
{
|
||||
Scope: instrumentation.Scope{
|
||||
Name: ScopeName,
|
||||
Version: Version(),
|
||||
},
|
||||
Metrics: []metricdata.Metrics{
|
||||
{
|
||||
Name: "go.schedule.duration",
|
||||
Description: "The time goroutines have spent in the scheduler in a runnable state before actually running.",
|
||||
Unit: "s",
|
||||
Data: metricdata.Histogram[float64]{
|
||||
Temporality: metricdata.CumulativeTemporality,
|
||||
DataPoints: histDp,
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
}, nil
|
||||
}
|
||||
|
||||
var emptySet = attribute.EmptySet()
|
||||
|
||||
func convertRuntimeHistogram(runtimeHist *metrics.Float64Histogram, ts time.Time) []metricdata.HistogramDataPoint[float64] {
|
||||
if runtimeHist == nil {
|
||||
return nil
|
||||
}
|
||||
bounds := runtimeHist.Buckets
|
||||
counts := runtimeHist.Counts
|
||||
if len(bounds) < 2 {
|
||||
// runtime histograms are guaranteed to have at least two bucket boundaries.
|
||||
return nil
|
||||
}
|
||||
// trim the first bucket since it is a lower bound. OTel histogram boundaries only have an upper bound.
|
||||
bounds = bounds[1:]
|
||||
if bounds[len(bounds)-1] == math.Inf(1) {
|
||||
// trim the last bucket if it is +Inf, since the +Inf boundary is implicit in OTel.
|
||||
bounds = bounds[:len(bounds)-1]
|
||||
} else {
|
||||
// if the last bucket is not +Inf, append an extra zero count since
|
||||
// the implicit +Inf bucket won't have any observations.
|
||||
counts = append(counts, 0)
|
||||
}
|
||||
count := uint64(0)
|
||||
sum := float64(0)
|
||||
for i, c := range counts {
|
||||
count += c
|
||||
// This computed sum is an underestimate, since it assumes each
|
||||
// observation happens at the bucket's lower bound.
|
||||
if i > 0 && count != 0 {
|
||||
sum += bounds[i-1] * float64(count)
|
||||
}
|
||||
}
|
||||
|
||||
return []metricdata.HistogramDataPoint[float64]{
|
||||
{
|
||||
StartTime: startTime,
|
||||
Count: count,
|
||||
Sum: sum,
|
||||
Time: ts,
|
||||
Bounds: bounds,
|
||||
BucketCounts: counts,
|
||||
Attributes: *emptySet,
|
||||
},
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue