From 2c1e812a1773c704fc7813c6d5cbee7129740225 Mon Sep 17 00:00:00 2001 From: romil-punetha Date: Thu, 14 May 2020 23:55:25 +0530 Subject: [PATCH 01/10] feat(opentelemetry-js): add enabling db query params --- .idea/encodings.xml | 4 + .idea/inspectionProfiles/Project_Default.xml | 7 + .idea/misc.xml | 6 + .idea/modules.xml | 8 + .idea/opentelemetry-js-contrib.iml | 12 + .idea/vcs.xml | 6 + .idea/workspace.xml | 636 ++++++++++++++++++ RELEASING.md | 2 +- package.json | 2 +- .../src/mongodb.ts | 10 +- .../test/mongodb.test.ts | 30 +- .../test/utils.ts | 12 +- 12 files changed, 726 insertions(+), 9 deletions(-) create mode 100644 .idea/encodings.xml create mode 100644 .idea/inspectionProfiles/Project_Default.xml create mode 100644 .idea/misc.xml create mode 100644 .idea/modules.xml create mode 100644 .idea/opentelemetry-js-contrib.iml create mode 100644 .idea/vcs.xml create mode 100644 .idea/workspace.xml diff --git a/.idea/encodings.xml b/.idea/encodings.xml new file mode 100644 index 00000000000..15a15b218a2 --- /dev/null +++ b/.idea/encodings.xml @@ -0,0 +1,4 @@ + + + + \ No newline at end of file diff --git a/.idea/inspectionProfiles/Project_Default.xml b/.idea/inspectionProfiles/Project_Default.xml new file mode 100644 index 00000000000..9c69411050e --- /dev/null +++ b/.idea/inspectionProfiles/Project_Default.xml @@ -0,0 +1,7 @@ + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 00000000000..28a804d8932 --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,6 @@ + + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 00000000000..0f17fe62c81 --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/opentelemetry-js-contrib.iml b/.idea/opentelemetry-js-contrib.iml new file mode 100644 index 00000000000..24643cc3744 --- /dev/null +++ b/.idea/opentelemetry-js-contrib.iml @@ -0,0 +1,12 @@ + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 00000000000..94a25f7f4cb --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/.idea/workspace.xml b/.idea/workspace.xml new file mode 100644 index 00000000000..a1fdcb4faa5 --- /dev/null +++ b/.idea/workspace.xml @@ -0,0 +1,636 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + enhancedDatabaseReporting + '?' + is + plugin + plugin.en + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + - - - - - - - - - - - - - - - - - - - - - - - - - - - project - - $PROJECT_DIR$/plugins/node/opentelemetry-plugin-mongodb/node_modules/mocha - $PROJECT_DIR$ - true - bdd - - SUITE - $PROJECT_DIR$/plugins/node/opentelemetry-plugin-mongodb/test/mongodb.test.ts - - - - - - - project - - $PROJECT_DIR$/plugins/node/opentelemetry-plugin-mongodb/node_modules/mocha - $PROJECT_DIR$ - true - bdd - - TEST - $PROJECT_DIR$/plugins/node/opentelemetry-plugin-mongodb/test/mongodb.test.ts - - - - - - - - - - - - - - - - - - - - - 1589264379132 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - \ No newline at end of file From 333ac7aaecc03ebf7bcd695029746883ea370aea Mon Sep 17 00:00:00 2001 From: romil-punetha Date: Fri, 15 May 2020 00:00:00 +0530 Subject: [PATCH 03/10] fix: lint --- plugins/node/opentelemetry-plugin-mongodb/src/mongodb.ts | 5 ++++- .../node/opentelemetry-plugin-mongodb/test/mongodb.test.ts | 7 +++---- plugins/node/opentelemetry-plugin-mongodb/test/utils.ts | 2 +- 3 files changed, 8 insertions(+), 6 deletions(-) diff --git a/plugins/node/opentelemetry-plugin-mongodb/src/mongodb.ts b/plugins/node/opentelemetry-plugin-mongodb/src/mongodb.ts index c8dfa1e6291..f05c0ea3d01 100644 --- a/plugins/node/opentelemetry-plugin-mongodb/src/mongodb.ts +++ b/plugins/node/opentelemetry-plugin-mongodb/src/mongodb.ts @@ -200,7 +200,10 @@ export class MongoDBPlugin extends BasePlugin { {} as { [key: string]: unknown } ); - const isEnhancedDatabaseReportingEnabled = this._config !== undefined && this._config.enhancedDatabaseReporting !== undefined && this._config.enhancedDatabaseReporting; + const isEnhancedDatabaseReportingEnabled = + this._config !== undefined && + this._config.enhancedDatabaseReporting !== undefined && + this._config.enhancedDatabaseReporting; if (isEnhancedDatabaseReportingEnabled) { query = command.query ?? command.q ?? command; } diff --git a/plugins/node/opentelemetry-plugin-mongodb/test/mongodb.test.ts b/plugins/node/opentelemetry-plugin-mongodb/test/mongodb.test.ts index 0c5fee494db..3b0a37cd87e 100644 --- a/plugins/node/opentelemetry-plugin-mongodb/test/mongodb.test.ts +++ b/plugins/node/opentelemetry-plugin-mongodb/test/mongodb.test.ts @@ -14,7 +14,7 @@ * limitations under the License. */ -import {context, PluginConfig, SpanKind} from '@opentelemetry/api'; +import { context, PluginConfig, SpanKind } from '@opentelemetry/api'; import { NoopLogger } from '@opentelemetry/core'; import { BasicTracerProvider } from '@opentelemetry/tracing'; import { AsyncHooksContextManager } from '@opentelemetry/context-async-hooks'; @@ -46,7 +46,7 @@ describe('MongoDBPlugin', () => { let client: mongodb.MongoClient; let collection: mongodb.Collection; const logger = new NoopLogger(); - const enhancedDbConfig: PluginConfig = {enhancedDatabaseReporting: true}; + const enhancedDbConfig: PluginConfig = { enhancedDatabaseReporting: true }; const provider = new BasicTracerProvider(); const memoryExporter = new InMemorySpanExporter(); const spanProcessor = new SimpleSpanProcessor(memoryExporter); @@ -240,13 +240,12 @@ describe('MongoDBPlugin', () => { it('should not create a child span for command', done => { const span = provider.getTracer('default').startSpan('indexRootSpan'); - collection.createIndex({a: 1}, (err, result) => { + collection.createIndex({ a: 1 }, (err, result) => { span.end(); assert.ifError(err); assert.strictEqual(memoryExporter.getFinishedSpans().length, 1); done(); }); }); - }); }); diff --git a/plugins/node/opentelemetry-plugin-mongodb/test/utils.ts b/plugins/node/opentelemetry-plugin-mongodb/test/utils.ts index 2f5bb3d425e..4734d2155a1 100644 --- a/plugins/node/opentelemetry-plugin-mongodb/test/utils.ts +++ b/plugins/node/opentelemetry-plugin-mongodb/test/utils.ts @@ -85,7 +85,7 @@ export function assertSpans( if (isEnhancedDatabaseReportingEnabled) { const dbStatement = mongoSpan.attributes[AttributeNames.DB_STATEMENT]; for (const key in dbStatement) { - assert.notStrictEqual(dbStatement[key], '?'); + assert.notStrictEqual(dbStatement[key], '?'); } } } From 8cc306308d53eb79cfc9d84c1ccba523997be12f Mon Sep 17 00:00:00 2001 From: romil-punetha Date: Fri, 15 May 2020 00:35:53 +0530 Subject: [PATCH 04/10] fix: pr review changes --- RELEASING.md | 2 +- .../src/mongodb.ts | 23 ++++++++----------- 2 files changed, 10 insertions(+), 15 deletions(-) diff --git a/RELEASING.md b/RELEASING.md index 82bfea7fa47..68b2f6c9567 100644 --- a/RELEASING.md +++ b/RELEASING.md @@ -1,4 +1,4 @@ -s# Releasing OpenTelemetry Packages (for Maintainers Only) +# Releasing OpenTelemetry Packages (for Maintainers Only) This document explains how to publish all OT modules at version x.y.z. Ensure that you’re following semver when choosing a version number. diff --git a/plugins/node/opentelemetry-plugin-mongodb/src/mongodb.ts b/plugins/node/opentelemetry-plugin-mongodb/src/mongodb.ts index f05c0ea3d01..1f6fd79f62d 100644 --- a/plugins/node/opentelemetry-plugin-mongodb/src/mongodb.ts +++ b/plugins/node/opentelemetry-plugin-mongodb/src/mongodb.ts @@ -192,21 +192,16 @@ export class MongoDBPlugin extends BasePlugin { }); if (command === undefined) return; - let query = Object.keys(command.query ?? command.q ?? command).reduce( - (obj, key) => { - obj[key] = '?'; - return obj; - }, - {} as { [key: string]: unknown } - ); - const isEnhancedDatabaseReportingEnabled = - this._config !== undefined && - this._config.enhancedDatabaseReporting !== undefined && - this._config.enhancedDatabaseReporting; - if (isEnhancedDatabaseReportingEnabled) { - query = command.query ?? command.q ?? command; - } + const query = this._config?.enhancedDatabaseReporting + ? command.query ?? command.q ?? command + : Object.keys(command.query ?? command.q ?? command).reduce( + (obj, key) => { + obj[key] = '?'; + return obj; + }, + {} as { [key: string]: unknown } + ); span.setAttribute('db.statement', JSON.stringify(query)); } From cc448728f95b275765d3504cbd7a4565ffd093a1 Mon Sep 17 00:00:00 2001 From: romil-punetha Date: Sat, 16 May 2020 02:35:07 +0530 Subject: [PATCH 05/10] docs: add readme for mongo example --- .circleci/config.yml | 2 +- examples/mongodb/README.md | 74 +++++++++++++++++++++++++ examples/mongodb/client.js | 73 ++++++++++++++++++++++++ examples/mongodb/package.json | 44 +++++++++++++++ examples/mongodb/server.js | 101 ++++++++++++++++++++++++++++++++++ examples/mongodb/tracer.js | 35 ++++++++++++ 6 files changed, 328 insertions(+), 1 deletion(-) create mode 100644 examples/mongodb/README.md create mode 100644 examples/mongodb/client.js create mode 100644 examples/mongodb/package.json create mode 100644 examples/mongodb/server.js create mode 100644 examples/mongodb/tracer.js diff --git a/.circleci/config.yml b/.circleci/config.yml index 5808e5ae6c1..faf28857bc0 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -29,7 +29,7 @@ redis_service: &redis_service image: redis mongo_service: &mongo_service - image: mongo + image: mongodb mysql_service: &mysql_service image: circleci/mysql:5.7 diff --git a/examples/mongodb/README.md b/examples/mongodb/README.md new file mode 100644 index 00000000000..837a5c014dd --- /dev/null +++ b/examples/mongodb/README.md @@ -0,0 +1,74 @@ +# Overview + +OpenTelemetry Mongodb 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. + +This is a modification of the HTTP example that executes multiple parallel requests that interact with a Mongodb server backend using the `mongo` npm module. The example displays traces using multiple connection methods. +- Create Collection Query +- Insert Document Query +- Fetch All Documents Query + + +## Installation + +```sh +$ # from this directory +$ 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) + +## Run the Application + +### Zipkin + + - Run the server + + ```sh + $ # from this directory + $ npm run server + ``` + + - Run the client + + ```sh + $ # from this directory + $ npm run client + ``` + +#### Zipkin UI +`server` script should output the `traceid` in the terminal (e.g `traceid: 4815c3d576d930189725f1f1d1bdfcc6`). +Go to Zipkin with your browser [http://localhost:9411/zipkin/traces/(your-trace-id)]() (e.g http://localhost:9411/zipkin/traces/4815c3d576d930189725f1f1d1bdfcc6) + +

+ +### Jaeger + + - Run the server + + ```sh + $ # from this directory + $ npm run server + ``` + + - Run the client + + ```sh + $ # from this directory + $ npm run client + ``` +#### Jaeger UI + +`server` script should output the `traceid` in the terminal (e.g `traceid: 4815c3d576d930189725f1f1d1bdfcc6`). +Go to Jaeger with your browser [http://localhost:16686/trace/(your-trace-id)]() (e.g http://localhost:16686/trace/4815c3d576d930189725f1f1d1bdfcc6) + +

+ +## Useful links +- For more information on OpenTelemetry, visit: +- For more information on OpenTelemetry for Node.js, visit: + +## LICENSE + +Apache License 2.0 diff --git a/examples/mongodb/client.js b/examples/mongodb/client.js new file mode 100644 index 00000000000..428209a5811 --- /dev/null +++ b/examples/mongodb/client.js @@ -0,0 +1,73 @@ +'use strict'; + +const tracer = require('./tracer')('example-mongodb-http-client'); +// eslint-disable-next-line import/order +const http = require('http'); + +/** A function which makes requests and handles response. */ +function makeRequest() { + // span corresponds to outgoing requests. Here, we have manually created + // the span, which is created to track work that happens outside of the + // request lifecycle entirely. + const span = tracer.startSpan('makeRequest'); + + let queries = 0; + let responses = 0; + + tracer.withSpan(span, () => { + queries += 1; + http.get({ + host: 'localhost', + port: 8080, + path: '/collection/', + }, (response) => { + const body = []; + response.on('data', (chunk) => body.push(chunk)); + response.on('end', () => { + responses += 1; + console.log(body.toString()); + if (responses === queries) span.end(); + }); + }); + }); + tracer.withSpan(span, () => { + queries += 1; + http.get({ + host: 'localhost', + port: 8080, + path: '/insert/', + }, (response) => { + const body = []; + response.on('data', (chunk) => body.push(chunk)); + response.on('end', () => { + responses += 1; + console.log(body.toString()); + if (responses === queries) span.end(); + }); + }); + }); + tracer.withSpan(span, () => { + queries += 1; + http.get({ + host: 'localhost', + port: 8080, + path: '/get/', + }, (response) => { + const body = []; + response.on('data', (chunk) => body.push(chunk)); + response.on('end', () => { + responses += 1; + console.log(body.toString()); + if (responses === queries) span.end(); + }); + }); + }); + + // The process must live for at least the interval past any traces that + // must be exported, or some risk being lost if they are recorded after the + // last export. + console.log('Sleeping 5 seconds before shutdown to ensure all records are flushed.'); + setTimeout(() => { console.log('Completed.'); }, 5000); +} + +makeRequest(); diff --git a/examples/mongodb/package.json b/examples/mongodb/package.json new file mode 100644 index 00000000000..7828822bb73 --- /dev/null +++ b/examples/mongodb/package.json @@ -0,0 +1,44 @@ +{ + "name": "mongodb-example", + "private": true, + "version": "0.7.0", + "description": "Example of mongodb integration with OpenTelemetry", + "main": "index.js", + "scripts": { + "zipkin:server": "cross-env EXPORTER=zipkin node ./server.js", + "zipkin:client": "cross-env EXPORTER=zipkin node ./client.js", + "jaeger:server": "cross-env EXPORTER=jaeger node ./server.js", + "jaeger:client": "cross-env EXPORTER=jaeger node ./client.js" + }, + "repository": { + "type": "git", + "url": "git+ssh://git@github.com/open-telemetry/opentelemetry-js.git" + }, + "keywords": [ + "opentelemetry", + "mongodb", + "tracing" + ], + "engines": { + "node": ">=8" + }, + "author": "OpenTelemetry Authors", + "license": "Apache-2.0", + "bugs": { + "url": "https://github.com/open-telemetry/opentelemetry-js/issues" + }, + "dependencies": { + "@opentelemetry/api": "^0.7.0", + "@opentelemetry/exporter-jaeger": "^0.7.0", + "@opentelemetry/exporter-zipkin": "^0.7.0", + "@opentelemetry/node": "^0.7.0", + "@opentelemetry/plugin-http": "^0.7.0", + "@opentelemetry/plugin-mongodb": "^0.7.0", + "@opentelemetry/tracing": "^0.7.0", + "mongodb": "^3.5.7" + }, + "homepage": "https://github.com/open-telemetry/opentelemetry-js#readme", + "devDependencies": { + "cross-env": "^6.0.0" + } +} diff --git a/examples/mongodb/server.js b/examples/mongodb/server.js new file mode 100644 index 00000000000..9fab80eabad --- /dev/null +++ b/examples/mongodb/server.js @@ -0,0 +1,101 @@ +'use strict'; + +// eslint-disable-next-line import/order +const tracer = require('./tracer')('example-mongodb-http-server'); +const MongoClient = require('mongodb').MongoClient; +const http = require('http'); +const url = "mongodb://localhost:27017/mydb"; +let db; + +/** Starts a HTTP server that receives requests on sample server port. */ +function startServer(port) { + + // Connect to db + MongoClient.connect(url, function(err, database) { + if(err) throw err; + db = database.db("mydb"); + }); + // Creates a server + const server = http.createServer(handleRequest); + // Starts the server + server.listen(port, (err) => { + if (err) { + throw err; + } + console.log(`Node HTTP listening on ${port}`); + }); +} + +/** A function which handles requests and send response. */ +function handleRequest(request, response) { + + const currentSpan = tracer.getCurrentSpan(); + // display traceid in the terminal + const { traceId } = currentSpan.context(); + console.log(`traceid: ${traceId}`); + console.log(`Jaeger URL: http://localhost:16686/trace/${traceId}`); + console.log(`Zipkin URL: http://localhost:9411/zipkin/traces/${traceId}`); + try { + const body = []; + request.on('error', (err) => console.log(err)); + request.on('data', (chunk) => body.push(chunk)); + request.on('end', async () => { + if (request.url === '/collection/') { + handleCreateCollection(response); + } else if (request.url === '/insert/') { + handleInsertQuery(response); + } else if (request.url === '/get/') { + handleGetQuery(response); + } else { + handleNotFound(response); + } + }); + } catch (err) { + console.log(err); + } +} + +startServer(8080); + +function handleInsertQuery(response) { + const obj = { name: "John", age: "20" }; + db.collection("users").insertOne(obj, function(err, res) { + if (err) { + console.log('Error code:', err.code); + response.end(err.message); + } else { + console.log("1 document inserted"); + response.end(); + } + }); + +} + +function handleGetQuery(response) { + db.collection("users").find({}, function(err, res) { + if (err) { + console.log('Error code:', err.code); + response.end(err.message); + } else { + console.log("1 document served"); + response.end(); + } + }); + +} + +function handleCreateCollection(response) { + db.createCollection("users", function(err, res) { + if (err) { + console.log('Error code:', err.code); + response.end(err.message); + } else { + console.log("1 collection created"); + response.end(); + } + }); +} + +function handleNotFound(response) { + response.end('not found'); +} diff --git a/examples/mongodb/tracer.js b/examples/mongodb/tracer.js new file mode 100644 index 00000000000..422f8b1cb81 --- /dev/null +++ b/examples/mongodb/tracer.js @@ -0,0 +1,35 @@ +'use strict'; + +const opentelemetry = require('@opentelemetry/api'); +const { NodeTracerProvider } = require('@opentelemetry/node'); +const { SimpleSpanProcessor } = require('@opentelemetry/tracing'); +const { JaegerExporter } = require('@opentelemetry/exporter-jaeger'); +const { ZipkinExporter } = require('@opentelemetry/exporter-zipkin'); + +module.exports = (serviceName) => { + const provider = new NodeTracerProvider({ + plugins: { + mongodb: { + enabled: true, + path: "@opentelemetry/plugin-mongodb", + enhancedDatabaseReporting: true + }, + http: { + enabled: true, + path: '@opentelemetry/plugin-http', + }, + }, + }); + + provider.addSpanProcessor(new SimpleSpanProcessor(new ZipkinExporter({ + serviceName, + }))); + provider.addSpanProcessor(new SimpleSpanProcessor(new JaegerExporter({ + serviceName, + }))); + + // Initialize the OpenTelemetry APIs to use the NodeTracerProvider bindings + provider.register(); + + return opentelemetry.trace.getTracer('mysql-example'); +}; From 1a3f126e156ee6b4ff48f2100bef3a5f183b09e5 Mon Sep 17 00:00:00 2001 From: Romil Punetha <47735999+romil-punetha@users.noreply.github.com> Date: Sat, 16 May 2020 03:03:13 +0530 Subject: [PATCH 06/10] fix: revert mongo image for tests --- .circleci/config.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.circleci/config.yml b/.circleci/config.yml index faf28857bc0..5808e5ae6c1 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -29,7 +29,7 @@ redis_service: &redis_service image: redis mongo_service: &mongo_service - image: mongodb + image: mongo mysql_service: &mysql_service image: circleci/mysql:5.7 From 7d35b4d51268ae62f149d972591020e8b994ae42 Mon Sep 17 00:00:00 2001 From: romil-punetha Date: Sat, 16 May 2020 03:18:52 +0530 Subject: [PATCH 07/10] docs: add comment and update readme.md --- examples/mongodb/README.md | 2 ++ plugins/node/opentelemetry-plugin-mongodb/src/mongodb.ts | 1 + 2 files changed, 3 insertions(+) diff --git a/examples/mongodb/README.md b/examples/mongodb/README.md index 837a5c014dd..b9673dcad42 100644 --- a/examples/mongodb/README.md +++ b/examples/mongodb/README.md @@ -7,6 +7,8 @@ This is a modification of the HTTP example that executes multiple parallel reque - Insert Document Query - Fetch All Documents Query +This example runs with the `enhancedDatabaseReporting` flag set to `true` in the `mongodb` plugin config. This allows for capturing parameters within the queries instead of obfuscating them. + ## Installation diff --git a/plugins/node/opentelemetry-plugin-mongodb/src/mongodb.ts b/plugins/node/opentelemetry-plugin-mongodb/src/mongodb.ts index 1f6fd79f62d..bbc03d1e88d 100644 --- a/plugins/node/opentelemetry-plugin-mongodb/src/mongodb.ts +++ b/plugins/node/opentelemetry-plugin-mongodb/src/mongodb.ts @@ -193,6 +193,7 @@ export class MongoDBPlugin extends BasePlugin { if (command === undefined) return; + // capture parameters within the query as well if enhancedDatabaseReporting is enabled. const query = this._config?.enhancedDatabaseReporting ? command.query ?? command.q ?? command : Object.keys(command.query ?? command.q ?? command).reduce( From 9cda0aeca7544626ae4c0e5fe105b4699ef89a4d Mon Sep 17 00:00:00 2001 From: romil-punetha Date: Sun, 17 May 2020 20:11:06 +0530 Subject: [PATCH 08/10] docs: updated readme as per http plugin's --- examples/mongodb/README.md | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/examples/mongodb/README.md b/examples/mongodb/README.md index b9673dcad42..f285244e323 100644 --- a/examples/mongodb/README.md +++ b/examples/mongodb/README.md @@ -7,7 +7,19 @@ This is a modification of the HTTP example that executes multiple parallel reque - Insert Document Query - Fetch All Documents Query -This example runs with the `enhancedDatabaseReporting` flag set to `true` in the `mongodb` plugin config. This allows for capturing parameters within the queries instead of obfuscating them. +### Http Plugin Options + +Mongodb plugin has few options available to choose from. You can set the following: + +| Options | Type | Description | +| ------- | ---- | ----------- | +| [`enabled`](https://github.com/open-telemetry/opentelemetry-js/blob/master/packages/opentelemetry-api/src/trace/instrumentation/Plugin.ts#L60) | `Boolean` | Whether to enable the plugin | +| [`path`](https://github.com/open-telemetry/opentelemetry-js/blob/master/packages/opentelemetry-api/src/trace/instrumentation/Plugin.ts#L66) | `String` | Path of the trace plugin to load. | +| [`ignoreMethods`](https://github.com/open-telemetry/opentelemetry-js/blob/master/packages/opentelemetry-api/src/trace/instrumentation/Plugin.ts#L71) | `String[]` | Request methods that match any string in ignoreMethods will not be traced | +| [`ignoreUrls`](https://github.com/open-telemetry/opentelemetry-js/blob/master/packages/opentelemetry-api/src/trace/instrumentation/Plugin.ts#L78) | Array | URLs that partially match any regex or exactly match the strings in ignoreUrls will not be traced | +| [`internalFilesExports`](https://github.com/open-telemetry/opentelemetry-js/blob/master/packages/opentelemetry-api/src/trace/instrumentation/Plugin.ts#L84) | `PluginInternalFiles` | List of internal files that need patch and are not exported by default | +| [`enhancedDatabaseReporting`](https://github.com/open-telemetry/opentelemetry-js/blob/master/packages/opentelemetry-api/src/trace/instrumentation/Plugin.ts#L91) | `string` | If true, additional information about query parameters and results will be attached (as `attributes`) to spans representing database operations | + ## Installation From de9adcf8d79b17b787b9809920d9aa7af3f522af Mon Sep 17 00:00:00 2001 From: romil-punetha Date: Sun, 17 May 2020 21:36:37 +0530 Subject: [PATCH 09/10] fix: readme for mongo plugin and example --- examples/mongodb/README.md | 16 +--------------- .../node/opentelemetry-plugin-mongodb/README.md | 11 +++++++++++ 2 files changed, 12 insertions(+), 15 deletions(-) diff --git a/examples/mongodb/README.md b/examples/mongodb/README.md index f285244e323..9958995d4d6 100644 --- a/examples/mongodb/README.md +++ b/examples/mongodb/README.md @@ -2,25 +2,11 @@ OpenTelemetry Mongodb 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. -This is a modification of the HTTP example that executes multiple parallel requests that interact with a Mongodb server backend using the `mongo` npm module. The example displays traces using multiple connection methods. +This is a modification of the Mongo example that executes multiple parallel requests that interact with a Mongodb server backend using the `mongo` npm module. The example displays traces using multiple connection methods. - Create Collection Query - Insert Document Query - Fetch All Documents Query -### Http Plugin Options - -Mongodb plugin has few options available to choose from. You can set the following: - -| Options | Type | Description | -| ------- | ---- | ----------- | -| [`enabled`](https://github.com/open-telemetry/opentelemetry-js/blob/master/packages/opentelemetry-api/src/trace/instrumentation/Plugin.ts#L60) | `Boolean` | Whether to enable the plugin | -| [`path`](https://github.com/open-telemetry/opentelemetry-js/blob/master/packages/opentelemetry-api/src/trace/instrumentation/Plugin.ts#L66) | `String` | Path of the trace plugin to load. | -| [`ignoreMethods`](https://github.com/open-telemetry/opentelemetry-js/blob/master/packages/opentelemetry-api/src/trace/instrumentation/Plugin.ts#L71) | `String[]` | Request methods that match any string in ignoreMethods will not be traced | -| [`ignoreUrls`](https://github.com/open-telemetry/opentelemetry-js/blob/master/packages/opentelemetry-api/src/trace/instrumentation/Plugin.ts#L78) | Array | URLs that partially match any regex or exactly match the strings in ignoreUrls will not be traced | -| [`internalFilesExports`](https://github.com/open-telemetry/opentelemetry-js/blob/master/packages/opentelemetry-api/src/trace/instrumentation/Plugin.ts#L84) | `PluginInternalFiles` | List of internal files that need patch and are not exported by default | -| [`enhancedDatabaseReporting`](https://github.com/open-telemetry/opentelemetry-js/blob/master/packages/opentelemetry-api/src/trace/instrumentation/Plugin.ts#L91) | `string` | If true, additional information about query parameters and results will be attached (as `attributes`) to spans representing database operations | - - ## Installation diff --git a/plugins/node/opentelemetry-plugin-mongodb/README.md b/plugins/node/opentelemetry-plugin-mongodb/README.md index d10ba0c68a2..d66ef7fe7ef 100644 --- a/plugins/node/opentelemetry-plugin-mongodb/README.md +++ b/plugins/node/opentelemetry-plugin-mongodb/README.md @@ -45,6 +45,17 @@ const provider = new NodeTracerProvider(); See [examples/mongodb](https://github.com/open-telemetry/opentelemetry-js/tree/master/examples/mongodb) for a short example. +### Mongo Plugin Options + +Mongodb plugin has few options available to choose from. You can set the following: + +| Options | Type | Description | +| ------- | ---- | ----------- | +| [`enhancedDatabaseReporting`](https://github.com/open-telemetry/opentelemetry-js/blob/master/packages/opentelemetry-api/src/trace/instrumentation/Plugin.ts#L91) | `string` | If true, additional information about query parameters and results will be attached (as `attributes`) to spans representing database operations | + + + + ## Useful links - For more information on OpenTelemetry, visit: - For more about OpenTelemetry JavaScript: From 80bed87daffe5561efdfeb02b837a8f98ad23581 Mon Sep 17 00:00:00 2001 From: romil-punetha Date: Mon, 18 May 2020 19:08:15 +0530 Subject: [PATCH 10/10] chore: code refactor --- .../opentelemetry-plugin-mongodb/src/mongodb.ts | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/plugins/node/opentelemetry-plugin-mongodb/src/mongodb.ts b/plugins/node/opentelemetry-plugin-mongodb/src/mongodb.ts index bbc03d1e88d..01ecaa9511b 100644 --- a/plugins/node/opentelemetry-plugin-mongodb/src/mongodb.ts +++ b/plugins/node/opentelemetry-plugin-mongodb/src/mongodb.ts @@ -194,15 +194,14 @@ export class MongoDBPlugin extends BasePlugin { if (command === undefined) return; // capture parameters within the query as well if enhancedDatabaseReporting is enabled. - const query = this._config?.enhancedDatabaseReporting - ? command.query ?? command.q ?? command - : Object.keys(command.query ?? command.q ?? command).reduce( - (obj, key) => { + const commandObj = command.query ?? command.q ?? command; + const query = + this._config?.enhancedDatabaseReporting === true + ? commandObj + : Object.keys(commandObj).reduce((obj, key) => { obj[key] = '?'; return obj; - }, - {} as { [key: string]: unknown } - ); + }, {} as { [key: string]: unknown }); span.setAttribute('db.statement', JSON.stringify(query)); }