diff --git a/CHANGELOG.md b/CHANGELOG.md index 845ba1c31a7..6dc65a9a996 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -56,6 +56,7 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm - Cleaned up indentations under `Unreleased/Fixed` of `./CHANGELOG.md`. (#7163) - Removed a duplicate instance of the `Changed` subheader under `1.18.0/0.43.0/0.12.0` in `./CHANGELOG.md`. (#7163) - Check for TLS related options to be set before creating TLS config `go.opentelemetry.io/contrib/otelconf`. (#6984) +- Fixed handling of the `OTEL_SEMCONV_STABILITY_OPT_IN` environment variable in `go.opentelemetry.io/contrib/instrumentation/github.com/gin-gonic/gin/otelgin`. (#7215) ### Removed diff --git a/instrumentation/github.com/gin-gonic/gin/otelgin/gin.go b/instrumentation/github.com/gin-gonic/gin/otelgin/gin.go index 0f7cbabbbac..cfb89fe4f24 100644 --- a/instrumentation/github.com/gin-gonic/gin/otelgin/gin.go +++ b/instrumentation/github.com/gin-gonic/gin/otelgin/gin.go @@ -53,7 +53,6 @@ func Middleware(service string, opts ...Option) gin.HandlerFunc { ) sc := semconv.NewHTTPServer(meter) - var hs semconv.HTTPServer return func(c *gin.Context) { requestStartTime := time.Now() @@ -87,8 +86,8 @@ func Middleware(service string, opts ...Option) gin.HandlerFunc { } opts := []oteltrace.SpanStartOption{ - oteltrace.WithAttributes(hs.RequestTraceAttrs(service, c.Request, requestTraceAttrOpts)...), - oteltrace.WithAttributes(hs.Route(c.FullPath())), + oteltrace.WithAttributes(sc.RequestTraceAttrs(service, c.Request, requestTraceAttrOpts)...), + oteltrace.WithAttributes(sc.Route(c.FullPath())), oteltrace.WithSpanKind(oteltrace.SpanKindServer), } var spanName string @@ -110,7 +109,7 @@ func Middleware(service string, opts ...Option) gin.HandlerFunc { c.Next() status := c.Writer.Status() - span.SetStatus(hs.Status(status)) + span.SetStatus(sc.Status(status)) if status > 0 { span.SetAttributes(semconv.HTTPStatusCode(status)) } diff --git a/instrumentation/github.com/gin-gonic/gin/otelgin/test/gin_test.go b/instrumentation/github.com/gin-gonic/gin/otelgin/test/gin_test.go index 3052f81818d..6459acc07aa 100644 --- a/instrumentation/github.com/gin-gonic/gin/otelgin/test/gin_test.go +++ b/instrumentation/github.com/gin-gonic/gin/otelgin/test/gin_test.go @@ -21,6 +21,7 @@ import ( "github.com/stretchr/testify/require" "go.opentelemetry.io/contrib/instrumentation/github.com/gin-gonic/gin/otelgin" + "go.opentelemetry.io/contrib/instrumentation/github.com/gin-gonic/gin/otelgin/internal/semconv" b3prop "go.opentelemetry.io/contrib/propagators/b3" "go.opentelemetry.io/otel" "go.opentelemetry.io/otel/attribute" @@ -556,3 +557,46 @@ func TestMetrics(t *testing.T) { }) } } + +func TestServerWithSemConvStabilityOptIn(t *testing.T) { + tests := []struct { + name string + setEnv bool + wantExistsHTTPMethod bool + }{ + { + "not set", + false, + false, + }, + { + "set to http/dup", + true, + true, + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if tt.setEnv { + t.Setenv(semconv.OTelSemConvStabilityOptIn, "http/dup") + } + + attrs := semconv.NewHTTPServer(nil). + RequestTraceAttrs("foobar", + httptest.NewRequest("GET", "/user/123", nil), + semconv.RequestTraceAttrsOpts{ + HTTPClientIP: "127.0.0.1", + }) + + var existsHTTPMethod bool + for _, attr := range attrs { + if attr.Key == "http.method" { + existsHTTPMethod = true + break + } + } + assert.Equal(t, tt.wantExistsHTTPMethod, existsHTTPMethod) + }) + } +}