@@ -49,8 +49,6 @@ function sample<T extends Transaction>(hub: Hub, transaction: T, sampleContext:
49
49
return transaction ;
50
50
}
51
51
52
- logger . log ( 'Tracing enabled' ) ;
53
-
54
52
// we have to test for a pre-existsing sampling decision, in case this transaction is a child transaction and has
55
53
// inherited its parent's decision
56
54
if ( transaction . sampled === undefined ) {
@@ -59,9 +57,20 @@ function sample<T extends Transaction>(hub: Hub, transaction: T, sampleContext:
59
57
const sampleRate =
60
58
typeof options . tracesSampler === 'function' ? options . tracesSampler ( sampleContext ) : options . tracesSampleRate ;
61
59
60
+ // since this is coming from the user, who knows what we might get
61
+ if ( ! isValidSampleRate ( sampleRate ) ) {
62
+ logger . warn ( `[Tracing] Discarding trace because of invalid sample rate.` ) ;
63
+ transaction . sampled = false ;
64
+ return transaction ;
65
+ }
66
+
62
67
// if the function returned 0, or if the sample rate is set to 0, it's a sign the transaction should be dropped
63
68
if ( ! sampleRate ) {
64
- logger . log ( 'Discarding trace because tracesSampler returned 0 or tracesSampleRate is set to 0' ) ;
69
+ logger . log (
70
+ `[Tracing] Discarding trace because ${
71
+ typeof options . tracesSampler === 'function' ? 'tracesSampler returned 0' : 'tracesSampleRate is set to 0'
72
+ } `,
73
+ ) ;
65
74
transaction . sampled = false ;
66
75
return transaction ;
67
76
}
@@ -71,7 +80,9 @@ function sample<T extends Transaction>(hub: Hub, transaction: T, sampleContext:
71
80
72
81
// if we're not going to keep it, we're done
73
82
if ( ! transaction . sampled ) {
74
- logger . log ( `Discarding trace because it's not included in the random sample (sampling rate = ${ sampleRate } )` ) ;
83
+ logger . log (
84
+ `[Tracing] Discarding trace because it's not included in the random sample (sampling rate = ${ sampleRate } )` ,
85
+ ) ;
75
86
return transaction ;
76
87
}
77
88
}
@@ -120,6 +131,25 @@ function getDefaultSampleContext(): SampleContext {
120
131
return defaultSampleContext ;
121
132
}
122
133
134
+ /**
135
+ * Checks the given sample rate to make sure it is valid (a number between 0 and 1).
136
+ */
137
+ function isValidSampleRate ( rate : unknown ) : boolean {
138
+ if ( ! ( typeof rate === 'number' ) ) {
139
+ logger . warn (
140
+ `[Tracing] Given sample rate is invalid. Sample rate must be a number between 0 and 1. Got ${ JSON . stringify (
141
+ rate ,
142
+ ) } of type ${ JSON . stringify ( typeof rate ) } .`,
143
+ ) ;
144
+ return false ;
145
+ }
146
+ if ( rate < 0 || rate > 1 ) {
147
+ logger . warn ( `[Tracing] Given sample rate is invalid. Sample rate must be between 0 and 1. Got ${ rate } .` ) ;
148
+ return false ;
149
+ }
150
+ return true ;
151
+ }
152
+
123
153
/**
124
154
* Creates a new transaction and adds a sampling decision if it doesn't yet have one.
125
155
*
0 commit comments