From 31014d73ae47dc6fd20fb95d4c52b04c090f4fc0 Mon Sep 17 00:00:00 2001 From: dmathieu <42@dmathieu.com> Date: Tue, 18 Mar 2025 09:23:31 +0100 Subject: [PATCH 1/2] otelrestful: use semconv package for request attributes and span status --- .../emicklei/go-restful/otelrestful/restful.go | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/instrumentation/github.com/emicklei/go-restful/otelrestful/restful.go b/instrumentation/github.com/emicklei/go-restful/otelrestful/restful.go index 2f5011b66c2..b6c7ea3a6ab 100644 --- a/instrumentation/github.com/emicklei/go-restful/otelrestful/restful.go +++ b/instrumentation/github.com/emicklei/go-restful/otelrestful/restful.go @@ -7,7 +7,6 @@ import ( "github.com/emicklei/go-restful/v3" "go.opentelemetry.io/contrib/instrumentation/github.com/emicklei/go-restful/otelrestful/internal/semconv" - "go.opentelemetry.io/contrib/instrumentation/github.com/emicklei/go-restful/otelrestful/internal/semconvutil" "go.opentelemetry.io/otel" "go.opentelemetry.io/otel/propagation" oteltrace "go.opentelemetry.io/otel/trace" @@ -45,7 +44,7 @@ func OTelFilter(service string, opts ...Option) restful.FilterFunction { spanName := route opts := []oteltrace.SpanStartOption{ - oteltrace.WithAttributes(semconvutil.HTTPServerRequest(service, r)...), + oteltrace.WithAttributes(semconvServer.RequestTraceAttrs(service, r)...), oteltrace.WithSpanKind(oteltrace.SpanKindServer), } if route != "" { @@ -70,9 +69,9 @@ func OTelFilter(service string, opts ...Option) restful.FilterFunction { chain.ProcessFilter(req, resp) status := resp.StatusCode() - span.SetStatus(semconvutil.HTTPServerStatus(status)) - if status > 0 { - span.SetAttributes(semconv.HTTPStatusCode(status)) - } + span.SetStatus(semconvServer.Status(status)) + span.SetAttributes(semconvServer.ResponseTraceAttrs(semconv.ResponseTelemetry{ + StatusCode: status, + })...) } } From b15c691fa055c6f059b171b09ff0fe2aad9d261b Mon Sep 17 00:00:00 2001 From: dmathieu <42@dmathieu.com> Date: Wed, 9 Apr 2025 10:37:18 +0200 Subject: [PATCH 2/2] fix attributes now that we don't have http/dup by default anymore --- .../go-restful/otelrestful/test/restful_test.go | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/instrumentation/github.com/emicklei/go-restful/otelrestful/test/restful_test.go b/instrumentation/github.com/emicklei/go-restful/otelrestful/test/restful_test.go index 4c0845086e5..eab2c737ed3 100644 --- a/instrumentation/github.com/emicklei/go-restful/otelrestful/test/restful_test.go +++ b/instrumentation/github.com/emicklei/go-restful/otelrestful/test/restful_test.go @@ -95,9 +95,9 @@ func TestChildSpanNames(t *testing.T) { t, spans[0], "/user/{id:[0-9]+}", - attribute.String("net.host.name", "foobar"), - attribute.Int("http.status_code", http.StatusOK), - attribute.String("http.method", "GET"), + attribute.String("server.address", "foobar"), + attribute.Int("http.response.status_code", http.StatusOK), + attribute.String("http.request.method", "GET"), attribute.String("http.route", "/user/{id:[0-9]+}"), ) @@ -110,9 +110,9 @@ func TestChildSpanNames(t *testing.T) { t, spans[1], "/book/{title}", - attribute.String("net.host.name", "foobar"), - attribute.Int("http.status_code", http.StatusOK), - attribute.String("http.method", "GET"), + attribute.String("server.address", "foobar"), + attribute.Int("http.response.status_code", http.StatusOK), + attribute.String("http.request.method", "GET"), attribute.String("http.route", "/book/{title}"), ) } @@ -336,6 +336,8 @@ func TestWithPublicEndpointFn(t *testing.T) { } func assertSpan(t *testing.T, span sdktrace.ReadOnlySpan, name string, attrs ...attribute.KeyValue) { + t.Helper() + assert.Equal(t, name, span.Name()) assert.Equal(t, oteltrace.SpanKindServer, span.SpanKind())