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 support for Dynamic Sampling #491

Merged
merged 18 commits into from Dec 5, 2022
Merged
6 changes: 3 additions & 3 deletions client.go
Expand Up @@ -624,12 +624,12 @@ func (client *Client) prepareEvent(event *Event, hint *EventHint, scope EventMod

event.Platform = "go"
event.Sdk = SdkInfo{
Name: "sentry.go",
Version: Version,
Name: SDKIdentifier,
Version: SDKVersion,
Integrations: client.listIntegrations(),
Packages: []SdkPackage{{
Name: "sentry-go",
Version: Version,
Version: SDKVersion,
}},
}

Expand Down
4 changes: 2 additions & 2 deletions client_test.go
Expand Up @@ -266,14 +266,14 @@ func TestCaptureEvent(t *testing.T) {
Platform: "go",
Sdk: SdkInfo{
Name: "sentry.go",
Version: Version,
Version: SDKVersion,
Integrations: []string{},
Packages: []SdkPackage{
{
// FIXME: name format doesn't follow spec in
// https://docs.sentry.io/development/sdk-dev/event-payloads/sdk/
Name: "sentry-go",
Version: Version,
Version: SDKVersion,
},
// TODO: perhaps the list of packages is incomplete or there
// should not be any package at all. We may include references
Expand Down
45 changes: 45 additions & 0 deletions dynamic_sampling_context.go
@@ -0,0 +1,45 @@
package sentry

import (
"strings"

"github.com/getsentry/sentry-go/internal/otel/baggage"
)

const (
sentryPrefix = "sentry-"
)

// DynamicSamplingContext holds information about the current event that can be used to make dynamic sampling decisions.
type DynamicSamplingContext struct {
Entries map[string]string
Frozen bool
}

func NewDynamicSamplingContext(header []byte) (DynamicSamplingContext, error) {
bag, err := baggage.Parse(string(header))
if err != nil {
return DynamicSamplingContext{}, err
}

entries := map[string]string{}
for _, member := range bag.Members() {
// We only store baggage members if their key starts with "sentry-".
if k, v := member.Key(), member.Value(); strings.HasPrefix(k, sentryPrefix) {
entries[strings.TrimPrefix(k, sentryPrefix)] = v
}
}

return DynamicSamplingContext{
Entries: entries,
Frozen: true,
}, nil
}

func (d DynamicSamplingContext) HasEntries() bool {
return len(d.Entries) > 0
}

func (d DynamicSamplingContext) String() string {
return ""
cleptric marked this conversation as resolved.
Show resolved Hide resolved
}
50 changes: 50 additions & 0 deletions dynamic_sampling_context_test.go
@@ -0,0 +1,50 @@
package sentry

import (
"testing"
)

func TestNewDynamicSamplingContext(t *testing.T) {
tests := []struct {
input []byte
want DynamicSamplingContext
}{
{
input: []byte(""),
want: DynamicSamplingContext{
Frozen: true,
Entries: map[string]string{},
},
},
{
input: []byte("sentry-trace_id=d49d9bf66f13450b81f65bc51cf49c03,sentry-public_key=public,sentry-sample_rate=1"),
want: DynamicSamplingContext{
Frozen: true,
Entries: map[string]string{
"trace_id": "d49d9bf66f13450b81f65bc51cf49c03",
"public_key": "public",
"sample_rate": "1",
},
},
},
{
input: []byte("sentry-trace_id=d49d9bf66f13450b81f65bc51cf49c03,sentry-public_key=public,sentry-sample_rate=1,foo=bar;foo;bar;bar=baz"),
want: DynamicSamplingContext{
Frozen: true,
Entries: map[string]string{
"trace_id": "d49d9bf66f13450b81f65bc51cf49c03",
"public_key": "public",
"sample_rate": "1",
},
},
},
}

for _, tc := range tests {
got, err := NewDynamicSamplingContext(tc.input)
if err != nil {
t.Fatal(err)
}
assertEqual(t, got, tc.want)
}
}
16 changes: 16 additions & 0 deletions interfaces.go
Expand Up @@ -215,6 +215,18 @@ type Exception struct {
Stacktrace *Stacktrace `json:"stacktrace,omitempty"`
}

type SDKMetaDataKey = string

const (
DynamicSamplingContextKey SDKMetaDataKey = "DynamicSamplingContext"
)

// SDKMetaData is a struct to stash data which is needed at some point in the SDK's event processing pipeline
// but which shouldn't get send to Sentry.
type SDKMetaData = struct {
cleptric marked this conversation as resolved.
Show resolved Hide resolved
DynamicSamplingContextKey DynamicSamplingContext
}

// EventID is a hexadecimal string representing a unique uuid4 for an Event.
// An EventID must be 32 characters long, lowercase and not have any dashes.
type EventID string
Expand Down Expand Up @@ -251,6 +263,10 @@ type Event struct {
Type string `json:"type,omitempty"`
StartTime time.Time `json:"start_timestamp"`
Spans []*Span `json:"spans,omitempty"`

// The fields below are not part of the final JSON payload.

SDKMetaData SDKMetaData `json:"-"`
cleptric marked this conversation as resolved.
Show resolved Hide resolved
}

// TODO: Event.Contexts map[string]interface{} => map[string]EventContext,
Expand Down