Skip to content

Commit

Permalink
fix(example-fastify): update example to use latest versions (#1899)
Browse files Browse the repository at this point in the history
* fix(example-fastify): update example to use latest versions

* fix(fastify-example): update screenshot
  • Loading branch information
pichlermarc authored Jan 26, 2024
1 parent fce7d3b commit 12834d5
Show file tree
Hide file tree
Showing 10 changed files with 2,429 additions and 121 deletions.
17 changes: 5 additions & 12 deletions examples/fastify/client.js
Original file line number Diff line number Diff line change
@@ -1,38 +1,31 @@
'use strict';

// eslint-disable-next-line import/order
const tracing = require('./tracing')('example-fastify-client');

const { tracer } = tracing;
const api = require('@opentelemetry/api');
const axios = require('axios').default;

const tracer = api.trace.getTracer('fastify-client');

function makeRequest() {
tracing.log('starting');
console.log('starting');
const span = tracer.startSpan('client.makeRequest()', {
kind: api.SpanKind.CLIENT,
});

api.context.with(api.trace.setSpan(api.ROOT_CONTEXT, span), async () => {
try {
const res = await axios.post('http://localhost:8080/run_test/1', {
// testing
// const res = await axios.post('http://localhost:8080/run_test2/1', {
headers: {
'Content-Type': 'application/json',
},
timeout: 3000,
});
tracing.log('status:', res.statusText);
console.log('status:', res.statusText);
span.setStatus({ code: api.SpanStatusCode.OK });
} catch (e) {
tracing.log('failed:', e.message);
console.log('failed:', e.message);
span.setStatus({ code: api.SpanStatusCode.ERROR, message: e.message });
}
span.end();
tracing.log('forcing spans to be exported');
await tracing.provider.shutdown();
tracing.log('all spans exported successfully.');
});
}

Expand Down
34 changes: 23 additions & 11 deletions examples/fastify/docker/collector-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,38 @@ receivers:
protocols:
grpc:
http:
cors_allowed_origins:
- http://*
- https://*
cors:
allowed_origins:
- http://*
- https://*

exporters:
zipkin:
endpoint: "http://zipkin-all-in-one:9411/api/v2/spans"
otlp:
endpoint: "jaeger:4317"
tls:
insecure: true
prometheus:
endpoint: "0.0.0.0:9464"
zipkin:
endpoint: "http://zipkin:9411/api/v2/spans"

processors:
batch:

service:
pipelines:
traces:
receivers: [otlp]
exporters: [zipkin]
processors: [batch]
receivers:
- otlp
exporters:
- otlp
- zipkin
processors:
- batch
metrics:
receivers: [otlp]
exporters: [prometheus]
processors: [batch]
receivers:
- otlp
exporters:
- prometheus
processors:
- batch
34 changes: 22 additions & 12 deletions examples/fastify/docker/docker-compose.yaml
Original file line number Diff line number Diff line change
@@ -1,21 +1,31 @@
version: "3"
services:
# Collector
collector:
image: otel/opentelemetry-collector:0.38.0
# image: otel/opentelemetry-collector:latest
command: ["--config=/conf/collector-config.yaml", "--log-level=DEBUG"]
image: otel/opentelemetry-collector:0.92.0
command: ["--config=/conf/collector-config.yaml"]
volumes:
- ./collector-config.yaml:/conf/collector-config.yaml
ports:
- "9464:9464"
- "4317:4317"
- "4318:4318"
- "4317:4317" # OTLP-grpc compatible endpoint (used by client/server)
- "4318:4318" # OTLP-http compatible endpoint (unused in this example)
depends_on:
- zipkin-all-in-one
- jaeger
- zipkin

# Zipkin
zipkin-all-in-one:
image: openzipkin/zipkin:latest
jaeger:
image: jaegertracing/all-in-one:1.52
ports:
- "9411:9411"
- "16686:16686" # frontend (to inspect traces)

zipkin:
image: openzipkin/zipkin:3
ports:
- "9411:9411" # frontend (to inspect traces)

prometheus:
container_name: prometheus
image: prom/prometheus:v2.49.0
volumes:
- ./prometheus.yaml:/etc/prometheus/prometheus.yml
ports:
- "9090:9090"
9 changes: 9 additions & 0 deletions examples/fastify/docker/prometheus.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
global:
scrape_interval: 15s # Default is every 1 minute.

scrape_configs:
- job_name: 'collector'
# metrics_path defaults to '/metrics'
# scheme defaults to 'http'.
static_configs:
- targets: ['collector:9464']
Binary file modified examples/fastify/images/trace1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
33 changes: 33 additions & 0 deletions examples/fastify/opentelemetry.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
'use strict';

const {
diag,
DiagConsoleLogger,
DiagLogLevel,
} = require('@opentelemetry/api');

diag.setLogger(new DiagConsoleLogger(), DiagLogLevel.WARN);

const { HttpInstrumentation } = require('@opentelemetry/instrumentation-http');
const { FastifyInstrumentation } = require('@opentelemetry/instrumentation-fastify');

const { OTLPTraceExporter } = require('@opentelemetry/exporter-trace-otlp-proto');
const { OTLPMetricExporter } = require('@opentelemetry/exporter-metrics-otlp-proto');
const { NodeSDK, metrics } = require('@opentelemetry/sdk-node');

const sdk = new NodeSDK({
instrumentations: [
HttpInstrumentation,
new FastifyInstrumentation(),
],
traceExporter: new OTLPTraceExporter(),
metricReader: new metrics.PeriodicExportingMetricReader({
exporter: new OTLPMetricExporter(),
}),
});

process.on('beforeExit', async () => {
await sdk.shutdown();
});

sdk.start();
Loading

0 comments on commit 12834d5

Please sign in to comment.