Skip to content

Commit

Permalink
chore(otelgin): Enhance gintrace tests with span status checks and er…
Browse files Browse the repository at this point in the history
…ror descriptions
  • Loading branch information
flc1125 committed Nov 26, 2024
1 parent a690136 commit 724c6d0
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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() {
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit 724c6d0

Please sign in to comment.