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

fix(otelecho): comply with span naming semconv #6365

Merged
merged 19 commits into from
Nov 28, 2024
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
14ba828
feat(otelecho): add SpanNameFormatter config and tests
flc1125 Nov 24, 2024
95c0f43
feat(otelecho): add SpanNameFormatter config and tests
flc1125 Nov 24, 2024
7deb0fe
refactor(otelecho): refine span name formatter and tests
flc1125 Nov 25, 2024
977f806
refactor(otelecho): refine span name formatter and tests
flc1125 Nov 25, 2024
8ae3b69
refactor(otelecho): refine span name formatter and tests
flc1125 Nov 25, 2024
c670673
Merge branch 'main' into otelecho-formatter
flc1125 Nov 25, 2024
8aa4055
feat(otelecho): Adjust the default span name
flc1125 Nov 26, 2024
27ce430
Merge branch 'main' into otelecho-formatter
flc1125 Nov 26, 2024
21235e9
feat(otelecho): Adjust the default span name
flc1125 Nov 26, 2024
3ee4cc8
✨ feat(echo): Enhance span name formatter and add tests for lowercase…
flc1125 Nov 26, 2024
23c9bc0
fix(otelecho): Upgrade assertions to requirements in echo_test.go for…
flc1125 Nov 26, 2024
301ecae
Merge remote-tracking branch 'upstream/main' into otelecho-formatter
flc1125 Nov 26, 2024
b120bdd
refactor(otelecho): comply with OTEL span naming specs
flc1125 Nov 26, 2024
9d9968e
Update CHANGELOG.md
flc1125 Nov 27, 2024
23dc6f3
Merge remote-tracking branch 'upstream/main' into otelecho-formatter
flc1125 Nov 27, 2024
6b0d7a7
test(otelecho): enhance test descriptions for clarity and coverage
flc1125 Nov 27, 2024
d3ac8c1
Merge remote-tracking branch 'upstream/main' into otelecho-formatter
flc1125 Nov 27, 2024
a651d37
Merge branch 'main' into otelecho-formatter
pellared Nov 27, 2024
c9a0fdf
Merge branch 'main' into otelecho-formatter
pellared Nov 27, 2024
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 @@ -12,6 +12,7 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm

