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

feat(mongodb): support v5 #1451

Merged
Merged
Show file tree
Hide file tree
Changes from 16 commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
c4efc68
feat(mongodb): add support for v5
GuillaumeDeconinck Mar 31, 2023
914c399
refactor(mongodb): avoid duplicating code for v4 & v5
GuillaumeDeconinck Apr 2, 2023
cded0c1
fix: lint
GuillaumeDeconinck Apr 2, 2023
7d2eead
refactor(mongodb): use range for mongodb version check
GuillaumeDeconinck Apr 2, 2023
e9118d3
chore(mongodb): make test commands consistent
GuillaumeDeconinck May 6, 2023
5e19d4f
Merge branch 'main' into feat/mongodb-support-v5
haddasbronfman May 14, 2023
35004dc
Merge branch 'main' into feat/mongodb-support-v5
GuillaumeDeconinck May 17, 2023
bae68b1
Merge branch 'main' into feat/mongodb-support-v5
GuillaumeDeconinck May 28, 2023
bf14bae
refactor(mongodb): adapt code based on main's changes
GuillaumeDeconinck May 31, 2023
582c6a4
chore: revert mongodb bump in devDependencies
GuillaumeDeconinck May 31, 2023
7a2cf4b
Merge branch 'main' into feat/mongodb-support-v5
GuillaumeDeconinck May 31, 2023
a74f02d
Merge branch 'main' into feat/mongodb-support-v5
pichlermarc Jun 6, 2023
e95ddbf
refactor: adapt code based on comments
GuillaumeDeconinck Jun 6, 2023
997c642
chore: re-use old order for tests suites
GuillaumeDeconinck Jun 6, 2023
e91dfe0
refactor: simply metrics tests
GuillaumeDeconinck Jun 6, 2023
9eacbf0
test: fix DB name in v5 tests
GuillaumeDeconinck Jun 6, 2023
6f740e7
Merge branch 'main' into feat/mongodb-support-v5
haddasbronfman Jun 7, 2023
eb63033
chore: revert MongoDB to 3.6.11 in package.json
GuillaumeDeconinck Jun 7, 2023
f123719
test: fix compilation issue due to different signatures (v3 vs v5)
GuillaumeDeconinck Jun 7, 2023
ea1e82f
Merge branch 'main' into feat/mongodb-support-v5
GuillaumeDeconinck Jun 12, 2023
f24648f
Merge branch 'main' into feat/mongodb-support-v5
GuillaumeDeconinck Jun 15, 2023
ff4f310
Merge branch 'main' into feat/mongodb-support-v5
pichlermarc Jun 16, 2023
669c57d
Merge branch 'main' into feat/mongodb-support-v5
pichlermarc Jun 19, 2023
4afcd91
Merge branch 'main' into feat/mongodb-support-v5
pichlermarc Jun 23, 2023
f23eeac
Merge branch 'main' into feat/mongodb-support-v5
GuillaumeDeconinck Jun 30, 2023
df6c722
fix: lint
pichlermarc Jun 30, 2023
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
6 changes: 4 additions & 2 deletions plugins/node/opentelemetry-instrumentation-mongodb/.tav.yml
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
mongodb:
jobs:
- versions: ">=3.3 <4"
commands: npm run test
commands: npm run test-v3
- versions: ">=4 <5"
commands: npm run test-new-versions
commands: npm run test-v4
- versions: ">=5 <6"
commands: npm run test-v5

