Skip to content

Commit

Permalink
✨ feat(echo): Enhance span name formatter and add tests for lowercase…
Browse files Browse the repository at this point in the history
… methods
  • Loading branch information
flc1125 committed Nov 26, 2024
1 parent 21235e9 commit 3ee4cc8
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 4 deletions.
3 changes: 2 additions & 1 deletion instrumentation/github.com/labstack/echo/otelecho/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package otelecho // import "go.opentelemetry.io/contrib/instrumentation/github.c

import (
"net/http"
"strings"

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

Expand All @@ -14,7 +15,7 @@ import (

// defaultSpanNameFormatter is the default function used for formatting span names.
var defaultSpanNameFormatter = func(path string, req *http.Request) string {
return req.Method + " " + path
return strings.ToUpper(req.Method) + " " + path
}

// SpanNameFormatter is a function that takes a path and an HTTP request and returns a span name.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ func TestSpanNameFormatter(t *testing.T) {
},
{
name: "custom",
formatter: func(path string, req *http.Request) string {
formatter: func(path string, _ *http.Request) string {
return "custom " + path
},
expected: "custom /user/:id",
Expand All @@ -244,14 +244,35 @@ func TestSpanNameFormatter(t *testing.T) {
return c.NoContent(http.StatusOK)
})

r := httptest.NewRequest("GET", "/user/123", nil)
r := httptest.NewRequest(http.MethodGet, "/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)
})
}

t.Run("test default span name formatter with lowercase method", func(t *testing.T) {
router := echo.New()
router.Use(otelecho.Middleware("foobar",
otelecho.WithTracerProvider(provider)),
)
router.GET("/user/:id", func(c echo.Context) error {
return c.NoContent(http.StatusOK)
})

for _, method := range []string{"Get", "GET", "get"} {
r := httptest.NewRequest(method, "/user/123", nil)
w := httptest.NewRecorder()
router.ServeHTTP(w, r)

spans := imsb.GetSpans()
assert.Len(t, spans, 1)
assert.Equal(t, "GET /user/:id", spans[0].Name)

imsb.Reset()
}
})
}

0 comments on commit 3ee4cc8

Please sign in to comment.