1818// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
1919// SPDX-License-Identifier: Apache-2.0
2020
21- import { Attributes , Context , Link , SpanKind , diag } from '@opentelemetry/api' ;
21+ import {
22+ Attributes ,
23+ Context ,
24+ DiagLogger ,
25+ Link ,
26+ SpanKind ,
27+ diag ,
28+ } from '@opentelemetry/api' ;
2229import {
2330 ParentBasedSampler ,
2431 Sampler ,
@@ -39,7 +46,9 @@ import {
3946 DEFAULT_TARGET_POLLING_INTERVAL_SECONDS ,
4047 RuleCache ,
4148} from './rule-cache' ;
49+
4250import { SamplingRuleApplier } from './sampling-rule-applier' ;
51+ import { PACKAGE_NAME } from './version' ;
4352
4453// 5 minute default sampling rules polling interval
4554const DEFAULT_RULES_POLLING_INTERVAL_SECONDS : number = 5 * 60 ;
@@ -97,6 +106,7 @@ export class _AWSXRayRemoteSampler implements Sampler {
97106 private awsProxyEndpoint : string ;
98107 private ruleCache : RuleCache ;
99108 private fallbackSampler : FallbackSampler ;
109+ private samplerDiag : DiagLogger ;
100110 private rulePoller : NodeJS . Timeout | undefined ;
101111 private targetPoller : NodeJS . Timeout | undefined ;
102112 private clientId : string ;
@@ -105,11 +115,15 @@ export class _AWSXRayRemoteSampler implements Sampler {
105115 private samplingClient : AWSXRaySamplingClient ;
106116
107117 constructor ( samplerConfig : AWSXRayRemoteSamplerConfig ) {
118+ this . samplerDiag = diag . createComponentLogger ( {
119+ namespace : PACKAGE_NAME ,
120+ } ) ;
121+
108122 if (
109123 samplerConfig . pollingInterval == null ||
110124 samplerConfig . pollingInterval < 10
111125 ) {
112- diag . warn (
126+ this . samplerDiag . warn (
113127 `'pollingInterval' is undefined or too small. Defaulting to ${ DEFAULT_RULES_POLLING_INTERVAL_SECONDS } seconds`
114128 ) ;
115129 this . rulePollingIntervalMillis =
@@ -130,7 +144,10 @@ export class _AWSXRayRemoteSampler implements Sampler {
130144 this . clientId = _AWSXRayRemoteSampler . generateClientId ( ) ;
131145 this . ruleCache = new RuleCache ( samplerConfig . resource ) ;
132146
133- this . samplingClient = new AWSXRaySamplingClient ( this . awsProxyEndpoint ) ;
147+ this . samplingClient = new AWSXRaySamplingClient (
148+ this . awsProxyEndpoint ,
149+ this . samplerDiag
150+ ) ;
134151
135152 // Start the Sampling Rules poller
136153 this . startSamplingRulesPoller ( ) ;
@@ -152,7 +169,9 @@ export class _AWSXRayRemoteSampler implements Sampler {
152169 links : Link [ ]
153170 ) : SamplingResult {
154171 if ( this . ruleCache . isExpired ( ) ) {
155- diag . debug ( 'Rule cache is expired, so using fallback sampling strategy' ) ;
172+ this . samplerDiag . debug (
173+ 'Rule cache is expired, so using fallback sampling strategy'
174+ ) ;
156175 return this . fallbackSampler . shouldSample (
157176 context ,
158177 traceId ,
@@ -177,13 +196,13 @@ export class _AWSXRayRemoteSampler implements Sampler {
177196 ) ;
178197 }
179198 } catch ( e : unknown ) {
180- diag . debug (
199+ this . samplerDiag . debug (
181200 'Unexpected error occurred when trying to match or applying a sampling rule' ,
182201 e
183202 ) ;
184203 }
185204
186- diag . debug (
205+ this . samplerDiag . debug (
187206 'Using fallback sampler as no rule match was found. This is likely due to a bug, since default rule should always match'
188207 ) ;
189208 return this . fallbackSampler . shouldSample (
@@ -259,7 +278,7 @@ export class _AWSXRayRemoteSampler implements Sampler {
259278 ) ;
260279 this . ruleCache . updateRules ( samplingRules ) ;
261280 } else {
262- diag . error (
281+ this . samplerDiag . error (
263282 'SamplingRuleRecords from GetSamplingRules request is not defined'
264283 ) ;
265284 }
@@ -289,14 +308,14 @@ export class _AWSXRayRemoteSampler implements Sampler {
289308 this . startSamplingTargetsPoller ( ) ;
290309
291310 if ( refreshSamplingRules ) {
292- diag . debug (
311+ this . samplerDiag . debug (
293312 'Performing out-of-band sampling rule polling to fetch updated rules.'
294313 ) ;
295314 clearInterval ( this . rulePoller ) ;
296315 this . startSamplingRulesPoller ( ) ;
297316 }
298317 } catch ( error : unknown ) {
299- diag . debug ( 'Error occurred when updating Sampling Targets' ) ;
318+ this . samplerDiag . debug ( 'Error occurred when updating Sampling Targets' ) ;
300319 }
301320 }
302321
0 commit comments