11// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
22// SPDX-License-Identifier: Apache-2.0
33
4- import { context , Span , SpanKind } from '@opentelemetry/api' ;
4+ import { Attributes , Context , context , Link , Span , SpanKind } from '@opentelemetry/api' ;
55import { Resource } from '@opentelemetry/resources' ;
66import { SamplingDecision , Tracer } from '@opentelemetry/sdk-trace-base' ;
77import { NodeTracerProvider } from '@opentelemetry/sdk-trace-node' ;
@@ -10,12 +10,18 @@ import { expect } from 'expect';
1010import * as nock from 'nock' ;
1111import * as sinon from 'sinon' ;
1212import { _AwsXRayRemoteSampler , AwsXRayRemoteSampler } from '../../src/sampler/aws-xray-remote-sampler' ;
13+ import { FallbackSampler } from '../../src/sampler/fallback-sampler' ;
1314
1415const DATA_DIR_SAMPLING_RULES = __dirname + '/data/test-remote-sampler_sampling-rules-response-sample.json' ;
1516const DATA_DIR_SAMPLING_TARGETS = __dirname + '/data/test-remote-sampler_sampling-targets-response-sample.json' ;
1617const TEST_URL = 'http://localhost:2000' ;
18+ export const testTraceId = '0af7651916cd43dd8448eb211c80319c' ;
1719
1820describe ( 'AwsXrayRemoteSampler' , ( ) => {
21+ afterEach ( ( ) => {
22+ sinon . restore ( ) ;
23+ } ) ;
24+
1925 it ( 'testCreateRemoteSamplerWithEmptyResource' , ( ) => {
2026 const sampler : AwsXRayRemoteSampler = new AwsXRayRemoteSampler ( { resource : Resource . EMPTY } ) ;
2127
@@ -81,21 +87,21 @@ describe('AwsXrayRemoteSampler', () => {
8187 setTimeout ( ( ) => {
8288 expect ( ( ( sampler as any ) . _root . _root . ruleCache as any ) . ruleAppliers [ 0 ] . samplingRule . RuleName ) . toEqual ( 'test' ) ;
8389 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
8591 ) . toEqual ( SamplingDecision . NOT_RECORD ) ;
8692
8793 setTimeout ( ( ) => {
8894 // restore function
8995 ( _AwsXRayRemoteSampler . prototype as any ) . getDefaultTargetPollingInterval = tmp ;
9096
9197 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
9399 ) . toEqual ( SamplingDecision . RECORD_AND_SAMPLED ) ;
94100 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
96102 ) . toEqual ( SamplingDecision . RECORD_AND_SAMPLED ) ;
97103 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
99105 ) . toEqual ( SamplingDecision . RECORD_AND_SAMPLED ) ;
100106
101107 done ( ) ;
@@ -112,40 +118,38 @@ describe('AwsXrayRemoteSampler', () => {
112118 } ) ;
113119 const attributes = { abc : '1234' } ;
114120
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- } ;
120121 const sampler = new AwsXRayRemoteSampler ( {
121122 resource : resource ,
122123 } ) ;
124+ const internalXraySampler = sampler [ '_root' ] [ '_root' ] as _AwsXRayRemoteSampler ;
125+ internalXraySampler [ 'getAndUpdateSamplingRules' ] ( ) ;
123126
124127 setTimeout ( ( ) => {
125128 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' ] ( ) ;
129133
130134 setTimeout ( ( ) => {
131135 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 ++ ) {
133139 if (
134- sampler . shouldSample ( context . active ( ) , '1234' , 'name' , SpanKind . CLIENT , attributes , [ ] ) . decision !==
140+ sampler . shouldSample ( context . active ( ) , testTraceId , 'name' , SpanKind . CLIENT , attributes , [ ] ) . decision !==
135141 SamplingDecision . NOT_RECORD
136142 ) {
137143 sampled ++ ;
138144 }
139145 }
146+ clock . restore ( ) ;
140147
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 ) ;
146150 done ( ) ;
147- } , 2000 ) ;
148- } , 100 ) ;
151+ } , 300 ) ;
152+ } , 300 ) ;
149153 } ) ;
150154
151155 it ( 'testSomeReservoir' , done => {
@@ -157,39 +161,54 @@ describe('AwsXrayRemoteSampler', () => {
157161 } ) ;
158162 const attributes = { abc : 'non-matching attribute value, use default rule' } ;
159163
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- } ;
165164 const sampler = new AwsXRayRemoteSampler ( {
166165 resource : resource ,
167166 } ) ;
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+ ) ;
168185
186+ internalXraySampler [ 'getAndUpdateSamplingRules' ] ( ) ;
169187 setTimeout ( ( ) => {
170188 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' ] ( ) ;
174194
175195 setTimeout ( ( ) => {
176196 const clock = sinon . useFakeTimers ( Date . now ( ) ) ;
177- clock . tick ( 2000 ) ;
197+ clock . tick ( 1000 ) ;
178198 let sampled = 0 ;
179199 for ( let i = 0 ; i < 100000 ; i ++ ) {
180200 if (
181- sampler . shouldSample ( context . active ( ) , '1234' , 'name' , SpanKind . CLIENT , attributes , [ ] ) . decision !==
201+ sampler . shouldSample ( context . active ( ) , testTraceId , 'name' , SpanKind . CLIENT , attributes , [ ] ) . decision !==
182202 SamplingDecision . NOT_RECORD
183203 ) {
184204 sampled ++ ;
185205 }
186206 }
187207 clock . restore ( ) ;
188- // restore function
189- ( _AwsXRayRemoteSampler . prototype as any ) . getDefaultTargetPollingInterval = tmp ;
208+
190209 expect ( sampled ) . toEqual ( 100 ) ;
191210 done ( ) ;
192- } , 2000 ) ;
211+ } , 300 ) ;
193212 } , 300 ) ;
194213 } ) ;
195214
0 commit comments