Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[mongodb] implement semantic db conventions #187

Merged
merged 3 commits into from
Sep 1, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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