Skip to content

Commit 78768e4

Browse files
committed
otelgin: update error handling to set attributes instead of recording errors
Fixes open-telemetry#8441 This change updates the error handling in otelgin middleware to follow the instrumentation guidelines established in open-telemetry#8386: - Replace span.RecordError() calls with span.SetAttributes(ErrorType()) - Set error.type attribute for the first error when c.Errors is not empty - Multiple errors are still captured in the status description via c.Errors.String() - Update tests to verify error.type attribute instead of span events This provides better consistency across OpenTelemetry instrumentation packages and follows the semantic conventions for error handling.
1 parent 8c6be24 commit 78768e4

2 files changed

Lines changed: 11 additions & 14 deletions

File tree

instrumentation/github.com/gin-gonic/gin/otelgin/gin.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import (
1515
"go.opentelemetry.io/otel/codes"
1616
"go.opentelemetry.io/otel/metric"
1717
"go.opentelemetry.io/otel/propagation"
18+
otelsemconv "go.opentelemetry.io/otel/semconv/v1.37.0"
1819
oteltrace "go.opentelemetry.io/otel/trace"
1920

2021
"go.opentelemetry.io/contrib/instrumentation/github.com/gin-gonic/gin/otelgin/internal/semconv"
@@ -120,9 +121,9 @@ func Middleware(service string, opts ...Option) gin.HandlerFunc {
120121

121122
if len(c.Errors) > 0 {
122123
span.SetStatus(codes.Error, c.Errors.String())
123-
for _, err := range c.Errors {
124-
span.RecordError(err.Err)
125-
}
124+
// Set error.type attribute for the first error
125+
// Multiple errors are already captured in the status description via c.Errors.String()
126+
span.SetAttributes(otelsemconv.ErrorType(c.Errors[0].Err))
126127
}
127128

128129
// Record the server-side attributes.

instrumentation/github.com/gin-gonic/gin/otelgin/gin_test.go

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import (
2727
"go.opentelemetry.io/otel/sdk/metric/metricdata/metricdatatest"
2828
sdktrace "go.opentelemetry.io/otel/sdk/trace"
2929
"go.opentelemetry.io/otel/sdk/trace/tracetest"
30+
semconv "go.opentelemetry.io/otel/semconv/v1.37.0"
3031
"go.opentelemetry.io/otel/trace"
3132
"go.opentelemetry.io/otel/trace/noop"
3233

@@ -202,15 +203,9 @@ func TestError(t *testing.T) {
202203
assert.Contains(t, attr, attribute.String("server.address", "foobar"))
203204
assert.Contains(t, attr, attribute.Int("http.response.status_code", http.StatusInternalServerError))
204205

205-
// verify the error events
206-
events := span.Events()
207-
require.Len(t, events, 2)
208-
assert.Equal(t, "exception", events[0].Name)
209-
assert.Contains(t, events[0].Attributes, attribute.String("exception.type", "*errors.errorString"))
210-
assert.Contains(t, events[0].Attributes, attribute.String("exception.message", "oh no one"))
211-
assert.Equal(t, "exception", events[1].Name)
212-
assert.Contains(t, events[1].Attributes, attribute.String("exception.type", "*errors.errorString"))
213-
assert.Contains(t, events[1].Attributes, attribute.String("exception.message", "oh no two"))
206+
// verify the error.type attribute is set (using first error)
207+
firstErr := errors.New("oh no one")
208+
assert.Contains(t, attr, semconv.ErrorType(firstErr))
214209

215210
// server errors set the status
216211
assert.Equal(t, codes.Error, span.Status().Code)
@@ -261,8 +256,9 @@ func TestSpanStatus(t *testing.T) {
261256

262257
require.Len(t, sr.Ended(), 1)
263258
assert.Equal(t, codes.Error, sr.Ended()[0].Status().Code)
264-
require.Len(t, sr.Ended()[0].Events(), 1)
265-
assert.Contains(t, sr.Ended()[0].Events()[0].Attributes, attribute.String("exception.message", "something went wrong"))
259+
// verify the error.type attribute is set
260+
err := errors.New("something went wrong")
261+
assert.Contains(t, sr.Ended()[0].Attributes(), semconv.ErrorType(err))
266262
})
267263
}
268264

0 commit comments

Comments
 (0)