Skip to content

Commit 5e0309b

Browse files
committed
[REFAC] use c.Request().Pattern in spanNameFormatter
1 parent 7d06fc6 commit 5e0309b

File tree

2 files changed

+1
-36
lines changed

2 files changed

+1
-36
lines changed

instrumentation/github.com/labstack/echo/otelecho/echo.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,6 @@ func Middleware(service string, opts ...Option) echo.MiddlewareFunc {
6464
request.Pattern = c.Path()
6565
opts := []oteltrace.SpanStartOption{
6666
oteltrace.WithAttributes(hs.RequestTraceAttrs(service, request)...),
67-
oteltrace.WithAttributes(hs.Route(request.Pattern)),
6867
oteltrace.WithSpanKind(oteltrace.SpanKindServer),
6968
}
7069
spanName := spanNameFormatter(c)
@@ -95,7 +94,7 @@ func Middleware(service string, opts ...Option) echo.MiddlewareFunc {
9594
}
9695

9796
func spanNameFormatter(c echo.Context) string {
98-
method, path := strings.ToUpper(c.Request().Method), c.Path()
97+
method, path := strings.ToUpper(c.Request().Method), c.Request().Pattern
9998
if !slices.Contains([]string{
10099
http.MethodGet, http.MethodHead,
101100
http.MethodPost, http.MethodPut,

instrumentation/github.com/labstack/echo/otelecho/test/echo_test.go

-34
Original file line numberDiff line numberDiff line change
@@ -256,37 +256,3 @@ func TestSpanNameFormatter(t *testing.T) {
256256
})
257257
}
258258
}
259-
260-
func TestHTTPRouteAttribute(t *testing.T) {
261-
// Set up an in-memory span recorder
262-
sr := tracetest.NewSpanRecorder()
263-
tp := trace.NewTracerProvider(trace.WithSpanProcessor(sr))
264-
265-
// Create Echo instance with middleware
266-
e := echo.New()
267-
e.Use(otelecho.Middleware("test-service", otelecho.WithTracerProvider(tp)))
268-
e.GET("/users/:id", func(c echo.Context) error {
269-
return c.String(http.StatusOK, "ok")
270-
})
271-
272-
// Simulate a request
273-
r := httptest.NewRequest("GET", "/users/123", nil)
274-
w := httptest.NewRecorder()
275-
e.ServeHTTP(w, r)
276-
277-
// Check response
278-
assert.Equal(t, http.StatusOK, w.Result().StatusCode)
279-
280-
// Verify span attributes
281-
spans := sr.Ended()
282-
assert.Len(t, spans, 1, "expected one span")
283-
284-
found := false
285-
for _, attr := range spans[0].Attributes() {
286-
if attr.Key == "http.route" && attr.Value.AsString() == "/users/:id" {
287-
found = true
288-
break
289-
}
290-
}
291-
assert.True(t, found, "http.route attribute not found or incorrect, got %v", spans[0].Attributes())
292-
}

0 commit comments

Comments
 (0)