diff --git a/CHANGELOG.md b/CHANGELOG.md index 6248e16f6b2..2e0dc9674e9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,7 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm ### Added - Add the new `go.opentelemetry.io/contrib/instrgen` package to provide auto-generated source code instrumentation. (#3068) +- Add `SpanNameFormatter` option to package `go.opentelemetry.io/contrib/instrumentation/github.com/gin-gonic/gin/otelgin`. (#3343) ### Changed diff --git a/instrumentation/github.com/gin-gonic/gin/otelgin/gintrace.go b/instrumentation/github.com/gin-gonic/gin/otelgin/gintrace.go index 0a0617df2ca..8d5779e9e67 100644 --- a/instrumentation/github.com/gin-gonic/gin/otelgin/gintrace.go +++ b/instrumentation/github.com/gin-gonic/gin/otelgin/gintrace.go @@ -73,7 +73,12 @@ func Middleware(service string, opts ...Option) gin.HandlerFunc { oteltrace.WithAttributes(httpconv.ServerRequest(service, c.Request)...), oteltrace.WithSpanKind(oteltrace.SpanKindServer), } - spanName := c.FullPath() + var spanName string + if cfg.SpanNameFormatter == nil { + spanName = c.FullPath() + } else { + spanName = cfg.SpanNameFormatter(c.Request) + } if spanName == "" { spanName = fmt.Sprintf("HTTP %s route not found", c.Request.Method) } else { diff --git a/instrumentation/github.com/gin-gonic/gin/otelgin/option.go b/instrumentation/github.com/gin-gonic/gin/otelgin/option.go index d8e1d1d9e3d..c13c3dad815 100644 --- a/instrumentation/github.com/gin-gonic/gin/otelgin/option.go +++ b/instrumentation/github.com/gin-gonic/gin/otelgin/option.go @@ -24,15 +24,19 @@ import ( ) type config struct { - TracerProvider oteltrace.TracerProvider - Propagators propagation.TextMapPropagator - Filters []Filter + TracerProvider oteltrace.TracerProvider + Propagators propagation.TextMapPropagator + Filters []Filter + SpanNameFormatter SpanNameFormatter } // Filter is a predicate used to determine whether a given http.request should // be traced. A Filter must return true if the request should be traced. type Filter func(*http.Request) bool +// SpanNameFormatter is used to set span name by http.request. +type SpanNameFormatter func(r *http.Request) string + // Option specifies instrumentation configuration options. type Option interface { apply(*config) @@ -76,3 +80,11 @@ func WithFilter(f ...Filter) Option { c.Filters = append(c.Filters, f...) }) } + +// WithSpanNameFormatter takes a function that will be called on every +// request and the returned string will become the Span Name. +func WithSpanNameFormatter(f func(r *http.Request) string) Option { + return optionFunc(func(c *config) { + c.SpanNameFormatter = f + }) +}