Skip to content

Commit

Permalink
fix(dynamic-sampling): Do not crash in Span.Finish() when Client is e…
Browse files Browse the repository at this point in the history
…mpty (#520)
  • Loading branch information
tonyo authored Jan 11, 2023
1 parent e081f7e commit 3c8662b
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 0 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
# Changelog

## Unreleased

### Bug Fixes

- fix(dynamic-sampling): Do not crash in Span.Finish() when Client is empty [#520](https://github.com/getsentry/sentry-go/pull/520)
- Fixes [#518](https://github.com/getsentry/sentry-go/issues/518)

## 0.16.0

The Sentry SDK team is happy to announce the immediate availability of Sentry Go SDK v0.16.0.
Expand Down
8 changes: 8 additions & 0 deletions dynamic_sampling_context.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,14 @@ func DynamicSamplingContextFromTransaction(span *Span) DynamicSamplingContext {
hub := hubFromContext(span.Context())
scope := hub.Scope()
client := hub.Client()

if client == nil || scope == nil {
return DynamicSamplingContext{
Entries: map[string]string{},
Frozen: false,
}
}

options := client.Options()

if traceID := span.TraceID.String(); traceID != "" {
Expand Down
13 changes: 13 additions & 0 deletions dynamic_sampling_context_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package sentry

import (
"context"
"strings"
"testing"
)
Expand Down Expand Up @@ -108,6 +109,18 @@ func TestDynamicSamplingContextFromTransaction(t *testing.T) {
},
},
},
// Empty context without a valid Client
{
input: func() *Span {
ctx := context.Background()
tx := StartTransaction(ctx, "op")
return tx
}(),
want: DynamicSamplingContext{
Frozen: false,
Entries: map[string]string{},
},
},
}

for _, tc := range tests {
Expand Down
9 changes: 9 additions & 0 deletions tracing_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -567,3 +567,12 @@ func TestSample(t *testing.T) {
t.Fatalf("got %s, want %s", got, SampledTrue)
}
}

func TestDoesNotCrashWithEmptyContext(t *testing.T) {
// This test makes sure that we can still start and finish transactions
// with empty context (for example, when Sentry SDK is not initialized)
ctx := context.Background()
tx := StartTransaction(ctx, "op")
tx.Sampled = SampledTrue
tx.Finish()
}

0 comments on commit 3c8662b

Please # to comment.