Skip to content

Commit 68ac404

Browse files
committed
This is v4.0.0
1 parent 0c6ad88 commit 68ac404

File tree

5 files changed

+47
-15
lines changed

5 files changed

+47
-15
lines changed

INSTALLING_OTEL.md

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,9 @@ You will find files in different versions in this repository
1010
- `v2a` is using on HTTP protocol for sending traces
1111
- `v2b` is using on GRPC protocol for sending traces
1212
- `v3` are files with Otel Instrumentation sending output to otel-collector (using grpc), and otel-Collector sending them to different backends (Jaeger and Lightstep)
13-
- `v4` are files when we add custom attributes and log events to the auto-instrumentation
14-
- `v5` are files when we add custom spans
15-
- `v6` are files when we add metrics
16-
- `v7` are files when we add custom metrics to the auto-instrumentation
13+
- `v4` are files when we add custom attributes, log events, and new spans to the auto-instrumentation
14+
- `v5` are files when we add metrics
15+
- `v6` are files when we add custom metrics to the auto-instrumentation
1716

1817
If you don't find files in a specific version, it may just be because this file is not impacted by the new features we are adding
1918

@@ -237,6 +236,8 @@ environment:
237236

238237
## v4 - Add custom attributes and log events
239238

239+
- Edit `docker-compose.yml` file, for each line `- OTEL_RESOURCE_ATTRIBUTES=service.name=<yourServiceName>`, add a new attribute `service.version=4.0.0` with a comma separator, so lines become something like `- OTEL_RESOURCE_ATTRIBUTES=service.name=<yourServiceName>,service.version=4.0.0`
240+
240241
- In `/src` folder of the web component, update file `index.js` file with code below:
241242
- Add the OpenTelemetry library by putting this at top of your code `const api = require('@opentelemetry/api');`
242243

