@@ -27,18 +27,76 @@ class Tracer implements TracerInterface {
27
27
this . provider = new ProviderService ( ) ;
28
28
}
29
29
30
+ /**
31
+ * Patch all AWS SDK v2 clients and create traces when your application makes calls to AWS services.
32
+ *
33
+ * If you want to patch a specific client use {@link captureAWSClient} and if you are using AWS SDK v3 use {@link captureAWSv3Client} instead.
34
+ *
35
+ * @see https://docs.aws.amazon.com/xray/latest/devguide/xray-sdk-nodejs-awssdkclients.html
36
+ *
37
+ * @example
38
+ * ```typescript
39
+ * import { Tracer } from '@aws-lambda-powertools/tracer';
40
+ *
41
+ * const tracer = new Tracer({ serviceName: 'my-service' });
42
+ * const AWS = tracer.captureAWS(require('aws-sdk'));
43
+ * ```
44
+ *
45
+ * @param aws - AWS SDK v2 import
46
+ * @returns AWS - Instrumented AWS SDK
47
+ */
30
48
public captureAWS < T > ( aws : T ) : T {
31
49
if ( this . tracingEnabled === false ) return aws ;
32
50
33
51
return this . provider . captureAWS ( aws ) ;
34
52
}
35
53
54
+ /**
55
+ * Patch a specific AWS SDK v2 client and create traces when your application makes calls to that AWS service.
56
+ *
57
+ * If you want to patch all clients use {@link captureAWS} and if you are using AWS SDK v3 use {@link captureAWSv3Client} instead.
58
+ *
59
+ * @see https://docs.aws.amazon.com/xray/latest/devguide/xray-sdk-nodejs-awssdkclients.html
60
+ *
61
+ * @example
62
+ * ```typescript
63
+ * import { S3 } from "aws-sdk";
64
+ * import { Tracer } from '@aws-lambda-powertools/tracer';
65
+ *
66
+ * const tracer = new Tracer({ serviceName: 'my-service' });
67
+ * tracer.captureAWS(require('aws-sdk'));
68
+ * const s3 = tracer.captureAWSClient(new S3({ apiVersion: "2006-03-01" }));
69
+ * ```
70
+ *
71
+ * @param service - AWS SDK v2 client
72
+ * @returns service - Instrumented AWS SDK v2 client
73
+ */
36
74
public captureAWSClient < T > ( service : T ) : T {
37
75
if ( this . tracingEnabled === false ) return service ;
38
76
39
77
return this . provider . captureAWSClient ( service ) ;
40
78
}
41
79
80
+ /**
81
+ * Patch an AWS SDK v3 client and create traces when your application makes calls to that AWS service.
82
+ *
83
+ * If you are using AWS SDK v2 use {@link captureAWSClient} instead.
84
+ *
85
+ * @see https://docs.aws.amazon.com/xray/latest/devguide/xray-sdk-nodejs-awssdkclients.html
86
+ *
87
+ * @example
88
+ * ```typescript
89
+ * import { S3Client } from "@aws-sdk/client-s3";
90
+ * import { Tracer } from '@aws-lambda-powertools/tracer';
91
+ *
92
+ * const tracer = new Tracer({ serviceName: 'my-service' });
93
+ * const client = new S3Client({});
94
+ * tracer.captureAWSv3Client(client);
95
+ * ```
96
+ *
97
+ * @param service - AWS SDK v3 client
98
+ * @returns service - Instrumented AWS SDK v3 client
99
+ */
42
100
public captureAWSv3Client < T > ( service : T ) : T {
43
101
if ( this . tracingEnabled === false ) return service ;
44
102
@@ -106,6 +164,28 @@ class Tracer implements TracerInterface {
106
164
} ;
107
165
}
108
166
167
+ /**
168
+ * Get the active segment or subsegment in the current scope.
169
+ *
170
+ * Usually you won't need to call this method unless you are manipulating segments using the escape hatch pattern.
171
+ *
172
+ * @see https://docs.aws.amazon.com/xray/latest/devguide/xray-concepts.html#xray-concepts-segments
173
+ * @see https://awslabs.github.io/aws-lambda-powertools-typescript/latest/core/tracer/#escape-hatch-mechanism
174
+ *
175
+ * @example
176
+ * ```typescript
177
+ * import { Tracer } from '@aws-lambda-powertools/tracer';
178
+ *
179
+ * const tracer = new Tracer({ serviceName: 'my-service' });
180
+ *
181
+ * export const handler = async (_event: any, _context: any) => {
182
+ * const currentSegment = tracer.getSegment();
183
+ * ... // Do something with segment
184
+ * }
185
+ * ```
186
+ *
187
+ * @returns segment - The active segment or subsegment in the current scope.
188
+ */
109
189
public getSegment ( ) : Segment | Subsegment {
110
190
const segment = this . provider . getSegment ( ) ;
111
191
if ( segment === undefined ) {
@@ -115,6 +195,17 @@ class Tracer implements TracerInterface {
115
195
return segment ;
116
196
}
117
197
198
+ /**
199
+ * Retrieve the current value of `ColdStart`.
200
+ *
201
+ * If Tracer has been initialized outside of the Lambda handler then the same instance
202
+ * of Tracer will be reused throghout the lifecycle of that same Lambda execution environment
203
+ * and this method will return `false` after the first invocation.
204
+ *
205
+ * @see https://docs.aws.amazon.com/lambda/latest/dg/runtimes-context.html
206
+ *
207
+ * @returns boolean - true if is cold start otherwise false
208
+ */
118
209
public static isColdStart ( ) : boolean {
119
210
if ( Tracer . coldStart === true ) {
120
211
Tracer . coldStart = false ;
@@ -125,6 +216,25 @@ class Tracer implements TracerInterface {
125
216
return false ;
126
217
}
127
218
219
+ /**
220
+ * Adds annotation to existing segment or subsegment.
221
+ *
222
+ * @see https://docs.aws.amazon.com/xray/latest/devguide/xray-sdk-nodejs-segment.html#xray-sdk-nodejs-segment-annotations
223
+ *
224
+ * @example
225
+ * ```typescript
226
+ * import { Tracer } from '@aws-lambda-powertools/tracer';
227
+ *
228
+ * const tracer = new Tracer({ serviceName: 'my-service' });
229
+ *
230
+ * export const handler = async (_event: any, _context: any) => {
231
+ * tracer.putAnnotation('PaymentStatus', "SUCCESS");
232
+ * }
233
+ * ```
234
+ *
235
+ * @param key - Annotation key
236
+ * @param value - Value for annotation
237
+ */
128
238
public putAnnotation ( key : string , value : string | number | boolean ) : void {
129
239
if ( this . tracingEnabled === false ) return ;
130
240
@@ -137,6 +247,27 @@ class Tracer implements TracerInterface {
137
247
document ?. addAnnotation ( key , value ) ;
138
248
}
139
249
250
+ /**
251
+ * Adds metadata to existing segment or subsegment.
252
+ *
253
+ * @see https://docs.aws.amazon.com/xray/latest/devguide/xray-sdk-nodejs-segment.html#xray-sdk-nodejs-segment-metadata
254
+ *
255
+ * @example
256
+ * ```typescript
257
+ * import { Tracer } from '@aws-lambda-powertools/tracer';
258
+ *
259
+ * const tracer = new Tracer({ serviceName: 'my-service' });
260
+ *
261
+ * export const handler = async (_event: any, _context: any) => {
262
+ * const res = someLogic()
263
+ * tracer.putAnnotation('PaymentResponse', res);
264
+ * }
265
+ * ```
266
+ *
267
+ * @param key - Metadata key
268
+ * @param value - Value for metadata
269
+ * @param timestamp - Namespace that metadata will lie under, if none is passed it will use the serviceName
270
+ */
140
271
public putMetadata ( key : string , value : unknown , namespace ?: string | undefined ) : void {
141
272
if ( this . tracingEnabled === false ) return ;
142
273
@@ -151,10 +282,40 @@ class Tracer implements TracerInterface {
151
282
document ?. addMetadata ( key , value , namespace ) ;
152
283
}
153
284
285
+ /**
286
+ * Sets the passed subsegment as the current active subsegment.
287
+ *
288
+ * If you are using a middleware or a decorator this is done automatically for you.
289
+ *
290
+ * @see https://docs.aws.amazon.com/xray/latest/devguide/xray-sdk-nodejs-subsegments.html
291
+ *
292
+ * @example
293
+ * ```typescript
294
+ * import { Tracer } from '@aws-lambda-powertools/tracer';
295
+ * import { Segment } from 'aws-xray-sdk-core';
296
+ *
297
+ * const tracer = new Tracer({ serviceName: 'my-service' });
298
+ *
299
+ * export const handler = async (_event: any, _context: any) => {
300
+ * const subsegment = new Subsegment('### foo.bar');
301
+ * tracer.setSegment(subsegment);
302
+ * }
303
+ * ```
304
+ *
305
+ * @param segment - Subsegment to set as the current segment
306
+ */
154
307
public setSegment ( segment : Segment | Subsegment ) : void {
155
308
return this . provider . setSegment ( segment ) ;
156
309
}
157
310
311
+ /**
312
+ * Add an error to the current segment or subsegment as metadata.
313
+ * Used internally by decoratorators and middlewares.
314
+ *
315
+ * @see https://docs.aws.amazon.com/xray/latest/devguide/xray-concepts.html#xray-concepts-errors
316
+ *
317
+ * @param error - Error to serialize as metadata
318
+ */
158
319
private addErrorAsMetadata ( error : Error ) : void {
159
320
const subsegment = this . getSegment ( ) ;
160
321
if ( this . captureError === false ) {
@@ -166,6 +327,15 @@ class Tracer implements TracerInterface {
166
327
subsegment . addError ( error , false ) ;
167
328
}
168
329
330
+ /**
331
+ * Add an data to the current segment or subsegment as metadata.
332
+ * Used internally by decoratorators and middlewares.
333
+ *
334
+ * @see https://docs.aws.amazon.com/xray/latest/devguide/xray-concepts.html#xray-concepts-errors
335
+ *
336
+ * @param data - Data to serialize as metadata
337
+ * @param methodName - Name of the method that is being traced
338
+ */
169
339
private addResponseAsMetadata ( data ?: unknown , methodName ?: string ) : void {
170
340
if ( data === undefined || this . captureResponse === false || this . tracingEnabled === false ) {
171
341
return ;
@@ -174,32 +344,62 @@ class Tracer implements TracerInterface {
174
344
this . putMetadata ( `${ methodName } response` , data ) ;
175
345
}
176
346
347
+ /**
348
+ * Add ColdStart annotation to the current segment or subsegment.
349
+ * Used internally by decoratorators and middlewares.
350
+ */
177
351
private annotateColdStart ( ) : void {
178
352
if ( Tracer . isColdStart ( ) ) {
179
353
this . putAnnotation ( 'ColdStart' , true ) ;
180
354
}
181
355
}
182
356
357
+ /**
358
+ * Getter for `customConfigService`.
359
+ * Used internally during initialization.
360
+ */
183
361
private getCustomConfigService ( ) : ConfigServiceInterface | undefined {
184
362
return this . customConfigService ;
185
363
}
186
364
365
+ /**
366
+ * Getter for `envVarsService`.
367
+ * Used internally during initialization.
368
+ */
187
369
private getEnvVarsService ( ) : EnvironmentVariablesService {
188
370
return < EnvironmentVariablesService > this . envVarsService ;
189
371
}
190
372
373
+ /**
374
+ * Determine if we are running in a Lambda execution environment.
375
+ * Used internally during initialization.
376
+ */
191
377
private isLambdaExecutionEnv ( ) : boolean {
192
378
return this . getEnvVarsService ( ) ?. getAwsExecutionEnv ( ) !== '' ;
193
379
}
194
380
381
+ /**
382
+ * Determine if we are running inside a SAM CLI process.
383
+ * Used internally during initialization.
384
+ */
195
385
private isLambdaSamCli ( ) : boolean {
196
386
return this . getEnvVarsService ( ) ?. getSamLocal ( ) !== '' ;
197
387
}
198
388
389
+ /**
390
+ * Validate that the service name provided is valid.
391
+ * Used internally during initialization.
392
+ *
393
+ * @param serviceName - Service name to validate
394
+ */
199
395
private isValidServiceName ( serviceName ?: string ) : boolean {
200
396
return typeof serviceName === 'string' && serviceName . trim ( ) . length > 0 ;
201
397
}
202
398
399
+ /**
400
+ * Setter for `captureError` based on configuration passed and environment variables.
401
+ * Used internally during initialization.
402
+ */
203
403
private setCaptureError ( ) : void {
204
404
const customConfigValue = this . getCustomConfigService ( ) ?. getTracingCaptureError ( ) ;
205
405
if ( customConfigValue !== undefined && customConfigValue . toLowerCase ( ) === 'false' ) {
@@ -216,6 +416,10 @@ class Tracer implements TracerInterface {
216
416
}
217
417
}
218
418
419
+ /**
420
+ * Setter for `captureResponse` based on configuration passed and environment variables.
421
+ * Used internally during initialization.
422
+ */
219
423
private setCaptureResponse ( ) : void {
220
424
const customConfigValue = this . getCustomConfigService ( ) ?. getTracingCaptureResponse ( ) ;
221
425
if ( customConfigValue !== undefined && customConfigValue . toLowerCase ( ) === 'false' ) {
@@ -232,14 +436,30 @@ class Tracer implements TracerInterface {
232
436
}
233
437
}
234
438
439
+ /**
440
+ * Setter for `customConfigService` based on configuration passed.
441
+ * Used internally during initialization.
442
+ *
443
+ * @param customConfigService - Custom configuration service to use
444
+ */
235
445
private setCustomConfigService ( customConfigService ?: ConfigServiceInterface ) : void {
236
446
this . customConfigService = customConfigService ? customConfigService : undefined ;
237
447
}
238
448
449
+ /**
450
+ * Setter and initializer for `envVarsService`.
451
+ * Used internally during initialization.
452
+ */
239
453
private setEnvVarsService ( ) : void {
240
454
this . envVarsService = new EnvironmentVariablesService ( ) ;
241
455
}
242
456
457
+ /**
458
+ * Method that reconciles the configuration passed with the environment variables.
459
+ * Used internally during initialization.
460
+ *
461
+ * @param options - Configuration passed to the tracer
462
+ */
243
463
private setOptions ( options : TracerOptions ) : Tracer {
244
464
const {
245
465
enabled,
@@ -257,6 +477,12 @@ class Tracer implements TracerInterface {
257
477
return this ;
258
478
}
259
479
480
+ /**
481
+ * Setter for `customConfigService` based on configurations passed and environment variables.
482
+ * Used internally during initialization.
483
+ *
484
+ * @param serviceName - Name of the service to use
485
+ */
260
486
private setServiceName ( serviceName ?: string ) : void {
261
487
if ( serviceName !== undefined && this . isValidServiceName ( serviceName ) ) {
262
488
this . serviceName = serviceName ;
@@ -279,6 +505,12 @@ class Tracer implements TracerInterface {
279
505
}
280
506
}
281
507
508
+ /**
509
+ * Setter for `tracingEnabled` based on configurations passed and environment variables.
510
+ * Used internally during initialization.
511
+ *
512
+ * @param enabled - Whether or not tracing is enabled
513
+ */
282
514
private setTracingEnabled ( enabled ?: boolean ) : void {
283
515
if ( enabled !== undefined && enabled === false ) {
284
516
this . tracingEnabled = enabled ;
0 commit comments