-
Notifications
You must be signed in to change notification settings - Fork 146
/
Tracer.ts
966 lines (883 loc) · 30.4 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
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
/**
* If the AWS_XRAY_CONTEXT_MISSING environment variable is not set, we set it to IGNORE_ERROR.
*
* This is to prevent the AWS X-Ray SDK from logging errors when using top-level await features that make HTTP requests.
* For example, when using the Parameters utility to fetch parameters during the initialization of the Lambda handler - See #2046
*/
if (
process.env.AWS_XRAY_CONTEXT_MISSING === '' ||
process.env.AWS_XRAY_CONTEXT_MISSING === undefined
) {
process.env.AWS_XRAY_CONTEXT_MISSING = 'IGNORE_ERROR';
}
import { Utility } from '@aws-lambda-powertools/commons';
import type {
AsyncHandler,
HandlerMethodDecorator,
SyncHandler,
} from '@aws-lambda-powertools/commons/types';
import type { Handler } from 'aws-lambda';
import type { Segment, Subsegment } from 'aws-xray-sdk-core';
import xraySdk from 'aws-xray-sdk-core';
import { EnvironmentVariablesService } from './config/EnvironmentVariablesService.js';
import { ProviderService } from './provider/ProviderService.js';
import type { ConfigServiceInterface } from './types/ConfigServiceInterface.js';
import type { ProviderServiceInterface } from './types/ProviderService.js';
import type {
AnyClass,
CaptureLambdaHandlerOptions,
CaptureMethodOptions,
MethodDecorator,
TracerInterface,
TracerOptions,
} from './types/Tracer.js';
const { Subsegment: XraySubsegment } = xraySdk;
/**
* ## 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
* * Automatically trace HTTP(s) clients and generate segments for each request
* * 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://docs.powertools.aws.dev/lambda/typescript/latest/core/tracer/).
*
* ### Functions usage with middleware
*
* If you use function-based Lambda handlers you can use the {@link Tracer.captureLambdaHandler} 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 { Tracer } from '@aws-lambda-powertools/tracer';
* import { captureLambdaHandler } from '@aws-lambda-powertools/tracer/middleware';
* import middy from '@middy/core';
*
* const tracer = new Tracer({ serviceName: 'serverlessAirline' });
*
* const lambdaHandler = async (_event: unknown, _context: unknown) => {
* ...
* };
*
* export const handler = middy(lambdaHandler).use(captureLambdaHandler(tracer));
* ```
*
* ### Object oriented usage with decorators
*
* If instead you use TypeScript Classes to wrap your Lambda handler you can use the {@link Tracer.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';
* import type { LambdaInterface } from '@aws-lambda-powertools/commons/types';
*
* const tracer = new Tracer({ serviceName: 'serverlessAirline' });
*
* class Lambda implements LambdaInterface {
* @tracer.captureLambdaHandler()
* public handler(_event: unknown, _context: unknown) {
* ...
* }
* }
*
* const handlerClass = new Lambda();
* export const handler = handlerClass.handler.bind(handlerClass);
* ```
*
* ### 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';
*
* const tracer = new Tracer({ serviceName: 'serverlessAirline' });
*
* export const handler = async (_event: unknown, _context: unknown) => {
* 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 {
* // ... your own logic goes here
* // 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 extends Utility implements TracerInterface {
/**
* The provider service interface used by the Tracer.
* This interface defines the methods and properties that a provider service must implement.
*/
public provider: ProviderServiceInterface;
/**
* Flag indicating whether to capture errors.
* This is used to determine if errors should be added to the trace as metadata.
*
* @default true
*/
private captureError = true;
/**
* Flag indicating whether to capture HTTP(s) requests.
* @default true
*/
private captureHTTPsRequests = true;
/**
* Flag indicating whether to capture response data.
* @default true
*/
private captureResponse = true;
/**
* The custom configuration service used by the Tracer.
*/
private customConfigService?: ConfigServiceInterface;
/**
* The environment variables service used by the Tracer, is always initialized in the constructor in setOptions().
*/
private envVarsService!: EnvironmentVariablesService;
// serviceName is always initialized in the constructor in setOptions()
/**
* The name of the service, is always initialized in the constructor in setOptions().
*/
private serviceName!: string;
/**
* Flag indicating whether tracing is enabled.
* @default true
*/
private tracingEnabled = true;
public constructor(options: TracerOptions = {}) {
super();
this.setOptions(options);
this.provider = new ProviderService();
if (this.isTracingEnabled() && this.captureHTTPsRequests) {
this.provider.captureHTTPsGlobal();
this.provider.instrumentFetch();
}
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
* @param [remote] - Whether the error was thrown by a remote service. Defaults to `false`
*/
public addErrorAsMetadata(error: Error, remote?: boolean): void {
if (!this.isTracingEnabled()) {
return;
}
const subsegment = this.getSegment();
if (subsegment === undefined) {
return;
}
if (!this.captureError) {
subsegment.addErrorFlag();
return;
}
subsegment.addError(error, remote || 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()) {
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', this.getColdStart());
}
}
/**
* 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: unknown, _context: unknown) => {
* ...
* }
* ```
*
* @deprecated Use {@link captureAWSv3Client} instead.
* @param aws - AWS SDK v2 import
*/
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: unknown, _context: unknown) => {
* ...
* }
* ```
* @deprecated Use {@link captureAWSv3Client} instead.
* @param service - AWS SDK v2 client
*/
/* v8 ignore start */ 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/aws-powertools/powertools-lambda-typescript/issues/524#issuecomment-1024493662
this.provider.captureAWSClient((service as T & { service: T }).service);
return service;
} catch {
throw error;
}
}
} /* v8 ignore stop */
/**
* 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: unknown, _context: unknown) => {
* ...
* }
* ```
*
* @param service - 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';
* import type { LambdaInterface } from '@aws-lambda-powertools/commons/types';
*
* const tracer = new Tracer({ serviceName: 'serverlessAirline' });
*
* class Lambda implements LambdaInterface {
* @tracer.captureLambdaHandler()
* public handler(_event: unknown, _context: unknown) {
* // ...
* }
* }
*
* const handlerClass = new Lambda();
* export const handler = handlerClass.handler.bind(handlerClass);
* ```
*
* @param options - (_optional_) Options for the decorator
*/
public captureLambdaHandler(
options?: CaptureLambdaHandlerOptions
): HandlerMethodDecorator {
return (_target, _propertyKey, descriptor) => {
// biome-ignore lint/style/noNonNullAssertion: The descriptor.value is the method this decorator decorates, it cannot be undefined.
const originalMethod = descriptor.value!;
const tracerRef = this;
// Use a function() {} instead of an () => {} arrow function so that we can
// access `myClass` as `this` in a decorated `myClass.myMethod()`.
descriptor.value = function (this: Handler, event, context, callback) {
if (!tracerRef.isTracingEnabled()) {
return originalMethod.apply(this, [event, context, callback]);
}
return tracerRef.provider.captureAsyncFunc(
`## ${process.env._HANDLER}`,
async (subsegment) => {
tracerRef.annotateColdStart();
tracerRef.addServiceNameAnnotation();
let result: unknown;
try {
result = await originalMethod.apply(this, [
event,
context,
callback,
]);
if (options?.captureResponse ?? true) {
tracerRef.addResponseAsMetadata(result, process.env._HANDLER);
}
} catch (error) {
tracerRef.addErrorAsMetadata(error as Error);
throw error;
} finally {
try {
subsegment?.close();
} catch (error) {
console.warn(
'Failed to close or serialize segment %s. We are catching the error but data might be lost.',
subsegment?.name,
error
);
}
}
return result;
}
);
} as SyncHandler<Handler> | AsyncHandler<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';
* import { LambdaInterface } from '@aws-lambda-powertools/commons';
*
* const tracer = new Tracer({ serviceName: 'serverlessAirline' });
*
* class Lambda implements LambdaInterface {
* @tracer.captureMethod()
* public myMethod(param: string) {
* // ...
* }
*
* public handler(_event: unknown, _context: unknown) {
* this.myMethod('foo');
* }
* }
*
* const handlerClass = new Lambda();
* export const handler = handlerClass.handler.bind(handlerClass);;
* ```
*
* @param options - (_optional_) Options for the decorator
*/
public captureMethod<T extends AnyClass>(
options?: CaptureMethodOptions
): MethodDecorator<T> {
return (_target, propertyKey, descriptor) => {
// biome-ignore lint/style/noNonNullAssertion: The descriptor.value is the method this decorator decorates, it cannot be undefined.
const originalMethod = descriptor.value!;
const tracerRef = this;
// Use a function() {} instead of an () => {} arrow function so that we can
// access `myClass` as `this` in a decorated `myClass.myMethod()`.
descriptor.value = function (...args: unknown[]) {
if (!tracerRef.isTracingEnabled()) {
return originalMethod.apply(this, [...args]);
}
const methodName = String(propertyKey);
const subsegmentName = options?.subSegmentName
? options.subSegmentName
: `### ${methodName}`;
return tracerRef.provider.captureAsyncFunc(
subsegmentName,
async (subsegment) => {
// biome-ignore lint/suspicious/noExplicitAny: we don't know the type of the result because we're decorating arbitrary functions
let result: any;
try {
result = await originalMethod.apply(this, [...args]);
if (options?.captureResponse ?? true) {
tracerRef.addResponseAsMetadata(result, methodName);
}
} catch (error) {
tracerRef.addErrorAsMetadata(error as Error);
throw error;
} finally {
try {
subsegment?.close();
} catch (error) {
console.warn(
'Failed to close or serialize segment %s. We are catching the error but data might be lost.',
subsegment?.name,
error
);
}
}
return result;
}
);
};
return descriptor;
};
}
/**
* Get the current root AWS X-Ray trace id.
*
* Utility method that returns the current AWS X-Ray Root trace id. Useful as correlation id for downstream processes.
*
* @see https://docs.aws.amazon.com/xray/latest/devguide/xray-concepts.html#xray-concepts-traces
*
* @example
* ```typescript
* import { Tracer } from '@aws-lambda-powertools/tracer';
*
* const tracer = new Tracer({ serviceName: 'serverlessAirline' });
*
* export const handler = async () => {
* try {
* ...
* } catch (err) {
* const rootTraceId = tracer.getRootXrayTraceId();
*
* // Example of returning an error response
* return {
* statusCode: 500,
* // Include the rootTraceId in the response so we can show a "contact support" button that
* // takes the customer to a customer service form with the trace as additional context.
* body: `Internal Error - Please contact support and quote the following id: ${rootTraceId}`,
* headers: { '_X_AMZN_TRACE_ID': rootTraceId },
* };
* }
* }
* ```
*/
public getRootXrayTraceId(): string | undefined {
return this.envVarsService.getXrayTraceId();
}
/**
* Get the active segment or subsegment (if any) 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://docs.powertools.aws.dev/lambda/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: unknown, _context: unknown) => {
* const currentSegment = tracer.getSegment();
* ... // Do something with segment
* }
* ```
*/
public getSegment(): Segment | Subsegment | undefined {
if (!this.isTracingEnabled()) {
return new XraySubsegment('## Dummy segment');
}
const segment = this.provider.getSegment();
if (segment === undefined) {
console.warn(
'Failed to get the current sub/segment from the context, this is likely because you are not using the Tracer in a Lambda function.'
);
}
return segment;
}
/**
* Get the current value of the AWS X-Ray Sampled flag.
*
* Utility method that returns the current AWS X-Ray Sampled flag.
*
* @see https://docs.aws.amazon.com/xray/latest/devguide/xray-concepts.html#xray-concepts-traces
*/
public isTraceSampled(): boolean {
if (!this.isTracingEnabled()) return false;
return this.envVarsService.getXrayTraceSampled();
}
/**
* Get the current value of the `tracingEnabled` property.
*
* You can use this method during manual instrumentation to determine
* if tracer is currently enabled.
*/
public isTracingEnabled(): boolean {
return this.tracingEnabled;
}
/**
* Add 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: unknown, _context: unknown) => {
* 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;
this.provider.putAnnotation(key, value);
}
/**
* Add 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: unknown, _context: unknown) => {
* 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;
this.provider.putMetadata(key, value, namespace || this.serviceName);
}
/**
* Set 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 { Subsegment } from 'aws-xray-sdk-core';
*
* const tracer = new Tracer({ serviceName: 'serverlessAirline' });
*
* export const handler = async (_event: unknown, _context: unknown) => {
* 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;
this.provider.setSegment(segment);
}
/**
* Getter for `customConfigService`.
* Used internally during initialization.
*/
private getCustomConfigService(): ConfigServiceInterface | undefined {
return this.customConfigService;
}
/**
* Get for `envVarsService`.
* Used internally during initialization.
*/
private getEnvVarsService(): EnvironmentVariablesService {
return this.envVarsService;
}
/**
* Determine if we are running inside an Amplify CLI process.
* Used internally during initialization.
*/
private isAmplifyCli(): boolean {
return (
this.getEnvVarsService().getAwsExecutionEnv() ===
'AWS_Lambda_amplify-mock'
);
}
/**
* 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() !== '';
}
/**
* Set `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;
}
}
/**
* Patch all HTTP(s) clients and create traces when your application makes calls outgoing calls.
*
* Calls using third-party HTTP request libraries, such as Axios, are supported as long as they use the native http
* module under the hood. Support for third-party HTTP request libraries is provided on a best effort basis.
*
* @see https://docs.aws.amazon.com/xray/latest/devguide/xray-sdk-nodejs-httpclients.html
*
* @param enabled - Whether or not to patch all HTTP clients
*/
private setCaptureHTTPsRequests(enabled?: boolean): void {
if (enabled !== undefined && !enabled) {
this.captureHTTPsRequests = false;
return;
}
const customConfigValue =
this.getCustomConfigService()?.getCaptureHTTPsRequests();
if (
customConfigValue !== undefined &&
customConfigValue.toLowerCase() === 'false'
) {
this.captureHTTPsRequests = false;
return;
}
const envVarsValue = this.getEnvVarsService().getCaptureHTTPsRequests();
if (envVarsValue.toLowerCase() === 'false') {
this.captureHTTPsRequests = false;
return;
}
}
/**
* Set `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;
}
}
/**
* Set `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;
}
/**
* Set and initialize `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, captureHTTPsRequests, customConfigService } =
options;
this.setEnvVarsService();
this.setCustomConfigService(customConfigService);
this.setTracingEnabled(enabled);
this.setCaptureResponse();
this.setCaptureError();
this.setServiceName(serviceName);
this.setCaptureHTTPsRequests(captureHTTPsRequests);
return this;
}
/**
* Set `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 && this.isValidServiceName(serviceName)) {
this.serviceName = serviceName;
return;
}
const customConfigValue = this.getCustomConfigService()?.getServiceName();
if (
customConfigValue !== undefined &&
this.isValidServiceName(customConfigValue)
) {
this.serviceName = customConfigValue;
return;
}
const envVarsValue = this.getEnvVarsService().getServiceName();
if (envVarsValue !== undefined && this.isValidServiceName(envVarsValue)) {
this.serviceName = envVarsValue;
return;
}
this.serviceName = this.getDefaultServiceName();
}
/**
* Set `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.isAmplifyCli() ||
this.isLambdaSamCli() ||
!this.isLambdaExecutionEnv()
) {
this.tracingEnabled = false;
}
}
}
export { Tracer };