Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(otelgin): enhance gin error tracking with span recording #6346

Open
wants to merge 23 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 14 commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
60f2e0c
feat(otelgin): enhance gin error tracking with span recording
flc1125 Nov 19, 2024
afe0635
Merge branch 'main' into gintrace-recorderror
flc1125 Nov 19, 2024
7817837
feat(otelgin): enhance gin error tracking with span recording
flc1125 Nov 20, 2024
dcc6c62
feat(otelgin): enhance gin error tracking with span recording
flc1125 Nov 20, 2024
f5d9a1c
Update CHANGELOG.md
flc1125 Nov 20, 2024
84f2f0e
test(otelgin): Add tests for span recording on errors
flc1125 Nov 21, 2024
e5ef74d
refactor(otelgin): Remove unused dependencies and simplify tests
flc1125 Nov 21, 2024
9cdd7d6
refactor(otelgin): Remove unused dependencies and simplify tests
flc1125 Nov 21, 2024
9b368d5
Merge branch 'main' into gintrace-recorderror
flc1125 Nov 21, 2024
cc1c612
test(instrumentation): remove `gin.errors` assertion in gintrace_test.go
flc1125 Nov 21, 2024
fc2dd22
Update instrumentation/github.com/gin-gonic/gin/otelgin/gintrace.go
flc1125 Nov 21, 2024
488835c
feat(otelgin): refine gin trace error handling and test cases
flc1125 Nov 21, 2024
92ff337
feat(otelgin): refine gin trace error handling and test cases
flc1125 Nov 21, 2024
23949c7
test(otelgin): remove redundant code
flc1125 Nov 21, 2024
5f403b8
chore(otelgin): refine error recording in gintrace
flc1125 Nov 23, 2024
1fa4d17
Merge branch 'main' into gintrace-recorderror
flc1125 Nov 23, 2024
05c91e7
Update CHANGELOG.md
flc1125 Nov 25, 2024
8c62a63
Update instrumentation/github.com/gin-gonic/gin/otelgin/test/gintrace…
flc1125 Nov 25, 2024
0a2260d
chore(otelgin): adjust imports sorting
flc1125 Nov 25, 2024
733d40a
Merge remote-tracking branch 'origin/gintrace-recorderror' into gintr…
flc1125 Nov 25, 2024
d280f2e
Merge branch 'main' into gintrace-recorderror
flc1125 Nov 25, 2024
3ab2b2c
feat(otelgin): enhance error handling and test coverage in gintrace
flc1125 Nov 25, 2024
a690136
Update instrumentation/github.com/gin-gonic/gin/otelgin/test/gintrace…
flc1125 Nov 26, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm
- Added support for providing `endpoint`, `pollingIntervalMs` and `initialSamplingRate` using environment variable `OTEL_TRACES_SAMPLER_ARG` in `go.opentelemetry.io/contrib/samples/jaegerremote`. (#6310)
- Added support exporting logs via OTLP over gRPC in `go.opentelemetry.io/contrib/config`. (#6340)

### Changed

- Record an error instead of setting the `gin.errors` attribute in `go.opentelemetry.io/contrib/instrumentation/github.com/gin-gonic/gin/otelgin`. (#6346)
flc1125 marked this conversation as resolved.
Show resolved Hide resolved

### Fixed

- Fixed the value for configuring the OTLP exporter to use `grpc` instead of `grpc/protobuf` in `go.opentelemetry.io/contrib/config`. (#6338)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,19 @@
package otelgin // import "go.opentelemetry.io/contrib/instrumentation/github.com/gin-gonic/gin/otelgin"

import (
"errors"
"fmt"

"github.com/gin-gonic/gin"

"go.opentelemetry.io/contrib/instrumentation/github.com/gin-gonic/gin/otelgin/internal/semconvutil"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/codes"
"go.opentelemetry.io/otel/propagation"
semconv "go.opentelemetry.io/otel/semconv/v1.20.0"
oteltrace "go.opentelemetry.io/otel/trace"

"go.opentelemetry.io/contrib/instrumentation/github.com/gin-gonic/gin/otelgin/internal/semconvutil"
flc1125 marked this conversation as resolved.
Show resolved Hide resolved
)

const (
Expand Down Expand Up @@ -95,7 +97,7 @@ func Middleware(service string, opts ...Option) gin.HandlerFunc {
span.SetAttributes(semconv.HTTPStatusCode(status))
}
if len(c.Errors) > 0 {
span.SetAttributes(attribute.String("gin.errors", c.Errors.String()))
span.RecordError(errors.New(c.Errors.String()))
flc1125 marked this conversation as resolved.
Show resolved Hide resolved
flc1125 marked this conversation as resolved.
Show resolved Hide resolved
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,14 @@ import (
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"go.opentelemetry.io/contrib/instrumentation/github.com/gin-gonic/gin/otelgin"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/codes"
sdktrace "go.opentelemetry.io/otel/sdk/trace"
"go.opentelemetry.io/otel/sdk/trace/tracetest"

"go.opentelemetry.io/otel/attribute"
oteltrace "go.opentelemetry.io/otel/trace"

"go.opentelemetry.io/contrib/instrumentation/github.com/gin-gonic/gin/otelgin"
flc1125 marked this conversation as resolved.
Show resolved Hide resolved
)

func init() {
Expand Down Expand Up @@ -91,6 +91,7 @@ func TestTrace200(t *testing.T) {
assert.Contains(t, attr, attribute.Int("http.status_code", http.StatusOK))
assert.Contains(t, attr, attribute.String("http.method", "GET"))
assert.Contains(t, attr, attribute.String("http.route", "/user/:id"))
assert.Empty(t, span.Events())
}

func TestError(t *testing.T) {
Expand Down Expand Up @@ -120,7 +121,13 @@ func TestError(t *testing.T) {
attr := span.Attributes()
assert.Contains(t, attr, attribute.String("net.host.name", "foobar"))
assert.Contains(t, attr, attribute.Int("http.status_code", http.StatusInternalServerError))
assert.Contains(t, attr, attribute.String("gin.errors", "Error #01: oh no\n"))
flc1125 marked this conversation as resolved.
Show resolved Hide resolved

// verify the error event
flc1125 marked this conversation as resolved.
Show resolved Hide resolved
events := span.Events()
assert.Len(t, events, 1)
assert.Equal(t, "exception", events[0].Name)
assert.Contains(t, events[0].Attributes, attribute.String("exception.type", "*errors.errorString"))
assert.Contains(t, events[0].Attributes, attribute.String("exception.message", "Error #01: oh no\n"))
// server errors set the status
assert.Equal(t, codes.Error, span.Status().Code)
}
Expand Down
Loading