Skip to content

Commit 8d13ba4

Browse files
committed
Add guide for datadog and aws.
1 parent c7275ad commit 8d13ba4

File tree

2 files changed

+207
-0
lines changed

2 files changed

+207
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,207 @@
1+
---
2+
description: Instrument your Nitric application with Datadog for monitoring.
3+
tags:
4+
- Monitoring
5+
- AWS
6+
languages:
7+
- typescript
8+
- javascript
9+
published_at: 2023-11-26
10+
---
11+
12+
# Instrumenting your Nitric application with Datadog
13+
14+
This guide walks you through the steps to instrument your Nitric app with Datadog without changing any application code.
15+
Once you're done, you'll be able to monitor your application from Datadog's dashboard.
16+
17+
<img
18+
src="/docs/images/guides/aws-datadog-monitoring/datadog-dashboard.png"
19+
style={{ maxWidth: 800, width: '100%' }}
20+
alt="Screenshot of Datadog dashboard showing metrics"
21+
/>
22+
23+
## Prerequisites
24+
25+
- [Node.js](https://nodejs.org/en/download/)
26+
- The [Nitric CLI](/get-started/installation)
27+
- An account with [Datadog](https://www.datadoghq.com/)
28+
- An account with [AWS](https://aws.amazon.com/)
29+
30+
## Getting started
31+
32+
Begin by creating a new project using the Nitric TypeScript template and installing dependencies:
33+
34+
```bash
35+
nitric new my-api ts-starter
36+
cd my-api
37+
npm install
38+
```
39+
40+
## Add Datadog libraries to your container image
41+
42+
Nitric applications deploy as containerized applications, you'll need to include the Datadog Lambda and tracing libraries in your container image:
43+
44+
```bash
45+
npm install datadog-lambda-js dd-trace
46+
```
47+
48+
## Add the Datadog Lambda extension
49+
50+
To enable Datadog monitoring, add the Datadog Lambda Extension to your container image by modifying your `node.dockerfile`. Add the following lines to the end of your dockerfile, just before you define your entrypoint `ENTRYPOINT ["node", "lib/index.js"]`.
51+
52+
1. **Fetch and copy the Datadog Lambda Extension**
53+
Retrieve the Lambda Extension from Amazon's Elastic Container Registry (ECR) and copy its contents into the `/opt/` directory of your image. This extension runs as a sidecar process, collecting and forwarding telemetry data to Datadog.
54+
55+
```dockerfile
56+
COPY --from=public.ecr.aws/datadog/lambda-extension:latest-alpine /opt/. /opt/
57+
```
58+
59+
2. **Remove the default Datadog handler file**
60+
The default `handler.js` file from `datadog-lambda-js` might contain unnecessary boilerplate functionality.
61+
62+
```dockerfile
63+
RUN rm node_modules/datadog-lambda-js/dist/handler.js
64+
```
65+
66+
3. **Define the container's entry point**
67+
Configure the Lambda runtime to use the specialized `handler.handler` file as the entry point:
68+
69+
```dockerfile
70+
CMD ["node_modules/datadog-lambda-js/dist/handler.handler"]
71+
```
72+
73+
### Final dockerfile
74+
75+
Here's your complete `node.dockerfile`:
76+
77+
```dockerfile title:node.dockerfile
78+
# syntax=docker/dockerfile:1
79+
FROM node:22.4.1-alpine as build
80+
81+
ARG HANDLER
82+
83+
# Python and make are required by certain native package build processes in NPM packages.
84+
RUN --mount=type=cache,sharing=locked,target=/etc/apk/cache \
85+
apk --update-cache add git g++ make py3-pip
86+
87+
RUN yarn global add typescript @vercel/ncc
88+
89+
WORKDIR /usr/app
90+
91+
COPY package.json *.lock *-lock.json ./
92+
93+
RUN yarn import || echo ""
94+
95+
RUN --mount=type=cache,sharing=locked,target=/tmp/.yarn_cache \
96+
set -ex && \
97+
yarn install --production --prefer-offline --frozen-lockfile --cache-folder /tmp/.yarn_cache
98+
99+
RUN test -f tsconfig.json || echo "{\"compilerOptions\":{\"esModuleInterop\":true,\"target\":\"es2015\",\"moduleResolution\":\"node\"}}" > tsconfig.json
100+
101+
COPY . .
102+
103+
RUN --mount=type=cache,target=/tmp/ncc-cache \
104+
ncc build ${HANDLER} -o lib/ -t
105+
106+
FROM node:22.4.1-alpine as final
107+
108+
RUN apk update && \
109+
apk add --no-cache ca-certificates && \
110+
update-ca-certificates
111+
112+
WORKDIR /usr/app
113+
114+
COPY . .
115+
116+
COPY --from=build /usr/app/node_modules/ ./node_modules/
117+
COPY --from=build /usr/app/lib/ ./lib/
118+
119+
# !diff +
120+
# Add Datadog Lambda Extension
121+
# !diff +
122+
COPY --from=public.ecr.aws/datadog/lambda-extension:latest-alpine /opt/. /opt/
123+
# !diff +
124+
125+
# !diff +
126+
# Remove default Datadog handler
127+
# !diff +
128+
RUN rm node_modules/datadog-lambda-js/dist/handler.js
129+
# !diff +
130+
131+
# !diff +
132+
# Define the entry point
133+
# !diff +
134+
CMD ["node_modules/datadog-lambda-js/dist/handler.handler"]
135+
136+
ENTRYPOINT ["node", "lib/index.js"]
137+
```
138+
139+
## Configure environment variables
140+
141+
Create an `.env` file in the root directory of your project and add/update the following variables.
142+
143+
```yaml title:.env
144+
DD_API_KEY=your-datadog-api-key
145+
DD_LOG_LEVEL=info
146+
DD_SERVICE=my-api
147+
DD_ENV=aws-dev-environment
148+
DD_SITE=datadoghq.com
149+
DD_VERSION=0.0.1
150+
AWS_LAMBDA_EXEC_WRAPPER=/opt/datadog_wrapper
151+
```
152+
153+
These variables allow Datadog to group and categorize your application so that you can use filters in the dashboard.
154+
155+
<Note>
156+
Your Datadog API key should be stored securely. For testing purposes, this
157+
example uses `DD_API_KEY`. In production, consider using
158+
`DD_API_KEY_SECRET_ARN` with an AWS Secrets Manager ARN.
159+
</Note>
160+
161+
## Deploy to AWS
162+
163+
To deploy the application to AWS, create a `stack` and configure the `AWS Pulumi` provider:
164+
165+
```bash
166+
nitric stack new dev
167+
```
168+
169+
Edit the `nitric.dev.yaml` file to set your target AWS region:
170+
171+
```yaml title:nitric.dev.yaml
172+
provider: nitric/[email protected]
173+
region: us-east-1
174+
```
175+
176+
Deploy your application with:
177+
178+
```bash
179+
nitric up
180+
```
181+
182+
<Note>
183+
Cloud deployments may incur costs. While many resources are available under
184+
free-tier pricing, consider the deployment costs carefully.
185+
</Note>
186+
187+
## View Metrics in Datadog
188+
189+
You'll need to run your application a few times to gather logs/metrics.
190+
191+
```
192+
curl https://XXXXXXXXXX.execute-api.us-east-1.amazonaws.com/hello/datadog
193+
```
194+
195+
Then log in to your [Datadog account](https://app.datadoghq.com/functions) and navigate to the serverless functions dashboard. You should see logs, traces, and custom metrics collected from your application.
196+
197+
<img
198+
src="/docs/images/guides/aws-datadog-monitoring/datadog-dashboard.png"
199+
style={{ maxWidth: 800, width: '100%' }}
200+
alt="Screenshot of Datadog dashboard showing metrics"
201+
/>
202+
203+
<Note>
204+
You'll need to create an AWS integration, follow the wizard at
205+
https://app.datadoghq.com/integrations which will help you connect your AWS
206+
Account to Datadog and provision the appropairate IAM.
207+
</Note>
Loading

0 commit comments

Comments
 (0)