Skip to content

Commit

Permalink
feat(tracer): fix default span name not being set as attribute (& imp…
Browse files Browse the repository at this point in the history
…rovement)
  • Loading branch information
tigerwill90 committed Feb 5, 2025
1 parent 212bba7 commit 7b11256
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 7 deletions.
14 changes: 10 additions & 4 deletions foxtrace.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ func (t middleware) trace(next fox.HandlerFunc) fox.HandlerFunc {
attributes = append(attributes, t.cfg.attrsFn(c)...)
}

opts := make([]trace.SpanStartOption, 0, 4)
opts := make([]trace.SpanStartOption, 0, 5)
opts = append(
opts,
trace.WithAttributes(attributes...),
Expand All @@ -87,11 +87,17 @@ func (t middleware) trace(next fox.HandlerFunc) fox.HandlerFunc {
}

if spanName == "" {
spanName = fmt.Sprintf("HTTP %s route not found", req.Method)
} else {
opts = append(opts, trace.WithAttributes(semconv.HTTPRoute(spanName)))
switch c.Scope() {
case fox.NoMethodHandler:
spanName = fmt.Sprintf("HTTP %s method not allowed", req.Method)
case fox.OptionsHandler:
spanName = fmt.Sprintf("HTTP %s route matched", req.Method)
default:
spanName = fmt.Sprintf("HTTP %s route not found", req.Method)
}
}

opts = append(opts, trace.WithAttributes(semconv.HTTPRoute(spanName)))
ctx, span := t.tracer.Start(ctx, spanName, opts...)
defer span.End()

Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,6 @@ require (
github.com/pmezard/go-difflib v1.0.0 // indirect
go.opentelemetry.io/auto/sdk v1.1.0 // indirect
go.opentelemetry.io/otel/metric v1.34.0 // indirect
golang.org/x/sys v0.29.0 // indirect
golang.org/x/sys v0.30.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ go.opentelemetry.io/otel/metric v1.34.0 h1:+eTR3U0MyfWjRDhmFMxe2SsW64QrZ84AOhvqS
go.opentelemetry.io/otel/metric v1.34.0/go.mod h1:CEDrp0fy2D0MvkXE+dPV7cMi8tWZwX3dmaIhwPOaqHE=
go.opentelemetry.io/otel/trace v1.34.0 h1:+ouXS2V8Rd4hp4580a8q23bg0azF2nI8cqLYnC8mh/k=
go.opentelemetry.io/otel/trace v1.34.0/go.mod h1:Svm7lSjQD7kG7KJ/MUHPVXSDGz2OX4h0M2jHBhmSfRE=
golang.org/x/sys v0.29.0 h1:TPYlXGxvx1MGTn2GiZDhnjPA9wZzZeGKHHmKhHYvgaU=
golang.org/x/sys v0.29.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/sys v0.30.0 h1:QjkSwP/36a20jFYWkSue1YwXzLmsV5Gfq7Eiy72C1uc=
golang.org/x/sys v0.30.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk=
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q=
Expand Down

0 comments on commit 7b11256

Please sign in to comment.