1
1
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2
2
// SPDX-License-Identifier: Apache-2.0
3
3
4
- import { context , Span , SpanKind } from '@opentelemetry/api' ;
4
+ import { Attributes , Context , context , Link , Span , SpanKind } from '@opentelemetry/api' ;
5
5
import { Resource } from '@opentelemetry/resources' ;
6
6
import { SamplingDecision , Tracer } from '@opentelemetry/sdk-trace-base' ;
7
7
import { NodeTracerProvider } from '@opentelemetry/sdk-trace-node' ;
@@ -10,12 +10,18 @@ import { expect } from 'expect';
10
10
import * as nock from 'nock' ;
11
11
import * as sinon from 'sinon' ;
12
12
import { _AwsXRayRemoteSampler , AwsXRayRemoteSampler } from '../../src/sampler/aws-xray-remote-sampler' ;
13
+ import { FallbackSampler } from '../../src/sampler/fallback-sampler' ;
13
14
14
15
const DATA_DIR_SAMPLING_RULES = __dirname + '/data/test-remote-sampler_sampling-rules-response-sample.json' ;
15
16
const DATA_DIR_SAMPLING_TARGETS = __dirname + '/data/test-remote-sampler_sampling-targets-response-sample.json' ;
16
17
const TEST_URL = 'http://localhost:2000' ;
18
+ export const testTraceId = '0af7651916cd43dd8448eb211c80319c' ;
17
19
18
20
describe ( 'AwsXrayRemoteSampler' , ( ) => {
21
+ afterEach ( ( ) => {
22
+ sinon . restore ( ) ;
23
+ } ) ;
24
+
19
25
it ( 'testCreateRemoteSamplerWithEmptyResource' , ( ) => {
20
26
const sampler : AwsXRayRemoteSampler = new AwsXRayRemoteSampler ( { resource : Resource . EMPTY } ) ;
21
27
@@ -81,21 +87,21 @@ describe('AwsXrayRemoteSampler', () => {
81
87
setTimeout ( ( ) => {
82
88
expect ( ( ( sampler as any ) . _root . _root . ruleCache as any ) . ruleAppliers [ 0 ] . samplingRule . RuleName ) . toEqual ( 'test' ) ;
83
89
expect (
84
- sampler . shouldSample ( context . active ( ) , '1234' , 'name' , SpanKind . CLIENT , { abc : '1234' } , [ ] ) . decision
90
+ sampler . shouldSample ( context . active ( ) , testTraceId , 'name' , SpanKind . CLIENT , { abc : '1234' } , [ ] ) . decision
85
91
) . toEqual ( SamplingDecision . NOT_RECORD ) ;
86
92
87
93
setTimeout ( ( ) => {
88
94
// restore function
89
95
( _AwsXRayRemoteSampler . prototype as any ) . getDefaultTargetPollingInterval = tmp ;
90
96
91
97
expect (
92
- sampler . shouldSample ( context . active ( ) , '1234' , 'name' , SpanKind . CLIENT , { abc : '1234' } , [ ] ) . decision
98
+ sampler . shouldSample ( context . active ( ) , testTraceId , 'name' , SpanKind . CLIENT , { abc : '1234' } , [ ] ) . decision
93
99
) . toEqual ( SamplingDecision . RECORD_AND_SAMPLED ) ;
94
100
expect (
95
- sampler . shouldSample ( context . active ( ) , '1234' , 'name' , SpanKind . CLIENT , { abc : '1234' } , [ ] ) . decision
101
+ sampler . shouldSample ( context . active ( ) , testTraceId , 'name' , SpanKind . CLIENT , { abc : '1234' } , [ ] ) . decision
96
102
) . toEqual ( SamplingDecision . RECORD_AND_SAMPLED ) ;
97
103
expect (
98
- sampler . shouldSample ( context . active ( ) , '1234' , 'name' , SpanKind . CLIENT , { abc : '1234' } , [ ] ) . decision
104
+ sampler . shouldSample ( context . active ( ) , testTraceId , 'name' , SpanKind . CLIENT , { abc : '1234' } , [ ] ) . decision
99
105
) . toEqual ( SamplingDecision . RECORD_AND_SAMPLED ) ;
100
106
101
107
done ( ) ;
@@ -112,40 +118,38 @@ describe('AwsXrayRemoteSampler', () => {
112
118
} ) ;
113
119
const attributes = { abc : '1234' } ;
114
120
115
- // Patch default target polling interval
116
- const tmp = ( _AwsXRayRemoteSampler . prototype as any ) . getDefaultTargetPollingInterval ;
117
- ( _AwsXRayRemoteSampler . prototype as any ) . getDefaultTargetPollingInterval = ( ) => {
118
- return 0.2 ; // seconds
119
- } ;
120
121
const sampler = new AwsXRayRemoteSampler ( {
121
122
resource : resource ,
122
123
} ) ;
124
+ const internalXraySampler = sampler [ '_root' ] [ '_root' ] as _AwsXRayRemoteSampler ;
125
+ internalXraySampler [ 'getAndUpdateSamplingRules' ] ( ) ;
123
126
124
127
setTimeout ( ( ) => {
125
128
expect ( ( ( sampler as any ) . _root . _root . ruleCache as any ) . ruleAppliers [ 0 ] . samplingRule . RuleName ) . toEqual ( 'test' ) ;
126
- expect ( sampler . shouldSample ( context . active ( ) , '1234' , 'name' , SpanKind . CLIENT , attributes , [ ] ) . decision ) . toEqual (
127
- SamplingDecision . NOT_RECORD
128
- ) ;
129
+ expect (
130
+ sampler . shouldSample ( context . active ( ) , testTraceId , 'name' , SpanKind . CLIENT , attributes , [ ] ) . decision
131
+ ) . toEqual ( SamplingDecision . NOT_RECORD ) ;
132
+ internalXraySampler [ 'getAndUpdateSamplingTargets' ] ( ) ;
129
133
130
134
setTimeout ( ( ) => {
131
135
let sampled = 0 ;
132
- for ( let i = 0 ; i < 100000 ; i ++ ) {
136
+ const clock = sinon . useFakeTimers ( Date . now ( ) ) ;
137
+ clock . tick ( 1000 ) ;
138
+ for ( let i = 0 ; i < 1000 ; i ++ ) {
133
139
if (
134
- sampler . shouldSample ( context . active ( ) , '1234' , 'name' , SpanKind . CLIENT , attributes , [ ] ) . decision !==
140
+ sampler . shouldSample ( context . active ( ) , testTraceId , 'name' , SpanKind . CLIENT , attributes , [ ] ) . decision !==
135
141
SamplingDecision . NOT_RECORD
136
142
) {
137
143
sampled ++ ;
138
144
}
139
145
}
146
+ clock . restore ( ) ;
140
147
141
- // restore function
142
- ( _AwsXRayRemoteSampler . prototype as any ) . getDefaultTargetPollingInterval = tmp ;
143
-
144
- expect ( ( sampler as any ) . _root . _root . ruleCache . ruleAppliers [ 0 ] . reservoirSampler . quota ) . toEqual ( 100000 ) ;
145
- expect ( sampled ) . toEqual ( 100000 ) ;
148
+ expect ( ( sampler as any ) . _root . _root . ruleCache . ruleAppliers [ 0 ] . reservoirSampler . quota ) . toEqual ( 1000 ) ;
149
+ expect ( sampled ) . toEqual ( 1000 ) ;
146
150
done ( ) ;
147
- } , 2000 ) ;
148
- } , 100 ) ;
151
+ } , 300 ) ;
152
+ } , 300 ) ;
149
153
} ) ;
150
154
151
155
it ( 'testSomeReservoir' , done => {
@@ -157,39 +161,54 @@ describe('AwsXrayRemoteSampler', () => {
157
161
} ) ;
158
162
const attributes = { abc : 'non-matching attribute value, use default rule' } ;
159
163
160
- // Patch default target polling interval
161
- const tmp = ( _AwsXRayRemoteSampler . prototype as any ) . getDefaultTargetPollingInterval ;
162
- ( _AwsXRayRemoteSampler . prototype as any ) . getDefaultTargetPollingInterval = ( ) => {
163
- return 2 ; // seconds
164
- } ;
165
164
const sampler = new AwsXRayRemoteSampler ( {
166
165
resource : resource ,
167
166
} ) ;
167
+ const internalXraySampler = sampler [ '_root' ] [ '_root' ] as _AwsXRayRemoteSampler ;
168
+ sinon
169
+ . stub ( sampler [ '_root' ] [ '_root' ] . fallbackSampler as FallbackSampler , 'shouldSample' )
170
+ . callsFake (
171
+ (
172
+ context : Context ,
173
+ traceId : string ,
174
+ spanName : string ,
175
+ spanKind : SpanKind ,
176
+ attributes : Attributes ,
177
+ links : Link [ ]
178
+ ) => {
179
+ return {
180
+ decision : SamplingDecision . NOT_RECORD ,
181
+ attributes : attributes ,
182
+ } ;
183
+ }
184
+ ) ;
168
185
186
+ internalXraySampler [ 'getAndUpdateSamplingRules' ] ( ) ;
169
187
setTimeout ( ( ) => {
170
188
expect ( ( ( sampler as any ) . _root . _root . ruleCache as any ) . ruleAppliers [ 0 ] . samplingRule . RuleName ) . toEqual ( 'test' ) ;
171
- expect ( sampler . shouldSample ( context . active ( ) , '1234' , 'name' , SpanKind . CLIENT , attributes , [ ] ) . decision ) . toEqual (
172
- SamplingDecision . NOT_RECORD
173
- ) ;
189
+ expect (
190
+ sampler . shouldSample ( context . active ( ) , testTraceId , 'name' , SpanKind . CLIENT , attributes , [ ] ) . decision
191
+ ) . toEqual ( SamplingDecision . RECORD_AND_SAMPLED ) ;
192
+
193
+ internalXraySampler [ 'getAndUpdateSamplingTargets' ] ( ) ;
174
194
175
195
setTimeout ( ( ) => {
176
196
const clock = sinon . useFakeTimers ( Date . now ( ) ) ;
177
- clock . tick ( 2000 ) ;
197
+ clock . tick ( 1000 ) ;
178
198
let sampled = 0 ;
179
199
for ( let i = 0 ; i < 100000 ; i ++ ) {
180
200
if (
181
- sampler . shouldSample ( context . active ( ) , '1234' , 'name' , SpanKind . CLIENT , attributes , [ ] ) . decision !==
201
+ sampler . shouldSample ( context . active ( ) , testTraceId , 'name' , SpanKind . CLIENT , attributes , [ ] ) . decision !==
182
202
SamplingDecision . NOT_RECORD
183
203
) {
184
204
sampled ++ ;
185
205
}
186
206
}
187
207
clock . restore ( ) ;
188
- // restore function
189
- ( _AwsXRayRemoteSampler . prototype as any ) . getDefaultTargetPollingInterval = tmp ;
208
+
190
209
expect ( sampled ) . toEqual ( 100 ) ;
191
210
done ( ) ;
192
- } , 2000 ) ;
211
+ } , 300 ) ;
193
212
} , 300 ) ;
194
213
} ) ;
195
214
0 commit comments