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
3 changes: 1 addition & 2 deletions .craft.yml
@@ -1,6 +1,5 @@
minVersion: 0.23.1
preReleaseCommand: bash scripts/craft-pre-release.sh
changelogPolicy: auto
changelogPolicy: simple
artifactProvider:
name: none
targets:
Expand Down
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
51 changes: 31 additions & 20 deletions client_test.go
Expand Up @@ -78,11 +78,15 @@ func TestCaptureMessageEmptyString(t *testing.T) {
},
}
got := transport.lastEvent
opts := cmp.Transformer("SimplifiedEvent", func(e *Event) *Event {
return &Event{
Exception: e.Exception,
}
})
opts := cmp.Options{
cmpopts.IgnoreFields(Event{}, "sdkMetaData"),
cmp.Transformer("SimplifiedEvent", func(e *Event) *Event {
return &Event{
Exception: e.Exception,
}
}),
}

if diff := cmp.Diff(want, got, opts); diff != "" {
t.Errorf("(-want +got):\n%s", diff)
}
Expand Down Expand Up @@ -266,14 +270,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 All @@ -282,7 +286,7 @@ func TestCaptureEvent(t *testing.T) {
},
}
got := transport.lastEvent
opts := cmp.Options{cmpopts.IgnoreFields(Event{}, "Release")}
opts := cmp.Options{cmpopts.IgnoreFields(Event{}, "Release", "sdkMetaData")}
if diff := cmp.Diff(want, got, opts); diff != "" {
t.Errorf("Event mismatch (-want +got):\n%s", diff)
}
Expand All @@ -309,11 +313,14 @@ func TestCaptureEventNil(t *testing.T) {
},
}
got := transport.lastEvent
opts := cmp.Transformer("SimplifiedEvent", func(e *Event) *Event {
return &Event{
Exception: e.Exception,
}
})
opts := cmp.Options{
cmpopts.IgnoreFields(Event{}, "sdkMetaData"),
cmp.Transformer("SimplifiedEvent", func(e *Event) *Event {
return &Event{
Exception: e.Exception,
}
}),
}
if diff := cmp.Diff(want, got, opts); diff != "" {
t.Errorf("(-want +got):\n%s", diff)
}
Expand Down Expand Up @@ -476,13 +483,17 @@ func TestRecover(t *testing.T) {
t.Fatalf("events = %s\ngot %d events, want 1", b, len(events))
}
got := events[0]
opts := cmp.Transformer("SimplifiedEvent", func(e *Event) *Event {
return &Event{
Message: e.Message,
Exception: e.Exception,
Level: e.Level,
}
})
opts := cmp.Options{
cmpopts.IgnoreFields(Event{}, "sdkMetaData"),
cmp.Transformer("SimplifiedEvent", func(e *Event) *Event {
return &Event{
Message: e.Message,
Exception: e.Exception,
Level: e.Level,
}
}),
}

if diff := cmp.Diff(want, got, opts); diff != "" {
t.Errorf("(-want +got):\n%s", diff)
}
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)
}
}
1 change: 1 addition & 0 deletions fasthttp/sentryfasthttp_test.go
Expand Up @@ -208,6 +208,7 @@ func TestIntegration(t *testing.T) {
sentry.Event{},
"Contexts", "EventID", "Extra", "Platform", "Modules",
"Release", "Sdk", "ServerName", "Tags", "Timestamp",
"sdkMetaData",
),
cmpopts.IgnoreMapEntries(func(k string, v string) bool {
// fasthttp changed Content-Length behavior in
Expand Down
1 change: 1 addition & 0 deletions http/sentryhttp_test.go
Expand Up @@ -216,6 +216,7 @@ func TestIntegration(t *testing.T) {
sentry.Event{},
"Contexts", "EventID", "Extra", "Platform", "Modules",
"Release", "Sdk", "ServerName", "Tags", "Timestamp",
"sdkMetaData",
),
cmpopts.IgnoreFields(
sentry.Request{},
Expand Down
10 changes: 10 additions & 0 deletions interfaces.go
Expand Up @@ -215,6 +215,12 @@ type Exception struct {
Stacktrace *Stacktrace `json:"stacktrace,omitempty"`
}

// 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 {
dsc 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 +257,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
}

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