@@ -5,26 +5,53 @@ import { API } from '../../src/api';
5
5
import { eventToSentryRequest } from '../../src/request' ;
6
6
7
7
describe ( 'eventToSentryRequest' , ( ) => {
8
- const api = new API ( 'https://[email protected] /12312012' ) ;
9
- const event : Event = {
10
- contexts : { trace : { trace_id : '1231201211212012' , span_id : '12261980' , op : 'pageload' } } ,
11
- environment : 'dogpark' ,
12
- event_id : '0908201304152013' ,
13
- release : 'off.leash.park' ,
8
+ const public_key = 'dogsarebadatkeepingsecrets' ;
9
+ const api = new API ( `https://${ public_key } @squirrelchasers.ingest.sentry.io/12312012` ) ;
10
+
11
+ const event_id = '1231201211212012' ;
12
+ const trace_id = '0908201304152013' ;
13
+ const environment = 'dogpark' ;
14
+ const release = 'off.leash.park' ;
15
+
16
+ const baseEvent : Event = {
17
+ event_id,
18
+ environment,
19
+ release,
20
+ contexts : { trace : { trace_id : trace_id , span_id : '12261980' , op : 'ball.fetch' } } ,
21
+ // TODO
22
+ user : { id : '1121' , username : 'CharlieDog' , ip_address : '11.21.20.12' } ,
23
+ } ;
24
+
25
+ const errorEvent : Event = {
26
+ ...baseEvent ,
27
+ exception : { values : [ { type : 'PuppyProblemsError' , value : 'Charlie ate the flip-flops :-(' } ] } ,
28
+ } ;
29
+
30
+ const tracestateObject = {
31
+ trace_id,
32
+ environment,
33
+ release,
34
+ public_key,
35
+ } ;
36
+
37
+ const transactionEvent : Event = {
38
+ ...baseEvent ,
14
39
spans : [ ] ,
40
+ tags : {
41
+ dog : 'Charlie' ,
42
+ __sentry_samplingMethod : TransactionSamplingMethod . Rate ,
43
+ __sentry_sampleRate : '.1121' ,
44
+ __sentry_tracestate : computeTracestate ( tracestateObject ) ,
45
+ } ,
15
46
transaction : '/dogs/are/great/' ,
16
47
type : 'transaction' ,
17
- user : { id : '1121' , username : 'CharlieDog' , ip_address : '11.21.20.12' } ,
18
48
} ;
19
49
20
50
it ( 'injects correct data for error/message events' , ( ) => {
21
- const event = {
22
- event_id : '1231201211212012' ,
23
- exception : { values : [ { type : 'PuppyProblemsError' , value : 'Charlie ate the flip-flops :-(' } ] } ,
24
- user : { id : '1121' , username : 'CharlieDog' , ip_address : '11.21.20.12' } ,
25
- } ;
51
+ const event = { ...errorEvent } ;
26
52
27
53
const result = eventToSentryRequest ( event , api ) ;
54
+
28
55
expect ( result . type ) . toEqual ( 'event' ) ;
29
56
expect ( result . url ) . toEqual (
30
57
'https://squirrelchasers.ingest.sentry.io/api/12312012/store/?sentry_key=dogsarebadatkeepingsecrets&sentry_version=7' ,
@@ -33,29 +60,9 @@ describe('eventToSentryRequest', () => {
33
60
} ) ;
34
61
35
62
it ( 'injects correct data for transaction events' , ( ) => {
36
- const eventId = '1231201211212012' ;
37
- const traceId = '0908201304152013' ;
38
- const environment = 'dogpark' ;
39
- const release = 'off.leash.park' ;
40
-
41
- const event = {
42
- contexts : { trace : { trace_id : traceId , span_id : '12261980' , op : 'pageload' } } ,
43
- environment,
44
- event_id : eventId ,
45
- release,
46
- spans : [ ] ,
47
- transaction : '/dogs/are/great/' ,
48
- tracestate : computeTracestate ( {
49
- trace_id : traceId ,
50
- environment,
51
- public_key : 'dogsarebadatkeepingsecrets' ,
52
- release,
53
- } ) ,
54
- type : 'transaction' ,
55
- user : { id : '1121' , username : 'CharlieDog' , ip_address : '11.21.20.12' } ,
56
- } ;
63
+ const event = { ...transactionEvent } ;
57
64
58
- const result = eventToSentryRequest ( event as Event , api ) ;
65
+ const result = eventToSentryRequest ( event , api ) ;
59
66
60
67
const [ envelopeHeaderString , itemHeaderString , eventString ] = result . body . split ( '\n' ) ;
61
68
@@ -71,14 +78,9 @@ describe('eventToSentryRequest', () => {
71
78
) ;
72
79
73
80
expect ( envelope . envelopeHeader ) . toEqual ( {
74
- event_id : eventId ,
81
+ event_id,
75
82
sent_at : expect . any ( String ) ,
76
- trace : {
77
- trace_id : traceId ,
78
- environment,
79
- public_key : 'dogsarebadatkeepingsecrets' ,
80
- release,
81
- } ,
83
+ trace : tracestateObject ,
82
84
} ) ;
83
85
expect ( envelope . itemHeader ) . toEqual ( {
84
86
type : 'transaction' ,
@@ -88,40 +90,47 @@ describe('eventToSentryRequest', () => {
88
90
} ) ;
89
91
90
92
[
91
- { method : TransactionSamplingMethod . Rate , rate : '0.1121' , dog : 'Charlie' } ,
92
- { method : TransactionSamplingMethod . Sampler , rate : '0.1231' , dog : 'Maisey' } ,
93
- { method : TransactionSamplingMethod . Inheritance , dog : 'Cory' } ,
94
- { method : TransactionSamplingMethod . Explicit , dog : 'Bodhi' } ,
93
+ // TODO kmclb - once tag types are loosened, don't need to cast rate and undefined to strings here
94
+ { __sentry_samplingMethod : TransactionSamplingMethod . Rate , __sentry_sampleRate : '0.1121' , dog : 'Charlie' } ,
95
+ { __sentry_samplingMethod : TransactionSamplingMethod . Sampler , __sentry_sampleRate : '0.1231' , dog : 'Maisey' } ,
96
+ { __sentry_samplingMethod : TransactionSamplingMethod . Inheritance , __sentry_sampleRate : '' , dog : 'Cory' } ,
97
+ { __sentry_samplingMethod : TransactionSamplingMethod . Explicit , __sentry_sampleRate : '' , dog : 'Bodhi' } ,
95
98
96
99
// this shouldn't ever happen (tags should always include at least the sampling method), but good to know that
97
100
// things won't blow up if it does happen
98
- { dog : 'Lucy' } ,
99
- ] . forEach ( ( { method, rate, dog } ) => {
100
- it ( `adds transaction sampling information to item header - ${ method } , ${ rate } , ${ dog } ` , ( ) => {
101
- // TODO kmclb - once tag types are loosened, don't need to cast to string here
102
- event . tags = { __sentry_samplingMethod : String ( method ) , __sentry_sampleRate : String ( rate ) , dog } ;
101
+ { __sentry_samplingMethod : '' , __sentry_sampleRate : '' , dog : 'Lucy' } ,
102
+ ] . forEach ( tags => {
103
+ const { __sentry_samplingMethod : method , __sentry_sampleRate : rate , dog } = tags ;
103
104
104
- const result = eventToSentryRequest ( event as Event , api ) ;
105
+ it ( `adds transaction sampling information to item header - ${ method } , ${ rate } , ${ dog } ` , ( ) => {
106
+ const event = {
107
+ ...transactionEvent ,
108
+ tags,
109
+ } ;
105
110
106
- const [ envelopeHeaderString , itemHeaderString , eventString ] = result . body . split ( '\n' ) ;
111
+ const result = eventToSentryRequest ( event , api ) ;
107
112
108
- const envelope = {
109
- envelopeHeader : JSON . parse ( envelopeHeaderString ) ,
110
- itemHeader : JSON . parse ( itemHeaderString ) ,
111
- event : JSON . parse ( eventString ) ,
112
- } ;
113
+ const itemHeaderJSON = result . body . split ( '\n' ) [ 1 ] ;
114
+ const itemHeader = JSON . parse ( itemHeaderJSON ) ;
113
115
114
- // the right stuff is added to the item header
115
- expect ( envelope . itemHeader ) . toEqual ( {
116
+ expect ( itemHeader ) . toEqual ( {
116
117
type : 'transaction' ,
117
118
// TODO kmclb - once tag types are loosened, don't need to cast to string here
118
119
sample_rates : [ { id : String ( method ) , rate : String ( rate ) } ] ,
119
120
} ) ;
120
-
121
- // show that it pops the right tags and leaves the rest alone
122
- expect ( '__sentry_samplingMethod' in envelope . event . tags ) . toBe ( false ) ;
123
- expect ( '__sentry_sampleRate' in envelope . event . tags ) . toBe ( false ) ;
124
- expect ( 'dog' in envelope . event . tags ) . toBe ( true ) ;
125
121
} ) ;
126
122
} ) ;
123
+
124
+ it ( 'filters out temporary tags before sending (but leaves other tags alone)' , ( ) => {
125
+ const event = { ...transactionEvent } ;
126
+
127
+ const result = eventToSentryRequest ( event , api ) ;
128
+
129
+ const eventString = result . body . split ( '\n' ) [ 2 ] ;
130
+ const envelopeEvent = JSON . parse ( eventString ) ;
131
+ const internalTags = Object . keys ( envelopeEvent . tags ) . filter ( tagName => tagName . startsWith ( '__sentry' ) ) ;
132
+
133
+ expect ( internalTags ) . toEqual ( [ ] ) ;
134
+ expect ( 'dog' in envelopeEvent . tags ) . toBe ( true ) ;
135
+ } ) ;
127
136
} ) ;
0 commit comments