You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Datadog Lambda Layer for Python (2.7, 3.6, 3.7 and 3.8) enables custom metric submission from AWS Lambda functions, and distributed tracing between serverful and serverless environments.
9
+
Datadog Lambda Library for Python (2.7, 3.6, 3.7 and 3.8) enables enhanced Lambda metrics, distributed tracing, and custom metric submission from AWS Lambda functions.
10
10
11
11
## IMPORTANT NOTE
12
12
13
13
AWS Lambda is expected to recieve a [breaking change](https://aws.amazon.com/blogs/compute/upcoming-changes-to-the-python-sdk-in-aws-lambda/) on **January 30, 2021**. If you are using Datadog Python Lambda layer version 7 or below, please upgrade to version 11.
14
14
15
15
## Installation
16
16
17
-
Datadog Lambda Layer can be added to a Lambda function via AWS Lambda console, [AWS CLI](https://docs.aws.amazon.com/lambda/latest/dg/configuration-layers.html#configuration-layers-using) or [Serverless Framework](https://serverless.com/framework/docs/providers/aws/guide/layers/#using-your-layers) using the following ARN.
Replace `<AWS_REGION>` with the AWS region where your Lambda function is published to. Replace `<PYTHON_RUNTIME>` with one of the following that matches your Lambda's Python runtime:
24
-
25
-
-`Datadog-Python27`
26
-
-`Datadog-Python36`
27
-
-`Datadog-Python37`
28
-
-`Datadog-Python38`
29
-
30
-
Replace `<VERSION>` with the latest layer version that can be found from [releases](https://github.com/DataDog/datadog-lambda-layer-python/releases). For example,
When developing your Lambda function locally where AWS Layer doesn't work, the Datadog Lambda layer can be installed from [PyPI](https://pypi.org/project/datadog-lambda/) by `pip install datadog-lambda` or adding `datadog-lambda` to your project's `requirements.txt`.
39
-
40
-
The minor version of the `datadog-lambda` package always match the layer version. E.g., datadog-lambda v0.5.0 matches the content in layer version 5.
41
-
42
-
### The Serverless Framework
43
-
44
-
[The Datadog Serverless Framework Plugin](https://github.com/DataDog/serverless-plugin-datadog) makes it easy to manage the Datadog instrumentation for all of your Lambda functions in one place.
45
-
46
-
Instead of the plugin, you can also use the sample `serverless.yml` below as a reference for manually including the Lambda Layer, enable AWS X-Ray tracing, and set up environment variables.
Alternatively, consider using [serverless-plugin-datadog](https://github.com/DataDog/serverless-plugin-datadog). The plugin can take care of adding lambda layers to your functions, and wrapping your handlers.
17
+
Check out our [installation instructions for Python](https://docs.datadoghq.com/serverless/installation/python/).
70
18
71
19
## Environment Variables
72
20
73
21
### DD_FLUSH_TO_LOG
74
22
75
-
Set to `true` (recommended) to send custom metrics asynchronously (with no added latency to your Lambda function executions) through CloudWatch Logs with the help of [Datadog Forwarder](https://github.com/DataDog/datadog-serverless-functions/tree/master/aws/logs_monitoring).
76
-
77
-
If set to `false`, you also need to set `DD_API_KEY` and `DD_SITE`.
23
+
Set to `true` (recommended) to send custom metrics asynchronously (with no added latency to your Lambda function executions) through CloudWatch Logs with the help of [Datadog Forwarder](https://github.com/DataDog/datadog-serverless-functions/tree/master/aws/logs_monitoring). Defaultss to `false`. If set to `false`, you also need to set `DD_API_KEY` and `DD_SITE`.
78
24
79
25
### DD_API_KEY
80
26
81
-
If `DD_FLUSH_TO_LOG` is set to false (not recommended), the Datadog API Key must be defined as one of the following environment variables:
27
+
If `DD_FLUSH_TO_LOG` is set to `false` (not recommended), the Datadog API Key must be defined by setting one of the following environment variables:
82
28
83
29
- DD_API_KEY - the Datadog API Key in plain-text, NOT recommended
84
30
- DD_KMS_API_KEY - the KMS-encrypted API Key, requires the `kms:Decrypt` permission
@@ -94,231 +40,43 @@ api._api_key = "MY_API_KEY"
94
40
95
41
### DD_SITE
96
42
97
-
If `DD_FLUSH_TO_LOG` is set to false (not recommended), and your data need to be sent to the Datadog EU site, you must set `DD_SITE` to `datadoghq.eu`.
43
+
If `DD_FLUSH_TO_LOG` is set to `false` (not recommended), and your data need to be sent to the Datadog EU site, you must set `DD_SITE` to `datadoghq.eu`. Defaults to `datadoghq.com`.
98
44
99
45
### DD_LOGS_INJECTION
100
46
101
-
Inject Datadog trace id into logs for [correlation](https://docs.datadoghq.com/tracing/connect_logs_and_traces/?tab=python). Defaults to true.
47
+
Inject Datadog trace id into logs for [correlation](https://docs.datadoghq.com/tracing/connect_logs_and_traces/?tab=python). Defaults to `true`.
102
48
103
49
### DD_LOG_LEVEL
104
50
105
-
Set to `debug` enable debug los from the Datadog Lambda Layer.
51
+
Set to `debug` enable debug los from the Datadog Lambda Layer. Defaults to `info`.
106
52
107
53
### DD_ENHANCED_METRICS
108
54
109
-
Generate enhanced Datadog Lambda integration metrics, such as, `aws.lambda.enhanced.invocations` and `aws.lambda.enhanced.errors`. Defaults to true.
55
+
Generate enhanced Datadog Lambda integration metrics, such as, `aws.lambda.enhanced.invocations` and `aws.lambda.enhanced.errors`. Defaults to `true`.
110
56
111
57
### DD_LAMBDA_HANDLER
112
58
113
59
For use with the [redirected handler](#Redirected-Handler) method. Location of your original lambda handler.
114
60
115
61
### DD_TRACE_ENABLED
116
62
117
-
When used with the [redirected handler](#Redirected-Handler) method, will auto initialize the tracer when set to true.
118
-
119
-
## Basic Usage
120
-
121
-
Datadog needs to be able to read headers from the incoming Lambda event. To do this, you must wrap your handler function with our library. We provide some easy ways of wrapping your handlers.
122
-
123
-
### Redirected Handler
124
-
125
-
We provide a swap in replacement handler, with zero required code changes.
63
+
When used with the [redirected handler](#Redirected-Handler) method, will auto initialize the tracer when set to true. Defaults to `false`.
126
64
127
-
1. Set the environment variable `DD_LAMBDA_HANDLER` to your regular handler location, eg. `myfunc.handler`.
128
-
2. Set your handler to `datadog_lambda.handler.handler`.
65
+
### DD_MERGE_XRAY_TRACES
129
66
130
-
### Manual Wrap
131
-
132
-
You might find it more convenient to wrap your handlers manually.
133
-
134
-
```python
135
-
import requests
136
-
from datadog_lambda.wrapper import datadog_lambda_wrapper
Set to `true` to merge the X-Ray trace and the Datadog trace, when using both the X-Ray and Datadog tracing. Defaults to `false`.
144
68
145
69
## Custom Metrics
146
70
147
-
Custom metrics can be submitted using `lambda_metric` and the Lambda handler function needs to be decorated with `@datadog_lambda_wrapper`. The metrics are submitted as [distribution metrics](https://docs.datadoghq.com/graphing/metrics/distributions/).
148
-
149
-
**IMPORTANT NOTE:** If you have already been submitting the same custom metric as non-distribution metric (e.g., gauge, count, or histogram) without using the Datadog Lambda Layer, you MUST pick a new metric name to use for `lambda_metric`. Otherwise that existing metric will be converted to a distribution metric and the historical data prior to the conversion will be no longer queryable.
150
-
151
-
```python
152
-
from datadog_lambda.wrapper import datadog_lambda_wrapper
153
-
from datadog_lambda.metric import lambda_metric
154
-
155
-
@datadog_lambda_wrapper
156
-
def lambda_handler(event, context):
157
-
lambda_metric(
158
-
"coffee_house.order_value", # metric
159
-
12.45, # value
160
-
tags=['product:latte', 'order:online'] # tags
161
-
)
162
-
```
163
-
164
-
### VPC
165
-
166
-
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/).
167
-
168
-
## Distributed Tracing
169
-
170
-
[Distributed tracing](https://docs.datadoghq.com/tracing/guide/distributed_tracing/?tab=python) 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.
171
-
172
-
Distributed tracing headers are language agnostic, e.g., a trace can be propagated between a Java service running on a host to a Lambda function written in Python.
71
+
Check out the instructions for [submitting custom metrics from AWS Lambda functions](https://docs.datadoghq.com/integrations/amazon_lambda/?tab=python#custom-metrics).
173
72
174
-
Because the trace context is propagated through HTTP request headers, the Lambda function needs to be triggered by AWS API Gateway or AWS Application Load Balancer.
73
+
## Tracing
175
74
176
-
To enable this feature, you simple need to decorate your Lambda handler function with `@datadog_lambda_wrapper`.
<
10000
/code>
75
+
Check out the instructions for [collecting traces from AWS Lambda functions](https://docs.datadoghq.com/integrations/amazon_lambda/?tab=python#trace-collection), and the [official documentation for Datadog trace client](http://pypi.datadoghq.com/trace/docs/index.html).
177
76
178
-
```python
179
-
import requests
180
-
from datadog_lambda.wrapper import datadog_lambda_wrapper
181
-
182
-
@datadog_lambda_wrapper
183
-
def lambda_handler(event, context):
184
-
requests.get("https://www.datadoghq.com")
185
-
```
186
-
187
-
Note, the Datadog Lambda Layer is only needed to enable _distributed_ tracing between Lambda and non-Lambda services. For standalone Lambda functions, traces can be found in Datadog APM after configuring [the X-Ray integration](https://docs.datadoghq.com/integrations/amazon_xray/).
188
-
189
-
### Patching
190
-
191
-
By default, widely used HTTP client libraries, such as `requests`, `urllib2` and `urllib.request` are patched automatically to inject Datadog trace context into outgoing requests.
192
-
193
-
You can also manually retrieve the Datadog trace context (i.e., http headers in a Python dict) and inject it to request headers when needed.
194
-
195
-
```python
196
-
import requests
197
-
from datadog_lambda.wrapper import datadog_lambda_wrapper
198
-
from datadog_lambda.tracing import get_dd_trace_context
The traces for your Lambda function are converted by Datadog from AWS X-Ray traces. X-Ray needs to sample the traces that the Datadog tracing agent decides to sample, in order to collect as many complete traces as possible. You can create X-Ray sampling rules to ensure requests with header `x-datadog-sampling-priority:1` or `x-datadog-sampling-priority:2` via API Gateway always get sampled by X-Ray.
209
-
210
-
These rules can be created using the following AWS CLI command.
The file content for `datadog-sampling-priority-1.json`:
218
-
219
-
```json
220
-
{
221
-
"SamplingRule": {
222
-
"RuleName": "Datadog-Sampling-Priority-1",
223
-
"ResourceARN": "*",
224
-
"Priority": 9998,
225
-
"FixedRate": 1,
226
-
"ReservoirSize": 100,
227
-
"ServiceName": "*",
228
-
"ServiceType": "AWS::APIGateway::Stage",
229
-
"Host": "*",
230
-
"HTTPMethod": "*",
231
-
"URLPath": "*",
232
-
"Version": 1,
233
-
"Attributes": {
234
-
"x-datadog-sampling-priority": "1"
235
-
}
236
-
}
237
-
}
238
-
```
239
-
240
-
The file content for `datadog-sampling-priority-2.json`:
241
-
242
-
```json
243
-
{
244
-
"SamplingRule": {
245
-
"RuleName": "Datadog-Sampling-Priority-2",
246
-
"ResourceARN": "*",
247
-
"Priority": 9999,
248
-
"FixedRate": 1,
249
-
"ReservoirSize": 100,
250
-
"ServiceName": "*",
251
-
"ServiceType": "AWS::APIGateway::Stage",
252
-
"Host": "*",
253
-
"HTTPMethod": "*",
254
-
"URLPath": "*",
255
-
"Version": 1,
256
-
"Attributes": {
257
-
"x-datadog-sampling-priority": "2"
258
-
}
259
-
}
260
-
}
261
-
```
262
-
263
-
### Non-proxy integration
264
-
265
-
If your Lambda function is triggered by API Gateway via [the non-proxy integration](https://docs.aws.amazon.com/apigateway/latest/developerguide/getting-started-lambda-non-proxy-integration.html), then you have to [set up a mapping template](https://aws.amazon.com/premiumsupport/knowledge-center/custom-headers-api-gateway-lambda/), which passes the Datadog trace context from the incoming HTTP request headers to the Lambda function via the `event` object.
266
-
267
-
If your Lambda function is deployed by the Serverless Framework, such a mapping template gets created by default.
268
-
269
-
## Log and Trace Correlations
270
-
271
-
By default, the Datadog trace id gets automatically injected into the logs for correlation, if using the standard python `logging` library.
272
-
273
-
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).
274
-
275
-
Set the environment variable `DD_LOGS_INJECTION` to `false` to disable this feature.
276
-
277
-
```python
278
-
from datadog_lambda.wrapper import datadog_lambda_wrapper
279
-
from ddtrace.helpers import get_correlation_ids
280
-
281
-
@datadog_lambda_wrapper
282
-
def lambda_handler(event, context):
283
-
trace_id, span_id = get_correlation_ids()
284
-
logger.info({
285
-
"message": "hello world",
286
-
"dd": {
287
-
"trace_id": trace_id,
288
-
"span_id": span_id
289
-
}
290
-
})
291
-
```
292
-
293
-
## Datadog Tracer (**Beta**)
294
-
295
-
You can now trace Lambda functions using Datadog APM's tracing libraries ([dd-trace-py](https://github.com/DataDog/dd-trace-py)).
296
-
297
-
1. If you are using the Lambda layer, upgrade it to at least version 15.
298
-
1. If you are using the pip package `datadog-lambda-python`, upgrade it to at least version `v2.15.0`.
299
-
1. Install (or update to) the latest version of [Datadog forwarder Lambda function](https://docs.datadoghq.com/integrations/amazon_web_services/?tab=allpermissions#set-up-the-datadog-lambda-function). Ensure the trace forwarding layer is attached to the forwarder, e.g., ARN for Python 2.7 `arn:aws:lambda:<AWS_REGION>:464622532012:layer:Datadog-Trace-Forwarder-Python27:4`.
300
-
1. Set the environment variable `DD_TRACE_ENABLED` to true on your function.
301
-
1. Instrument your function using `dd-trace`.
302
-
303
-
```py
304
-
from datadog_lambda.metric import lambda_metric
305
-
from datadog_lambda.wrapper import datadog_lambda_wrapper
306
-
307
-
from ddtrace import tracer
308
-
309
-
@datadog_lambda_wrapper
310
-
def hello(event, context):
311
-
return {
312
-
"statusCode": 200,
313
-
"body": get_message()
314
-
}
315
-
316
-
@tracer.wrap()
317
-
def get_message():
318
-
return "hello world"
319
-
```
77
+
## Enhanced Metrics
320
78
321
-
You can also use `dd-trace` and the X-Ray tracer together and merge the traces into one, using the environment variable `DD_MERGE_XRAY_TRACES` to true on your function.
79
+
Check out the official documentation on [Datadog Lambda enhanced metrics](https://docs.datadoghq.com/integrations/amazon_lambda/?tab=python#real-time-enhanced-lambda-metrics).
0 commit comments