Skip to content

Commit

Permalink
refactor: adapt code based on comments
Browse files Browse the repository at this point in the history
  • Loading branch information
GuillaumeDeconinck committed Jun 6, 2023
1 parent a74f02d commit e95ddbf
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 58 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +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": "npm run test-v3",
"test-v3": "nyc ts-mocha -p tsconfig.json --require '@opentelemetry/contrib-test-utils' 'test/**/mongodb-v3.test.ts'",
"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-v4": "nyc ts-mocha -p tsconfig.json --require '@opentelemetry/contrib-test-utils' 'test/**/mongodb-v4.test.ts' 'test/mongodb-v4-v5.metrics.test.ts'",
"test-v5": "nyc ts-mocha -p tsconfig.json --require '@opentelemetry/contrib-test-utils' 'test/**/mongodb-v5.test.ts' 'test/mongodb-v4-v5.metrics.test.ts'",
"test-all-versions": "tav",
"tdd": "npm run test -- --watch-extensions ts --watch",
"clean": "rimraf build/*",
Expand Down Expand Up @@ -61,7 +62,7 @@
"@types/mongodb": "3.6.20",
"@types/node": "18.11.7",
"mocha": "7.2.0",
"mongodb": "3.6.11",
"mongodb": "4",
"nyc": "15.1.0",
"rimraf": "5.0.0",
"test-all-versions": "5.0.1",
Expand All @@ -74,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 @@ -21,34 +21,15 @@ import { MongoDBInstrumentation } from '../src';
// TODO: use test-utils after the new package has released.
import {
AggregationTemporality,
DataPoint,
DataPointType,
InMemoryMetricExporter,
MeterProvider,
PeriodicExportingMetricReader,
ResourceMetrics,
} from '@opentelemetry/sdk-metrics';

import {
getInstrumentation,
registerInstrumentationTesting,
} from '@opentelemetry/contrib-test-utils';

import * as assert from 'assert';

// Get instrumentation (singleton)
let instrumentation: MongoDBInstrumentation;
{
const instance: MongoDBInstrumentation | undefined = getInstrumentation();
if (!instance) {
instrumentation = new MongoDBInstrumentation();
registerInstrumentationTesting(instrumentation);
} else {
instrumentation = instance;
}
}

import { accessCollection, DEFAULT_MONGO_HOST } from './utils';
import { MongoClient } from 'mongodb';
import * as mongodb from 'mongodb';

const otelTestingMeterProvider = new MeterProvider();
const inMemoryMetricsExporter = new InMemoryMetricExporter(
Expand All @@ -60,28 +41,55 @@ const metricReader = new PeriodicExportingMetricReader({
exportTimeoutMillis: 100,
});

otelTestingMeterProvider.addMetricReader(metricReader);
import { getInstrumentation } from '@opentelemetry/contrib-test-utils';

instrumentation.setMeterProvider(otelTestingMeterProvider);
const instrumentation: MongoDBInstrumentation | undefined =
getInstrumentation();

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

import * as assert from 'assert';

async function waitForNumberOfExports(
exporter: InMemoryMetricExporter,
numberOfExports: number
numberOfExports: number,
numberOfDataPoints = 0
): Promise<ResourceMetrics[]> {
if (numberOfExports <= 0) {
throw new Error('numberOfExports must be greater than or equal to 0');
}

let totalExports = 0;
while (totalExports < numberOfExports) {
let totalDataPoints = 0;
while (
totalExports < numberOfExports ||
totalDataPoints < numberOfDataPoints
) {
await new Promise(resolve => setTimeout(resolve, 20));
const exportedMetrics = exporter.getMetrics();
totalExports = exportedMetrics.length;
if (totalExports > 0) {
totalDataPoints =
exportedMetrics[0].scopeMetrics[0].metrics[0].dataPoints.length;
}
}

return exporter.getMetrics();
}

function filterDataPointsForDB(
dataPoints: DataPoint<Number>[],
dbName: string
): DataPoint<Number>[] {
return dataPoints.filter(v => {
const poolName = v?.attributes?.['pool.name'];
if (poolName && typeof poolName === 'string') {
return poolName.includes(dbName);
}
return false;
});
}

describe('MongoDBInstrumentation-Metrics', () => {
// For these tests, mongo must be running. Add RUN_MONGODB_TESTS to run
// these tests.
Expand All @@ -94,10 +102,16 @@ describe('MongoDBInstrumentation-Metrics', () => {

const HOST = process.env.MONGODB_HOST || DEFAULT_MONGO_HOST;
const PORT = process.env.MONGODB_PORT || '27017';
const DB_NAME = process.env.MONGODB_DB || 'opentelemetry-tests';
const DB_NAME = process.env.MONGODB_DB || 'opentelemetry-tests-metrics';
const COLLECTION_NAME = 'test-metrics';
const URL = `mongodb://${HOST}:${PORT}/${DB_NAME}`;
let client: MongoClient;
let client: mongodb.MongoClient;

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

beforeEach(function mongoBeforeEach(done) {
// Skipping all tests in beforeEach() is a workaround. Mocha does not work
Expand Down Expand Up @@ -137,18 +151,21 @@ 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 = filterDataPointsForDB(metrics[0].dataPoints, DB_NAME);
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}`
);
});
Expand All @@ -169,17 +186,20 @@ describe('MongoDBInstrumentation-Metrics', () => {
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 = filterDataPointsForDB(metrics[0].dataPoints, DB_NAME);
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
Original file line number Diff line number Diff line change
Expand Up @@ -24,23 +24,14 @@ import {
MongoResponseHookInformation,
} from '../src';
import {
getInstrumentation,
registerInstrumentationTesting,
getTestSpans,
resetMemoryExporter,
} from '@opentelemetry/contrib-test-utils';

// Get instrumentation (singleton)
let instrumentation: MongoDBInstrumentation;
{
const instance: MongoDBInstrumentation | undefined = getInstrumentation();
if (!instance) {
instrumentation = new MongoDBInstrumentation();
registerInstrumentationTesting(instrumentation);
} else {
instrumentation = instance;
}
}
const instrumentation = registerInstrumentationTesting(
new MongoDBInstrumentation()
);

import * as mongodb from 'mongodb';
import { assertSpans, accessCollection, DEFAULT_MONGO_HOST } from './utils';
Expand All @@ -61,7 +52,7 @@ describe('MongoDBInstrumentation-Tracing-v4', () => {

const HOST = process.env.MONGODB_HOST || DEFAULT_MONGO_HOST;
const PORT = process.env.MONGODB_PORT || '27017';
const DB_NAME = process.env.MONGODB_DB || 'opentelemetry-tests';
const DB_NAME = process.env.MONGODB_DB || 'opentelemetry-tests-traces';
const COLLECTION_NAME = 'test-traces';
const URL = `mongodb://${HOST}:${PORT}/${DB_NAME}`;

Expand Down

0 comments on commit e95ddbf

Please sign in to comment.