Skip to content

Commit 0681511

Browse files
authored
docs(tracer): update annotation & metadata docs to include full code (#1704)
1 parent df21ec8 commit 0681511

File tree

3 files changed

+20
-2
lines changed

3 files changed

+20
-2
lines changed

Diff for: docs/core/tracer.md

+8-2
Original file line numberDiff line numberDiff line change
@@ -129,16 +129,22 @@ When using the `captureLambdaHandler` decorator or middleware, Tracer performs t
129129
=== "Annotations"
130130
You can add annotations using `putAnnotation` method.
131131

132-
```typescript hl_lines="9"
132+
```typescript hl_lines="12"
133133
--8<-- "docs/snippets/tracer/putAnnotation.ts"
134134
```
135+
136+
1. When Lambda starts an invocation [the X-Ray SDk creates a segment called `facade`](https://docs.aws.amazon.com/xray/latest/devguide/xray-sdk-nodejs-subsegments.html#xray-sdk-nodejs-subsegments-lambda). This segment cannot be annotated or modified by your code, so you need to create a new subsegment. This is done automatically by Tracer when using the [decorator or middleware patterns](./tracer.md/#lambda-handler)
137+
2. To correctly trace the current and subsequent invocations you need to restore the original segment, this is done automatically by Tracer when using the [decorator or middleware patterns](./tracer.md/#lambda-handler).
135138
=== "Metadata"
136139
You can add metadata using `putMetadata` method.
137140

138-
```typescript hl_lines="9-11"
141+
```typescript hl_lines="12-14"
139142
--8<-- "docs/snippets/tracer/putMetadata.ts"
140143
```
141144

145+
1. When Lambda starts an invocation [the X-Ray SDk creates a segment called `facade`](https://docs.aws.amazon.com/xray/latest/devguide/xray-sdk-nodejs-subsegments.html#xray-sdk-nodejs-subsegments-lambda). This segment cannot be modified by your code, so you need to create a new subsegment. This is done automatically by Tracer when using the [decorator or middleware patterns](./tracer.md/#lambda-handler)
146+
2. To correctly trace the current and subsequent invocations you need to restore the original segment, this is done automatically by Tracer when using the [decorator or middleware patterns](./tracer.md/#lambda-handler).
147+
142148
<figure>
143149
<img src="../../media/tracer_utility_showcase_2.png" loading="lazy" alt="Screenshot of the Amazon CloudWatch Console showing an example of segments and subsegments generated and with metadata set for the handler"/>
144150
<figcaption>Tracer showcase - Handler Metadata</figcaption>

Diff for: docs/snippets/tracer/putAnnotation.ts

+6
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,11 @@ export const handler = async (
66
_event: unknown,
77
_context: unknown
88
): Promise<void> => {
9+
const handlerSegment = tracer.getSegment()?.addNewSubsegment('### handler');
10+
handlerSegment && tracer.setSegment(handlerSegment); // (1)!
11+
912
tracer.putAnnotation('successfulBooking', true);
13+
14+
handlerSegment?.close();
15+
handlerSegment && tracer.setSegment(handlerSegment?.parent); // (2)!
1016
};

Diff for: docs/snippets/tracer/putMetadata.ts

+6
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,13 @@ export const handler = async (
66
_event: unknown,
77
_context: unknown
88
): Promise<void> => {
9+
const handlerSegment = tracer.getSegment()?.addNewSubsegment('### handler');
10+
handlerSegment && tracer.setSegment(handlerSegment); // (1)!
11+
912
tracer.putMetadata('paymentResponse', {
1013
foo: 'bar',
1114
});
15+
16+
handlerSegment?.close();
17+
handlerSegment && tracer.setSegment(handlerSegment?.parent); // (2)!
1218
};

0 commit comments

Comments
 (0)