Skip to content
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm
- Allow configuring samplers in `go.opentelemetry.io/contrib/otelconf`. (#7148)
- Slog log bridge now sets `SeverityText` attribute using source value in `go.opentelemetry.io/contrib/bridges/otelslog`. (#7198)
- Add `http.route` metric attribute in `go.opentelemetry.io/contrib/instrumentation/github.com/gin-gonic/gin/otelgin`. (#7275)
- Add the `WithSpanStartOptions` option to add custom options to new spans `go.opentelemetry.io/contrib/instrumentation/github.com/gin-gonic/gin/otelgin`. (#7261)
- Add instrumentation support for `go.mongodb.org/mongo-driver/v2` in `go.opentelemetry.io/contrib/instrumentation/go.mongodb.org/mongo-driver/v2/mongo/otelmongo`. (#6539)

### Changed
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
type config struct {
TracerProvider oteltrace.TracerProvider
Propagators propagation.TextMapPropagator
SpanStartOptions []oteltrace.SpanStartOption
Filters []Filter
GinFilters []GinFilter
SpanNameFormatter SpanNameFormatter
Expand Down Expand Up @@ -89,6 +90,14 @@ func WithPropagators(propagators propagation.TextMapPropagator) Option {
})
}

// WithSpanStartOptions configures an additional set of
// trace.SpanStartOptions, which are applied to each new span.
func WithSpanStartOptions(opts ...oteltrace.SpanStartOption) Option {
return optionFunc(func(c *config) {
c.SpanStartOptions = append(c.SpanStartOptions, opts...)
})
}

// WithTracerProvider specifies a tracer provider to use for creating a tracer.
// If none is specified, the global provider is used.
func WithTracerProvider(provider oteltrace.TracerProvider) Option {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ const (
// server handling the request.
func Middleware(service string, opts ...Option) gin.HandlerFunc {
cfg := config{}

for _, opt := range opts {
opt.apply(&cfg)
}
Expand Down Expand Up @@ -95,6 +96,8 @@ func Middleware(service string, opts ...Option) gin.HandlerFunc {
oteltrace.WithSpanKind(oteltrace.SpanKindServer),
}

opts = append(opts, cfg.SpanStartOptions...)

spanName := cfg.SpanNameFormatter(c)
if spanName == "" {
spanName = fmt.Sprintf("HTTP %s route not found", c.Request.Method)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,31 @@ func TestSpanStatus(t *testing.T) {
})
}

func TestWithSpanOptions_CustomAttributesAndSpanKind(t *testing.T) {
sr := tracetest.NewSpanRecorder()
provider := sdktrace.NewTracerProvider(sdktrace.WithSpanProcessor(sr))

customAttr := attribute.String("custom.key", "custom.value")

router := gin.New()
router.Use(otelgin.Middleware("foobar",
otelgin.WithTracerProvider(provider),
otelgin.WithSpanStartOptions(trace.WithAttributes(customAttr)),
))
router.GET("/test", func(c *gin.Context) {})

r := httptest.NewRequest("GET", "/test", nil)
w := httptest.NewRecorder()
router.ServeHTTP(w, r)

spans := sr.Ended()
require.Len(t, spans, 1)

span := spans[0]
assert.Contains(t, span.Attributes(), customAttr)
assert.Equal(t, trace.SpanKindServer, span.SpanKind())
}

func TestSpanName(t *testing.T) {
sr := tracetest.NewSpanRecorder()
provider := sdktrace.NewTracerProvider(
Expand Down
Loading