|
| 1 | +--- |
| 2 | +title: Instrumenting a Node.js Cloud Run Container In-Process |
| 3 | +further_reading: |
| 4 | +- link: '/tracing/trace_collection/automatic_instrumentation/dd_libraries/nodejs/#getting-started' |
| 5 | + tag: 'Documentation' |
| 6 | + text: 'Tracing Node.js Applications' |
| 7 | +--- |
| 8 | + |
| 9 | +## 1. Install the Tracer |
| 10 | + |
| 11 | +In your main application, add the `dd-trace-js` library. |
| 12 | + |
| 13 | +```shell |
| 14 | +npm install dd-trace --save |
| 15 | +``` |
| 16 | + |
| 17 | +Then, add this to your application code to initialize the tracer: |
| 18 | +```javascript |
| 19 | +const tracer = require('dd-trace').init({ |
| 20 | + logInjection: true, |
| 21 | +}); |
| 22 | +``` |
| 23 | + |
| 24 | +For more information, see [Tracing Node.js applications](https://docs.datadoghq.com/tracing/trace_collection/automatic_instrumentation/dd_libraries/nodejs/#getting-started). |
| 25 | + |
| 26 | +## 2. Install Serverless-Init |
| 27 | + |
| 28 | +Add the following instructions and arguments to your Dockerfile. |
| 29 | + |
| 30 | +```dockerfile |
| 31 | +COPY --from=datadog/serverless-init:1 /datadog-init /app/datadog-init |
| 32 | +ENV NODE_OPTIONS="--require dd-trace/init" |
| 33 | +ENV DD_SERVICE=datadog-demo-run-nodejs |
| 34 | +ENV DD_ENV=datadog-demo |
| 35 | +ENV DD_VERSION=1 |
| 36 | +ENTRYPOINT ["/app/datadog-init"] |
| 37 | +CMD ["/nodejs/bin/node", "/path/to/your/app.js"] |
| 38 | +``` |
| 39 | + |
| 40 | +#### Explanation |
| 41 | + |
| 42 | +1. Copy the Datadog `serverless-init` into your Docker image. |
| 43 | + |
| 44 | + ```dockerfile |
| 45 | + COPY --from=datadog/serverless-init:1 /datadog-init /app/datadog-init |
| 46 | + ``` |
| 47 | + |
| 48 | +2. Specify that the `dd-trace/init` module is required when the Node.js process starts. |
| 49 | + |
| 50 | + ```dockerfile |
| 51 | + ENV NODE_OPTIONS="--require dd-trace/init" |
| 52 | + ``` |
| 53 | + |
| 54 | +3. (Optional) Add Datadog tags. |
| 55 | + |
| 56 | + ```dockerfile |
| 57 | + ENV DD_SERVICE=datadog-demo-run-nodejs |
| 58 | + ENV DD_ENV=datadog-demo |
| 59 | + ENV DD_VERSION=1 |
| 60 | + ``` |
| 61 | + |
| 62 | +4. Change the entrypoint to wrap your application in the Datadog `serverless-init` process. |
| 63 | + **Note**: If you already have an entrypoint defined inside your Dockerfile, see the [alternative configuration](#alt-node). |
| 64 | + |
| 65 | + ```dockerfile |
| 66 | + ENTRYPOINT ["/app/datadog-init"] |
| 67 | + ``` |
| 68 | + |
| 69 | +5. Execute your binary application wrapped in the entrypoint. Adapt this line to your needs. |
| 70 | + ```dockerfile |
| 71 | + CMD ["node", "/path/to/your/app.js"] |
| 72 | + ``` |
| 73 | + |
| 74 | +#### Alternative configuration {#alt-node} |
| 75 | +If you already have an entrypoint defined inside your Dockerfile, you can instead modify the CMD argument. |
| 76 | + |
| 77 | +```dockerfile |
| 78 | +COPY --from=datadog/serverless-init:1 /datadog-init /app/datadog-init |
| 79 | +RUN npm install --prefix /dd_tracer/node dd-trace --save |
| 80 | +ENV DD_SERVICE=datadog-demo-run-nodejs |
| 81 | +ENV DD_ENV=datadog-demo |
| 82 | +ENV DD_VERSION=1 |
| 83 | +CMD ["/app/datadog-init", "/nodejs/bin/node", "/path/to/your/app.js"] |
| 84 | +``` |
| 85 | + |
| 86 | +## 3. Setup Logs |
| 87 | + |
| 88 | +To enable logging, set the environment variable `DD_LOGS_ENABLED=true`. This allows serverless-init to read logs from stdout and stderr. |
| 89 | + |
| 90 | +If you want multiline logs to be preserved in a single log message, we recommend writing your logs in JSON format. For example: |
| 91 | +```javascript |
| 92 | +const tracer = require('dd-trace').init({ |
| 93 | + logInjection: true, |
| 94 | +}); |
| 95 | +const { createLogger, format, transports } = require('winston'); |
| 96 | + |
| 97 | +const logger = createLogger({ |
| 98 | + level: 'info', |
| 99 | + exitOnError: false, |
| 100 | + format: format.json(), |
| 101 | + transports: [ |
| 102 | + new transports.File({ filename: `/shared-volume/logs/app.log` }), |
| 103 | + new transports.Console() |
| 104 | + ], |
| 105 | +}); |
| 106 | + |
| 107 | +logger.info(`Hello world!`); |
| 108 | +``` |
0 commit comments