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 all commits
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
7 changes: 7 additions & 0 deletions CHANGELOG.md
@@ -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
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,
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Made it more explicit to make sure there are no default-value surprises.

}
}

options := client.Options()

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