Skip to content

Commit 4f40809

Browse files
authored
Merge pull request #65 from DataDog/chris.agocs/docs_for_adding_request_id_to_logs
Add documentation and example for adding request ID to logs.
2 parents 3d02db4 + 046eb73 commit 4f40809

File tree

1 file changed

+48
-5
lines changed

1 file changed

+48
-5
lines changed

README.md

Lines changed: 48 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -87,11 +87,8 @@ If you set the value of this variable to "true" then the Lambda layer will incre
8787

8888
### DD_LOGS_INJECTION
8989

90-
By default, the Datadog trace id gets automatically injected into the logs for correlation, if using `console` or a logging library supported for [automatic](https://docs.datadoghq.com/tracing/connect_logs_and_traces/?tab=nodejs#automatic-trace-id-injection) trace id injection.
91-
92-
See instructions for [manual](https://docs.datadoghq.com/tracing/connect_logs_and_traces/?tab=nodejs#manual-trace-id-injection) trace id injection, if using other logging libraries.
93-
94-
Set the environment variable `DD_LOGS_INJECTION` to `false` to disable this feature.
90+
Controlls whether or not the Datadog Trace ID is injected into log lines. See [DD_LOGS_INJECTION](#DD_LOGS_INJECTION-environment-variable) under
91+
the Trace & Log Correlation section below.
9592

9693
## Usage
9794

@@ -136,6 +133,52 @@ sendDistributionMetric(
136133

137134
If your Lambda function is associated with a VPC, you need to ensure it has access to the [public internet](https://aws.amazon.com/premiumsupport/knowledge-center/internet-access-lambda-function/).
138135

136+
## Trace & Log Correlation
137+
138+
### Using the Datadog Tracer
139+
140+
If you are using the [Datadog Tracer](#datadog-tracer-experimental), follow [these instructions](https://docs.datadoghq.com/tracing/connect_logs_and_traces/nodejs/#automatic-trace-id-injection)
141+
to set up automatic trace-log correlation.
142+
143+
### Without using the Datadog Tracer
144+
145+
In order to correlate logs emitted by your Lambda with specific invocations, it
146+
is necessary to add the AWS Request ID to your logs. This is done automatically
147+
for `console.log()`, but you will have to implement this for other logging libraries.
148+
149+
The AWS Request ID is available in the context that is passed to your lambda handler,
150+
as `context.awsRequestId`. It should be included in your log line as `lambda.request_id`.
151+
152+
For example, using the [Pino](https://getpino.io/) logger:
153+
154+
```typescript
155+
const logger = require('pino')();
156+
157+
exports.handler = async function(event, context) {
158+
159+
//This sets up your request-specific logger to emit logs with the Request ID property.
160+
const req_logger = logger.child({ 'lambda.request_id': context.awsRequestId });
161+
162+
//Carry on with whatever the lambda needs to do
163+
const work = do.Work();
164+
165+
//Write a log message
166+
req_logger.info("Work complete");
167+
168+
return work;
169+
}
170+
171+
```
172+
173+
### DD_LOGS_INJECTION environment variable
174+
175+
By default, the Datadog trace id gets automatically injected into the logs for correlation, if using `console` or a logging library supported for [automatic](https://docs.datadoghq.com/tracing/connect_logs_and_traces/?tab=nodejs#automatic-trace-id-injection) trace id injection.
176+
177+
See instructions for [manual](https://docs.datadoghq.com/tracing/connect_logs_and_traces/?tab=nodejs#manual-trace-id-injection) trace id injection, if using other logging libraries.
178+
179+
Set the environment variable `DD_LOGS_INJECTION` to `false` to disable this feature.
180+
181+
139182
## Distributed Tracing
140183

141184
[Distributed tracing](https://docs.datadoghq.com/tracing/guide/distributed_tracing/?tab=nodejs) allows you to propagate a trace context from a service running on a host to a service running on AWS Lambda, and vice versa, so you can see performance end-to-end. Linking is implemented by injecting Datadog trace context into the HTTP request headers.

0 commit comments

Comments
 (0)