Skip to content

Commit 751c50a

Browse files
committed
Update README
1 parent b14d6e0 commit 751c50a

File tree

1 file changed

+197
-1
lines changed

1 file changed

+197
-1
lines changed

README.md

Lines changed: 197 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,197 @@
1-
# datadog-lambda-layer-python
1+
# datadog-lambda-layer-python
2+
3+
Datadog Lambda Layer for Python (2.7, 3.6 and 3.7) enables custom metric submission from AWS Lambda functions, and distributed tracing between serverful and serverless environments.
4+
5+
## Installation
6+
7+
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.
8+
9+
```
10+
arn:aws:lambda:<AWS_REGION>:464622532012:layer:Datadog-Python37:<VERSION>
11+
```
12+
13+
Replace `<AWS_REGION>` with the region where your Lambda function lives, and `<VERSION>` with the desired (or the latest) version that can be found from [CHANGELOG](CHANGELOG.md).
14+
15+
The following Datadog environment variables must be defined via [AWS CLI](https://docs.aws.amazon.com/lambda/latest/dg/env_variables.html) or [Serverless Framework](https://serverless-stack.com/chapters/serverless-environment-variables.html):
16+
17+
* DATADOG_API_KEY
18+
* DATADOG_APP_KEY
19+
20+
### The Serverless Framework
21+
22+
If your Lambda function is deployed using the Serverless Framework, refer to this sample `serverless.yml`.
23+
24+
```yaml
25+
provider:
26+
name: aws
27+
runtime: python3.7
28+
tracing:
29+
lambda: true
30+
apiGateway: true
31+
32+
functions:
33+
hello:
34+
handler: handler.hello
35+
events:
36+
- http:
37+
path: hello
38+
method: get
39+
layers:
40+
- arn:aws:lambda:us-east-1:464622532012:layer:Datadog-Python37:1
41+
environment:
42+
DATADOG_API_KEY: xxx
43+
DATADOG_APP_KEY: yyy
44+
```
45+
46+
47+
## Basic Usage
48+
49+
```python
50+
import requests
51+
from datadog_lambda.wrapper import datadog_lambda_wrapper
52+
from datadog_lambda.metric import lambda_metric
53+
54+
@datadog_lambda_wrapper
55+
def lambda_handler(event, context):
56+
lambda_metric("my_metric", 10, tags=['tag:value'])
57+
requests.get("https://www.datadoghq.com")
58+
```
59+
60+
61+
## Custom Metrics
62+
63+
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/).
64+
65+
```python
66+
from datadog_lambda.metric import lambda_metric
67+
68+
@datadog_lambda_wrapper
69+
def lambda_handler(event, context):
70+
lambda_metric(
71+
"coffee_house.order_value", # metric
72+
12.45, # value
73+
tags=['product:latte', 'order:online'] # tags
74+
)
75+
```
76+
77+
### VPC
78+
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/).
79+
80+
81+
## Distributed Tracing
82+
83+
[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.
84+
85+
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.
86+
87+
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.
88+
89+
To enable this feature, you simple need to decorate your Lambda handler function with `@datadog_lambda_wrapper`.
90+
91+
```python
92+
import requests
93+
from datadog_lambda.wrapper import datadog_lambda_wrapper
94+
95+
@datadog_lambda_wrapper
96+
def lambda_handler(event, context):
97+
requests.get("https://www.datadoghq.com")
98+
```
99+
100+
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/).
101+
102+
### Patching
103+
104+
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.
105+
106+
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.
107+
108+
```python
109+
import requests
110+
from datadog_lambda.wrapper import datadog_lambda_wrapper
111+
from datadog_lambda.tracing import get_dd_trace_context
112+
113+
@datadog_lambda_wrapper
114+
def lambda_handler(event, context):
115+
headers = get_dd_trace_context()
116+
requests.get("https://www.datadoghq.com", headers=headers)
117+
```
118+
119+
### Sampling
120+
121+
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.
122+
123+
These rules can be created using the following AWS CLI command.
124+
125+
```bash
126+
aws xray create-sampling-rule --cli-input-json file://datadog-sampling-priority-1.json
127+
aws xray create-sampling-rule --cli-input-json file://datadog-sampling-priority-2.json
128+
```
129+
130+
The file content for `datadog-sampling-priority-1.json`:
131+
```json
132+
{
133+
"SamplingRule": {
134+
"RuleName": "Datadog-Sampling-Priority-1",
135+
"ResourceARN": "*",
136+
"Priority": 9998,
137+
"FixedRate": 1,
138+
"ReservoirSize": 100,
139+
"ServiceName": "*",
140+
"ServiceType": "AWS::APIGateway::Stage",
141+
"Host": "*",
142+
"HTTPMethod": "*",
143+
"URLPath": "*",
144+
"Version": 1,
145+
"Attributes": {
146+
"x-datadog-sampling-priority": "1"
147+
}
148+
}
149+
}
150+
```
151+
152+
The file content for `datadog-sampling-priority-2.json`:
153+
```json
154+
{
155+
"SamplingRule": {
156+
"RuleName": "Datadog-Sampling-Priority-2",
157+
"ResourceARN": "*",
158+
"Priority": 9999,
159+
"FixedRate": 1,
160+
"ReservoirSize": 100,
161+
"ServiceName": "*",
162+
"ServiceType": "AWS::APIGateway::Stage",
163+
"Host": "*",
164+
"HTTPMethod": "*",
165+
"URLPath": "*",
166+
"Version": 1,
167+
"Attributes": {
168+
"x-datadog-sampling-priority": "2"
169+
}
170+
}
171+
}
172+
```
173+
174+
### Non-proxy integration
175+
176+
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.
177+
178+
If your Lambda function is deployed by the Serverless Framework, such a mapping template gets created by default.
179+
180+
181+
## Opening Issues
182+
183+
If you encounter a bug with this package, we want to hear about it. Before opening a new issue, search the existing issues to avoid duplicates.
184+
185+
When opening an issue, include the Datadog Lambda Layer version, Python version, and stack trace if available. In addition, include the steps to reproduce when appropriate.
186+
187+
You can also open an issue for a feature request.
188+
189+
190+
## Contributing
191+
192+
If you find an issue with this package and have a fix, please feel free to open a pull request following the [procedures](CONTRIBUTING.md).
193+
194+
195+
## License
196+
197+
TBA

0 commit comments

Comments
 (0)