-
Notifications
You must be signed in to change notification settings - Fork 213
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
675 additions
and
55 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -97,14 +97,13 @@ func TestDynamicSamplingContextFromTransaction(t *testing.T) { | |
want: DynamicSamplingContext{ | ||
Frozen: true, | ||
Entries: map[string]string{ | ||
"sample_rate": "1", | ||
"trace_id": "d49d9bf66f13450b81f65bc51cf49c03", | ||
"public_key": "public", | ||
"release": "1.0.0", | ||
"environment": "test", | ||
"transaction": "name", | ||
"user_segment": "user_segment", | ||
"sampled": "true", | ||
"sample_rate": "1", | ||
"trace_id": "d49d9bf66f13450b81f65bc51cf49c03", | ||
"public_key": "public", | ||
"release": "1.0.0", | ||
"environment": "test", | ||
"transaction": "name", | ||
"sampled": "true", | ||
}, | ||
}, | ||
}, | ||
|
@@ -181,3 +180,68 @@ func TestString(t *testing.T) { | |
} | ||
testutils.AssertBaggageStringsEqual(t, dsc.String(), "sentry-trace_id=d49d9bf66f13450b81f65bc51cf49c03,sentry-public_key=public,sentry-sample_rate=1") | ||
} | ||
|
||
func TestDynamicSamplingContextFromScope(t *testing.T) { | ||
tests := map[string]struct { | ||
scope *Scope | ||
client *Client | ||
expected DynamicSamplingContext | ||
}{ | ||
"Valid input": { | ||
scope: &Scope{ | ||
propagationContext: PropagationContext{ | ||
TraceID: TraceIDFromHex("d49d9bf66f13450b81f65bc51cf49c03"), | ||
SpanID: SpanIDFromHex("a9f442f9330b4e09"), | ||
}, | ||
}, | ||
client: func() *Client { | ||
dsn, _ := NewDsn("http://[email protected]/sentry/1") | ||
return &Client{ | ||
options: ClientOptions{ | ||
Dsn: dsn.String(), | ||
Release: "1.0.0", | ||
Environment: "production", | ||
}, | ||
dsn: dsn, | ||
} | ||
}(), | ||
expected: DynamicSamplingContext{ | ||
Entries: map[string]string{ | ||
"trace_id": "d49d9bf66f13450b81f65bc51cf49c03", | ||
"public_key": "public", | ||
"release": "1.0.0", | ||
"environment": "production", | ||
}, | ||
Frozen: true, | ||
}, | ||
}, | ||
"Nil client": { | ||
scope: &Scope{ | ||
propagationContext: PropagationContext{ | ||
TraceID: TraceIDFromHex("d49d9bf66f13450b81f65bc51cf49c03"), | ||
SpanID: SpanIDFromHex("a9f442f9330b4e09"), | ||
}, | ||
}, | ||
client: nil, | ||
expected: DynamicSamplingContext{ | ||
Entries: map[string]string{}, | ||
Frozen: false, | ||
}, | ||
}, | ||
"Nil scope": { | ||
scope: nil, | ||
client: &Client{}, | ||
expected: DynamicSamplingContext{ | ||
Entries: map[string]string{}, | ||
Frozen: false, | ||
}, | ||
}, | ||
} | ||
|
||
for name, tt := range tests { | ||
t.Run(name, func(t *testing.T) { | ||
result := DynamicSamplingContextFromScope(tt.scope, tt.client) | ||
assertEqual(t, tt.expected, result) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,12 +2,14 @@ package sentry | |
|
||
import ( | ||
"context" | ||
"errors" | ||
"fmt" | ||
"sync" | ||
"testing" | ||
|
||
"github.com/google/go-cmp/cmp" | ||
"github.com/google/go-cmp/cmp/cmpopts" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
const testDsn = "http://[email protected]/1337" | ||
|
@@ -324,6 +326,88 @@ func TestHasHubOnContextReturnsFalseIfHubIsNotThere(t *testing.T) { | |
assertEqual(t, false, HasHubOnContext(ctx)) | ||
} | ||
|
||
func TestHub_ContinueTrace(t *testing.T) { | ||
newScope := func() *Scope { | ||
return &Scope{contexts: make(map[string]Context)} | ||
} | ||
|
||
mockClient := &Client{options: ClientOptions{EnableTracing: true}} | ||
|
||
tests := map[string]struct { | ||
hub *Hub | ||
trace string | ||
baggage string | ||
expectedErr error | ||
expectedSpan bool // Whether a SpanOption is expected to be returned | ||
checkScope func(t *testing.T, scope *Scope) // Additional checks on the scope | ||
}{ | ||
"Valid trace and baggage": { | ||
hub: NewHub(mockClient, newScope()), | ||
trace: "4fbfb1b884c8532962a3c0b7b834428e-a9f442f9330b4e09", | ||
baggage: "sentry-release=1.0.0,sentry-environment=production", | ||
expectedErr: nil, | ||
expectedSpan: true, | ||
checkScope: func(t *testing.T, scope *Scope) { | ||
assert.Equal(t, "4fbfb1b884c8532962a3c0b7b834428e", scope.propagationContext.TraceID.String()) | ||
}, | ||
}, | ||
"Invalid trace": { | ||
hub: NewHub(mockClient, newScope()), | ||
trace: "invalid", | ||
baggage: "sentry-release=1.0.0,sentry-environment=production", | ||
expectedErr: nil, | ||
expectedSpan: true, | ||
checkScope: func(t *testing.T, scope *Scope) { | ||
assert.NotEmpty(t, scope.propagationContext.TraceID.String()) | ||
}, | ||
}, | ||
"Invalid baggage": { | ||
hub: NewHub(mockClient, newScope()), | ||
trace: "4fbfb1b884c8532962a3c0b7b834428e-a9f442f9330b4e09", | ||
baggage: "invalid_baggage", | ||
expectedErr: errors.New("invalid baggage list-member: \"invalid_baggage\""), | ||
expectedSpan: false, | ||
checkScope: func(t *testing.T, scope *Scope) { | ||
assert.Equal(t, "00000000000000000000000000000000", scope.propagationContext.TraceID.String()) | ||
}, | ||
}, | ||
"Tracing not enabled": { | ||
hub: NewHub(&Client{options: ClientOptions{EnableTracing: false}}, newScope()), | ||
trace: "4fbfb1b884c8532962a3c0b7b834428e-a9f442f9330b4e09", | ||
baggage: "sentry-release=1.0.0,sentry-environment=production", | ||
expectedErr: nil, | ||
expectedSpan: false, | ||
checkScope: func(t *testing.T, scope *Scope) { | ||
assert.Equal(t, "4fbfb1b884c8532962a3c0b7b834428e", scope.propagationContext.TraceID.String()) | ||
assert.Contains(t, scope.contexts, "trace") | ||
}, | ||
}, | ||
} | ||
|
||
for name, tt := range tests { | ||
t.Run(name, func(t *testing.T) { | ||
opt, err := tt.hub.ContinueTrace(tt.trace, tt.baggage) | ||
|
||
if tt.expectedErr != nil { | ||
assert.Error(t, err, "expected error, got nil") | ||
assert.Equal(t, tt.expectedErr.Error(), err.Error()) | ||
} else { | ||
assert.NoError(t, err, "expected no error, got one") | ||
} | ||
|
||
// Check for expected SpanOption | ||
if tt.expectedSpan { | ||
assert.NotNil(t, opt, "expected SpanOption, got nil") | ||
} else { | ||
assert.Nil(t, opt, "expected no SpanOption, got one") | ||
} | ||
|
||
// Additional checks on the scope | ||
tt.checkScope(t, tt.hub.Scope()) | ||
}) | ||
} | ||
} | ||
|
||
func TestGetHubFromContext(t *testing.T) { | ||
hub, _, _ := setupHubTest() | ||
ctx := context.Background() | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.