@@ -249,23 +250,33 @@ activeSpan.setAttribute('nbLoop', nbLoop);
249250
activeSpan.setAttribute('weather', weather);
250251
```
251252

252-
- in the `main()` function, in the `app.get("/api/data", (req, res) => {` part, add code to create custom log events
253+
- In the `main()` function, in the `app.get("/api/data", (req, res) => {` part, add code to create custom log events
253254
```
254255
// access the current span from active context
255256
let activeSpan = api.trace.getSpan(api.context.active());
256257
// log an event and include some structured data.
257258
activeSpan.addEvent(`Running on http://${HOST}:${PORT}`);
258-
activeSpan.addEvent(`Calling the /api/data service`);
259259
```
260260

261-
262-
## v5 - Create custom spans
261+
- Replace the `generateWork` function with code below
262+
```
263+
async function generateWork(nb) {
264+
for (let i = 0; i < Number(nb); i++) {
265+
let span = tracer.startSpan(`Looping ${i}`);
266+
// log an event and include some structured data.
267+
span.addEvent(`*** DOING SOMETHING ${i}`);
268+
// wait for 50ms to simulate some work
269+
await sleep(50);
270+
span.end();
271+
}
272+
}
273+
```
263274

264275

265-
## v6 - Add resources metrics
276+
## v5 - Add resources metrics
266277

267278

268-
## v7 - Add custom metrics to your traces
279+
## v6 - Add custom metrics to your traces
269280

270281

271282

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@ Need to build a microservices application? Learn how to do this with [Bootstrapp
2121
- `v1` are files with Otel Instrumentation sending output (traces or metrics) to console logs
2222
- `v2` are files with Otel Instrumentation sending output to otel-collector
2323
- `v3` are files with Otel Instrumentation sending output to otel-collector, and otel-Collector sending them to different backends (Jaeger and Lightstep)
24-
- `v4` are files when we add custom attributes and log events to the auto-instrumentation
24+
- `v4` are files when we add custom attributes, log events, and new spans to the auto-instrumentation
25+
2526

2627
## Important files
2728

docker-compose.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ services:
3131
- DBHOST=mongodb://db:27017
3232
- NODE_ENV=development
3333
- OTEL_EXPORTER_OTLP_ENDPOINT=grpc://otel-collector:4317
34-
- OTEL_RESOURCE_ATTRIBUTES=service.name=service
34+
- OTEL_RESOURCE_ATTRIBUTES=service.name=service,service.version=4.0.0
3535
depends_on:
3636
- db
3737
restart: always
@@ -51,7 +51,7 @@ services:
5151
- PORT=80
5252
- NODE_ENV=development
5353
- OTEL_EXPORTER_OTLP_ENDPOINT=grpc://otel-collector:4317
54-
- OTEL_RESOURCE_ATTRIBUTES=service.name=web
54+
- OTEL_RESOURCE_ATTRIBUTES=service.name=web,service.version=4.0.0
5555
depends_on:
5656
- service
5757
restart: always

web/src/index.js

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ const axios = require('axios');
77
const PORT = process.env.PORT || 80;
88
const HOST = process.env.HOST || "0.0.0.0";
99
const SERVICE_URL = process.env.SERVICE_URL || "http://service";
10+
// import the open telemetry api library
11+
const api = require('@opentelemetry/api');
12+
// create a tracer and name it after your package
13+
const tracer = api.trace.getTracer('myInstrumentation');
1014

1115
// App
1216
const app = express();
@@ -34,9 +38,12 @@ async function checkWeather(weather, res) {
3438

3539
async function generateWork(nb) {
3640
for (let i = 0; i < Number(nb); i++) {
37-
console.log(`*** DOING SOMETHING ${i}`);
41+
let span = tracer.startSpan(`Looping ${i}`);
42+
// log an event and include some structured data.
43+
span.addEvent(`*** DOING SOMETHING ${i}`);
3844
// wait for 50ms to simulate some work
3945
await sleep(50);
46+
span.end();
4047
}
4148
}
4249

@@ -45,6 +52,11 @@ async function main() {
4552
app.get("/", (req, res) => {
4653
let nbLoop = req.query.loop;
4754
let weather = req.query.weather;
55+
// access the current span from active context
56+
let activeSpan = api.trace.getSpan(api.context.active());
57+
// add an attribute
58+
activeSpan.setAttribute('nbLoop', nbLoop);
59+
activeSpan.setAttribute('weather', weather);
4860
// generate some workload
4961
if (nbLoop != undefined) {
5062
generateWork(nbLoop);
@@ -58,6 +70,10 @@ async function main() {
5870
});
5971

6072
app.get("/api/data", (req, res) => {
73+
// access the current span from active context
74+
let activeSpan = api.trace.getSpan(api.context.active());
75+
// log an event and include some structured data.
76+
activeSpan.addEvent(`Running on http://${HOST}:${PORT}`);
6177
axios.get(SERVICE_URL + "/api/data")
6278
.then(response => {
6379
res.json(response.data);

web/src/tracing.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,12 @@ const { OTLPTraceExporter } = require('@opentelemetry/exporter-trace-otlp-grpc')
66
const { getNodeAutoInstrumentations } = require("@opentelemetry/auto-instrumentations-node");
77

88
const sdk = new opentelemetry.NodeSDK({
9+
// sending traces to console for debugging
10+
// traceExporter: new opentelemetry.tracing.ConsoleSpanExporter(),
911
traceExporter: new OTLPTraceExporter({}),
10-
// traceExporter: new OTLPTraceExporter({url: 'grpc://otel-collector:4317'}),
12+
// if you want to set url in code
13+
// traceExporter: new OTLPTraceExporter({url: 'http://satellite:8383'}), // For Lightstep microsatellite
14+
// traceExporter: new OTLPTraceExporter({url: 'grpc://otel-collector:4317'}), // For Otel Colelctor
1115
instrumentations: [getNodeAutoInstrumentations()]
1216
});
1317

0 commit comments

Comments
 (0)