Skip to content

Commit

Permalink
feat(bunotel): ability to override span names
Browse files Browse the repository at this point in the history
  • Loading branch information
NathanBaulch committed Feb 8, 2025
1 parent e8c87a9 commit 04e2125
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 6 deletions.
9 changes: 9 additions & 0 deletions extra/bunotel/option.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package bunotel

import (
"github.com/uptrace/bun"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/metric"
semconv "go.opentelemetry.io/otel/semconv/v1.12.0"
Expand Down Expand Up @@ -33,6 +34,14 @@ func WithFormattedQueries(format bool) Option {
}
}

// WithSpanNameFormatter takes a function that determines the span name
// for a given query event.
func WithSpanNameFormatter(f func(*bun.QueryEvent) string) Option {
return func(h *QueryHook) {
h.spanNameFormatter = f
}
}

// WithTracerProvider returns an Option to use the TracerProvider when
// creating a Tracer.
func WithTracerProvider(tp trace.TracerProvider) Option {
Expand Down
17 changes: 11 additions & 6 deletions extra/bunotel/otel.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,12 @@ import (
)

type QueryHook struct {
attrs []attribute.KeyValue
formatQueries bool
tracer trace.Tracer
meter metric.Meter
queryHistogram metric.Int64Histogram
attrs []attribute.KeyValue
formatQueries bool
tracer trace.Tracer
meter metric.Meter
queryHistogram metric.Int64Histogram
spanNameFormatter func(*bun.QueryEvent) string
}

var _ bun.QueryHook = (*QueryHook)(nil)
Expand Down Expand Up @@ -86,7 +87,11 @@ func (h *QueryHook) AfterQuery(ctx context.Context, event *bun.QueryEvent) {
return
}

span.SetName(operation)
name := operation
if h.spanNameFormatter != nil {
name = h.spanNameFormatter(event)
}
span.SetName(name)
defer span.End()

query := h.eventQuery(event)
Expand Down

0 comments on commit 04e2125

Please sign in to comment.