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 14 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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ 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)

### Changed

- Change the span name to be `GET /path` so it complies with the OTel HTTP semantic conventions in `go.opentelemetry.io/contrib/instrumentation/github.com/labstack/echo/otelecho`. (#6365)

### Fixed

- Fix broken AWS presigned URLs when using instrumentation in `go.opentelemetry.io/contrib/instrumentation/github.com/aws/aws-sdk-go-v2/otelaws`. (#5975)
Expand Down
31 changes: 24 additions & 7 deletions instrumentation/github.com/labstack/echo/otelecho/echo.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@
package otelecho // import "go.opentelemetry.io/contrib/instrumentation/github.com/labstack/echo/otelecho"

import (
"fmt"
"net/http"
"slices"
"strings"

"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 @@ -67,10 +68,7 @@ func Middleware(service string, opts ...Option) echo.MiddlewareFunc {
rAttr := semconv.HTTPRoute(path)
opts = append(opts, oteltrace.WithAttributes(rAttr))
}
spanName := c.Path()
if spanName == "" {
spanName = fmt.Sprintf("HTTP %s route not found", request.Method)
}
spanName := spanNameFormatter(c)

ctx, span := tracer.Start(ctx, spanName, opts...)
defer span.End()
Expand All @@ -96,3 +94,22 @@ func Middleware(service string, opts ...Option) echo.MiddlewareFunc {
}
}
}

func spanNameFormatter(c echo.Context) string {
method, path := strings.ToUpper(c.Request().Method), c.Path()
if !slices.Contains([]string{
http.MethodGet, http.MethodHead,
http.MethodPost, http.MethodPut,
http.MethodPatch, http.MethodDelete,
http.MethodConnect, http.MethodOptions,
http.MethodTrace,
}, method) {
method = "HTTP"
}

if path != "" {
return method + " " + path
}

return method
}
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 @@ -86,7 +84,7 @@ func TestTrace200(t *testing.T) {
spans := sr.Ended()
require.Len(t, spans, 1)
span := spans[0]
assert.Equal(t, "/user/:id", span.Name())
assert.Equal(t, "GET /user/:id", span.Name())
assert.Equal(t, oteltrace.SpanKindServer, span.SpanKind())
attrs := span.Attributes()
assert.Contains(t, attrs, attribute.String("net.host.name", "foobar"))
Expand Down Expand Up @@ -118,7 +116,7 @@ func TestError(t *testing.T) {
spans := sr.Ended()
require.Len(t, spans, 1)
span := spans[0]
assert.Equal(t, "/server_err", span.Name())
assert.Equal(t, "GET /server_err", span.Name())
attrs := span.Attributes()
assert.Contains(t, attrs, attribute.String("net.host.name", "foobar"))
assert.Contains(t, attrs, attribute.Int("http.status_code", http.StatusInternalServerError))
Expand Down Expand Up @@ -177,7 +175,7 @@ func TestStatusError(t *testing.T) {
spans := sr.Ended()
require.Len(t, spans, 1)
span := spans[0]
assert.Equal(t, "/err", span.Name())
assert.Equal(t, "GET /err", span.Name())
assert.Equal(t, tc.spanCode, span.Status().Code)

attrs := span.Attributes()
Expand All @@ -202,3 +200,59 @@ func TestErrorNotSwallowedByMiddleware(t *testing.T) {
err := h(c)
assert.Equal(t, assert.AnError, err)
}

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

tests := []struct {
name string
method string
path string
url string
expected string
}{
// Test for standard methods
{"", http.MethodGet, "/user/:id", "/user/123", "GET /user/:id"},
flc1125 marked this conversation as resolved.
Show resolved Hide resolved
{"", http.MethodHead, "/user/:id", "/user/123", "HEAD /user/:id"},
{"", http.MethodPost, "/user/:id", "/user/123", "POST /user/:id"},
{"", http.MethodPut, "/user/:id", "/user/123", "PUT /user/:id"},
{"", http.MethodPatch, "/user/:id", "/user/123", "PATCH /user/:id"},
{"", http.MethodDelete, "/user/:id", "/user/123", "DELETE /user/:id"},
{"", http.MethodConnect, "/user/:id", "/user/123", "CONNECT /user/:id"},
{"", http.MethodOptions, "/user/:id", "/user/123", "OPTIONS /user/:id"},
{"", http.MethodTrace, "/user/:id", "/user/123", "TRACE /user/:id"},
{"", http.MethodGet, "/", "/", "GET /"},

// Test for no route
{"", http.MethodGet, "/", "/user/id", "GET"},

// Test for case-insensitive method
{"", "get", "/user/123", "/user/123", "GET /user/123"},
{"", "Get", "/user/123", "/user/123", "GET /user/123"},
{"", "GET", "/user/:id", "/user/123", "GET /user/:id"},

// Test for invalid method
{"", "INVALID", "/user/123", "/user/123", "HTTP /user/123"},
}

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

router := echo.New()
router.Use(otelecho.Middleware("foobar", otelecho.WithTracerProvider(provider)))
router.Add(test.method, test.path, func(c echo.Context) error {
return c.NoContent(http.StatusOK)
})

r := httptest.NewRequest(test.method, test.url, nil)
w := httptest.NewRecorder()
router.ServeHTTP(w, r)

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