Skip to content

Commit

Permalink
add span start end end options to gin configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
dmathieu committed Mar 6, 2024
1 parent a111e3e commit 832c0e6
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 3 deletions.
6 changes: 3 additions & 3 deletions instrumentation/github.com/gin-gonic/gin/otelgin/gintrace.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,10 @@ func Middleware(service string, opts ...Option) gin.HandlerFunc {
c.Request = c.Request.WithContext(savedCtx)
}()
ctx := cfg.Propagators.Extract(savedCtx, propagation.HeaderCarrier(c.Request.Header))
opts := []oteltrace.SpanStartOption{
opts := append(cfg.SpanStartOptions, []oteltrace.SpanStartOption{
oteltrace.WithAttributes(semconvutil.HTTPServerRequest(service, c.Request)...),
oteltrace.WithSpanKind(oteltrace.SpanKindServer),
}
}...)
var spanName string
if cfg.SpanNameFormatter == nil {
spanName = c.FullPath()
Expand All @@ -75,7 +75,7 @@ func Middleware(service string, opts ...Option) gin.HandlerFunc {
opts = append(opts, oteltrace.WithAttributes(rAttr))
}
ctx, span := tracer.Start(ctx, spanName, opts...)
defer span.End()
defer span.End(cfg.SpanEndOptions...)

// pass the span through the request context
c.Request = c.Request.WithContext(ctx)
Expand Down
18 changes: 18 additions & 0 deletions instrumentation/github.com/gin-gonic/gin/otelgin/option.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ type config struct {
Propagators propagation.TextMapPropagator
Filters []Filter
SpanNameFormatter SpanNameFormatter
SpanStartOptions []oteltrace.SpanStartOption
SpanEndOptions []oteltrace.SpanEndOption
}

// Filter is a predicate used to determine whether a given http.request should
Expand Down Expand Up @@ -77,3 +79,19 @@ func WithSpanNameFormatter(f func(r *http.Request) string) Option {
c.SpanNameFormatter = f
})
}

// WithSpanStartOption applies options to all the HTTP span created by the
// instrumentation.
func WithSpanStartOption(o ...oteltrace.SpanStartOption) Option {
return optionFunc(func(c *config) {
c.SpanStartOptions = append(c.SpanStartOptions, o...)
})
}

// WithSpanEndOption applies options when ending the HTTP span created by the
// instrumentation.
func WithSpanEndOption(o ...oteltrace.SpanEndOption) Option {
return optionFunc(func(c *config) {
c.SpanEndOptions = append(c.SpanEndOptions, o...)
})
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"go.opentelemetry.io/otel/codes"
sdktrace "go.opentelemetry.io/otel/sdk/trace"
"go.opentelemetry.io/otel/sdk/trace/tracetest"
"go.opentelemetry.io/otel/trace"

"go.opentelemetry.io/otel/attribute"
oteltrace "go.opentelemetry.io/otel/trace"
Expand Down Expand Up @@ -179,6 +180,39 @@ func TestSpanName(t *testing.T) {
}
}

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

// setup
router := gin.New()
router.Use(otelgin.Middleware(
"foobar",
otelgin.WithTracerProvider(provider),
otelgin.WithSpanStartOption(
trace.WithAttributes(attribute.String("spanStart", "true")),
),
))

// configure a handler that returns an error and 5xx status
// code
router.GET("/", func(c *gin.Context) {
})
r := httptest.NewRequest("GET", "/", nil)
w := httptest.NewRecorder()
router.ServeHTTP(w, r)
response := w.Result()
assert.Equal(t, http.StatusOK, response.StatusCode)

// verify the errors and status are correct
spans := sr.Ended()
require.Len(t, spans, 1)
span := spans[0]
assert.Equal(t, "/", span.Name())
attr := span.Attributes()
assert.Contains(t, attr, attribute.String("spanStart", "true"))
}

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

0 comments on commit 832c0e6

Please sign in to comment.