Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: Update the OTel Express integration example #2532

Merged
merged 12 commits into from
Nov 27, 2024
Merged
26 changes: 2 additions & 24 deletions examples/express/README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Overview

OpenTelemetry Express Instrumentation allows the user to automatically collect trace data and export them to the backend of choice (we can use Zipkin or Jaeger for this example), to give observability to distributed systems.
OpenTelemetry Express Instrumentation allows the user to automatically collect trace data and export them to the backend of choice (we can Jaeger for this example), to give observability to distributed systems.
sp6370 marked this conversation as resolved.
Show resolved Hide resolved

This is a simple example that demonstrates tracing calls made to Express API. The example
shows key aspects of tracing such as
Expand All @@ -17,32 +17,10 @@ shows key aspects of tracing such as
npm install
```

Setup [Zipkin Tracing](https://zipkin.io/pages/quickstart.html)
or
Setup [Jaeger Tracing](https://www.jaegertracing.io/docs/latest/getting-started/#all-in-one)
Setup [Jaeger Tracing](https://www.jaegertracing.io/docs/2.0/getting-started/#in-docker)
sp6370 marked this conversation as resolved.
Show resolved Hide resolved

## Run the Application

### Zipkin

Run the server:

```sh
npm run zipkin:server
```

Then run the client in a separate terminal:

```sh
npm run zipkin:client
```

After a short time, the generated traces should be available in the Zipkin UI.
Visit <http://localhost:9411/zipkin> and click the "RUN QUERY" button to view
recent traces, then click "SHOW" on a given trace.

<p align="center"><img alt="Zipkin UI with trace" src="./images/zipkin.jpg?raw=true"/></p>

### Jaeger

Run the server:
sp6370 marked this conversation as resolved.
Show resolved Hide resolved
Expand Down
Binary file removed examples/express/images/zipkin.jpg
Binary file not shown.
21 changes: 9 additions & 12 deletions examples/express/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@
"version": "0.1.0",
"description": "Example of Express integration with OpenTelemetry",
"scripts": {
"zipkin:server": "cross-env EXPORTER=zipkin ts-node src/server.ts",
"zipkin:client": "cross-env EXPORTER=zipkin ts-node src/client.ts",
"jaeger:server": "cross-env EXPORTER=jaeger ts-node src/server.ts",
"jaeger:client": "cross-env EXPORTER=jaeger ts-node src/client.ts",
sp6370 marked this conversation as resolved.
Show resolved Hide resolved
"compile": "tsc -p ."
Expand All @@ -30,23 +28,22 @@
},
"homepage": "https://github.com/open-telemetry/opentelemetry-js-contrib/tree/main/examples/express#readme",
"dependencies": {
"@opentelemetry/api": "^1.3.0",
"@opentelemetry/exporter-jaeger": "^1.18.1",
"@opentelemetry/exporter-trace-otlp-proto": "^0.48.0",
"@opentelemetry/exporter-zipkin": "^1.18.1",
"@opentelemetry/instrumentation": "^0.48.0",
"@opentelemetry/instrumentation-express": "^0.34.1",
"@opentelemetry/instrumentation-http": "^0.48.0",
"@opentelemetry/resources": "^1.18.1",
"@opentelemetry/sdk-trace-base": "^1.18.1",
"@opentelemetry/sdk-trace-node": "^1.18.1",
"@opentelemetry/api": "^1.9.0",
"@opentelemetry/exporter-trace-otlp-proto": "^0.54.2",
"@opentelemetry/instrumentation": "^0.54.2",
"@opentelemetry/instrumentation-express": "^0.44.0",
"@opentelemetry/instrumentation-http": "^0.54.2",
"@opentelemetry/resources": "^1.27.0",
"@opentelemetry/sdk-trace-base": "^1.27.0",
"@opentelemetry/sdk-trace-node": "^1.27.0",
"@opentelemetry/semantic-conventions": "^1.27.0",
"axios": "^1.6.0",
"cross-env": "^7.0.3",
"express": "^4.17.1"
},
"devDependencies": {
"@types/express": "^4.17.13",
"@types/node": "18.18.14",
"ts-node": "^10.6.0",
"typescript": "4.4.4"
}
Expand Down
21 changes: 9 additions & 12 deletions examples/express/src/tracer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import { SpanKind, Attributes } from "@opentelemetry/api";

const opentelemetry = require('@opentelemetry/api');
import opentelemetry = require('@opentelemetry/api');

// Not functionally required but gives some insight what happens behind the scenes
const { diag, DiagConsoleLogger, DiagLogLevel } = opentelemetry;
Expand All @@ -12,33 +12,30 @@ import { registerInstrumentations } from '@opentelemetry/instrumentation';
import { NodeTracerProvider } from '@opentelemetry/sdk-trace-node';
import { Sampler, AlwaysOnSampler, SimpleSpanProcessor } from '@opentelemetry/sdk-trace-base';
import { OTLPTraceExporter } from '@opentelemetry/exporter-trace-otlp-proto';
import { ZipkinExporter } from '@opentelemetry/exporter-zipkin';
import { Resource } from '@opentelemetry/resources';
import { SEMRESATTRS_SERVICE_NAME, SEMATTRS_HTTP_ROUTE } from '@opentelemetry/semantic-conventions';
import { ATTR_SERVICE_NAME, ATTR_HTTP_ROUTE } from '@opentelemetry/semantic-conventions';

const Exporter = (process.env.EXPORTER || '').toLowerCase().startsWith('z') ? ZipkinExporter : OTLPTraceExporter;
const Exporter = OTLPTraceExporter;
sp6370 marked this conversation as resolved.
Show resolved Hide resolved
import { ExpressInstrumentation } from '@opentelemetry/instrumentation-express';
const { HttpInstrumentation } = require('@opentelemetry/instrumentation-http');
import {HttpInstrumentation} from '@opentelemetry/instrumentation-http';

export const setupTracing = (serviceName: string) => {
const provider = new NodeTracerProvider({
resource: new Resource({
[SEMRESATTRS_SERVICE_NAME]: serviceName,
[ATTR_SERVICE_NAME]: serviceName,
}),
sampler: filterSampler(ignoreHealthCheck, new AlwaysOnSampler()),
});
registerInstrumentations({
tracerProvider: provider,
instrumentations: [
// Express instrumentation expects HTTP layer to be instrumented
HttpInstrumentation,
ExpressInstrumentation,
new HttpInstrumentation(),
new ExpressInstrumentation(),
],
});

const exporter = new Exporter({
serviceName,
});
const exporter = new Exporter({});

sp6370 marked this conversation as resolved.
Show resolved Hide resolved
provider.addSpanProcessor(new SimpleSpanProcessor(exporter));

Expand All @@ -65,5 +62,5 @@ function filterSampler(filterFn: FilterFunction, parent: Sampler): Sampler {
}

function ignoreHealthCheck(spanName: string, spanKind: SpanKind, attributes: Attributes) {
return spanKind !== opentelemetry.SpanKind.SERVER || attributes[SEMATTRS_HTTP_ROUTE] !== "/health";
return spanKind !== opentelemetry.SpanKind.SERVER || attributes[ATTR_HTTP_ROUTE] !== "/health";
}
Loading