-
Notifications
You must be signed in to change notification settings - Fork 153
/
Copy pathTracer.ts
782 lines (699 loc) · 24.5 KB
/
Tracer.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
import { Handler } from 'aws-lambda';
import { TracerInterface } from '.';
import { ConfigServiceInterface, EnvironmentVariablesService } from './config';
import { HandlerMethodDecorator, TracerOptions, MethodDecorator } from './types';
import { ProviderService, ProviderServiceInterface } from './provider';
import { Segment, Subsegment } from 'aws-xray-sdk-core';
/**
* ## Intro
* Tracer is an opinionated thin wrapper for [AWS X-Ray SDK for Node.js](https://github.com/aws/aws-xray-sdk-node).
*
* Tracing data can be visualized through AWS X-Ray Console.
*
* ## Key features
* * Auto capture cold start as annotation, and responses or full exceptions as metadata
* * Auto-disable when not running in AWS Lambda environment
* * Support tracing functions via decorators, middleware, and manual instrumentation
* * Support tracing AWS SDK v2 and v3 via AWS X-Ray SDK for Node.js
*
* ## Usage
*
* For more usage examples, see [our documentation](https://awslabs.github.io/aws-lambda-powertools-typescript/latest/core/tracer/).
*
* ### Functions usage with middleware
*
* If you use function-based Lambda handlers you can use the [captureLambdaHandler()](./_aws_lambda_powertools_tracer.Tracer.html) middy middleware to automatically:
* * handle the subsegment lifecycle
* * add the `ServiceName` and `ColdStart` annotations
* * add the function response as metadata
* * add the function error as metadata (if any)
*
* @example
* ```typescript
* import { captureLambdaHandler, Tracer } from '@aws-lambda-powertools/tracer';
* import middy from '@middy/core';
*
* const tracer = new Tracer({ serviceName: 'serverlessAirline' });
*
* export const handler = middy(async (_event: any, _context: any) => {
* ...
* }).use(captureLambdaHandler(tracer));
* ```
*
* ### Object oriented usage with decorators
*
* If instead you use TypeScript Classes to wrap your Lambda handler you can use the [@tracer.captureLambdaHandler()](./_aws_lambda_powertools_tracer.Tracer.html#captureLambdaHandler) decorator to automatically:
* * handle the subsegment lifecycle
* * add the `ServiceName` and `ColdStart` annotations
* * add the function response as metadata
* * add the function error as metadata (if any)
*
* @example
* ```typescript
* import { Tracer } from '@aws-lambda-powertools/tracer';
*
* const tracer = new Tracer({ serviceName: 'serverlessAirline' });
*
* // FYI: Decorator might not render properly in VSCode mouse over due to https://github.com/microsoft/TypeScript/issues/39371 and might show as *@tracer* instead of `@tracer.captureLambdaHandler`
*
* class Lambda {
* @tracer.captureLambdaHandler()
* public handler(event: any, context: any) {
* ...
* }
* }
*
* export const handlerClass = new Lambda();
* export const handler = handlerClass.handler;
* ```
*
* ### Functions usage with manual instrumentation
*
* If you prefer to manually instrument your Lambda handler you can use the methods in the tracer class directly.
*
* @example
* ```typescript
* import { Tracer } from '@aws-lambda-powertools/tracer';
* import { Segment } from 'aws-xray-sdk-core';
*
* const tracer = new Tracer({ serviceName: 'serverlessAirline' });
*
* export const handler = async (_event: any, context: any) => {
* const segment = tracer.getSegment(); // This is the facade segment (the one that is created by AWS Lambda)
* // Create subsegment for the function & set it as active
* const subsegment = segment.addNewSubsegment(`## ${process.env._HANDLER}`);
* tracer.setSegment(subsegment);
*
* // Annotate the subsegment with the cold start & serviceName
* tracer.annotateColdStart();
* tracer.addServiceNameAnnotation();
*
* let res;
* try {
* res = ...
* // Add the response as metadata
* tracer.addResponseAsMetadata(res, process.env._HANDLER);
* } catch (err) {
* // Add the error as metadata
* tracer.addErrorAsMetadata(err as Error);
* throw err;
* } finally {
* // Close the subsegment
* subsegment.close();
* // Set the facade segment as active again
* tracer.setSegment(segment);
* }
*
* return res;
* }
* ```
*/
class Tracer implements TracerInterface {
public static coldStart: boolean = true;
public provider: ProviderServiceInterface;
private captureError: boolean = true;
private captureResponse: boolean = true;
private customConfigService?: ConfigServiceInterface;
private envVarsService?: EnvironmentVariablesService;
private serviceName?: string;
private tracingEnabled: boolean = true;
public constructor(options: TracerOptions = {}) {
this.setOptions(options);
this.provider = new ProviderService();
if (!this.isTracingEnabled()) {
// Tell x-ray-sdk to not throw an error if context is missing but tracing is disabled
this.provider.setContextMissingStrategy(() => ({}));
}
}
/**
* Add an error to the current segment or subsegment as metadata.
*
* @see https://docs.aws.amazon.com/xray/latest/devguide/xray-concepts.html#xray-concepts-errors
*
* @param error - Error to serialize as metadata
*/
public addErrorAsMetadata(error: Error): void {
if (!this.isTracingEnabled()) {
return;
}
const subsegment = this.getSegment();
if (!this.captureError) {
subsegment.addErrorFlag();
return;
}
subsegment.addError(error, false);
}
/**
* Add response data to the current segment or subsegment as metadata.
*
* @see https://docs.aws.amazon.com/xray/latest/devguide/xray-concepts.html#xray-concepts-annotations
*
* @param data - Data to serialize as metadata
* @param methodName - Name of the method that is being traced
*/
public addResponseAsMetadata(data?: unknown, methodName?: string): void {
if (data === undefined || !this.captureResponse || !this.isTracingEnabled()) {
return;
}
this.putMetadata(`${methodName} response`, data);
}
/**
* Add service name to the current segment or subsegment as annotation.
*
*/
public addServiceNameAnnotation(): void {
if (!this.isTracingEnabled() || this.serviceName === undefined) {
return;
}
this.putAnnotation('Service', this.serviceName);
}
/**
* Add ColdStart annotation to the current segment or subsegment.
*
* If Tracer has been initialized outside the Lambda handler then the same instance
* of Tracer will be reused throughout the lifecycle of that same Lambda execution environment
* and this method will annotate `ColdStart: false` after the first invocation.
*
* @see https://docs.aws.amazon.com/lambda/latest/dg/runtimes-context.html
*/
public annotateColdStart(): void {
if (this.isTracingEnabled()) {
this.putAnnotation('ColdStart', Tracer.coldStart);
}
if (Tracer.coldStart) {
Tracer.coldStart = false;
}
}
/**
* Patch all AWS SDK v2 clients and create traces when your application makes calls to AWS services.
*
* If you want to patch a specific client use {@link captureAWSClient} and if you are using AWS SDK v3 use {@link captureAWSv3Client} instead.
*
* @see https://docs.aws.amazon.com/xray/latest/devguide/xray-sdk-nodejs-awssdkclients.html
*
* @example
* ```typescript
* import { Tracer } from '@aws-lambda-powertools/tracer';
*
* const tracer = new Tracer({ serviceName: 'serverlessAirline' });
* const AWS = tracer.captureAWS(require('aws-sdk'));
*
* export const handler = async (_event: any, _context: any) => {
* ...
* }
* ```
*
* @param aws - AWS SDK v2 import
* @returns AWS - Instrumented AWS SDK
*/
public captureAWS<T>(aws: T): T {
if (!this.isTracingEnabled()) return aws;
return this.provider.captureAWS(aws);
}
/**
* Patch a specific AWS SDK v2 client and create traces when your application makes calls to that AWS service.
*
* If you want to patch all clients use {@link captureAWS} and if you are using AWS SDK v3 use {@link captureAWSv3Client} instead.
*
* @see https://docs.aws.amazon.com/xray/latest/devguide/xray-sdk-nodejs-awssdkclients.html
*
* @example
* ```typescript
* import { S3 } from "aws-sdk";
* import { Tracer } from '@aws-lambda-powertools/tracer';
*
* const tracer = new Tracer({ serviceName: 'serverlessAirline' });
* const s3 = tracer.captureAWSClient(new S3({ apiVersion: "2006-03-01" }));
*
* export const handler = async (_event: any, _context: any) => {
* ...
* }
* ```
*
* @param service - AWS SDK v2 client
* @returns service - Instrumented AWS SDK v2 client
*/
public captureAWSClient<T>(service: T): T {
if (!this.isTracingEnabled()) return service;
try {
return this.provider.captureAWSClient(service);
} catch (error) {
try {
// This is needed because some aws-sdk clients like AWS.DynamoDB.DocumentDB don't comply with the same
// instrumentation contract like most base clients.
// For detailed explanation see: https://github.com/awslabs/aws-lambda-powertools-typescript/issues/524#issuecomment-1024493662
this.provider.captureAWSClient((service as T & { service: T }).service);
return service;
} catch {
throw error;
}
}
}
/**
* Patch an AWS SDK v3 client and create traces when your application makes calls to that AWS service.
*
* If you are using AWS SDK v2 use {@link captureAWSClient} instead.
*
* @see https://docs.aws.amazon.com/xray/latest/devguide/xray-sdk-nodejs-awssdkclients.html
*
* @example
* ```typescript
* import { S3Client } from "@aws-sdk/client-s3";
* import { Tracer } from '@aws-lambda-powertools/tracer';
*
* const tracer = new Tracer({ serviceName: 'serverlessAirline' });
* const client = new S3Client({});
* tracer.captureAWSv3Client(client);
*
* export const handler = async (_event: any, _context: any) => {
* ...
* }
* ```
*
* @param service - AWS SDK v3 client
* @returns service - Instrumented AWS SDK v3 client
*/
public captureAWSv3Client<T>(service: T): T {
if (!this.isTracingEnabled()) return service;
return this.provider.captureAWSv3Client(service);
}
/**
* A decorator automating capture of metadata and annotations on segments or subsegments for a Lambda Handler.
*
* Using this decorator on your handler function will automatically:
* * handle the subsegment lifecycle
* * add the `ColdStart` annotation
* * add the function response as metadata
* * add the function error as metadata (if any)
*
* Note: Currently TypeScript only supports decorators on classes and methods. If you are using the
* function syntax, you should use the middleware instead.
*
* @example
* ```typescript
* import { Tracer } from '@aws-lambda-powertools/tracer';
*
* const tracer = new Tracer({ serviceName: 'serverlessAirline' });
*
* class Lambda {
* @tracer.captureLambdaHandler()
* public handler(event: any, context: any) {
* ...
* }
* }
*
* export const handlerClass = new Lambda();
* export const handler = handlerClass.handler;
* ```
*
* @decorator Class
*/
public captureLambdaHandler(): HandlerMethodDecorator {
return (target, _propertyKey, descriptor) => {
const originalMethod = descriptor.value;
descriptor.value = ((event, context, callback) => {
if (!this.isTracingEnabled()) {
return originalMethod?.apply(target, [ event, context, callback ]);
}
return this.provider.captureAsyncFunc(`## ${process.env._HANDLER}`, async subsegment => {
this.annotateColdStart();
this.addServiceNameAnnotation();
let result: unknown;
try {
result = await originalMethod?.apply(target, [ event, context, callback ]);
this.addResponseAsMetadata(result, process.env._HANDLER);
} catch (error) {
this.addErrorAsMetadata(error as Error);
throw error;
} finally {
subsegment?.close();
subsegment?.flush();
}
return result;
});
}) as Handler;
return descriptor;
};
}
/**
* A decorator automating capture of metadata and annotations on segments or subsegments for an arbitrary function.
*
* Using this decorator on your function will automatically:
* * handle the subsegment lifecycle
* * add the function response as metadata
* * add the function error as metadata (if any)
*
* Note: Currently TypeScript only supports decorators on classes and methods. If you are using the
* function syntax, you should use the middleware instead.
*
* @example
* ```typescript
* import { Tracer } from '@aws-lambda-powertools/tracer';
*
* const tracer = new Tracer({ serviceName: 'serverlessAirline' });
*
* class Lambda {
* @tracer.captureMethod()
* public myMethod(param: any) {
* ...
* }
*
* public handler(event: any, context: any) {
* ...
* }
* }
*
* export const handlerClass = new Lambda();
* export const myMethod = handlerClass.myMethod;
* export const handler = handlerClass.handler;
* ```
*
* @decorator Class
*/
public captureMethod(): MethodDecorator {
return (target, _propertyKey, descriptor) => {
const originalMethod = descriptor.value;
descriptor.value = (...args: unknown[]) => {
if (!this.isTracingEnabled()) {
return originalMethod?.apply(target, [...args]);
}
return this.provider.captureAsyncFunc(`### ${originalMethod.name}`, async subsegment => {
let result;
try {
result = await originalMethod?.apply(this, [...args]);
this.addResponseAsMetadata(result, originalMethod.name);
} catch (error) {
this.addErrorAsMetadata(error as Error);
// TODO: should this error be thrown?? If thrown we get a ERR_UNHANDLED_REJECTION. If not aren't we are basically catching a Customer error?
// throw error;
} finally {
subsegment?.close();
}
return result;
});
};
return descriptor;
};
}
/**
* Retrieve the current value of `ColdStart`.
*
* If Tracer has been initialized outside the Lambda handler then the same instance
* of Tracer will be reused throughout the lifecycle of that same Lambda execution environment
* and this method will return `false` after the first invocation.
*
* @see https://docs.aws.amazon.com/lambda/latest/dg/runtimes-context.html
*
* @returns boolean - `true` if is cold start, otherwise `false`
*/
public static getColdStart(): boolean {
if (Tracer.coldStart) {
Tracer.coldStart = false;
return true;
}
return false;
}
/**
* Get the active segment or subsegment in the current scope.
*
* Usually you won't need to call this method unless you are creating custom subsegments or using manual mode.
*
* @see https://docs.aws.amazon.com/xray/latest/devguide/xray-concepts.html#xray-concepts-segments
* @see https://awslabs.github.io/aws-lambda-powertools-typescript/latest/core/tracer/#escape-hatch-mechanism
*
* @example
* ```typescript
* import { Tracer } from '@aws-lambda-powertools/tracer';
*
* const tracer = new Tracer({ serviceName: 'serverlessAirline' });
*
* export const handler = async (_event: any, _context: any) => {
* const currentSegment = tracer.getSegment();
* ... // Do something with segment
* }
* ```
*
* @returns segment - The active segment or subsegment in the current scope.
*/
public getSegment(): Segment | Subsegment {
if (!this.isTracingEnabled()) {
return new Subsegment('## Dummy segment');
}
const segment = this.provider.getSegment();
if (segment === undefined) {
throw new Error('Failed to get the current sub/segment from the context.');
}
return segment;
}
/**
* Get the current value of the `tracingEnabled` property.
*
* You can use this method during manual instrumentation to determine
* if tracer is currently enabled.
*
* @returns tracingEnabled - `true` if tracing is enabled, `false` otherwise.
*/
public isTracingEnabled(): boolean {
return this.tracingEnabled;
}
/**
* Adds annotation to existing segment or subsegment.
*
* @see https://docs.aws.amazon.com/xray/latest/devguide/xray-sdk-nodejs-segment.html#xray-sdk-nodejs-segment-annotations
*
* @example
* ```typescript
* import { Tracer } from '@aws-lambda-powertools/tracer';
*
* const tracer = new Tracer({ serviceName: 'serverlessAirline' });
*
* export const handler = async (_event: any, _context: any) => {
* tracer.putAnnotation('successfulBooking', true);
* }
* ```
*
* @param key - Annotation key
* @param value - Value for annotation
*/
public putAnnotation(key: string, value: string | number | boolean): void {
if (!this.isTracingEnabled()) return;
const document = this.getSegment();
if (document instanceof Segment) {
console.warn('You cannot annotate the main segment in a Lambda execution environment');
return;
}
document?.addAnnotation(key, value);
}
/**
* Adds metadata to existing segment or subsegment.
*
* @see https://docs.aws.amazon.com/xray/latest/devguide/xray-sdk-nodejs-segment.html#xray-sdk-nodejs-segment-metadata
*
* @example
* ```typescript
* import { Tracer } from '@aws-lambda-powertools/tracer';
*
* const tracer = new Tracer({ serviceName: 'serverlessAirline' });
*
* export const handler = async (_event: any, _context: any) => {
* const res = someLogic();
* tracer.putMetadata('paymentResponse', res);
* }
* ```
*
* @param key - Metadata key
* @param value - Value for metadata
* @param namespace - Namespace that metadata will lie under, if none is passed it will use the serviceName
*/
public putMetadata(key: string, value: unknown, namespace?: string | undefined): void {
if (!this.isTracingEnabled()) return;
const document = this.getSegment();
if (document instanceof Segment) {
console.warn('You cannot add metadata to the main segment in a Lambda execution environment');
return;
}
namespace = namespace || this.serviceName;
document?.addMetadata(key, value, namespace);
}
/**
* Sets the passed subsegment as the current active subsegment.
*
* If you are using a middleware or a decorator this is done automatically for you.
*
* @see https://docs.aws.amazon.com/xray/latest/devguide/xray-sdk-nodejs-subsegments.html
*
* @example
* ```typescript
* import { Tracer } from '@aws-lambda-powertools/tracer';
* import { Segment } from 'aws-xray-sdk-core';
*
* const tracer = new Tracer({ serviceName: 'serverlessAirline' });
*
* export const handler = async (_event: any, _context: any) => {
* const subsegment = new Subsegment('### foo.bar');
* tracer.setSegment(subsegment);
* }
* ```
*
* @param segment - Subsegment to set as the current segment
*/
public setSegment(segment: Segment | Subsegment): void {
if (!this.isTracingEnabled()) return;
return this.provider.setSegment(segment);
}
/**
* Getter for `customConfigService`.
* Used internally during initialization.
*/
private getCustomConfigService(): ConfigServiceInterface | undefined {
return this.customConfigService;
}
/**
* Getter for `envVarsService`.
* Used internally during initialization.
*/
private getEnvVarsService(): EnvironmentVariablesService {
return <EnvironmentVariablesService> this.envVarsService;
}
/**
* Determine if we are running in a Lambda execution environment.
* Used internally during initialization.
*/
private isLambdaExecutionEnv(): boolean {
return this.getEnvVarsService()?.getAwsExecutionEnv() !== '';
}
/**
* Determine if we are running inside a SAM CLI process.
* Used internally during initialization.
*/
private isLambdaSamCli(): boolean {
return this.getEnvVarsService()?.getSamLocal() !== '';
}
/**
* Validate that the service name provided is valid.
* Used internally during initialization.
*
* @param serviceName - Service name to validate
*/
private static isValidServiceName(serviceName?: string): boolean {
return typeof serviceName === 'string' && serviceName.trim().length > 0;
}
/**
* Setter for `captureError` based on configuration passed and environment variables.
* Used internally during initialization.
*/
private setCaptureError(): void {
const customConfigValue = this.getCustomConfigService()?.getTracingCaptureError();
if (customConfigValue !== undefined && customConfigValue.toLowerCase() === 'false') {
this.captureError = false;
return;
}
const envVarsValue = this.getEnvVarsService()?.getTracingCaptureError();
if (envVarsValue.toLowerCase() === 'false') {
this.captureError = false;
return;
}
}
/**
* Setter for `captureResponse` based on configuration passed and environment variables.
* Used internally during initialization.
*/
private setCaptureResponse(): void {
const customConfigValue = this.getCustomConfigService()?.getTracingCaptureResponse();
if (customConfigValue !== undefined && customConfigValue.toLowerCase() === 'false') {
this.captureResponse = false;
return;
}
const envVarsValue = this.getEnvVarsService()?.getTracingCaptureResponse();
if (envVarsValue.toLowerCase() === 'false') {
this.captureResponse = false;
return;
}
}
/**
* Setter for `customConfigService` based on configuration passed.
* Used internally during initialization.
*
* @param customConfigService - Custom configuration service to use
*/
private setCustomConfigService(customConfigService?: ConfigServiceInterface): void {
this.customConfigService = customConfigService ? customConfigService : undefined;
}
/**
* Setter and initializer for `envVarsService`.
* Used internally during initialization.
*/
private setEnvVarsService(): void {
this.envVarsService = new EnvironmentVariablesService();
}
/**
* Method that reconciles the configuration passed with the environment variables.
* Used internally during initialization.
*
* @param options - Configuration passed to the tracer
*/
private setOptions(options: TracerOptions): Tracer {
const {
enabled,
serviceName,
customConfigService
} = options;
this.setEnvVarsService();
this.setCustomConfigService(customConfigService);
this.setTracingEnabled(enabled);
this.setCaptureResponse();
this.setCaptureError();
this.setServiceName(serviceName);
return this;
}
/**
* Setter for `customConfigService` based on configurations passed and environment variables.
* Used internally during initialization.
*
* @param serviceName - Name of the service to use
*/
private setServiceName(serviceName?: string): void {
if (serviceName !== undefined && Tracer.isValidServiceName(serviceName)) {
this.serviceName = serviceName;
return;
}
const customConfigValue = this.getCustomConfigService()?.getServiceName();
if (customConfigValue !== undefined && Tracer.isValidServiceName(customConfigValue)) {
this.serviceName = customConfigValue;
return;
}
const envVarsValue = this.getEnvVarsService()?.getServiceName();
if (envVarsValue !== undefined && Tracer.isValidServiceName(envVarsValue)) {
this.serviceName = envVarsValue;
return;
}
}
/**
* Setter for `tracingEnabled` based on configurations passed and environment variables.
* Used internally during initialization.
*
* @param enabled - Whether or not tracing is enabled
*/
private setTracingEnabled(enabled?: boolean): void {
if (enabled !== undefined && !enabled) {
this.tracingEnabled = enabled;
return;
}
const customConfigValue = this.getCustomConfigService()?.getTracingEnabled();
if (customConfigValue !== undefined && customConfigValue.toLowerCase() === 'false') {
this.tracingEnabled = false;
return;
}
const envVarsValue = this.getEnvVarsService()?.getTracingEnabled();
if (envVarsValue.toLowerCase() === 'false') {
this.tracingEnabled = false;
return;
}
if (this.isLambdaSamCli() || !this.isLambdaExecutionEnv()) {
this.tracingEnabled = false;
}
}
}
export {
Tracer
};