Skip to content

Commit

Permalink
fix(*): migrate away from deprecated addMetricReader for SDK 2.0
Browse files Browse the repository at this point in the history
This migrates from the deprecated `addMetricReader` method to the
constructor option in preparation for the upcoming SDK 2.0 release.

All usages are in tests and example code, there are no user-facing
changes.

Fixes #2646
  • Loading branch information
chancancode committed Jan 16, 2025
1 parent e57f3e4 commit f3bbbb0
Show file tree
Hide file tree
Showing 10 changed files with 37 additions and 26 deletions.
5 changes: 3 additions & 2 deletions examples/mysql/src/tracer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,15 @@ const EXPORTER = process.env.EXPORTER || '';
export const setupTracing = (serviceName: string) => {

//metrics:
const meterProvider = new MeterProvider()
const metricExporter = new OTLPMetricExporter();
const metricReader = new PeriodicExportingMetricReader({
exporter: metricExporter,
exportIntervalMillis: 100,
exportTimeoutMillis: 100,
});
meterProvider.addMetricReader(metricReader);
const meterProvider = new MeterProvider({
readers: [metricReader],
});

//traces:
const tracerProvider = new NodeTracerProvider({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,9 @@ describe(`${ConventionalNamePrefix.NodeJs}.eventloop`, function () {

beforeEach(() => {
metricReader = new TestMetricReader();
meterProvider = new MeterProvider();
meterProvider.addMetricReader(metricReader);
meterProvider = new MeterProvider({
readers: [metricReader],
});
});

for (const metricName in metricNames) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,9 @@ describe(`${ConventionalNamePrefix.NodeJs}.${ATTR_NODEJS_EVENT_LOOP_TIME}`, func

beforeEach(() => {
metricReader = new TestMetricReader();
meterProvider = new MeterProvider();
meterProvider.addMetricReader(metricReader);
meterProvider = new MeterProvider({
readers: [metricReader],
});
});

it('should not export before being enabled', async function () {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,9 @@ describe(`${ConventionalNamePrefix.NodeJs}.${ATTR_NODEJS_EVENT_LOOP_UTILIZATION}

beforeEach(() => {
metricReader = new TestMetricReader();
meterProvider = new MeterProvider();
meterProvider.addMetricReader(metricReader);
meterProvider = new MeterProvider({
readers: [metricReader],
});
});

it('should not export before being enabled', async function () {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,9 @@ describe('nodejs.heap_space', function () {

beforeEach(() => {
metricReader = new TestMetricReader();
meterProvider = new MeterProvider();
meterProvider.addMetricReader(metricReader);
meterProvider = new MeterProvider({
readers: [metricReader],
});
});

for (const metricName in metricNames) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,9 @@ describe('instrumentation', function () {

beforeEach(() => {
metricReader = new TestMetricReader();
meterProvider = new MeterProvider();
meterProvider.addMetricReader(metricReader);
meterProvider = new MeterProvider({
readers: [metricReader],
});
});

it('should not export before being enabled', async function () {
Expand Down
5 changes: 3 additions & 2 deletions plugins/node/instrumentation-undici/test/metrics.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,13 @@ describe('UndiciInstrumentation metrics tests', function () {
const hostname = 'localhost';
const mockServer = new MockServer();
const provider = new NodeTracerProvider();
const meterProvider = new MeterProvider();
const metricsMemoryExporter = new InMemoryMetricExporter(
AggregationTemporality.DELTA
);
const metricReader = new MockMetricsReader(metricsMemoryExporter);
meterProvider.addMetricReader(metricReader);
const meterProvider = new MeterProvider({
readers: [metricReader],
});

before(function (done) {
// Do not test if the `fetch` global API is not available
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -152,10 +152,11 @@ describe('force flush', () => {
});

it('should force flush MeterProvider', async () => {
const provider = new MeterProvider();
provider.addMetricReader(
new PeriodicExportingMetricReader({ exporter: metricMemoryExporter })
);
const provider = new MeterProvider({
readers: [
new PeriodicExportingMetricReader({ exporter: metricMemoryExporter }),
],
});
let forceFlushed = false;
const forceFlush = () =>
new Promise<void>(resolve => {
Expand Down Expand Up @@ -198,10 +199,11 @@ describe('force flush', () => {
});
nodeTracerProvider.forceFlush = tracerForceFlush;

const meterProvider = new MeterProvider();
meterProvider.addMetricReader(
new PeriodicExportingMetricReader({ exporter: metricMemoryExporter })
);
const meterProvider = new MeterProvider({
readers: [
new PeriodicExportingMetricReader({ exporter: metricMemoryExporter }),
],
});
let meterForceFlushed = false;
const meterForceFlush = () =>
new Promise<void>(resolve => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ import {
ResourceMetrics,
} from '@opentelemetry/sdk-metrics';

const otelTestingMeterProvider = new MeterProvider();
const inMemoryMetricsExporter = new InMemoryMetricExporter(
AggregationTemporality.CUMULATIVE
);
Expand All @@ -37,6 +36,9 @@ const metricReader = new PeriodicExportingMetricReader({
exportIntervalMillis: 100,
exportTimeoutMillis: 100,
});
const otelTestingMeterProvider = new MeterProvider({
readers: [metricReader],
});

import { registerInstrumentationTesting } from '@opentelemetry/contrib-test-utils';
const instrumentation = registerInstrumentationTesting(
Expand Down Expand Up @@ -85,7 +87,6 @@ describe('MongoDBInstrumentation-Metrics', () => {
let collection: Collection;

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

shouldTest = true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,6 @@ describe('mysql@2.x-Metrics', () => {
const shouldTest = testMysql || testMysqlLocally; // Skips these tests if false (default)

function initMeterProvider() {
otelTestingMeterProvider = new MeterProvider();
inMemoryMetricsExporter = new InMemoryMetricExporter(
AggregationTemporality.CUMULATIVE
);
Expand All @@ -76,8 +75,10 @@ describe('mysql@2.x-Metrics', () => {
exportIntervalMillis: 100,
exportTimeoutMillis: 100,
});
otelTestingMeterProvider = new MeterProvider({
readers: [metricReader],
});

otelTestingMeterProvider.addMetricReader(metricReader);
instrumentation.setMeterProvider(otelTestingMeterProvider);
}

Expand Down

0 comments on commit f3bbbb0

Please sign in to comment.