Skip to content

Commit

Permalink
fix(opentelemetry-instrumentation-mongodb): fix span attributes with …
Browse files Browse the repository at this point in the history
…unified topology (#663)

Co-authored-by: Valentin Marchaud <[email protected]>
Co-authored-by: Daniel Dyla <[email protected]>
  • Loading branch information
3 people authored Oct 12, 2021
1 parent 2383aaf commit aeadca8
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -382,14 +382,24 @@ export class MongoDBInstrumentation extends InstrumentationBase<
) {
// add network attributes to determine the remote server
if (topology && topology.s) {
span.setAttributes({
[SemanticAttributes.NET_HOST_NAME]: `${
topology.s.options?.host ?? topology.s.host
}`,
[SemanticAttributes.NET_HOST_PORT]: `${
topology.s.options?.port ?? topology.s.port
}`,
});
let host = topology.s.options?.host ?? topology.s.host;
let port: string | undefined = (
topology.s.options?.port ?? topology.s.port
)?.toString();
if (host == null || port == null) {
const address = topology.description?.address;
if (address) {
const addressSegments = address.split(':');
host = addressSegments[0];
port = addressSegments[1];
}
}
if (host?.length && port?.length) {
span.setAttributes({
[SemanticAttributes.NET_HOST_NAME]: host,
[SemanticAttributes.NET_HOST_PORT]: port,
});
}
}

// The namespace is a combination of the database name and the name of the
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,10 @@ export type MongoInternalTopology = {
host?: string;
port?: number;
};
// mongodb@3 with useUnifiedTopology option
description?: {
address?: string;
};
};

export enum MongodbCommandType {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import {
getTestSpans,
resetMemoryExporter,
} from '@opentelemetry/contrib-test-utils';
import { lookup } from 'dns';

const instrumentation = registerInstrumentationTesting(
new MongoDBInstrumentation()
Expand Down Expand Up @@ -404,6 +405,55 @@ describe('MongoDBInstrumentation', () => {
});
});

describe('MongoDb useUnifiedTopology enabled', () => {
let client: mongodb.MongoClient;
let collection: mongodb.Collection;
before(done => {
accessCollection(URL, DB_NAME, COLLECTION_NAME, {
useUnifiedTopology: true,
})
.then(result => {
client = result.client;
collection = result.collection;
done();
})
.catch((err: Error) => {
console.log(
'Skipping test-mongodb. Could not connect. Run MongoDB to test'
);
shouldTest = false;
done();
});
});
after(() => {
if (client) {
client.close();
}
});
it('should generate correct span attributes', done => {
const span = trace.getTracer('default').startSpan('findRootSpan');
context.with(trace.setSpan(context.active(), span), () => {
collection.find({ a: 1 }).toArray((err, results) => {
span.end();
const [mongoSpan] = getTestSpans();
assert.ifError(err);
lookup(process.env.MONGODB_HOST || 'localhost', (err, address) => {
if (err) return done(err);
assert.strictEqual(
mongoSpan.attributes[SemanticAttributes.NET_HOST_NAME],
address
);
assert.strictEqual(
mongoSpan.attributes[SemanticAttributes.NET_HOST_PORT],
process.env.MONGODB_PORT || '27017'
);
done();
});
});
});
});
});

/** Should intercept command */
describe('Removing Instrumentation', () => {
it('should unpatch plugin', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,11 @@ export interface MongoDBAccess {
export function accessCollection(
url: string,
dbName: string,
collectionName: string
collectionName: string,
options: mongodb.MongoClientOptions = {}
): Promise<MongoDBAccess> {
return new Promise((resolve, reject) => {
mongodb.MongoClient.connect(url, (err, client) => {
mongodb.MongoClient.connect(url, options, (err, client) => {
if (err) {
reject(err);
return;
Expand Down

0 comments on commit aeadca8

Please sign in to comment.