Skip to content

Enable log trace context injection by default #44

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 8 commits into from
Feb 10, 2020
Merged
Show file tree
Hide file tree
Changes from all 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
7 changes: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ If your Lambda function powers a performance-critical task (e.g., a consumer-fac

- DD_FLUSH_TO_LOG

To connect logs and traces, set the environment variable below to `True`. The default format of the AWS provided `LambdaLoggerHandler` will be overridden to inject `dd.trace_id` and `dd.span_id`. The default Datadog lambda log integration pipeline will automatically parse them and map the `dd.trace_id` into the reserved [trace_id attribute](https://docs.datadoghq.com/logs/processing/#trace-id-attribute).
Inject Datadog trace id into logs for [correlation](https://docs.datadoghq.com/tracing/connect_logs_and_traces/?tab=python). Defaults to true.

- DD_LOGS_INJECTION

Expand Down Expand Up @@ -235,10 +235,11 @@ If your Lambda function is triggered by API Gateway via [the non-proxy integrati
If your Lambda function is deployed by the Serverless Framework, such a mapping template gets created by default.

## Log and Trace Correlations
By default, the Datadog trace id gets automatically injected into the logs for correlation, if using the standard python `logging` library.

To connect logs and traces, set the environment variable `DD_LOGS_INJECTION` to `True`. The log format of the AWS provided `LambdaLoggerHandler` will be overridden to inject `dd.trace_id` and `dd.span_id`. The default Datadog lambda log integration pipeline will automatically parse them and map the `dd.trace_id` into the reserved attribute [trace_id](https://docs.datadoghq.com/logs/processing/#trace-id-attribute).
If you use a custom logger handler to log in json, you can inject the ids using the helper function `get_correlation_ids` [manually](https://docs.datadoghq.com/tracing/connect_logs_and_traces/?tab=python#manual-trace-id-injection).

If you use a custom logger handler to log in json, you can manually inject the ids using the helper function `get_correlation_ids`.
Set the environment variable `DD_LOGS_INJECTION` to `false` to disable this feature.

```python
from datadog_lambda.wrapper import datadog_lambda_wrapper
Expand Down
2 changes: 1 addition & 1 deletion datadog_lambda/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# The minor version corresponds to the Lambda layer version.
# E.g.,, version 0.5.0 gets packaged into layer version 5.
__version__ = "1.12.0"
__version__ = "2.13.0"


import os
Expand Down
4 changes: 3 additions & 1 deletion datadog_lambda/wrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,9 @@ class _LambdaDecorator(object):
def __init__(self, func):
self.func = func
self.flush_to_log = os.environ.get("DD_FLUSH_TO_LOG", "").lower() == "true"
self.logs_injection = os.environ.get("DD_LOGS_INJECTION", "").lower() == "true"
self.logs_injection = (
os.environ.get("DD_LOGS_INJECTION", "true").lower() == "true"
)

# Inject trace correlation ids to logs
if self.logs_injection:
Expand Down
2 changes: 1 addition & 1 deletion tests/test_wrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ def lambda_handler(event, context):
self.mock_wrapper_lambda_stats.flush.assert_called()
self.mock_extract_dd_trace_context.assert_called_with(lambda_event)
self.mock_set_correlation_ids.assert_called()
self.mock_inject_correlation_ids.assert_not_called()
self.mock_inject_correlation_ids.assert_called()
self.mock_patch_all.assert_called()

def test_datadog_lambda_wrapper_flush_to_log(self):
Expand Down