Skip to content

Commit 12834d5

Browse files
authored
fix(example-fastify): update example to use latest versions (#1899)
* fix(example-fastify): update example to use latest versions * fix(fastify-example): update screenshot
1 parent fce7d3b commit 12834d5

File tree

10 files changed

+2429
-121
lines changed

10 files changed

+2429
-121
lines changed

examples/fastify/client.js

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,31 @@
11
'use strict';
22

3-
// eslint-disable-next-line import/order
4-
const tracing = require('./tracing')('example-fastify-client');
5-
6-
const { tracer } = tracing;
73
const api = require('@opentelemetry/api');
84
const axios = require('axios').default;
95

6+
const tracer = api.trace.getTracer('fastify-client');
7+
108
function makeRequest() {
11-
tracing.log('starting');
9+
console.log('starting');
1210
const span = tracer.startSpan('client.makeRequest()', {
1311
kind: api.SpanKind.CLIENT,
1412
});
1513

1614
api.context.with(api.trace.setSpan(api.ROOT_CONTEXT, span), async () => {
1715
try {
1816
const res = await axios.post('http://localhost:8080/run_test/1', {
19-
// testing
20-
// const res = await axios.post('http://localhost:8080/run_test2/1', {
2117
headers: {
2218
'Content-Type': 'application/json',
2319
},
2420
timeout: 3000,
2521
});
26-
tracing.log('status:', res.statusText);
22+
console.log('status:', res.statusText);
2723
span.setStatus({ code: api.SpanStatusCode.OK });
2824
} catch (e) {
29-
tracing.log('failed:', e.message);
25+
console.log('failed:', e.message);
3026
span.setStatus({ code: api.SpanStatusCode.ERROR, message: e.message });
3127
}
3228
span.end();
33-
tracing.log('forcing spans to be exported');
34-
await tracing.provider.shutdown();
35-
tracing.log('all spans exported successfully.');
3629
});
3730
}
3831

examples/fastify/docker/collector-config.yaml

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,26 +3,38 @@ receivers:
33
protocols:
44
grpc:
55
http:
6-
cors_allowed_origins:
7-
- http://*
8-
- https://*
6+
cors:
7+
allowed_origins:
8+
- http://*
9+
- https://*
910

1011
exporters:
11-
zipkin:
12-
endpoint: "http://zipkin-all-in-one:9411/api/v2/spans"
12+
otlp:
13+
endpoint: "jaeger:4317"
14+
tls:
15+
insecure: true
1316
prometheus:
1417
endpoint: "0.0.0.0:9464"
18+
zipkin:
19+
endpoint: "http://zipkin:9411/api/v2/spans"
1520

1621
processors:
1722
batch:
1823

1924
service:
2025
pipelines:
2126
traces:
22-
receivers: [otlp]
23-
exporters: [zipkin]
24-
processors: [batch]
27+
receivers:
28+
- otlp
29+
exporters:
30+
- otlp
31+
- zipkin
32+
processors:
33+
- batch
2534
metrics:
26-
receivers: [otlp]
27-
exporters: [prometheus]
28-
processors: [batch]
35+
receivers:
36+
- otlp
37+
exporters:
38+
- prometheus
39+
processors:
40+
- batch
Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,31 @@
11
version: "3"
22
services:
3-
# Collector
43
collector:
5-
image: otel/opentelemetry-collector:0.38.0
6-
# image: otel/opentelemetry-collector:latest
7-
command: ["--config=/conf/collector-config.yaml", "--log-level=DEBUG"]
4+
image: otel/opentelemetry-collector:0.92.0
5+
command: ["--config=/conf/collector-config.yaml"]
86
volumes:
97
- ./collector-config.yaml:/conf/collector-config.yaml
108
ports:
11-
- "9464:9464"
12-
- "4317:4317"
13-
- "4318:4318"
9+
- "4317:4317" # OTLP-grpc compatible endpoint (used by client/server)
10+
- "4318:4318" # OTLP-http compatible endpoint (unused in this example)
1411
depends_on:
15-
- zipkin-all-in-one
12+
- jaeger
13+
- zipkin
1614

17-
# Zipkin
18-
zipkin-all-in-one:
19-
image: openzipkin/zipkin:latest
15+
jaeger:
16+
image: jaegertracing/all-in-one:1.52
2017
ports:
21-
- "9411:9411"
18+
- "16686:16686" # frontend (to inspect traces)
19+
20+
zipkin:
21+
image: openzipkin/zipkin:3
22+
ports:
23+
- "9411:9411" # frontend (to inspect traces)
24+
25+
prometheus:
26+
container_name: prometheus
27+
image: prom/prometheus:v2.49.0
28+
volumes:
29+
- ./prometheus.yaml:/etc/prometheus/prometheus.yml
30+
ports:
31+
- "9090:9090"
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
global:
2+
scrape_interval: 15s # Default is every 1 minute.
3+
4+
scrape_configs:
5+
- job_name: 'collector'
6+
# metrics_path defaults to '/metrics'
7+
# scheme defaults to 'http'.
8+
static_configs:
9+
- targets: ['collector:9464']

examples/fastify/images/trace1.png

-99.3 KB
Loading

examples/fastify/opentelemetry.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
'use strict';
2+
3+
const {
4+
diag,
5+
DiagConsoleLogger,
6+
DiagLogLevel,
7+
} = require('@opentelemetry/api');
8+
9+
diag.setLogger(new DiagConsoleLogger(), DiagLogLevel.WARN);
10+
11+
const { HttpInstrumentation } = require('@opentelemetry/instrumentation-http');
12+
const { FastifyInstrumentation } = require('@opentelemetry/instrumentation-fastify');
13+
14+
const { OTLPTraceExporter } = require('@opentelemetry/exporter-trace-otlp-proto');
15+
const { OTLPMetricExporter } = require('@opentelemetry/exporter-metrics-otlp-proto');
16+
const { NodeSDK, metrics } = require('@opentelemetry/sdk-node');
17+
18+
const sdk = new NodeSDK({
19+
instrumentations: [
20+
HttpInstrumentation,
21+
new FastifyInstrumentation(),
22+
],
23+
traceExporter: new OTLPTraceExporter(),
24+
metricReader: new metrics.PeriodicExportingMetricReader({
25+
exporter: new OTLPMetricExporter(),
26+
}),
27+
});
28+
29+
process.on('beforeExit', async () => {
30+
await sdk.shutdown();
31+
});
32+
33+
sdk.start();

0 commit comments

Comments
 (0)