Skip to content

Commit

Permalink
This is v4.0.0
Browse files Browse the repository at this point in the history
  • Loading branch information
dfinas committed May 12, 2022
1 parent 0c6ad88 commit 68ac404
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 15 deletions.
31 changes: 21 additions & 10 deletions INSTALLING_OTEL.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,9 @@ You will find files in different versions in this repository
- `v2a` is using on HTTP protocol for sending traces
- `v2b` is using on GRPC protocol for sending traces
- `v3` are files with Otel Instrumentation sending output to otel-collector (using grpc), and otel-Collector sending them to different backends (Jaeger and Lightstep)
- `v4` are files when we add custom attributes and log events to the auto-instrumentation
- `v5` are files when we add custom spans
- `v6` are files when we add metrics
- `v7` are files when we add custom metrics to the auto-instrumentation
- `v4` are files when we add custom attributes, log events, and new spans to the auto-instrumentation
- `v5` are files when we add metrics
- `v6` are files when we add custom metrics to the auto-instrumentation

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

Expand Down Expand Up @@ -237,6 +236,8 @@ environment:

## v4 - Add custom attributes and log events

- 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`

- In `/src` folder of the web component, update file `index.js` file with code below:
- Add the OpenTelemetry library by putting this at top of your code `const api = require('@opentelemetry/api');`

Expand All @@ -249,23 +250,33 @@ activeSpan.setAttribute('nbLoop', nbLoop);
activeSpan.setAttribute('weather', weather);
```

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


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


## v6 - Add resources metrics
## v5 - Add resources metrics


## v7 - Add custom metrics to your traces
## v6 - Add custom metrics to your traces



Expand Down
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ Need to build a microservices application? Learn how to do this with [Bootstrapp
- `v1` are files with Otel Instrumentation sending output (traces or metrics) to console logs
- `v2` are files with Otel Instrumentation sending output to otel-collector
- `v3` are files with Otel Instrumentation sending output to otel-collector, and otel-Collector sending them to different backends (Jaeger and Lightstep)
- `v4` are files when we add custom attributes and log events to the auto-instrumentation
- `v4` are files when we add custom attributes, log events, and new spans to the auto-instrumentation


## Important files

Expand Down
4 changes: 2 additions & 2 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ services:
- DBHOST=mongodb://db:27017
- NODE_ENV=development
- OTEL_EXPORTER_OTLP_ENDPOINT=grpc://otel-collector:4317
- OTEL_RESOURCE_ATTRIBUTES=service.name=service
- OTEL_RESOURCE_ATTRIBUTES=service.name=service,service.version=4.0.0
depends_on:
- db
restart: always
Expand All @@ -51,7 +51,7 @@ services:
- PORT=80
- NODE_ENV=development
- OTEL_EXPORTER_OTLP_ENDPOINT=grpc://otel-collector:4317
- OTEL_RESOURCE_ATTRIBUTES=service.name=web
- OTEL_RESOURCE_ATTRIBUTES=service.name=web,service.version=4.0.0
depends_on:
- service
restart: always
Expand Down
18 changes: 17 additions & 1 deletion web/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ const axios = require('axios');
const PORT = process.env.PORT || 80;
const HOST = process.env.HOST || "0.0.0.0";
const SERVICE_URL = process.env.SERVICE_URL || "http://service";
// import the open telemetry api library
const api = require('@opentelemetry/api');
// create a tracer and name it after your package
const tracer = api.trace.getTracer('myInstrumentation');

// App
const app = express();
Expand Down Expand Up @@ -34,9 +38,12 @@ async function checkWeather(weather, res) {

async function generateWork(nb) {
for (let i = 0; i < Number(nb); i++) {
console.log(`*** DOING SOMETHING ${i}`);
let span = tracer.startSpan(`Looping ${i}`);
// log an event and include some structured data.
span.addEvent(`*** DOING SOMETHING ${i}`);
// wait for 50ms to simulate some work
await sleep(50);
span.end();
}
}

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

app.get("/api/data", (req, res) => {
// access the current span from active context
let activeSpan = api.trace.getSpan(api.context.active());
// log an event and include some structured data.
activeSpan.addEvent(`Running on http://${HOST}:${PORT}`);
axios.get(SERVICE_URL + "/api/data")
.then(response => {
res.json(response.data);
Expand Down
6 changes: 5 additions & 1 deletion web/src/tracing.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,12 @@ const { OTLPTraceExporter } = require('@opentelemetry/exporter-trace-otlp-grpc')
const { getNodeAutoInstrumentations } = require("@opentelemetry/auto-instrumentations-node");

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

Expand Down

0 comments on commit 68ac404

Please sign in to comment.