From 5c69ada82c1331409271dc3be9e1fc93a9b45f1f Mon Sep 17 00:00:00 2001 From: Anton Ovchinnikov Date: Tue, 20 Dec 2022 17:43:03 +0100 Subject: [PATCH 1/3] fix(dynamic-sampling): Do not crash when Client is empty --- dynamic_sampling_context.go | 5 +++++ tracing_test.go | 9 +++++++++ 2 files changed, 14 insertions(+) diff --git a/dynamic_sampling_context.go b/dynamic_sampling_context.go index 3e54839ba..4f5559c16 100644 --- a/dynamic_sampling_context.go +++ b/dynamic_sampling_context.go @@ -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 != "" { diff --git a/tracing_test.go b/tracing_test.go index 8902f75d3..3cc13dff7 100644 --- a/tracing_test.go +++ b/tracing_test.go @@ -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() +} From af529f782bf2369ae290ca11d43683c58eb32390 Mon Sep 17 00:00:00 2001 From: Anton Ovchinnikov Date: Mon, 9 Jan 2023 17:37:40 +0100 Subject: [PATCH 2/3] Add another test --- dynamic_sampling_context.go | 5 ++++- dynamic_sampling_context_test.go | 13 +++++++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/dynamic_sampling_context.go b/dynamic_sampling_context.go index 4f5559c16..06feb2195 100644 --- a/dynamic_sampling_context.go +++ b/dynamic_sampling_context.go @@ -45,7 +45,10 @@ func DynamicSamplingContextFromTransaction(span *Span) DynamicSamplingContext { client := hub.Client() if client == nil || scope == nil { - return DynamicSamplingContext{} + return DynamicSamplingContext{ + Entries: map[string]string{}, + Frozen: false, + } } options := client.Options() diff --git a/dynamic_sampling_context_test.go b/dynamic_sampling_context_test.go index 6cca56591..5299bc1ea 100644 --- a/dynamic_sampling_context_test.go +++ b/dynamic_sampling_context_test.go @@ -1,6 +1,7 @@ package sentry import ( + "context" "strings" "testing" ) @@ -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 { From 3bd85b88dc4432b154f24a7e5938fd85168c7d73 Mon Sep 17 00:00:00 2001 From: Anton Ovchinnikov Date: Mon, 9 Jan 2023 17:37:51 +0100 Subject: [PATCH 3/3] update changelog --- CHANGELOG.md | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index ca66fa510..4442cb5bf 100644 --- a/CHANGELOG.md +++ b/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.