From 0fc1ed316f097817af1ac00a3d2820adc834f7c9 Mon Sep 17 00:00:00 2001 From: David VanLaningham <64915094+dsvanlani@users.noreply.github.com> Date: Thu, 28 Nov 2024 16:56:40 -0500 Subject: [PATCH] chore(examples/mongodb): Update mongodb example dependencies (#2531) --- examples/mongodb/package.json | 21 ++++++++-------- examples/mongodb/src/server.ts | 44 ++++++++++++++++++---------------- examples/mongodb/src/tracer.ts | 13 +++++----- 3 files changed, 40 insertions(+), 38 deletions(-) diff --git a/examples/mongodb/package.json b/examples/mongodb/package.json index fac44e38b9..03d745eeba 100644 --- a/examples/mongodb/package.json +++ b/examples/mongodb/package.json @@ -30,21 +30,20 @@ "url": "https://github.com/open-telemetry/opentelemetry-js-contrib/issues" }, "dependencies": { - "@opentelemetry/api": "^1.0.0", - "@opentelemetry/exporter-jaeger": "^1.0.0", - "@opentelemetry/exporter-zipkin": "^1.0.0", - "@opentelemetry/instrumentation": "^0.48.0", - "@opentelemetry/instrumentation-http": "^0.48.0", - "@opentelemetry/instrumentation-mongodb": "^0.32.0", - "@opentelemetry/sdk-trace-node": "^1.0.0", - "@opentelemetry/sdk-trace-base": "^1.0.0", + "@opentelemetry/api": "^1.9.0", + "@opentelemetry/exporter-zipkin": "^1.27.0", + "@opentelemetry/instrumentation": "^0.54.2", + "@opentelemetry/instrumentation-http": "^0.54.2", + "@opentelemetry/instrumentation-mongodb": "^0.48.0", + "@opentelemetry/sdk-trace-node": "^1.27.0", + "@opentelemetry/sdk-trace-base": "^1.27.0", "@opentelemetry/semantic-conventions": "^1.27.0", - "mongodb": "^3.6.11" + "mongodb": "^6.10.0" }, "homepage": "https://github.com/open-telemetry/opentelemetry-js-contrib#readme", "devDependencies": { "cross-env": "^7.0.3", - "ts-node": "^10.6.0", - "typescript": "4.4.4" + "ts-node": "^10.9.2", + "typescript": "5.6.3" } } diff --git a/examples/mongodb/src/server.ts b/examples/mongodb/src/server.ts index b308fdc934..aa16cfa1e6 100644 --- a/examples/mongodb/src/server.ts +++ b/examples/mongodb/src/server.ts @@ -2,17 +2,17 @@ import * as api from '@opentelemetry/api'; import { setupTracing } from './tracer'; -setupTracing('example-mongodb-server') +setupTracing('example-mongodb-server'); import { accessDB } from './utils'; import * as http from 'http'; import { IncomingMessage, ServerResponse } from 'http'; import * as mongodb from 'mongodb'; -import {Collection} from "mongodb"; +import { Collection } from 'mongodb'; -const DB_NAME = 'mydb' -const COLLECTION_NAME = 'users' +const DB_NAME = 'mydb'; +const COLLECTION_NAME = 'users'; const URL = `mongodb://localhost:27017/${DB_NAME}`; let db: mongodb.Db; @@ -28,7 +28,6 @@ function startServer(port: number) { throw err; }); - // Creates a server const server = http.createServer(handleRequest); // Starts the server @@ -40,16 +39,20 @@ function startServer(port: number) { /** A function which handles requests and send response. */ function handleRequest(request: IncomingMessage, response: ServerResponse) { const currentSpan = api.trace.getSpan(api.context.active()); - // display traceID in the terminal - const traceId = currentSpan?.spanContext(); - console.log(`traceid: ${traceId}`); - console.log(`Jaeger URL: http://localhost:16686/trace/${traceId}`); - console.log(`Zipkin URL: http://localhost:9411/zipkin/traces/${traceId}`); + if (currentSpan) { + // display traceID in the terminal + const { traceId } = currentSpan?.spanContext(); + console.log(`traceid: ${traceId}`); + console.log(`Jaeger URL: http://localhost:16686/trace/${traceId}`); + console.log(`Zipkin URL: http://localhost:9411/zipkin/traces/${traceId}`); + } else { + console.log('No active span found'); + } try { const body = []; - request.on('error', (err) => console.log(err)); - request.on('data', (chunk) => body.push(chunk)); + request.on('error', err => console.log(err)); + request.on('data', chunk => body.push(chunk)); request.on('end', async () => { if (request.url === '/collection/') { handleCreateCollection(response); @@ -71,12 +74,13 @@ startServer(8080); function handleInsertQuery(response: ServerResponse) { const obj = { name: 'John', age: '20' }; const usersCollection: Collection = db.collection(COLLECTION_NAME); - usersCollection.insertOne(obj) + usersCollection + .insertOne(obj) .then(() => { console.log('1 document inserted'); - // find document to test context propagation using callback - usersCollection.findOne({}, function () { - response.end(); + // find document to test context + usersCollection.findOne({}).then(res => { + console.log(JSON.stringify(res)); }); }) .catch(err => { @@ -87,8 +91,8 @@ function handleInsertQuery(response: ServerResponse) { function handleGetQuery(response: ServerResponse) { const usersCollection: Collection = db.collection(COLLECTION_NAME); - usersCollection. - find({}) + usersCollection + .find({}) .toArray() .then(() => { console.log('1 document served'); @@ -96,7 +100,7 @@ function handleGetQuery(response: ServerResponse) { }) .catch(err => { throw err; - }) + }); } function handleCreateCollection(response: ServerResponse) { @@ -108,7 +112,7 @@ function handleCreateCollection(response: ServerResponse) { .catch(err => { console.log('Error code:', err.code); response.end(err.message); - }); + }); } function handleNotFound(response: ServerResponse) { diff --git a/examples/mongodb/src/tracer.ts b/examples/mongodb/src/tracer.ts index 4e63724503..a9599dec35 100644 --- a/examples/mongodb/src/tracer.ts +++ b/examples/mongodb/src/tracer.ts @@ -1,21 +1,20 @@ -import * as api from "@opentelemetry/api"; +import * as api from '@opentelemetry/api'; import { NodeTracerProvider } from '@opentelemetry/sdk-trace-node'; import { Resource } from '@opentelemetry/resources'; import { SimpleSpanProcessor } from '@opentelemetry/sdk-trace-base'; -import { JaegerExporter } from '@opentelemetry/exporter-jaeger'; +import { OTLPTraceExporter } from '@opentelemetry/exporter-trace-otlp-http'; import { ZipkinExporter } from '@opentelemetry/exporter-zipkin'; import { registerInstrumentations } from '@opentelemetry/instrumentation'; import { HttpInstrumentation } from '@opentelemetry/instrumentation-http'; import { MongoDBInstrumentation } from '@opentelemetry/instrumentation-mongodb'; -import { SEMRESATTRS_SERVICE_NAME } from '@opentelemetry/semantic-conventions'; - +import { ATTR_SERVICE_NAME } from '@opentelemetry/semantic-conventions'; export const setupTracing = (serviceName: string): api.Tracer => { const provider = new NodeTracerProvider({ resource: new Resource({ - [SEMRESATTRS_SERVICE_NAME]: serviceName - }) + [ATTR_SERVICE_NAME]: serviceName, + }), }); // Initialize the OpenTelemetry APIs to use the NodeTracerProvider bindings @@ -32,7 +31,7 @@ export const setupTracing = (serviceName: string): api.Tracer => { }); provider.addSpanProcessor(new SimpleSpanProcessor(new ZipkinExporter())); - provider.addSpanProcessor(new SimpleSpanProcessor(new JaegerExporter())); + provider.addSpanProcessor(new SimpleSpanProcessor(new OTLPTraceExporter())); return api.trace.getTracer('mongodb-example'); };