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

fix(dynamic-sampling): Do not crash in Span.Finish() when Client is empty #520

Merged
merged 4 commits into from Jan 11, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 5 additions & 0 deletions dynamic_sampling_context.go
Expand Up @@ -43,6 +43,11 @@ func DynamicSamplingContextFromTransaction(span *Span) DynamicSamplingContext {
hub := hubFromContext(span.Context())
scope := hub.Scope()
client := hub.Client()

if client == nil || scope == nil {
return DynamicSamplingContext{}
}

options := client.Options()

if traceID := span.TraceID.String(); traceID != "" {
Expand Down
9 changes: 9 additions & 0 deletions tracing_test.go
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()
}