Skip to content

Commit 0b7fca7

Browse files
committed
chore: remove superfluous config and MR comments
1 parent 1a0e9d4 commit 0b7fca7

File tree

4 files changed

+56
-99
lines changed

4 files changed

+56
-99
lines changed

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ The next release will require at least [Go 1.21].
2727
- Add support for Summary metrics to `go.opentelemetry.io/contrib/bridges/prometheus`. (#5089)
2828
- Add support for Exponential (native) Histograms in `go.opentelemetry.io/contrib/bridges/prometheus`. (#5093)
2929
- Implemented setting the `cloud.resource_id` resource attribute in `go.opentelemetry.io/detectors/aws/ecs` based on the ECS Metadata v4 endpoint. (#5091)
30-
- Add support to record panics in `go.opentelemetry.io.contrib/instrumentation/github.com/gin-gonic/gin/otelgin` (#5088)
30+
- Add support to record panics in `go.opentelemetry.io/contrib/instrumentation/github.com/gin-gonic/gin/otelgin`. (#5090)
3131

3232
### Removed
3333

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

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -76,14 +76,12 @@ func Middleware(service string, opts ...Option) gin.HandlerFunc {
7676
}
7777
ctx, span := tracer.Start(ctx, spanName, opts...)
7878
defer func() {
79-
if cfg.RecordPanicConfig.enabled {
80-
if r := recover(); r != nil {
81-
err := fmt.Errorf("error handling request: %s", r)
82-
span.RecordError(err, oteltrace.WithStackTrace(cfg.RecordPanicConfig.stackTrace))
83-
span.SetStatus(codes.Error, "panic recovered")
84-
span.End()
85-
panic(r)
86-
}
79+
if r := recover(); r != nil {
80+
err := fmt.Errorf("%+v", r)
81+
span.RecordError(err, oteltrace.WithStackTrace(cfg.RecordPanicStackTrace))
82+
span.SetStatus(codes.Error, err.Error())
83+
span.End()
84+
panic(r)
8785
}
8886
span.End()
8987
}()

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

Lines changed: 8 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -13,51 +13,11 @@ import (
1313
)
1414

1515
type config struct {
16-
TracerProvider oteltrace.TracerProvider
17-
Propagators propagation.TextMapPropagator
18-
Filters []Filter
19-
SpanNameFormatter SpanNameFormatter
20-
RecordPanicConfig RecordPanicConfig
21-
}
22-
23-
// RecordPanicConfig is a configuration for panic recording.
24-
type RecordPanicConfig struct {
25-
enabled bool
26-
stackTrace bool
27-
}
28-
29-
// RecordPanicOption applies a configuration for panic recording.
30-
type RecordPanicOption interface {
31-
apply(*RecordPanicConfig)
32-
}
33-
34-
type recordPanicOptionFunc func(*RecordPanicConfig)
35-
36-
func (o recordPanicOptionFunc) apply(c *RecordPanicConfig) {
37-
o(c)
38-
}
39-
40-
// WithRecordPanicEnabled enables panic recording based on the provided boolean.
41-
func WithRecordPanicEnabled(enabled bool) RecordPanicOption {
42-
return recordPanicOptionFunc(func(cfg *RecordPanicConfig) {
43-
cfg.enabled = enabled
44-
})
45-
}
46-
47-
// WithRecordPanicStackTrace enables recording of the panic stack trace based on the provided boolean.
48-
func WithRecordPanicStackTrace(stackTrace bool) RecordPanicOption {
49-
return recordPanicOptionFunc(func(cfg *RecordPanicConfig) {
50-
cfg.stackTrace = stackTrace
51-
})
52-
}
53-
54-
// NewRecordPanicConfig creates a new RecordPanicConfig with the provided options.
55-
func NewRecordPanicConfig(opts ...RecordPanicOption) RecordPanicConfig {
56-
cfg := RecordPanicConfig{}
57-
for _, opt := range opts {
58-
opt.apply(&cfg)
59-
}
60-
return cfg
16+
TracerProvider oteltrace.TracerProvider
17+
Propagators propagation.TextMapPropagator
18+
Filters []Filter
19+
SpanNameFormatter SpanNameFormatter
20+
RecordPanicStackTrace bool
6121
}
6222

6323
// Filter is a predicate used to determine whether a given http.request should
@@ -119,9 +79,9 @@ func WithSpanNameFormatter(f func(r *http.Request) string) Option {
11979
})
12080
}
12181

122-
// WithRecordPanicConfig specifies a configuration for panic recording.
123-
func WithRecordPanicConfig(cfg RecordPanicConfig) Option {
82+
// WithRecordPanicStackTrace specifies whether to record the stack trace of a panic.
83+
func WithRecordPanicStackTrace(stackTrace bool) Option {
12484
return optionFunc(func(c *config) {
125-
c.RecordPanicConfig = cfg
85+
c.RecordPanicStackTrace = stackTrace
12686
})
12787
}

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

Lines changed: 41 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -261,48 +261,47 @@ func TestRecordPanic(t *testing.T) {
261261
c.Next()
262262
}
263263

264-
t.Run("panic recovered and recorded", func(t *testing.T) {
265-
sr := tracetest.NewSpanRecorder()
266-
provider := sdktrace.NewTracerProvider()
267-
provider.RegisterSpanProcessor(sr)
268-
router := gin.New()
269-
recordPanicConfig := otelgin.NewRecordPanicConfig(otelgin.WithRecordPanicEnabled(true), otelgin.WithRecordPanicStackTrace(true))
270-
router.Use(recoveryMiddleware, otelgin.Middleware("potato", otelgin.WithTracerProvider(provider), otelgin.WithRecordPanicConfig(recordPanicConfig)))
271-
router.GET("/user/:id", func(c *gin.Context) { panic("corn") })
272-
router.ServeHTTP(httptest.NewRecorder(), httptest.NewRequest("GET", "/user/123", nil))
273-
274-
require.Len(t, sr.Ended(), 1, "should emit a span")
275-
span := sr.Ended()[0]
276-
assert.Equal(t, span.Status().Code, codes.Error, "should set Error status for panics")
277-
require.Len(t, span.Events(), 1, "should emit an event")
278-
event := span.Events()[0]
279-
assert.Equal(t, event.Name, "exception")
280-
281-
var foundStackTrace bool
282-
283-
for _, attr := range event.Attributes {
284-
if attr.Key == "exception.stacktrace" {
285-
foundStackTrace = true
286-
break
287-
}
288-
}
264+
testCases := []struct {
265+
name string
266+
expectStackTrace bool
267+
}{
268+
{
269+
name: "should record stack trace",
270+
expectStackTrace: true,
271+
},
272+
{
273+
name: "should not record stack trace",
274+
expectStackTrace: false,
275+
},
276+
}
289277

290-
assert.True(t, foundStackTrace, "should record a stack trace")
291-
})
278+
for _, tc := range testCases {
279+
t.Run(tc.name, func(t *testing.T) {
280+
sr := tracetest.NewSpanRecorder()
281+
provider := sdktrace.NewTracerProvider()
282+
provider.RegisterSpanProcessor(sr)
283+
router := gin.New()
284+
router.Use(recoveryMiddleware, otelgin.Middleware("potato", otelgin.WithTracerProvider(provider), otelgin.WithRecordPanicStackTrace(tc.expectStackTrace)))
285+
router.GET("/user/:id", func(c *gin.Context) { panic("corn") })
286+
router.ServeHTTP(httptest.NewRecorder(), httptest.NewRequest("GET", "/user/123", nil))
292287

293-
t.Run("panic recovered and not recorded", func(t *testing.T) {
294-
sr := tracetest.NewSpanRecorder()
295-
provider := sdktrace.NewTracerProvider()
296-
provider.RegisterSpanProcessor(sr)
297-
router := gin.New()
298-
recordPanicConfig := otelgin.NewRecordPanicConfig(otelgin.WithRecordPanicEnabled(false))
299-
router.Use(recoveryMiddleware, otelgin.Middleware("potato", otelgin.WithTracerProvider(provider), otelgin.WithRecordPanicConfig(recordPanicConfig)))
300-
router.GET("/user/:id", func(c *gin.Context) { panic("corn") })
301-
router.ServeHTTP(httptest.NewRecorder(), httptest.NewRequest("GET", "/user/123", nil))
302-
303-
require.Len(t, sr.Ended(), 1, "should emit a span")
304-
span := sr.Ended()[0]
305-
assert.Equal(t, span.Status().Code, codes.Unset, "should set Unset status for unrecovered panics")
306-
require.Empty(t, span.Events(), "should not emit an event")
307-
})
288+
require.Len(t, sr.Ended(), 1, "should emit a span")
289+
span := sr.Ended()[0]
290+
assert.Equal(t, span.Status().Code, codes.Error, "should set Error status for panics")
291+
require.Len(t, span.Events(), 1, "should emit an event")
292+
event := span.Events()[0]
293+
assert.Equal(t, event.Name, "exception")
294+
295+
var foundStackTrace bool
296+
297+
for _, attr := range event.Attributes {
298+
if attr.Key == "exception.stacktrace" {
299+
foundStackTrace = true
300+
break
301+
}
302+
}
303+
304+
assert.Equal(t, tc.expectStackTrace, foundStackTrace, "should record a stack trace")
305+
})
306+
}
308307
}

0 commit comments

Comments
 (0)