Skip to content

Commit

Permalink
doc: migrate away from defunct addMetricReader in examples (open-te…
Browse files Browse the repository at this point in the history
  • Loading branch information
chancancode authored Jan 24, 2025
1 parent 0ae25f1 commit f927e82
Show file tree
Hide file tree
Showing 11 changed files with 94 additions and 75 deletions.
19 changes: 11 additions & 8 deletions doc/metrics.md
Original file line number Diff line number Diff line change
Expand Up @@ -504,8 +504,9 @@ const options = { port: 9464 };
const exporter = new PrometheusExporter(options);

// Creates MeterProvider and installs the exporter as a MetricReader
const meterProvider = new MeterProvider();
meterProvider.addMetricReader(exporter);
const meterProvider = new MeterProvider({
readers: [exporter],
});
const meter = meterProvider.getMeter('example-prometheus');

// Now, start recording data
Expand Down Expand Up @@ -541,12 +542,14 @@ const collectorOptions = {
concurrencyLimit: 1, // an optional limit on pending requests
};
const exporter = new OTLPMetricExporter(collectorOptions);
const meterProvider = new MeterProvider({});

meterProvider.addMetricReader(new PeriodicExportingMetricReader({
exporter: metricExporter,
exportIntervalMillis: 1000,
}));
const meterProvider = new MeterProvider({
readers: [
new PeriodicExportingMetricReader({
exporter,
exportIntervalMillis: 1000,
}),
],
});

// Now, start recording data
const meter = meterProvider.getMeter('example-meter');
Expand Down
14 changes: 8 additions & 6 deletions examples/opentelemetry-web/examples/metrics/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,16 @@ function stopMetrics() {
function startMetrics() {
console.log('STARTING METRICS');

const meterProvider = new MeterProvider();
const meterProvider = new MeterProvider({
readers: [
new PeriodicExportingMetricReader({
exporter: new OTLPMetricExporter(),
exportIntervalMillis: 1000
}),
],
});
metrics.setGlobalMeterProvider(meterProvider);

meterProvider.addMetricReader(new PeriodicExportingMetricReader({
exporter: new OTLPMetricExporter(),
exportIntervalMillis: 1000
}));

meter = meterProvider.getMeter('example-exporter-collector')

const requestCounter = meter.createCounter('requests', {
Expand Down
15 changes: 7 additions & 8 deletions examples/otlp-exporter-node/metrics.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,16 +39,15 @@ const meterProvider = new MeterProvider({
[SEMRESATTRS_SERVICE_NAME]: 'basic-metric-service',
}),
views: [expHistogramView],
readers: [
new PeriodicExportingMetricReader({
exporter: metricExporter,
// exporter: new ConsoleMetricExporter(),
exportIntervalMillis: 1000,
}),
],
});

meterProvider.addMetricReader(
new PeriodicExportingMetricReader({
exporter: metricExporter,
// exporter: new ConsoleMetricExporter(),
exportIntervalMillis: 1000,
})
);

const meter = meterProvider.getMeter('example-exporter-collector');

const requestCounter = meter.createCounter('requests', {
Expand Down
28 changes: 15 additions & 13 deletions experimental/examples/opencensus-shim/setup.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,22 +53,24 @@ module.exports = function setup(serviceName) {
spanProcessors: [
new BatchSpanProcessor(new OTLPTraceExporter(), {
scheduledDelayMillis: 5000,
})
]
}),
],
});
tracerProvider.register();

const meterProvider = new MeterProvider({ resource });
meterProvider.addMetricReader(
new PrometheusExporter({
metricProducers: [
new OpenCensusMetricProducer({
openCensusMetricProducerManager:
oc.Metrics.getMetricProducerManager(),
}),
],
})
);
const meterProvider = new MeterProvider({
resource,
readers: [
new PrometheusExporter({
metricProducers: [
new OpenCensusMetricProducer({
openCensusMetricProducerManager:
oc.Metrics.getMetricProducerManager(),
}),
],
}),
],
});
metrics.setGlobalMeterProvider(meterProvider);

// Start OpenCensus tracing
Expand Down
5 changes: 3 additions & 2 deletions experimental/examples/prometheus/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,9 @@ const exporter = new PrometheusExporter({}, () => {
});

// Creates MeterProvider and installs the exporter as a MetricReader
const meterProvider = new MeterProvider();
meterProvider.addMetricReader(exporter);
const meterProvider = new MeterProvider({
readers: [exporter],
});
const meter = meterProvider.getMeter('example-prometheus');

// Creates metric instruments
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,14 @@ const collectorOptions = {
};

const metricExporter = new OTLPMetricExporter(collectorOptions);
const meterProvider = new MeterProvider({});

meterProvider.addMetricReader(new PeriodicExportingMetricReader({
exporter: metricExporter,
exportIntervalMillis: 1000,
}));
const meterProvider = new MeterProvider({
readers: [
new PeriodicExportingMetricReader({
exporter: metricExporter,
exportIntervalMillis: 1000,
}),
],
});

['SIGINT', 'SIGTERM'].forEach(signal => {
process.on(signal, () => meterProvider.shutdown().catch(console.error));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,14 @@ const collectorOptions = {
concurrencyLimit: 1, // an optional limit on pending requests
};
const metricExporter = new OTLPMetricExporter(collectorOptions);
const meterProvider = new MeterProvider({});

meterProvider.addMetricReader(new PeriodicExportingMetricReader({
exporter: metricExporter,
exportIntervalMillis: 1000,
}));
const meterProvider = new MeterProvider({
readers: [
new PeriodicExportingMetricReader({
exporter: metricExporter,
exportIntervalMillis: 1000,
}),
],
});

// Now, start recording data
const meter = meterProvider.getMeter('example-meter');
Expand All @@ -58,12 +60,14 @@ const collectorOptions = {
concurrencyLimit: 1, // an optional limit on pending requests
};
const metricExporter = new OTLPMetricExporter(collectorOptions);
const meterProvider = new MeterProvider({});

meterProvider.addMetricReader(new PeriodicExportingMetricReader({
exporter: metricExporter,
exportIntervalMillis: 1000,
}));
const meterProvider = new MeterProvider({
readers: [
new PeriodicExportingMetricReader({
exporter: metricExporter,
exportIntervalMillis: 1000,
}),
],
});

// Now, start recording data
const meter = meterProvider.getMeter('example-meter');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,14 @@ const collectorOptions = {
url: '<opentelemetry-collector-url>', // url is optional and can be omitted - default is http://localhost:4318/v1/metrics
};
const metricExporter = new OTLPMetricExporter(collectorOptions);
const meterProvider = new MeterProvider({});

meterProvider.addMetricReader(new PeriodicExportingMetricReader({
exporter: metricExporter,
exportIntervalMillis: 1000,
}));
const meterProvider = new MeterProvider({
readers: [
new PeriodicExportingMetricReader({
exporter: metricExporter,
exportIntervalMillis: 1000,
}),
],
});

// Now, start recording data
const meter = meterProvider.getMeter('example-meter');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,9 @@ const options = {port: 9464};
const exporter = new PrometheusExporter(options);

// Creates MeterProvider and installs the exporter as a MetricReader
const meterProvider = new MeterProvider();
meterProvider.addMetricReader(exporter);
const meterProvider = new MeterProvider({
readers: [exporter],
});
const meter = meterProvider.getMeter('example-prometheus');

// Now, start recording data
Expand Down
22 changes: 12 additions & 10 deletions experimental/packages/shim-opencensus/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,16 +79,18 @@ OpenCensus metrics can be collected and sent to an OpenTelemetry exporter by pro
through the OpenTelemetry Prometheus exporter:

```js
meterProvider.addMetricReader(
new PrometheusExporter({
metricProducers: [
new OpenCensusMetricProducer({
openCensusMetricProducerManager:
oc.Metrics.getMetricProducerManager(),
}),
],
})
);
new MeterProvider({
readers: [
new PrometheusExporter({
metricProducers: [
new OpenCensusMetricProducer({
openCensusMetricProducerManager:
oc.Metrics.getMetricProducerManager(),
}),
],
}),
],
});
```

## Example
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,13 @@ interface OpenCensusMetricProducerOptions {
*
* @example
* ```
* const meterProvider = new MeterProvider();
* const reader = new PeriodicExportingMetricReader({
* metricProducers: [new OpenCensusMetricProducer()],
* exporter: exporter,
* });
* meterProvider.addMetricReader(reader);
* const meterProvider = new MeterProvider({
* readers: [reader],
* });
* ```
*/
export class OpenCensusMetricProducer implements MetricProducer {
Expand Down

0 comments on commit f927e82

Please sign in to comment.