- Added support for providing `endpoint`, `pollingIntervalMs` and `initialSamplingRate` using environment variable `OTEL_TRACES_SAMPLER_ARG` in `go.opentelemetry.io/contrib/samples/jaegerremote`. (#6310)
- Added support exporting logs via OTLP over gRPC in `go.opentelemetry.io/contrib/config`. (#6340)
- Added support custom span name in `go.opentelemetry.io/contrib/instrumentation/github.com/labstack/echo/otelecho`. (#6365)

### Fixed

Expand Down
26 changes: 23 additions & 3 deletions instrumentation/github.com/labstack/echo/otelecho/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,28 @@
package otelecho // import "go.opentelemetry.io/contrib/instrumentation/github.com/labstack/echo/otelecho"

import (
"net/http"

"github.com/labstack/echo/v4/middleware"

"go.opentelemetry.io/otel/propagation"
oteltrace "go.opentelemetry.io/otel/trace"
)

// defaultSpanNameFormatter is the default function used for formatting span names.
var defaultSpanNameFormatter = func(path string, _ *http.Request) string {
return path
}

// SpanNameFormatter is a function that takes a path and an HTTP request and returns a span name.
type SpanNameFormatter func(string, *http.Request) string

// config is used to configure the mux middleware.
type config struct {
TracerProvider oteltrace.TracerProvider
Propagators propagation.TextMapPropagator
Skipper middleware.Skipper
TracerProvider oteltrace.TracerProvider
Propagators propagation.TextMapPropagator
Skipper middleware.Skipper
spanNameFormatter SpanNameFormatter
}

// Option specifies instrumentation configuration options.
Expand Down Expand Up @@ -55,3 +66,12 @@ func WithSkipper(skipper middleware.Skipper) Option {
cfg.Skipper = skipper
})
}

// WithSpanNameFormatter specifies a function to use for formatting span names.
func WithSpanNameFormatter(formatter SpanNameFormatter) Option {
return optionFunc(func(cfg *config) {
if formatter != nil {
cfg.spanNameFormatter = formatter
}
})
}
9 changes: 6 additions & 3 deletions instrumentation/github.com/labstack/echo/otelecho/echo.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,8 @@ import (
"github.com/labstack/echo/v4"
"github.com/labstack/echo/v4/middleware"

"go.opentelemetry.io/otel"

"go.opentelemetry.io/contrib/instrumentation/github.com/labstack/echo/otelecho/internal/semconvutil"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/propagation"
semconv "go.opentelemetry.io/otel/semconv/v1.20.0"
Expand Down Expand Up @@ -45,6 +44,10 @@ func Middleware(service string, opts ...Option) echo.MiddlewareFunc {
cfg.Skipper = middleware.DefaultSkipper
}

if cfg.spanNameFormatter == nil {
cfg.spanNameFormatter = defaultSpanNameFormatter
}

return func(next echo.HandlerFunc) echo.HandlerFunc {
return func(c echo.Context) error {
if cfg.Skipper(c) {
Expand All @@ -67,7 +70,7 @@ func Middleware(service string, opts ...Option) echo.MiddlewareFunc {
rAttr := semconv.HTTPRoute(path)
opts = append(opts, oteltrace.WithAttributes(rAttr))
}
spanName := c.Path()
spanName := cfg.spanNameFormatter(c.Path(), c.Request())
if spanName == "" {
spanName = fmt.Sprintf("HTTP %s route not found", request.Method)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,11 @@ import (
"github.com/labstack/echo/v4"
"github.com/stretchr/testify/assert"

b3prop "go.opentelemetry.io/contrib/propagators/b3"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/propagation"
"go.opentelemetry.io/otel/trace"
"go.opentelemetry.io/otel/trace/noop"

b3prop "go.opentelemetry.io/contrib/propagators/b3"
)

func TestGetSpanNotInstrumented(t *testing.T) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,15 @@ import (
"testing"

"github.com/labstack/echo/v4"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"go.opentelemetry.io/contrib/instrumentation/github.com/labstack/echo/otelecho"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/codes"
"go.opentelemetry.io/otel/sdk/trace"
"go.opentelemetry.io/otel/sdk/trace/tracetest"

"go.opentelemetry.io/otel/attribute"
oteltrace "go.opentelemetry.io/otel/trace"
)

Expand Down Expand Up @@ -202,3 +200,57 @@ func TestErrorNotSwallowedByMiddleware(t *testing.T) {
err := h(c)
assert.Equal(t, assert.AnError, err)
}

func TestSpanNameFormatter(t *testing.T) {
imsb := tracetest.NewInMemoryExporter()
otel.SetTracerProvider(trace.NewTracerProvider(
trace.WithSyncer(imsb),
))

tests := []struct {
name string
formatter otelecho.SpanNameFormatter
expected string
}{
{
name: "default",
formatter: nil,
expected: "/user/:id",
},
{
name: "custom",
formatter: func(path string, req *http.Request) string {
return "custom " + path
},
expected: "custom /user/:id",
},
{
name: "has_request",
formatter: func(path string, req *http.Request) string {
return req.Method + " " + path
},
expected: "GET /user/:id",
pellared marked this conversation as resolved.
Show resolved Hide resolved
},
}

for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
defer imsb.Reset()

router := echo.New()
router.Use(otelecho.Middleware("foobar", otelecho.WithSpanNameFormatter(test.formatter)))
router.GET("/user/:id", func(c echo.Context) error {
return c.NoContent(http.StatusOK)
})

r := httptest.NewRequest("GET", "/user/123", nil)
w := httptest.NewRecorder()

router.ServeHTTP(w, r)

spans := imsb.GetSpans()
assert.Len(t, spans, 1)
assert.Equal(t, test.expected, spans[0].Name)
})
}
}
Loading