> ## Documentation Index
> Fetch the complete documentation index at: https://docs.trace0hq.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Node.js — Containers & VMs

> Instrument a Node.js application with OpenTelemetry and send traces, metrics, and logs to Trace0.

<Note>
  This guide covers Node.js applications running on **ECS**, **EC2**, **Kubernetes**, or any container/VM environment. For AWS Lambda, see the [Node.js Lambda guide](/setup/javascript/lambda).
</Note>

## 1. Install the OpenTelemetry SDK

```bash theme={null}
npm install --save @opentelemetry/auto-instrumentations-node
```

## 2. Enable Auto Instrumentation

Enable auto instrumentation by requiring the `auto-instrumentations-node` module using the `--require` flag:

```bash theme={null}
node --require '@opentelemetry/auto-instrumentations-node/register' app.js
```

## 3. Set Environment Variables

Add these environment variables to your service. Replace `YOUR_TRACE0_ENV_API_KEY` with the API key from your Trace0 environment.

| Variable                      | Value                               |
| ----------------------------- | ----------------------------------- |
| `OTEL_EXPORTER_OTLP_ENDPOINT` | `https://app.trace0hq.com/api`      |
| `OTEL_EXPORTER_OTLP_HEADERS`  | `X-API-KEY=YOUR_TRACE0_ENV_API_KEY` |
| `OTEL_EXPORTER_OTLP_PROTOCOL` | `http/protobuf`                     |

## 4. Add Trace0 logger

To correlate logs with traces in Trace0, the [@trace0/otel-logger](https://www.npmjs.com/package/@trace0/otel-logger) library must be installed in your app. This is necessary because the OpenTelemetry JavaScript library does not yet automatically bridge console output into the OTel Logs pipeline.

First, install the library:

```bash theme={null}
npm install --save @trace0/otel-logger
```

Then add as the first import in your app entry point:

```typescript theme={null}
import '@trace0/otel-logger'; // must be first
```

Logs will be flushed to Trace0 automatically every 5 seconds in the background.

Finally, add these signal handlers to guarantee delivery of the final log batch on shutdown.

```typescript theme={null}
import '@trace0/otel-logger'; // must be first
import { flush } from '@trace0/otel-logger';

process.once('SIGTERM', async () => { await flush(); process.exit(0); });
process.once('SIGINT',  async () => { await flush(); process.exit(0); });
```

The library will automatically capture all logging output, inject OTel trace context (traceId, spanId), and export the logs to the Trace0 ingest endpoint.

## 5. Kubernetes Setup

If your application runs on Kubernetes, including `Amazon EKS`, `Google Kubernetes Engine` or `Azure Kubernetes Service`, add the following environment variables to your container specification.

```yaml theme={null}
env:
  - name: K8S_POD_UID
    valueFrom:
      fieldRef:
        fieldPath: metadata.uid

  - name: K8S_POD_NAME
    valueFrom:
      fieldRef:
        fieldPath: metadata.name

  - name: K8S_NAMESPACE_NAME
    valueFrom:
      fieldRef:
        fieldPath: metadata.namespace

  - name: K8S_CONTAINER_NAME
    value: my-container-name

  - name: OTEL_RESOURCE_ATTRIBUTES
    value: k8s.pod.uid=$(K8S_POD_UID),k8s.pod.name=$(K8S_POD_NAME),k8s.namespace.name=$(K8S_NAMESPACE_NAME),k8s.container.name=$(K8S_CONTAINER_NAME)
```

## 6. Deploy Service

Deploy your service and watch traces, metrics, and logs appear in the [Trace0 dashboard](https://app.trace0hq.com) within seconds.

## Example Service

See our [Node.js Express EC2 example app](https://github.com/Trace0-HQ/trace0-examples/tree/main/node-js-express-ec2) for a working service you can deploy to your AWS account.
