Skip to content

Commit d7773a2

Browse files
sp6370trentm
andauthored
chore: Update the express example (open-telemetry#2532)
Signed-off-by: Sudhanshu Pandey <[email protected]> Co-authored-by: Trent Mick <[email protected]>
1 parent 6ee7e7f commit d7773a2

File tree

4 files changed

+37
-55
lines changed

4 files changed

+37
-55
lines changed

examples/express/README.md

Lines changed: 18 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Overview
22

3-
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.
3+
OpenTelemetry Express Instrumentation allows the user to automatically collect trace data and export them to the backend of choice (we use Jaeger for this example), to give observability to distributed systems.
44

55
This is a simple example that demonstrates tracing calls made to Express API. The example
66
shows key aspects of tracing such as
@@ -17,44 +17,34 @@ shows key aspects of tracing such as
1717
npm install
1818
```
1919

20-
Setup [Zipkin Tracing](https://zipkin.io/pages/quickstart.html)
21-
or
22-
Setup [Jaeger Tracing](https://www.jaegertracing.io/docs/latest/getting-started/#all-in-one)
23-
24-
## Run the Application
25-
26-
### Zipkin
27-
28-
Run the server:
29-
30-
```sh
31-
npm run zipkin:server
20+
Start Jaeger in Docker for receiving tracing data (see [the Jaeger docs](https://www.jaegertracing.io/docs/2.0/getting-started/#in-docker) for more details about running Jaeger):
21+
22+
```bash
23+
docker run --rm --name jaeger \
24+
-p 5778:5778 \
25+
-p 16686:16686 \
26+
-p 4317:4317 \
27+
-p 4318:4318 \
28+
-p 14250:14250 \
29+
-p 14268:14268 \
30+
-p 9411:9411 \
31+
jaegertracing/jaeger:2.0.0 \
32+
--set receivers.otlp.protocols.http.endpoint=0.0.0.0:4318 \
33+
--set receivers.otlp.protocols.grpc.endpoint=0.0.0.0:4317
3234
```
3335

34-
Then run the client in a separate terminal:
35-
36-
```sh
37-
npm run zipkin:client
38-
```
39-
40-
After a short time, the generated traces should be available in the Zipkin UI.
41-
Visit <http://localhost:9411/zipkin> and click the "RUN QUERY" button to view
42-
recent traces, then click "SHOW" on a given trace.
43-
44-
<p align="center"><img alt="Zipkin UI with trace" src="./images/zipkin.jpg?raw=true"/></p>
45-
46-
### Jaeger
36+
## Run the Application
4737

4838
Run the server:
4939

5040
```sh
51-
npm run jaeger:server
41+
npm run server
5242
```
5343

5444
Then run the client in a separate terminal:
5545

5646
```sh
57-
npm run jaeger:client
47+
npm run client
5848
```
5949

6050
Visit the Jaeger UI at <http://localhost:16686/search>, select a service (e.g. "example-express-client"), click "Find Traces", then click on a trace to view it.

examples/express/images/zipkin.jpg

-102 KB
Binary file not shown.

examples/express/package.json

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,8 @@
44
"version": "0.1.0",
55
"description": "Example of Express integration with OpenTelemetry",
66
"scripts": {
7-
"zipkin:server": "cross-env EXPORTER=zipkin ts-node src/server.ts",
8-
"zipkin:client": "cross-env EXPORTER=zipkin ts-node src/client.ts",
9-
"jaeger:server": "cross-env EXPORTER=jaeger ts-node src/server.ts",
10-
"jaeger:client": "cross-env EXPORTER=jaeger ts-node src/client.ts",
7+
"server": "ts-node src/server.ts",
8+
"client": "ts-node src/client.ts",
119
"compile": "tsc -p ."
1210
},
1311
"repository": {
@@ -30,23 +28,21 @@
3028
},
3129
"homepage": "https://github.com/open-telemetry/opentelemetry-js-contrib/tree/main/examples/express#readme",
3230
"dependencies": {
33-
"@opentelemetry/api": "^1.3.0",
34-
"@opentelemetry/exporter-jaeger": "^1.18.1",
35-
"@opentelemetry/exporter-trace-otlp-proto": "^0.48.0",
36-
"@opentelemetry/exporter-zipkin": "^1.18.1",
37-
"@opentelemetry/instrumentation": "^0.48.0",
38-
"@opentelemetry/instrumentation-express": "^0.34.1",
39-
"@opentelemetry/instrumentation-http": "^0.48.0",
40-
"@opentelemetry/resources": "^1.18.1",
41-
"@opentelemetry/sdk-trace-base": "^1.18.1",
42-
"@opentelemetry/sdk-trace-node": "^1.18.1",
31+
"@opentelemetry/api": "^1.9.0",
32+
"@opentelemetry/exporter-trace-otlp-proto": "^0.54.2",
33+
"@opentelemetry/instrumentation": "^0.54.2",
34+
"@opentelemetry/instrumentation-express": "^0.44.0",
35+
"@opentelemetry/instrumentation-http": "^0.54.2",
36+
"@opentelemetry/resources": "^1.27.0",
37+
"@opentelemetry/sdk-trace-base": "^1.27.0",
38+
"@opentelemetry/sdk-trace-node": "^1.27.0",
4339
"@opentelemetry/semantic-conventions": "^1.27.0",
4440
"axios": "^1.6.0",
45-
"cross-env": "^7.0.3",
4641
"express": "^4.17.1"
4742
},
4843
"devDependencies": {
4944
"@types/express": "^4.17.13",
45+
"@types/node": "18.18.14",
5046
"ts-node": "^10.6.0",
5147
"typescript": "4.4.4"
5248
}

examples/express/src/tracer.ts

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

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

5-
const opentelemetry = require('@opentelemetry/api');
5+
import opentelemetry = require('@opentelemetry/api');
66

77
// Not functionally required but gives some insight what happens behind the scenes
88
const { diag, DiagConsoleLogger, DiagLogLevel } = opentelemetry;
@@ -12,33 +12,29 @@ import { registerInstrumentations } from '@opentelemetry/instrumentation';
1212
import { NodeTracerProvider } from '@opentelemetry/sdk-trace-node';
1313
import { Sampler, AlwaysOnSampler, SimpleSpanProcessor } from '@opentelemetry/sdk-trace-base';
1414
import { OTLPTraceExporter } from '@opentelemetry/exporter-trace-otlp-proto';
15-
import { ZipkinExporter } from '@opentelemetry/exporter-zipkin';
1615
import { Resource } from '@opentelemetry/resources';
17-
import { SEMRESATTRS_SERVICE_NAME, SEMATTRS_HTTP_ROUTE } from '@opentelemetry/semantic-conventions';
16+
import { ATTR_SERVICE_NAME, ATTR_HTTP_ROUTE } from '@opentelemetry/semantic-conventions';
1817

19-
const Exporter = (process.env.EXPORTER || '').toLowerCase().startsWith('z') ? ZipkinExporter : OTLPTraceExporter;
2018
import { ExpressInstrumentation } from '@opentelemetry/instrumentation-express';
21-
const { HttpInstrumentation } = require('@opentelemetry/instrumentation-http');
19+
import {HttpInstrumentation} from '@opentelemetry/instrumentation-http';
2220

2321
export const setupTracing = (serviceName: string) => {
2422
const provider = new NodeTracerProvider({
2523
resource: new Resource({
26-
[SEMRESATTRS_SERVICE_NAME]: serviceName,
24+
[ATTR_SERVICE_NAME]: serviceName,
2725
}),
2826
sampler: filterSampler(ignoreHealthCheck, new AlwaysOnSampler()),
2927
});
3028
registerInstrumentations({
3129
tracerProvider: provider,
3230
instrumentations: [
3331
// Express instrumentation expects HTTP layer to be instrumented
34-
HttpInstrumentation,
35-
ExpressInstrumentation,
32+
new HttpInstrumentation(),
33+
new ExpressInstrumentation(),
3634
],
3735
});
3836

39-
const exporter = new Exporter({
40-
serviceName,
41-
});
37+
const exporter = new OTLPTraceExporter({});
4238

4339
provider.addSpanProcessor(new SimpleSpanProcessor(exporter));
4440

@@ -65,5 +61,5 @@ function filterSampler(filterFn: FilterFunction, parent: Sampler): Sampler {
6561
}
6662

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

0 commit comments

Comments
 (0)