-
Notifications
You must be signed in to change notification settings - Fork 36
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
Changes from 3 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
e313e66
Add documentation and example for adding request ID to logs.
4794944
Fix a couple typos in the example
d132d16
Remove `@`
agocs ec0296c
update docs per pr comments
54b58a2
Fix DD_TRACE_ID description
046eb73
Automatic trace-log correlation
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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/). | ||
|
||
## 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`. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this |
||
|
||
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. | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
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.There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍 Updated!