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

feat: Add Span.SetData() #542

Merged
merged 2 commits into from Jan 23, 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
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -7,6 +7,7 @@
- Add SentryTraceHeader and SentryBaggageHeader constants ([#538](https://github.com/getsentry/sentry-go/pull/538/))
- Add `Span.SetDynamicSamplingContext` method ([#539](https://github.com/getsentry/sentry-go/pull/539/))
- Add DSN getters ([#540](https://github.com/getsentry/sentry-go/pull/540))
- Add Span.SetData() ([#542](https://github.com/getsentry/sentry-go/pull/542))

## 0.17.0

Expand Down
10 changes: 10 additions & 0 deletions tracing.go
Expand Up @@ -211,6 +211,16 @@ func (s *Span) SetTag(name, value string) {
s.Tags[name] = value
}

// SetData sets a data on the span. It is recommended to use SetData instead of
// accessing the data map directly as SetData takes care of initializing the map
// when necessary.
func (s *Span) SetData(name, value string) {
if s.Data == nil {
s.Data = make(map[string]interface{})
}
s.Data[name] = value
}

// TODO(tracing): maybe add shortcuts to get/set transaction name. Right now the
// transaction name is in the Scope, as it has existed there historically, prior
// to tracing.
Expand Down
24 changes: 24 additions & 0 deletions tracing_test.go
Expand Up @@ -313,6 +313,30 @@ func TestStartTransaction(t *testing.T) {
}
}

func TestSetTag(t *testing.T) {
ctx := NewTestContext(ClientOptions{
EnableTracing: true,
})
span := StartSpan(ctx, "Test Span")
span.SetTag("key", "value")

if (span.Tags == nil) || (span.Tags["key"] != "value") {
t.Fatalf("Tags mismatch, got %v", span.Tags)
}
}

func TestSetData(t *testing.T) {
ctx := NewTestContext(ClientOptions{
EnableTracing: true,
})
span := StartSpan(ctx, "Test Span")
span.SetData("key", "value")

if (span.Data == nil) || (span.Data["key"] != "value") {
t.Fatalf("Data mismatch, got %v", span.Data)
}
}

// testContextKey is used to store a value in a context so that we can check
// that SDK operations on that context preserve the original context values.
type testContextKey struct{}
Expand Down