Skip to content

Commit

Permalink
Merge branch 'master' into fix-express-async-layer
Browse files Browse the repository at this point in the history
  • Loading branch information
vmarchaud authored Sep 1, 2020
2 parents 0071af0 + 2915c43 commit e97aa05
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 29 deletions.
1 change: 1 addition & 0 deletions plugins/node/opentelemetry-plugin-mongodb/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
"devDependencies": {
"@opentelemetry/context-async-hooks": "0.10.2",
"@opentelemetry/node": "0.10.2",
"@opentelemetry/semantic-conventions": "^0.10.2",
"@opentelemetry/tracing": "0.10.2",
"@types/mocha": "7.0.2",
"@types/mongodb": "3.5.25",
Expand Down
27 changes: 17 additions & 10 deletions plugins/node/opentelemetry-plugin-mongodb/src/mongodb.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,26 +16,26 @@

import { BasePlugin } from '@opentelemetry/core';
import { CanonicalCode, Span, SpanKind } from '@opentelemetry/api';
import * as mongodb from 'mongodb';
import type * as mongodb from 'mongodb';
import * as shimmer from 'shimmer';
import {
AttributeNames,
Func,
MongodbCommandType,
MongoInternalCommand,
MongoInternalTopology,
} from './types';
import { VERSION } from './version';
import {
DatabaseAttribute,
GeneralAttribute,
} from '@opentelemetry/semantic-conventions';

/** MongoDBCore instrumentation plugin for OpenTelemetry */
export class MongoDBPlugin extends BasePlugin<typeof mongodb> {
private readonly _SERVER_METHODS = ['insert', 'update', 'remove', 'command'];
private readonly _CURSOR_METHODS = ['_next', 'next'];
private _hasPatched: boolean = false;

private readonly _COMPONENT = 'mongodb';
private readonly _DB_TYPE = 'mongodb';

readonly supportedVersions = ['>=2 <4'];

constructor(readonly moduleName: string) {
Expand Down Expand Up @@ -175,19 +175,26 @@ export class MongoDBPlugin extends BasePlugin<typeof mongodb> {
// add network attributes to determine the remote server
if (topology && topology.s) {
span.setAttributes({
[AttributeNames.PEER_HOSTNAME]: `${
[GeneralAttribute.NET_HOST_NAME]: `${
topology.s.options?.host ?? topology.s.host
}`,
[AttributeNames.PEER_PORT]: `${
[GeneralAttribute.NET_HOST_PORT]: `${
topology.s.options?.port ?? topology.s.port
}`,
});
}

// The namespace is a combination of the database name and the name of the
// collection or index, like so: [database-name].[collection-or-index-name].
// It could be a string or an instance of MongoDBNamespace, as such we
// always coerce to a string to extract db and collection.
const [dbName, dbCollection] = ns.toString().split('.');

// add database related attributes
span.setAttributes({
[AttributeNames.DB_INSTANCE]: `${ns}`,
[AttributeNames.DB_TYPE]: this._DB_TYPE,
[AttributeNames.COMPONENT]: this._COMPONENT,
[DatabaseAttribute.DB_SYSTEM]: 'mongodb',
[DatabaseAttribute.DB_NAME]: dbName,
[DatabaseAttribute.DB_MONGODB_COLLECTION]: dbCollection,
});

if (command === undefined) return;
Expand Down
15 changes: 0 additions & 15 deletions plugins/node/opentelemetry-plugin-mongodb/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,21 +40,6 @@ 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',
}

export enum MongodbCommandType {
CREATE_INDEXES = 'createIndexes',
FIND_AND_MODIFY = 'findAndModify',
Expand Down
14 changes: 10 additions & 4 deletions plugins/node/opentelemetry-plugin-mongodb/test/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,10 @@ import { CanonicalCode, SpanKind } from '@opentelemetry/api';
import { ReadableSpan } from '@opentelemetry/tracing';
import * as assert from 'assert';
import * as mongodb from 'mongodb';
import { AttributeNames } from '../src/types';
import {
DatabaseAttribute,
GeneralAttribute,
} from '@opentelemetry/semantic-conventions';

export interface MongoDBAccess {
client: mongodb.MongoClient;
Expand Down Expand Up @@ -75,16 +78,19 @@ export function assertSpans(
const [mongoSpan] = spans;
assert.strictEqual(mongoSpan.name, expectedName);
assert.strictEqual(mongoSpan.kind, expectedKind);
assert.strictEqual(mongoSpan.attributes[AttributeNames.COMPONENT], 'mongodb');
assert.strictEqual(
mongoSpan.attributes[AttributeNames.PEER_HOSTNAME],
mongoSpan.attributes[DatabaseAttribute.DB_SYSTEM],
'mongodb'
);
assert.strictEqual(
mongoSpan.attributes[GeneralAttribute.NET_HOST_NAME],
process.env.MONGODB_HOST || 'localhost'
);
assert.strictEqual(mongoSpan.status.code, CanonicalCode.OK);

if (isEnhancedDatabaseReportingEnabled) {
const dbStatement = mongoSpan.attributes[
AttributeNames.DB_STATEMENT
DatabaseAttribute.DB_STATEMENT
] as any;
for (const key in dbStatement) {
assert.notStrictEqual(dbStatement[key], '?');
Expand Down

0 comments on commit e97aa05

Please sign in to comment.