Skip to content

Commit 1d3e72d

Browse files
committed
add otel guide
1 parent 99250df commit 1d3e72d

File tree

1 file changed

+200
-0
lines changed

1 file changed

+200
-0
lines changed

content/guides/opentelemetry.md

Lines changed: 200 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,200 @@
1+
---
2+
title: Instrumenting a JavaScript App with OpenTelemetry
3+
description: &desc Learn how to instrument a JavaScript application using OpenTelemetry in a Dockerized environment.
4+
keywords: OpenTelemetry, observability, tracing
5+
linktitle: Instrumenting JS Apps with OpenTelemetry
6+
summary: *desc
7+
tags: [app-dev, observability]
8+
languages: [JavaScript]
9+
params:
10+
time: 10 minutes
11+
---
12+
13+
OpenTelemetry (OTEL) is an open-source observability framework that provides a set of APIs, SDKs, and tools for collecting telemetry data, such as metrics, logs, and traces, from applications. With OpenTelemetry, developers can obtain valuable insights into how their services perform in production or during local development.
14+
15+
A key component of OpenTelemetry is the OpenTelemetry Protocol (OTLP) a general-purpose, vendor-agnostic protocol designed to transmit telemetry data efficiently and reliably. OTLP supports multiple data types (traces, metrics, logs) over HTTP or gRPC, making it the default and recommended protocol for communication between instrumented applications, the OpenTelemetry Collector, and backends such as Jaeger or Prometheus.
16+
17+
This guide walks you through how to instrument a simple Node.js application with OpenTelemetry and run both the app and a collector using Docker. This setup is ideal for local development and testing observability before integrating with external observability platforms like Prometheus, Jaeger, or Grafana.
18+
19+
In this guide, you'll learn how to:
20+
21+
- How to set up OpenTelemetry in a Node.js app.
22+
- How to run an OpenTelemetry Collector in Docker.
23+
- How to visualize traces with Jaeger.
24+
- How to use Docker Compose to manage the full observability stack.
25+
26+
## Using OpenTelemetry with Docker
27+
28+
the official [docker image for opentelemetry](https://hub.docker.com/r/otel/opentelemetry-collector-contrib) provides a convenient way to deploy and manage dex instances. Opentelemetry is available for various cpu architectures, including amd64, armv7, and arm64, ensuring compatibility with different devices and platforms. Same for the [Jaeger docekr image](https://hub.docker.com/r/jaegertracing/jaeger).
29+
30+
## Prerequisites
31+
32+
[Docker Compose](/compose/): Recommended for managing multi-container Docker applications.
33+
34+
Basic knowledge of Node.js and Docker.
35+
36+
## Project Structure
37+
38+
Create the project directory:
39+
```bash
40+
mkdir otel-js-app
41+
cd otel-js-app
42+
```
43+
44+
```bash
45+
otel-js-app/
46+
├── docker-compose.yaml
47+
├── collector-config.yaml
48+
├── app/
49+
│ ├── package.json
50+
│ ├── app.js
51+
│ └── tracer.js
52+
```
53+
54+
## Create a Simple Node.js App
55+
56+
Initialize a basic Node.js app:
57+
58+
```bash
59+
mkdir app && cd app
60+
npm init -y
61+
npm install express @opentelemetry/api @opentelemetry/sdk-node \
62+
@opentelemetry/auto-instrumentations-node \
63+
@opentelemetry/exporter-trace-otlp-http
64+
```
65+
66+
Now, add the application logic:
67+
68+
```js
69+
// app/app.js
70+
const express = require('express');
71+
require('./tracer'); // Initialize OpenTelemetry
72+
73+
const app = express();
74+
75+
app.get('/', (req, res) => {
76+
res.send('Hello from OpenTelemetry demo app!');
77+
});
78+
79+
const PORT = 3000;
80+
app.listen(PORT, () => {
81+
console.log(`App listening at http://localhost:${PORT}`);
82+
});
83+
```
84+
85+
## Configure OpenTelemetry Tracing
86+
87+
Create the tracer configuration file:
88+
89+
```js
90+
// app/tracer.js
91+
const { NodeSDK } = require('@opentelemetry/sdk-node');
92+
const { getNodeAutoInstrumentations } = require('@opentelemetry/auto-instrumentations-node');
93+
const { OTLPTraceExporter } = require('@opentelemetry/exporter-trace-otlp-http');
94+
95+
const sdk = new NodeSDK({
96+
traceExporter: new OTLPTraceExporter({
97+
url: 'http://collector:4318/v1/traces',
98+
}),
99+
instrumentations: [getNodeAutoInstrumentations()],
100+
});
101+
102+
sdk.start();
103+
```
104+
105+
## Configure the OpenTelemetry Collector
106+
107+
Create a collector-config.yaml file at the root:
108+
109+
```yaml
110+
# collector-config.yaml
111+
receivers:
112+
otlp:
113+
protocols:
114+
http:
115+
116+
exporters:
117+
logging:
118+
loglevel: debug
119+
jaeger:
120+
endpoint: jaeger:14250
121+
tls:
122+
insecure: true
123+
124+
service:
125+
pipelines:
126+
traces:
127+
receivers: [otlp]
128+
exporters: [logging, jaeger]
129+
```
130+
131+
## Add Docker Compose Configuration
132+
133+
Create the `docker-compose.yaml` file:
134+
135+
```yaml
136+
version: '3.9'
137+
138+
services:
139+
app:
140+
build: ./app
141+
ports:
142+
- "3000:3000"
143+
environment:
144+
- NODE_ENV=development
145+
depends_on:
146+
- collector
147+
148+
collector:
149+
image: otel/opentelemetry-collector:latest
150+
volumes:
151+
- ./collector-config.yaml:/etc/otelcol/config.yaml
152+
command: ["--config=/etc/otelcol/config.yaml"]
153+
ports:
154+
- "4318:4318" # OTLP
155+
156+
jaeger:
157+
image: jaegertracing/all-in-one:latest
158+
ports:
159+
- "16686:16686" # UI
160+
- "14250:14250" # Collector gRPC
161+
```
162+
163+
Now, add the `Dockerfile` inside the `app/` folder:
164+
165+
```dockerfile
166+
# app/Dockerfile
167+
FROM node:18
168+
169+
WORKDIR /usr/src/app
170+
COPY . .
171+
RUN npm install
172+
173+
CMD ["node", "app.js"]
174+
```
175+
176+
## Start the Stack
177+
178+
Start all services with Docker Compose:
179+
180+
```bash
181+
docker compose up --build
182+
```
183+
184+
Once the services are running:
185+
186+
Visit your app at [http://localhost:3000](http://localhost:3000)
187+
188+
View traces at [http://localhost:16686](http://localhost:16686) in the Jaeger UI
189+
190+
## Verify Traces in Jaeger
191+
192+
After visiting your app's root endpoint, open Jaeger’s UI, search for the service (default is usually unknown_service unless explicitly named), and check the traces.
193+
194+
You should see spans for the HTTP request, middleware, and auto-instrumented libraries.
195+
196+
## Conclusion
197+
198+
You now have a fully functional OpenTelemetry setup using Docker Compose. You've instrumented a basic JavaScript app to export traces and visualized them using Jaeger. This architecture is extendable for more complex applications and observability pipelines using Prometheus, Grafana, or cloud-native exporters.
199+
200+
For advanced topics such as custom span creation, metrics, and logs, consult the OpenTelemetry JavaScript docs.

0 commit comments

Comments
 (0)