Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(bunotel): Ability to override span names #1120

Merged
merged 1 commit into from
Feb 10, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading