Skip to content

Add documentation and example for adding request ID to logs. #65

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Mar 31, 2020
Merged
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,36 @@ 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/).

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this title can be misleading, because this only applies when you are not using datadog tracer.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should have a section Trace & Log Correlation and break it down by using datadog tracer and using xray tracer.

Copy link
Contributor

@tianchu tianchu Mar 27, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's move the description for DD_LOGS_INJECTION to this new section with a link, since it's getting a little bit complicated.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using the datadog tracer, trace-log correlation should just work, right? Or is there something special they need to do?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Correct, it just works if I'm using datadog tracer. However, reading this section as a user, I wouldn't know that. Even if I'm using the datadog tracer, I still might think I need to follow the instructions in this section.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this is a super big deal though, definitely not a blocker for merging this PR, we can always improve the structure later.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 Updated!

## 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`.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this @ sign needed?


For example, using the [Pino](https://getpino.io/) logger:

```typescript
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 message
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.
Expand Down