From a95c2cc342c91643b94b9eb02ee05a8a66b634d9 Mon Sep 17 00:00:00 2001 From: vmarchaud Date: Sun, 3 Nov 2019 18:15:17 +0100 Subject: [PATCH] chore(review): fix CI tests + use enum for attributes names --- .circleci/config.yml | 3 ++ .../src/mongodb.ts | 38 ++++++++++++++----- .../src/types.ts | 15 ++++++++ .../test/mongodb.test.ts | 29 +++++++++----- .../tsconfig.json | 3 +- 5 files changed, 67 insertions(+), 21 deletions(-) diff --git a/.circleci/config.yml b/.circleci/config.yml index dac79c43a2..e0bcbee841 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -7,6 +7,9 @@ test_env: &test_env POSTGRES_DB: circle_database POSTGRES_HOST: localhost POSTGRES_PORT: 5432 + MONGODB_HOST: localhost + MONGODB_PORT: 27017 + MONGODB_DB: opentelemetry-tests postgres_service: &postgres_service image: circleci/postgres:9.6-alpine diff --git a/packages/opentelemetry-plugin-mongodb-core/src/mongodb.ts b/packages/opentelemetry-plugin-mongodb-core/src/mongodb.ts index f0f81f8f9a..070a3c8018 100644 --- a/packages/opentelemetry-plugin-mongodb-core/src/mongodb.ts +++ b/packages/opentelemetry-plugin-mongodb-core/src/mongodb.ts @@ -19,7 +19,12 @@ import { BasePlugin } from '@opentelemetry/core'; import { Span, SpanKind, CanonicalCode } from '@opentelemetry/types'; -import { Func, MongoInternalCommand, MongoInternalTopology } from './types'; +import { + Func, + MongoInternalCommand, + MongoInternalTopology, + AttributeNames, +} from './types'; import * as mongodb from 'mongodb'; import * as shimmer from 'shimmer'; @@ -72,10 +77,14 @@ export class MongoDBPlugin extends BasePlugin { /** Unpatches all MongoDB patched functions. */ unpatch(): void { - shimmer.massUnwrap([this._moduleExports.Server.prototype], this - ._SERVER_METHODS as never[]); - shimmer.massUnwrap([this._moduleExports.Cursor.prototype], this - ._CURSOR_METHODS as never[]); + shimmer.massUnwrap( + [this._moduleExports.Server.prototype], + this._SERVER_METHODS as never[] + ); + shimmer.massUnwrap( + [this._moduleExports.Cursor.prototype], + this._CURSOR_METHODS as never[] + ); } /** Creates spans for Command operations */ @@ -167,13 +176,18 @@ export class MongoDBPlugin extends BasePlugin { ) { // add network attributes to determine the remote server if (topology && topology.s && topology.s.options) { - span.setAttribute('peer.hostname', `${topology.s.options.host}`); - span.setAttribute('peer.port', `${topology.s.options.port}`); + span.setAttributes({ + [AttributeNames.PEER_HOSTNAME]: `${topology.s.options.host}`, + [AttributeNames.PEER_PORT]: `${topology.s.options.port}`, + }); } // add database related attributes - span.setAttribute('db.instance', `${ns}`); - span.setAttribute('db.type', `mongodb`); - span.setAttribute('component', 'mongodb-core'); + span.setAttributes({ + [AttributeNames.DB_INSTANCE]: `${ns}`, + [AttributeNames.DB_TYPE]: `mongodb`, + [AttributeNames.COMPONENT]: 'mongodb-core', + }); + if (command === undefined) return; const query = Object.keys(command.query || command.q || {}).reduce( (obj, key) => { @@ -226,6 +240,10 @@ export class MongoDBPlugin extends BasePlugin { code: CanonicalCode.UNKNOWN, message: error.message, }); + } else { + span.setStatus({ + code: CanonicalCode.OK, + }); } span.end(); return resultHandler.apply(this, (arguments as unknown) as unknown[]); diff --git a/packages/opentelemetry-plugin-mongodb-core/src/types.ts b/packages/opentelemetry-plugin-mongodb-core/src/types.ts index f76cd663ab..4763f662d5 100644 --- a/packages/opentelemetry-plugin-mongodb-core/src/types.ts +++ b/packages/opentelemetry-plugin-mongodb-core/src/types.ts @@ -34,3 +34,18 @@ export type MongoInternalTopology = { }; }; }; + +export enum AttributeNames { + // required by https://github.com/open-telemetry/opentelemetry-specification/blob/master/specification/data-semantic-conventions.md#databases-client-calls + COMPONENT = 'component', + DB_TYPE = 'db.type', + DB_INSTANCE = 'db.instance', + DB_STATEMENT = 'db.statement', + PEER_ADDRESS = 'peer.address', + PEER_HOSTNAME = 'peer.host', + + PEER_PORT = 'peer.port', + PEER_IPV4 = 'peer.ipv4', + PEER_IPV6 = 'peer.ipv6', + PEER_SERVICE = 'peer.service', +} diff --git a/packages/opentelemetry-plugin-mongodb-core/test/mongodb.test.ts b/packages/opentelemetry-plugin-mongodb-core/test/mongodb.test.ts index 365a089572..522532a0b2 100644 --- a/packages/opentelemetry-plugin-mongodb-core/test/mongodb.test.ts +++ b/packages/opentelemetry-plugin-mongodb-core/test/mongodb.test.ts @@ -18,8 +18,9 @@ import { NodeTracer } from '@opentelemetry/node'; import * as assert from 'assert'; import * as mongodb from 'mongodb'; import { plugin } from '../src'; -import { SpanKind } from '@opentelemetry/types'; +import { SpanKind, CanonicalCode } from '@opentelemetry/types'; import { NoopLogger } from '@opentelemetry/core'; +import { AttributeNames } from '../src/types'; import { InMemorySpanExporter, SimpleSpanProcessor, @@ -71,23 +72,33 @@ function assertSpans( assert(span.endTime instanceof Array); assert(span.endTime.length === 2); }); - assert.strictEqual(spans[0].name, expectedName); - assert.strictEqual(spans[0].kind, expectedKind); + const [mongoSpan] = spans; + assert.strictEqual(mongoSpan.name, expectedName); + assert.strictEqual(mongoSpan.kind, expectedKind); + assert.strictEqual( + mongoSpan.attributes[AttributeNames.COMPONENT], + 'mongodb-core' + ); + assert.strictEqual( + mongoSpan.attributes[AttributeNames.PEER_HOSTNAME], + process.env.MONGODB_HOST || 'localhost' + ); + assert.strictEqual(mongoSpan.status.code, CanonicalCode.OK); } describe('MongoDBPlugin', () => { - // For these tests, mongo must be running. Add OPENTELEMETRY_MONGODB_TESTS to run + // For these tests, mongo must be running. Add RUN_MONGODB_TESTS to run // these tests. - const OPENTELEMETRY_MONGODB_TESTS = process.env - .OPENTELEMETRY_MONGODB_TESTS as string; + const RUN_MONGODB_TESTS = process.env.RUN_MONGODB_TESTS as string; let shouldTest = true; - if (!OPENTELEMETRY_MONGODB_TESTS) { + if (!RUN_MONGODB_TESTS) { console.log('Skipping test-mongodb. Run MongoDB to test'); shouldTest = false; } - const URL = 'mongodb://localhost:27017'; - const DB_NAME = 'opentelemetry-tests'; + const URL = `mongodb://${process.env.MONGODB_HOST || 'localhost'}:${process + .env.MONGODB_PORT || '27017'}`; + const DB_NAME = process.env.MONGODB_DB || 'opentelemetry-tests'; const COLLECTION_NAME = 'test'; let client: mongodb.MongoClient; diff --git a/packages/opentelemetry-plugin-mongodb-core/tsconfig.json b/packages/opentelemetry-plugin-mongodb-core/tsconfig.json index a2042cd68b..3e83278f6c 100644 --- a/packages/opentelemetry-plugin-mongodb-core/tsconfig.json +++ b/packages/opentelemetry-plugin-mongodb-core/tsconfig.json @@ -5,7 +5,6 @@ "outDir": "build" }, "include": [ - "src/**/*.ts", - "test/**/*.ts" + "src/**/*.ts" ] }