Skip to content

Commit ced7a84

Browse files
chore(metrics): Add metrics & traces to DB migration (grafana#97181)
Signed-off-by: Dave Henderson <dave.henderson@grafana.com>
1 parent 0025876 commit ced7a84

File tree

4 files changed

+216
-69
lines changed

4 files changed

+216
-69
lines changed

pkg/infra/metrics/metricutil/utils.go

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
package metricutil
22

33
import (
4+
"context"
45
"errors"
56
"fmt"
67
"strings"
8+
"time"
79

810
"github.com/prometheus/client_golang/prometheus"
11+
"go.opentelemetry.io/otel/trace"
912
)
1013

1114
// SanitizeLabelName removes all invalid chars from the label name.
@@ -84,3 +87,35 @@ func copyLabelSet(ls prometheus.Labels) prometheus.Labels {
8487
}
8588
return newLs
8689
}
90+
91+
// wraps prometheus.NewHistogram, adding a central native histogram config
92+
func NewHistogramVec(opts prometheus.HistogramOpts, labels []string) *prometheus.HistogramVec {
93+
if opts.NativeHistogramBucketFactor == 0 {
94+
// Enable native histograms, with the factor suggested in the docs
95+
opts.NativeHistogramBucketFactor = 1.1
96+
}
97+
if opts.NativeHistogramMaxBucketNumber == 0 {
98+
// OTel default
99+
opts.NativeHistogramMaxBucketNumber = 160
100+
}
101+
if opts.NativeHistogramMinResetDuration == 0 {
102+
// Reset buckets every hour by default - override this if you want to
103+
// keep buckets around for longer
104+
opts.NativeHistogramMinResetDuration = 1 * time.Hour
105+
}
106+
107+
return prometheus.NewHistogramVec(opts, labels)
108+
}
109+
110+
func ObserveWithExemplar(ctx context.Context, histogram prometheus.Observer, value float64) {
111+
traceID := trace.SpanContextFromContext(ctx).TraceID()
112+
if traceID.IsValid() {
113+
histogram.(prometheus.ExemplarObserver).ObserveWithExemplar(
114+
value,
115+
prometheus.Labels{"traceID": traceID.String()},
116+
)
117+
return
118+
}
119+
120+
histogram.Observe(value)
121+
}

pkg/infra/tracing/tracing_config.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,9 @@ import (
55
"os"
66
"strings"
77

8-
"github.com/grafana/grafana/pkg/setting"
98
"go.opentelemetry.io/otel/attribute"
9+
10+
"github.com/grafana/grafana/pkg/setting"
1011
)
1112

1213
type TracingConfig struct {

0 commit comments

Comments
 (0)