Skip to content

Commit 45e964b

Browse files
authored
Merge branch 'main' into aws-sdk-component-owner-trivikr
2 parents 9098772 + 0fc1ed3 commit 45e964b

File tree

18 files changed

+107
-120
lines changed

18 files changed

+107
-120
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: 10 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,51 +1,39 @@
11
'use strict';
22

3-
import { SpanKind, Attributes } from "@opentelemetry/api";
4-
5-
const opentelemetry = require('@opentelemetry/api');
6-
7-
// Not functionally required but gives some insight what happens behind the scenes
8-
const { diag, DiagConsoleLogger, DiagLogLevel } = opentelemetry;
9-
diag.setLogger(new DiagConsoleLogger(), DiagLogLevel.INFO);
10-
3+
import { trace, SamplingDecision, SpanKind, Attributes } from '@opentelemetry/api';
114
import { registerInstrumentations } from '@opentelemetry/instrumentation';
125
import { NodeTracerProvider } from '@opentelemetry/sdk-trace-node';
136
import { Sampler, AlwaysOnSampler, SimpleSpanProcessor } from '@opentelemetry/sdk-trace-base';
147
import { OTLPTraceExporter } from '@opentelemetry/exporter-trace-otlp-proto';
15-
import { ZipkinExporter } from '@opentelemetry/exporter-zipkin';
168
import { Resource } from '@opentelemetry/resources';
17-
import { SEMRESATTRS_SERVICE_NAME, SEMATTRS_HTTP_ROUTE } from '@opentelemetry/semantic-conventions';
18-
19-
const Exporter = (process.env.EXPORTER || '').toLowerCase().startsWith('z') ? ZipkinExporter : OTLPTraceExporter;
9+
import { ATTR_SERVICE_NAME, ATTR_HTTP_ROUTE } from '@opentelemetry/semantic-conventions';
2010
import { ExpressInstrumentation } from '@opentelemetry/instrumentation-express';
21-
const { HttpInstrumentation } = require('@opentelemetry/instrumentation-http');
11+
import { HttpInstrumentation } from '@opentelemetry/instrumentation-http';
2212

2313
export const setupTracing = (serviceName: string) => {
2414
const provider = new NodeTracerProvider({
2515
resource: new Resource({
26-
[SEMRESATTRS_SERVICE_NAME]: serviceName,
16+
[ATTR_SERVICE_NAME]: serviceName,
2717
}),
2818
sampler: filterSampler(ignoreHealthCheck, new AlwaysOnSampler()),
2919
});
3020
registerInstrumentations({
3121
tracerProvider: provider,
3222
instrumentations: [
3323
// Express instrumentation expects HTTP layer to be instrumented
34-
HttpInstrumentation,
35-
ExpressInstrumentation,
24+
new HttpInstrumentation(),
25+
new ExpressInstrumentation(),
3626
],
3727
});
3828

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

4331
provider.addSpanProcessor(new SimpleSpanProcessor(exporter));
4432

4533
// Initialize the OpenTelemetry APIs to use the NodeTracerProvider bindings
4634
provider.register();
4735

48-
return opentelemetry.trace.getTracer(serviceName);
36+
return trace.getTracer(serviceName);
4937
};
5038

