summaryrefslogtreecommitdiff
path: root/vendor/github.com/uptrace/bun/extra/bunotel/otel.go
diff options
context:
space:
mode:
Diffstat (limited to 'vendor/github.com/uptrace/bun/extra/bunotel/otel.go')
-rw-r--r--vendor/github.com/uptrace/bun/extra/bunotel/otel.go33
1 files changed, 18 insertions, 15 deletions
diff --git a/vendor/github.com/uptrace/bun/extra/bunotel/otel.go b/vendor/github.com/uptrace/bun/extra/bunotel/otel.go
index b9737b0bc..d0672e7c2 100644
--- a/vendor/github.com/uptrace/bun/extra/bunotel/otel.go
+++ b/vendor/github.com/uptrace/bun/extra/bunotel/otel.go
@@ -20,20 +20,12 @@ import (
"github.com/uptrace/opentelemetry-go-extra/otelsql"
)
-var (
- tracer = otel.Tracer("github.com/uptrace/bun")
- meter = otel.Meter("github.com/uptrace/bun")
-
- queryHistogram, _ = meter.Int64Histogram(
- "go.sql.query_timing",
- metric.WithDescription("Timing of processed queries"),
- metric.WithUnit("milliseconds"),
- )
-)
-
type QueryHook struct {
- attrs []attribute.KeyValue
- formatQueries bool
+ attrs []attribute.KeyValue
+ formatQueries bool
+ tracer trace.Tracer
+ meter metric.Meter
+ queryHistogram metric.Int64Histogram
}
var _ bun.QueryHook = (*QueryHook)(nil)
@@ -43,6 +35,17 @@ func NewQueryHook(opts ...Option) *QueryHook {
for _, opt := range opts {
opt(h)
}
+ if h.tracer == nil {
+ h.tracer = otel.Tracer("github.com/uptrace/bun")
+ }
+ if h.meter == nil {
+ h.meter = otel.Meter("github.com/uptrace/bun")
+ }
+ h.queryHistogram, _ = h.meter.Int64Histogram(
+ "go.sql.query_timing",
+ metric.WithDescription("Timing of processed queries"),
+ metric.WithUnit("milliseconds"),
+ )
return h
}
@@ -57,7 +60,7 @@ func (h *QueryHook) Init(db *bun.DB) {
}
func (h *QueryHook) BeforeQuery(ctx context.Context, event *bun.QueryEvent) context.Context {
- ctx, _ = tracer.Start(ctx, "", trace.WithSpanKind(trace.SpanKindClient))
+ ctx, _ = h.tracer.Start(ctx, "", trace.WithSpanKind(trace.SpanKindClient))
return ctx
}
@@ -75,7 +78,7 @@ func (h *QueryHook) AfterQuery(ctx context.Context, event *bun.QueryEvent) {
}
dur := time.Since(event.StartTime)
- queryHistogram.Record(ctx, dur.Milliseconds(), metric.WithAttributes(labels...))
+ h.queryHistogram.Record(ctx, dur.Milliseconds(), metric.WithAttributes(labels...))
span := trace.SpanFromContext(ctx)
if !span.IsRecording() {