From e313e66381c20f611aed2b141f4fbe2bf1c701e1 Mon Sep 17 00:00:00 2001 From: "chris.agocs" Date: Fri, 27 Mar 2020 15:43:31 -0400 Subject: [PATCH 1/6] Add documentation and example for adding request ID to logs. --- README.md | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/README.md b/README.md index 5f1d3d67..0e481cf1 100644 --- a/README.md +++ b/README.md @@ -136,6 +136,37 @@ sendDistributionMetric( 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/). +## Log correlation with other logging libraries + +In order to correlate logs emitted by your Lambda with specific invocations, it +is necessary to add the AWS Request ID to your logs. This is done automatically +for `console.log()`, but you will have to implement this for other logging libraries. + +The AWS Request ID is available in the context that is passed to your lambda handler, +as `context.awsRequestId`. It should be included in your log line as `@lambda.request_id`. + +For example, using the [Pino](https://getpino.io/) logger: + +```js + +const logger = require('pino')() + +exports.handler = async function(event, context) { + + //This sets up your request-specific logger to emit logs with the Request ID property. + const req_logger = logger.child({ '@lambda.request_id': context.awsRequestId }); + + //Carry on with whatever the lambda needs to do + const work = do.Work(); + + //Write a log messagew + req_logger.info("Work complete"); + + return work; +} + +``` + ## Distributed Tracing [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. From 4794944adbf2d6f37482f1231f593bd9b4ce1c79 Mon Sep 17 00:00:00 2001 From: "chris.agocs" Date: Fri, 27 Mar 2020 15:52:53 -0400 Subject: [PATCH 2/6] Fix a couple typos in the example --- README.md | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 0e481cf1..28088b26 100644 --- a/README.md +++ b/README.md @@ -147,9 +147,8 @@ as `context.awsRequestId`. It should be included in your log line as `@lambda.re For example, using the [Pino](https://getpino.io/) logger: -```js - -const logger = require('pino')() +```typescript +const logger = require('pino')(); exports.handler = async function(event, context) { @@ -159,7 +158,7 @@ exports.handler = async function(event, context) { //Carry on with whatever the lambda needs to do const work = do.Work(); - //Write a log messagew + //Write a log message req_logger.info("Work complete"); return work; From d132d165d78a7903bf0282f78cd664bd451187d0 Mon Sep 17 00:00:00 2001 From: Christopher Agocs Date: Fri, 27 Mar 2020 16:19:29 -0400 Subject: [PATCH 3/6] Remove `@` Co-Authored-By: Stephen Firrincieli --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 28088b26..0d23f5ed 100644 --- a/README.md +++ b/README.md @@ -153,7 +153,7 @@ const logger = require('pino')(); exports.handler = async function(event, context) { //This sets up your request-specific logger to emit logs with the Request ID property. - const req_logger = logger.child({ '@lambda.request_id': context.awsRequestId }); + const req_logger = logger.child({ 'lambda.request_id': context.awsRequestId }); //Carry on with whatever the lambda needs to do const work = do.Work(); From ec0296c55d96e6ccfdb86b9da84a50a1b6407930 Mon Sep 17 00:00:00 2001 From: "chris.agocs" Date: Mon, 30 Mar 2020 12:09:17 -0400 Subject: [PATCH 4/6] update docs per pr comments --- README.md | 27 ++++++++++++++++++++------- 1 file changed, 20 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index 0d23f5ed..99d299d1 100644 --- a/README.md +++ b/README.md @@ -87,11 +87,8 @@ If you set the value of this variable to "true" then the Lambda layer will incre ### DD_LOGS_INJECTION -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. - -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. - -Set the environment variable `DD_LOGS_INJECTION` to `false` to disable this feature. +Controlls whether or not the request ID is injected into log lines. See [DD_LOGS_INJECTION](#DD_LOGS_INJECTION-environment-variable) under +the Trace & Log Correlation section below. ## Usage @@ -136,14 +133,21 @@ sendDistributionMetric( 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/). -## Log correlation with other logging libraries +## Trace & Log Correlation + +### Using the Datadog Tracer + +If you are using the [Datadog Tracer](#datadog-tracer-experimental), your log messages +will be correlated within the appropriate traces automatically. + +### Without using the Datadog Tracer In order to correlate logs emitted by your Lambda with specific invocations, it is necessary to add the AWS Request ID to your logs. This is done automatically for `console.log()`, but you will have to implement this for other logging libraries. The AWS Request ID is available in the context that is passed to your lambda handler, -as `context.awsRequestId`. It should be included in your log line as `@lambda.request_id`. +as `context.awsRequestId`. It should be included in your log line as `lambda.request_id`. For example, using the [Pino](https://getpino.io/) logger: @@ -166,6 +170,15 @@ exports.handler = async function(event, context) { ``` +### DD_LOGS_INJECTION environment variable + +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. + +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. + +Set the environment variable `DD_LOGS_INJECTION` to `false` to disable this feature. + + ## Distributed Tracing [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. From 54b58a253d4225ae129accc5b66e08c44d6014bd Mon Sep 17 00:00:00 2001 From: "chris.agocs" Date: Mon, 30 Mar 2020 12:43:57 -0400 Subject: [PATCH 5/6] Fix DD_TRACE_ID description --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 99d299d1..35560156 100644 --- a/README.md +++ b/README.md @@ -87,7 +87,7 @@ If you set the value of this variable to "true" then the Lambda layer will incre ### DD_LOGS_INJECTION -Controlls whether or not the request ID is injected into log lines. See [DD_LOGS_INJECTION](#DD_LOGS_INJECTION-environment-variable) under +Controlls whether or not the Datadog Trace ID is injected into log lines. See [DD_LOGS_INJECTION](#DD_LOGS_INJECTION-environment-variable) under the Trace & Log Correlation section below. ## Usage From 046eb73ab9842723599dfe5b7e6355249068e346 Mon Sep 17 00:00:00 2001 From: "chris.agocs" Date: Mon, 30 Mar 2020 13:03:48 -0400 Subject: [PATCH 6/6] Automatic trace-log correlation --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 35560156..a81efc76 100644 --- a/README.md +++ b/README.md @@ -137,8 +137,8 @@ If your Lambda function is associated with a VPC, you need to ensure it has acce ### Using the Datadog Tracer -If you are using the [Datadog Tracer](#datadog-tracer-experimental), your log messages -will be correlated within the appropriate traces automatically. +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) +to set up automatic trace-log correlation. ### Without using the Datadog Tracer