5139
type FilterFunction = (spanName: string, spanKind: SpanKind, attributes: Attributes) => boolean;
@@ -54,7 +42,7 @@ function filterSampler(filterFn: FilterFunction, parent: Sampler): Sampler {
5442
return {
5543
shouldSample(ctx, tid, spanName, spanKind, attr, links) {
5644
if (!filterFn(spanName, spanKind, attr)) {
57-
return { decision: opentelemetry.SamplingDecision.NOT_RECORD };
45+
return { decision: SamplingDecision.NOT_RECORD };
5846
}
5947
return parent.shouldSample(ctx, tid, spanName, spanKind, attr, links);
6048
},
@@ -65,5 +53,5 @@ function filterSampler(filterFn: FilterFunction, parent: Sampler): Sampler {
6553
}
6654

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

examples/mongodb/package.json

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -30,21 +30,20 @@
3030
"url": "https://github.com/open-telemetry/opentelemetry-js-contrib/issues"
3131
},
3232
"dependencies": {
33-
"@opentelemetry/api": "^1.0.0",
34-
"@opentelemetry/exporter-jaeger": "^1.0.0",
35-
"@opentelemetry/exporter-zipkin": "^1.0.0",
36-
"@opentelemetry/instrumentation": "^0.48.0",
37-
"@opentelemetry/instrumentation-http": "^0.48.0",
38-
"@opentelemetry/instrumentation-mongodb": "^0.32.0",
39-
"@opentelemetry/sdk-trace-node": "^1.0.0",
40-
"@opentelemetry/sdk-trace-base": "^1.0.0",
33+
"@opentelemetry/api": "^1.9.0",
34+
"@opentelemetry/exporter-zipkin": "^1.27.0",
35+
"@opentelemetry/instrumentation": "^0.54.2",
36+
"@opentelemetry/instrumentation-http": "^0.54.2",
37+
"@opentelemetry/instrumentation-mongodb": "^0.48.0",
38+
"@opentelemetry/sdk-trace-node": "^1.27.0",
39+
"@opentelemetry/sdk-trace-base": "^1.27.0",
4140
"@opentelemetry/semantic-conventions": "^1.27.0",
42-
"mongodb": "^3.6.11"
41+
"mongodb": "^6.10.0"
4342
},
4443
"homepage": "https://github.com/open-telemetry/opentelemetry-js-contrib#readme",
4544
"devDependencies": {
4645
"cross-env": "^7.0.3",
47-
"ts-node": "^10.6.0",
48-
"typescript": "4.4.4"
46+
"ts-node": "^10.9.2",
47+
"typescript": "5.6.3"
4948
}
5049
}

examples/mongodb/src/server.ts

Lines changed: 24 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,17 @@ import * as api from '@opentelemetry/api';
22

33
import { setupTracing } from './tracer';
44

5-
setupTracing('example-mongodb-server')
5+
setupTracing('example-mongodb-server');
66

77
import { accessDB } from './utils';
88

99
import * as http from 'http';
1010
import { IncomingMessage, ServerResponse } from 'http';
1111
import * as mongodb from 'mongodb';
12-
import {Collection} from "mongodb";
12+
import { Collection } from 'mongodb';
1313

14-
const DB_NAME = 'mydb'
15-
const COLLECTION_NAME = 'users'
14+
const DB_NAME = 'mydb';
15+
const COLLECTION_NAME = 'users';
1616
const URL = `mongodb://localhost:27017/${DB_NAME}`;
1717

1818
let db: mongodb.Db;
@@ -28,7 +28,6 @@ function startServer(port: number) {
2828
throw err;
2929
});
3030

