Skip to content

Commit

Permalink
chore(review): fix CI tests + use enum for attributes names
Browse files Browse the repository at this point in the history
  • Loading branch information
vmarchaud committed Nov 15, 2019
1 parent 7f203f4 commit a95c2cc
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 21 deletions.
3 changes: 3 additions & 0 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
38 changes: 28 additions & 10 deletions packages/opentelemetry-plugin-mongodb-core/src/mongodb.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down Expand Up @@ -72,10 +77,14 @@ export class MongoDBPlugin extends BasePlugin<typeof mongodb> {

/** 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 */
Expand Down Expand Up @@ -167,13 +176,18 @@ export class MongoDBPlugin extends BasePlugin<typeof mongodb> {
) {
// 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) => {
Expand Down Expand Up @@ -226,6 +240,10 @@ export class MongoDBPlugin extends BasePlugin<typeof mongodb> {
code: CanonicalCode.UNKNOWN,
message: error.message,
});
} else {
span.setStatus({
code: CanonicalCode.OK,
});
}
span.end();
return resultHandler.apply(this, (arguments as unknown) as unknown[]);
Expand Down
15 changes: 15 additions & 0 deletions packages/opentelemetry-plugin-mongodb-core/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
}
29 changes: 20 additions & 9 deletions packages/opentelemetry-plugin-mongodb-core/test/mongodb.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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;
Expand Down
3 changes: 1 addition & 2 deletions packages/opentelemetry-plugin-mongodb-core/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
"outDir": "build"
},
"include": [
"src/**/*.ts",
"test/**/*.ts"
"src/**/*.ts"
]
}

0 comments on commit a95c2cc

Please sign in to comment.