# Fix missing `contrib-test-utils` package
pretest: npm run --prefix ../../../ lerna:link
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@
"repository": "open-telemetry/opentelemetry-js-contrib",
"scripts": {
"docker:start": "docker run -e MONGODB_DB=opentelemetry-tests -e MONGODB_PORT=27017 -e MONGODB_HOST=127.0.0.1 -p 27017:27017 --rm mongo",
"test": "nyc ts-mocha -p tsconfig.json --require '@opentelemetry/contrib-test-utils' 'test/**/mongodb-v3.test.ts'",
"test-new-versions": "nyc ts-mocha -p tsconfig.json --require '@opentelemetry/contrib-test-utils' 'test/mongodb-v4**.test.ts'",
"test": "npm run test-v3",
"test-v3": "nyc ts-mocha -p tsconfig.json --require '@opentelemetry/contrib-test-utils' 'test/**/mongodb-v3.test.ts'",
pichlermarc marked this conversation as resolved.
Show resolved Hide resolved
"test-v4": "nyc ts-mocha -p tsconfig.json --require '@opentelemetry/contrib-test-utils' 'test/mongodb-v4-v5.metrics.test.ts' 'test/**/mongodb-v4.test.ts'",
"test-v5": "nyc ts-mocha -p tsconfig.json --require '@opentelemetry/contrib-test-utils' 'test/mongodb-v4-v5.metrics.test.ts' 'test/**/mongodb-v5.test.ts'",
"test-all-versions": "tav",
"tdd": "npm run test -- --watch-extensions ts --watch",
"clean": "rimraf build/*",
Expand Down Expand Up @@ -51,16 +53,16 @@
},
"devDependencies": {
"@opentelemetry/api": "^1.3.0",
"@opentelemetry/contrib-test-utils": "^0.33.3",
"@opentelemetry/context-async-hooks": "^1.8.0",
"@opentelemetry/contrib-test-utils": "^0.33.3",
"@opentelemetry/sdk-trace-base": "^1.8.0",
"@opentelemetry/sdk-trace-node": "^1.8.0",
"@types/bson": "4.0.5",
"@types/mocha": "7.0.2",
"@types/mongodb": "3.6.20",
"@types/node": "18.11.7",
"mocha": "7.2.0",
"mongodb": "3.6.11",
"@types/mongodb": "3.6.20",
"@types/bson": "4.0.5",
"mongodb": "4",
pichlermarc marked this conversation as resolved.
Show resolved Hide resolved
"nyc": "15.1.0",
"rimraf": "5.0.0",
"test-all-versions": "5.0.1",
Expand All @@ -73,4 +75,4 @@
"@opentelemetry/semantic-conventions": "^1.0.0"
},
"homepage": "https://github.com/open-telemetry/opentelemetry-js-contrib/tree/main/plugins/node/opentelemetry-instrumentation-mongodb#readme"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -95,25 +95,25 @@ export class MongoDBInstrumentation extends InstrumentationBase {
),
new InstrumentationNodeModuleDefinition<any>(
'mongodb',
['4.*'],
['4.*', '5.*'],
undefined,
undefined,
[
new InstrumentationNodeModuleFile<V4Connection>(
'mongodb/lib/cmap/connection.js',
['4.*'],
['4.*', '5.*'],
v4PatchConnection,
v4UnpatchConnection
),
new InstrumentationNodeModuleFile<V4Connect>(
'mongodb/lib/cmap/connect.js',
['4.*'],
['4.*', '5.*'],
v4PatchConnect,
v4UnpatchConnect
),
new InstrumentationNodeModuleFile<V4Session>(
'mongodb/lib/sessions.js',
['4.*'],
['4.*', '5.*'],
v4PatchSessions,
v4UnpatchSessions
),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ import * as mongodb from 'mongodb';
import { assertSpans, accessCollection, DEFAULT_MONGO_HOST } from './utils';
import { SemanticAttributes } from '@opentelemetry/semantic-conventions';

describe('MongoDBInstrumentation', () => {
describe('MongoDBInstrumentation-Tracing-v3', () => {
function create(config: MongoDBInstrumentationConfig = {}) {
instrumentation.setConfig(config);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,6 @@ import {
ResourceMetrics,
} from '@opentelemetry/sdk-metrics';

import * as mongodb from 'mongodb';

const otelTestingMeterProvider = new MeterProvider();
const inMemoryMetricsExporter = new InMemoryMetricExporter(
AggregationTemporality.CUMULATIVE
Expand All @@ -40,17 +38,13 @@ const metricReader = new PeriodicExportingMetricReader({
exportTimeoutMillis: 100,
});

otelTestingMeterProvider.addMetricReader(metricReader);

import { registerInstrumentationTesting } from '@opentelemetry/contrib-test-utils';
const instrumentation = registerInstrumentationTesting(
new MongoDBInstrumentation()
);

instrumentation.setMeterProvider(otelTestingMeterProvider);

import { accessCollection, DEFAULT_MONGO_HOST } from './utils';

import * as mongodb from 'mongodb';
import * as assert from 'assert';

async function waitForNumberOfExports(
Expand Down Expand Up @@ -86,28 +80,33 @@ describe('MongoDBInstrumentation-Metrics', () => {
const DB_NAME = process.env.MONGODB_DB || 'opentelemetry-tests-metrics';
const COLLECTION_NAME = 'test-metrics';
const URL = `mongodb://${HOST}:${PORT}/${DB_NAME}`;

let client: mongodb.MongoClient;
let collection: mongodb.Collection;

before(() => {
otelTestingMeterProvider.addMetricReader(metricReader);
instrumentation?.setMeterProvider(otelTestingMeterProvider);
});

beforeEach(function mongoBeforeEach(done) {
// Skipping all tests in beforeEach() is a workaround. Mocha does not work
// properly when skipping tests in before() on nested describe() calls.
// https://github.com/mochajs/mocha/issues/2819
if (!shouldTest) {
this.skip();
}

inMemoryMetricsExporter.reset();
done();
});

it('Should add connection usage metrics', async () => {
const result = await accessCollection(URL, DB_NAME, COLLECTION_NAME);
client = result.client;
collection = result.collection;
const collection = result.collection;
const insertData = [{ a: 1 }, { a: 2 }, { a: 3 }];
await collection.insertMany(insertData);
await collection.deleteMany({});
let exportedMetrics = await waitForNumberOfExports(
const exportedMetrics = await waitForNumberOfExports(
inMemoryMetricsExporter,
1
);
Expand All @@ -126,43 +125,55 @@ describe('MongoDBInstrumentation-Metrics', () => {
metrics[0].descriptor.name,
'db.client.connections.usage'
);
assert.strictEqual(metrics[0].dataPoints.length, 2);
assert.strictEqual(metrics[0].dataPoints[0].value, 0);
assert.strictEqual(metrics[0].dataPoints[0].attributes['state'], 'used');

// Checking dataPoints
const dataPoints = metrics[0].dataPoints;
assert.strictEqual(dataPoints.length, 2);
assert.strictEqual(dataPoints[0].value, 0);
assert.strictEqual(dataPoints[0].attributes['state'], 'used');
assert.strictEqual(
metrics[0].dataPoints[0].attributes['pool.name'],
dataPoints[0].attributes['pool.name'],
`mongodb://${HOST}:${PORT}/${DB_NAME}`
);

assert.strictEqual(metrics[0].dataPoints[1].value, 1);
assert.strictEqual(metrics[0].dataPoints[1].attributes['state'], 'idle');
assert.strictEqual(dataPoints[1].value, 1);
assert.strictEqual(dataPoints[1].attributes['state'], 'idle');
assert.strictEqual(
metrics[0].dataPoints[1].attributes['pool.name'],
dataPoints[1].attributes['pool.name'],
`mongodb://${HOST}:${PORT}/${DB_NAME}`
);
});

it('Should add disconnection usage metrics', async () => {
await client.close();

exportedMetrics = await waitForNumberOfExports(inMemoryMetricsExporter, 2);
const exportedMetrics = await waitForNumberOfExports(
inMemoryMetricsExporter,
2
);
assert.strictEqual(exportedMetrics.length, 2);
metrics = exportedMetrics[1].scopeMetrics[0].metrics;
const metrics = exportedMetrics[1].scopeMetrics[0].metrics;
assert.strictEqual(metrics.length, 1);
assert.strictEqual(metrics[0].dataPointType, DataPointType.SUM);

assert.strictEqual(
metrics[0].descriptor.description,
'The number of connections that are currently in state described by the state attribute.'
);
assert.strictEqual(metrics[0].dataPoints.length, 2);
assert.strictEqual(metrics[0].dataPoints[0].value, 0);
assert.strictEqual(metrics[0].dataPoints[0].attributes['state'], 'used');

// Checking dataPoints
const dataPoints = metrics[0].dataPoints;
assert.strictEqual(dataPoints.length, 2);
assert.strictEqual(dataPoints[0].value, 0);
assert.strictEqual(dataPoints[0].attributes['state'], 'used');
assert.strictEqual(
metrics[0].dataPoints[0].attributes['pool.name'],
dataPoints[0].attributes['pool.name'],
`mongodb://${HOST}:${PORT}/${DB_NAME}`
);
assert.strictEqual(metrics[0].dataPoints[1].value, 0);
assert.strictEqual(metrics[0].dataPoints[1].attributes['state'], 'idle');
assert.strictEqual(dataPoints[1].value, 0);
assert.strictEqual(dataPoints[1].attributes['state'], 'idle');
assert.strictEqual(
metrics[0].dataPoints[1].attributes['pool.name'],
dataPoints[1].attributes['pool.name'],
`mongodb://${HOST}:${PORT}/${DB_NAME}`
);
});
Expand Down
Loading