diff --git a/.circleci/config.yml b/.circleci/config.yml index c232290f08a..26bc841f6f7 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 f0f81f8f9a0..907e59c0ea5 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'; @@ -167,13 +172,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 +236,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 f76cd663ab4..4763f662d5c 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 365a089572a..522532a0b24 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 a2042cd68b1..3e83278f6c2 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" ] }