From 724c6d0a73013eb8a66d6046c7c88e7074c65efa Mon Sep 17 00:00:00 2001 From: Flc Date: Tue, 26 Nov 2024 17:51:54 +0800 Subject: [PATCH] chore(otelgin): Enhance gintrace tests with span status checks and error descriptions --- .../github.com/gin-gonic/gin/otelgin/gintrace.go | 2 +- .../gin-gonic/gin/otelgin/test/gintrace_test.go | 8 ++++++-- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/instrumentation/github.com/gin-gonic/gin/otelgin/gintrace.go b/instrumentation/github.com/gin-gonic/gin/otelgin/gintrace.go index 18ede5d6b9e..ef666d37cac 100644 --- a/instrumentation/github.com/gin-gonic/gin/otelgin/gintrace.go +++ b/instrumentation/github.com/gin-gonic/gin/otelgin/gintrace.go @@ -95,7 +95,7 @@ func Middleware(service string, opts ...Option) gin.HandlerFunc { span.SetAttributes(semconv.HTTPStatusCode(status)) } if len(c.Errors) > 0 { - span.SetStatus(codes.Error, "gin error") + span.SetStatus(codes.Error, c.Errors.String()) for _, err := range c.Errors { span.RecordError(err.Err) } diff --git a/instrumentation/github.com/gin-gonic/gin/otelgin/test/gintrace_test.go b/instrumentation/github.com/gin-gonic/gin/otelgin/test/gintrace_test.go index b480f7db9d8..099fdd484d0 100644 --- a/instrumentation/github.com/gin-gonic/gin/otelgin/test/gintrace_test.go +++ b/instrumentation/github.com/gin-gonic/gin/otelgin/test/gintrace_test.go @@ -18,8 +18,6 @@ import ( "github.com/stretchr/testify/require" "golang.org/x/net/context" - "go.opentelemetry.io/contrib/instrumentation/github.com/gin-gonic/gin/otelgin" - b3prop "go.opentelemetry.io/contrib/propagators/b3" "go.opentelemetry.io/otel" "go.opentelemetry.io/otel/attribute" "go.opentelemetry.io/otel/codes" @@ -28,6 +26,9 @@ import ( "go.opentelemetry.io/otel/sdk/trace/tracetest" "go.opentelemetry.io/otel/trace" "go.opentelemetry.io/otel/trace/noop" + + "go.opentelemetry.io/contrib/instrumentation/github.com/gin-gonic/gin/otelgin" + b3prop "go.opentelemetry.io/contrib/propagators/b3" ) func init() { @@ -165,6 +166,8 @@ func TestTrace200(t *testing.T) { assert.Contains(t, attr, attribute.String("http.method", "GET")) assert.Contains(t, attr, attribute.String("http.route", "/user/:id")) assert.Empty(t, span.Events()) + assert.Equal(t, codes.Unset, span.Status().Code) + assert.Empty(t, span.Status().Description) } func TestError(t *testing.T) { @@ -208,6 +211,7 @@ func TestError(t *testing.T) { // server errors set the status assert.Equal(t, codes.Error, span.Status().Code) + assert.Equal(t, "Error #01: oh no one\nError #02: oh no two\n", span.Status().Description) } func TestSpanStatus(t *testing.T) {