31-
3231
// Creates a server
3332
const server = http.createServer(handleRequest);
3433
// Starts the server
@@ -40,16 +39,20 @@ function startServer(port: number) {
4039
/** A function which handles requests and send response. */
4140
function handleRequest(request: IncomingMessage, response: ServerResponse) {
4241
const currentSpan = api.trace.getSpan(api.context.active());
43-
// display traceID in the terminal
44-
const traceId = currentSpan?.spanContext();
45-
console.log(`traceid: ${traceId}`);
46-
console.log(`Jaeger URL: http://localhost:16686/trace/${traceId}`);
47-
console.log(`Zipkin URL: http://localhost:9411/zipkin/traces/${traceId}`);
42+
if (currentSpan) {
43+
// display traceID in the terminal
44+
const { traceId } = currentSpan?.spanContext();
45+
console.log(`traceid: ${traceId}`);
46+
console.log(`Jaeger URL: http://localhost:16686/trace/${traceId}`);
47+
console.log(`Zipkin URL: http://localhost:9411/zipkin/traces/${traceId}`);
48+
} else {
49+
console.log('No active span found');
50+
}
4851

4952
try {
5053
const body = [];
51-
request.on('error', (err) => console.log(err));
52-
request.on('data', (chunk) => body.push(chunk));
54+
request.on('error', err => console.log(err));
55+
request.on('data', chunk => body.push(chunk));
5356
request.on('end', async () => {
5457
if (request.url === '/collection/') {
5558
handleCreateCollection(response);
@@ -71,12 +74,13 @@ startServer(8080);
7174
function handleInsertQuery(response: ServerResponse) {
7275
const obj = { name: 'John', age: '20' };
7376
const usersCollection: Collection = db.collection(COLLECTION_NAME);
74-
usersCollection.insertOne(obj)
77+
usersCollection
78+
.insertOne(obj)
7579
.then(() => {
7680
console.log('1 document inserted');
77-
// find document to test context propagation using callback
78-
usersCollection.findOne({}, function () {
79-
response.end();
81+
// find document to test context
82+
usersCollection.findOne({}).then(res => {
83+
console.log(JSON.stringify(res));
8084
});
8185
})
8286
.catch(err => {
@@ -87,16 +91,16 @@ function handleInsertQuery(response: ServerResponse) {
8791

8892
function handleGetQuery(response: ServerResponse) {
8993
const usersCollection: Collection = db.collection(COLLECTION_NAME);
90-
usersCollection.
91-
find({})
94+
usersCollection
95+
.find({})
9296
.toArray()
9397
.then(() => {
9498
console.log('1 document served');
9599
response.end();
96100
})
97101
.catch(err => {
98102
throw err;
99-
})
103+
});
100104
}
101105

102106
function handleCreateCollection(response: ServerResponse) {
@@ -108,7 +112,7 @@ function handleCreateCollection(response: ServerResponse) {
108112
.catch(err => {
109113
console.log('Error code:', err.code);
110114
response.end(err.message);
111-
});
115+
});
112116
}
113117

114118
function handleNotFound(response: ServerResponse) {

examples/mongodb/src/tracer.ts

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,20 @@
1-
import * as api from "@opentelemetry/api";
1+
import * as api from '@opentelemetry/api';
22

33
import { NodeTracerProvider } from '@opentelemetry/sdk-trace-node';
44
import { Resource } from '@opentelemetry/resources';
55
import { SimpleSpanProcessor } from '@opentelemetry/sdk-trace-base';
6-
import { JaegerExporter } from '@opentelemetry/exporter-jaeger';
6+
import { OTLPTraceExporter } from '@opentelemetry/exporter-trace-otlp-http';
77
import { ZipkinExporter } from '@opentelemetry/exporter-zipkin';
88
import { registerInstrumentations } from '@opentelemetry/instrumentation';
99
import { HttpInstrumentation } from '@opentelemetry/instrumentation-http';
1010
import { MongoDBInstrumentation } from '@opentelemetry/instrumentation-mongodb';
11-
import { SEMRESATTRS_SERVICE_NAME } from '@opentelemetry/semantic-conventions';
12-
11+
import { ATTR_SERVICE_NAME } from '@opentelemetry/semantic-conventions';
1312

1413
export const setupTracing = (serviceName: string): api.Tracer => {
1514
const provider = new NodeTracerProvider({
1615
resource: new Resource({
17-
[SEMRESATTRS_SERVICE_NAME]: serviceName
18-
})
16+
[ATTR_SERVICE_NAME]: serviceName,
17+
}),
1918
});
2019

2120
// Initialize the OpenTelemetry APIs to use the NodeTracerProvider bindings
@@ -32,7 +31,7 @@ export const setupTracing = (serviceName: string): api.Tracer => {
3231
});
3332

3433
provider.addSpanProcessor(new SimpleSpanProcessor(new ZipkinExporter()));
35-
provider.addSpanProcessor(new SimpleSpanProcessor(new JaegerExporter()));
34+
provider.addSpanProcessor(new SimpleSpanProcessor(new OTLPTraceExporter()));
3635

3736
return api.trace.getTracer('mongodb-example');
3837
};

examples/web/.npmrc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
package-lock=false

examples/web/README.md

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,26 @@ This example shows how to use [@opentelemetry/sdk-trace-web][] with different in
99
npm install
1010
```
1111

12+
## Start a collector and trace viewer
13+
14+
Optionally, you can use the provided Docker Compose file to start an OpenTelemetry Collector and a Zipkin to view collected traces.
15+
You can skip this step if you have your own collector already setup.
16+
17+
```sh
18+
npm run docker:start
19+
```
20+
1221
## Run the Application
1322

1423
```sh
1524
# from this directory
1625
npm start
1726
```
1827

19-
By default, the application will run on port `8090`.
28+
- Open the application at <http://localhost:8090>.
29+
- Click around in each of the example sub-paths to create some tracing data.
30+
- Open Zipkin at <http://127.0.0.1:9411/zipkin/> and search for some traces (click "Run Query").
31+
2032

2133
## More information
2234

0 commit comments

Comments
 (0)