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(sdk-metrics)!: export factory functions over classes #4932

Open
wants to merge 8 commits into
base: next
Choose a base branch
from
1 change: 1 addition & 0 deletions CHANGELOG_NEXT.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
* refactor(resources)!: replace `ResourceAttributes` with `Attributes` [#5016](https://github.com/open-telemetry/opentelemetry-js/pull/5016) @david-luna
* feat(sdk-metrics)!: drop `View` and `Aggregation` in favor of `ViewOptions` and `AggregationOption` [#4931](https://github.com/open-telemetry/opentelemetry-js/pull/4931) @pichlermarc
* refactor(sdk-trace-base)!: remove `new Span` constructor in favor of `Tracer.startSpan` API [#5048](https://github.com/open-telemetry/opentelemetry-js/pull/5048) @david-luna
* feat(sdk-metrics)!: use factory functions over classes [#4932](https://github.com/open-telemetry/opentelemetry-js/pull/4932) @pichlermarc

### :rocket: (Enhancement)

Expand Down
2 changes: 1 addition & 1 deletion doc/metrics.md
Original file line number Diff line number Diff line change
Expand Up @@ -543,7 +543,7 @@ const collectorOptions = {
const exporter = new OTLPMetricExporter(collectorOptions);
const meterProvider = new MeterProvider({});

meterProvider.addMetricReader(new PeriodicExportingMetricReader({
meterProvider.addMetricReader(createPeriodicExportingMetricReader({
exporter: metricExporter,
exportIntervalMillis: 1000,
}));
Expand Down
2 changes: 1 addition & 1 deletion examples/opentelemetry-web/examples/metrics/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ function startMetrics() {
const meterProvider = new MeterProvider();
metrics.setGlobalMeterProvider(meterProvider);

meterProvider.addMetricReader(new PeriodicExportingMetricReader({
meterProvider.addMetricReader(createPeriodicExportingMetricReader({
exporter: new OTLPMetricExporter(),
exportIntervalMillis: 1000
}));
Expand Down
2 changes: 1 addition & 1 deletion examples/otlp-exporter-node/metrics.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ const meterProvider = new MeterProvider({
});

meterProvider.addMetricReader(
new PeriodicExportingMetricReader({
createPeriodicExportingMetricReader({
exporter: metricExporter,
// exporter: new ConsoleMetricExporter(),
exportIntervalMillis: 1000,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ const collectorOptions = {
const metricExporter = new OTLPMetricExporter(collectorOptions);
const meterProvider = new MeterProvider({});

meterProvider.addMetricReader(new PeriodicExportingMetricReader({
meterProvider.addMetricReader(createPeriodicExportingMetricReader({
exporter: metricExporter,
exportIntervalMillis: 1000,
}));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import * as grpc from '@grpc/grpc-js';
import { VERSION } from '@opentelemetry/core';
import {
AggregationType,
MeterProvider,
createMeterProvider,
MetricReader,
} from '@opentelemetry/sdk-metrics';
import {
Expand All @@ -55,7 +55,7 @@ const testResource = new Resource({
});

let reader = new TestMetricReader();
let meterProvider = new MeterProvider({
let meterProvider = createMeterProvider({
resource: testResource,
readers: [reader],
});
Expand All @@ -68,7 +68,7 @@ export async function collect() {

export function setUp() {
reader = new TestMetricReader();
meterProvider = new MeterProvider({
meterProvider = createMeterProvider({
resource: testResource,
views: [
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ const collectorOptions = {
const metricExporter = new OTLPMetricExporter(collectorOptions);
const meterProvider = new MeterProvider({});

meterProvider.addMetricReader(new PeriodicExportingMetricReader({
meterProvider.addMetricReader(createPeriodicExportingMetricReader({
exporter: metricExporter,
exportIntervalMillis: 1000,
}));
Expand All @@ -60,7 +60,7 @@ const collectorOptions = {
const metricExporter = new OTLPMetricExporter(collectorOptions);
const meterProvider = new MeterProvider({});

meterProvider.addMetricReader(new PeriodicExportingMetricReader({
meterProvider.addMetricReader(createPeriodicExportingMetricReader({
exporter: metricExporter,
exportIntervalMillis: 1000,
}));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import * as assert from 'assert';
import { InstrumentationScope, VERSION } from '@opentelemetry/core';
import {
AggregationType,
MeterProvider,
createMeterProvider,
MetricReader,
ViewOptions,
} from '@opentelemetry/sdk-metrics';
Expand Down Expand Up @@ -78,7 +78,7 @@ const defaultResource = Resource.default().merge(
);

let reader = new TestMetricReader();
let meterProvider = new MeterProvider({
let meterProvider = createMeterProvider({
resource: defaultResource,
readers: [reader],
});
Expand All @@ -90,7 +90,7 @@ export async function collect() {

export function setUp(views?: ViewOptions[]) {
reader = new TestMetricReader();
meterProvider = new MeterProvider({
meterProvider = createMeterProvider({
resource: defaultResource,
views,
readers: [reader],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ const collectorOptions = {
const metricExporter = new OTLPMetricExporter(collectorOptions);
const meterProvider = new MeterProvider({});

meterProvider.addMetricReader(new PeriodicExportingMetricReader({
meterProvider.addMetricReader(createPeriodicExportingMetricReader({
exporter: metricExporter,
exportIntervalMillis: 1000,
}));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import { Resource } from '@opentelemetry/resources';
import * as assert from 'assert';
import {
AggregationType,
MeterProvider,
createMeterProvider,
MetricReader,
} from '@opentelemetry/sdk-metrics';
import {
Expand Down Expand Up @@ -54,7 +54,7 @@ const testResource = new Resource({
});

let reader = new TestMetricReader();
let meterProvider = new MeterProvider({
let meterProvider = createMeterProvider({
resource: testResource,
readers: [reader],
});
Expand All @@ -67,7 +67,7 @@ export async function collect() {

export function setUp() {
reader = new TestMetricReader();
meterProvider = new MeterProvider({
meterProvider = createMeterProvider({
resource: testResource,
views: [
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,13 @@
* limitations under the License.
*/

import { Counter, Meter, ObservableResult } from '@opentelemetry/api';
import { MeterProvider } from '@opentelemetry/sdk-metrics';
import {
Counter,
Meter,
MeterProvider,
ObservableResult,
} from '@opentelemetry/api';
import { createMeterProvider } from '@opentelemetry/sdk-metrics';
import * as assert from 'assert';
import * as sinon from 'sinon';
import * as http from 'http';
Expand Down Expand Up @@ -200,12 +205,12 @@ describe('PrometheusExporter', () => {

describe('export', () => {
let exporter: PrometheusExporter;
let meterProvider: MeterProvider;
let meterProvider: ReturnType<typeof createMeterProvider>;
let meter: Meter;

beforeEach(async () => {
exporter = new PrometheusExporter();
meterProvider = new MeterProvider({
meterProvider = createMeterProvider({
readers: [exporter],
});
meter = meterProvider.getMeter('test-prometheus', '1');
Expand Down Expand Up @@ -471,7 +476,7 @@ describe('PrometheusExporter', () => {
let exporter: PrometheusExporter;

function setup(reader: PrometheusExporter) {
meterProvider = new MeterProvider({
meterProvider = createMeterProvider({
readers: [exporter],
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,11 @@ import * as assert from 'assert';
import { Attributes, UpDownCounter } from '@opentelemetry/api';
import {
AggregationTemporality,
AggregationType,
createMeterProvider,
DataPoint,
DataPointType,
Histogram,
MeterProvider,
MetricReader,
} from '@opentelemetry/sdk-metrics';
import * as sinon from 'sinon';
Expand All @@ -35,7 +36,6 @@ import {
serviceName,
} from './util';
import { Resource } from '@opentelemetry/resources';
import { AggregationType } from '@opentelemetry/sdk-metrics';

const attributes = {
foo1: 'bar1',
Expand Down Expand Up @@ -82,7 +82,7 @@ describe('PrometheusSerializer', () => {
describe('Singular', () => {
async function testSerializer(serializer: PrometheusSerializer) {
const reader = new TestMetricReader();
const meterProvider = new MeterProvider({
const meterProvider = createMeterProvider({
views: [
{
aggregation: { type: AggregationType.SUM },
Expand Down Expand Up @@ -132,7 +132,7 @@ describe('PrometheusSerializer', () => {
describe('Histogram', () => {
async function testSerializer(serializer: PrometheusSerializer) {
const reader = new TestMetricReader();
const meterProvider = new MeterProvider({
const meterProvider = createMeterProvider({
views: [
{
aggregation: {
Expand Down Expand Up @@ -202,7 +202,7 @@ describe('PrometheusSerializer', () => {
describe('monotonic Sum', () => {
async function testSerializer(serializer: PrometheusSerializer) {
const reader = new TestMetricReader();
const meterProvider = new MeterProvider({
const meterProvider = createMeterProvider({
views: [
{
aggregation: { type: AggregationType.SUM },
Expand Down Expand Up @@ -257,7 +257,7 @@ describe('PrometheusSerializer', () => {
describe('non-monotonic Sum', () => {
async function testSerializer(serializer: PrometheusSerializer) {
const reader = new TestMetricReader();
const meterProvider = new MeterProvider({
const meterProvider = createMeterProvider({
views: [
{
aggregation: { type: AggregationType.SUM },
Expand Down Expand Up @@ -311,7 +311,7 @@ describe('PrometheusSerializer', () => {
describe('Gauge', () => {
async function testSerializer(serializer: PrometheusSerializer) {
const reader = new TestMetricReader();
const meterProvider = new MeterProvider({
const meterProvider = createMeterProvider({
views: [
{
aggregation: {
Expand Down Expand Up @@ -367,7 +367,7 @@ describe('PrometheusSerializer', () => {
describe('with ExplicitBucketHistogramAggregation', () => {
async function testSerializer(serializer: PrometheusSerializer) {
const reader = new TestMetricReader();
const meterProvider = new MeterProvider({
const meterProvider = createMeterProvider({
views: [
{
aggregation: {
Expand Down Expand Up @@ -425,7 +425,7 @@ describe('PrometheusSerializer', () => {
it('serialize cumulative metric record on instrument that allows negative values', async () => {
const serializer = new PrometheusSerializer();
const reader = new TestMetricReader();
const meterProvider = new MeterProvider({
const meterProvider = createMeterProvider({
views: [
{
aggregation: {
Expand Down Expand Up @@ -481,7 +481,7 @@ describe('PrometheusSerializer', () => {
options: Partial<{ unit: string; exportAll: boolean }> = {}
) {
const reader = new TestMetricReader();
const meterProvider = new MeterProvider({
const meterProvider = createMeterProvider({
views: [
{
aggregation: { type: AggregationType.SUM },
Expand Down Expand Up @@ -573,7 +573,7 @@ describe('PrometheusSerializer', () => {
fn: (counter: UpDownCounter) => void
) {
const reader = new TestMetricReader();
const meterProvider = new MeterProvider({
const meterProvider = createMeterProvider({
views: [
{
aggregation: { type: AggregationType.SUM },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@
*/
import {
AggregationTemporality,
createInMemoryMetricExporter,
createMeterProvider,
DataPointType,
InMemoryMetricExporter,
MeterProvider,
} from '@opentelemetry/sdk-metrics';
import { NodeTracerProvider } from '@opentelemetry/sdk-trace-node';
import {
Expand Down Expand Up @@ -57,11 +57,11 @@ const protocol = 'http';
const hostname = 'localhost';
const pathname = '/test';
const tracerProvider = new NodeTracerProvider();
const metricsMemoryExporter = new InMemoryMetricExporter(
AggregationTemporality.DELTA
);
const metricsMemoryExporter = createInMemoryMetricExporter({
aggregationTemporality: AggregationTemporality.DELTA,
});
const metricReader = new TestMetricReader(metricsMemoryExporter);
const meterProvider = new MeterProvider({ readers: [metricReader] });
const meterProvider = createMeterProvider({ readers: [metricReader] });

instrumentation.setTracerProvider(tracerProvider);
instrumentation.setMeterProvider(meterProvider);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,9 @@ import {
SpanCustomizationHook,
} from '../../src';

import { MeterProvider } from '@opentelemetry/sdk-metrics';
import { createMeterProvider } from '@opentelemetry/sdk-metrics';
import { LoggerProvider } from '@opentelemetry/sdk-logs';
import { MeterProvider } from '@opentelemetry/api';

interface TestInstrumentationConfig extends InstrumentationConfig {
isActive?: boolean;
Expand Down Expand Up @@ -84,7 +85,7 @@ describe('BaseInstrumentation', () => {
describe('setMeterProvider', () => {
let otelTestingMeterProvider: MeterProvider;
beforeEach(() => {
otelTestingMeterProvider = new MeterProvider();
otelTestingMeterProvider = createMeterProvider();
});
it('should call _updateMetricInstruments', () => {
let called = true;
Expand Down
14 changes: 7 additions & 7 deletions experimental/packages/opentelemetry-sdk-node/src/sdk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,11 @@ import {
Resource,
ResourceDetectionConfig,
} from '@opentelemetry/resources';
import {
createMeterProvider,
MetricReader,
ViewOptions,
} from '@opentelemetry/sdk-metrics';
import {
LogRecordProcessor,
LoggerProvider,
Expand All @@ -48,11 +53,6 @@ import {
import { OTLPLogExporter as OTLPHttpLogExporter } from '@opentelemetry/exporter-logs-otlp-http';
import { OTLPLogExporter as OTLPGrpcLogExporter } from '@opentelemetry/exporter-logs-otlp-grpc';
import { OTLPLogExporter as OTLPProtoLogExporter } from '@opentelemetry/exporter-logs-otlp-proto';
import {
MeterProvider,
MetricReader,
ViewOptions,
} from '@opentelemetry/sdk-metrics';
import {
BatchSpanProcessor,
SpanProcessor,
Expand Down Expand Up @@ -105,7 +105,7 @@ export class NodeSDK {

private _tracerProvider?: NodeTracerProvider | TracerProviderWithEnvExporters;
private _loggerProvider?: LoggerProvider;
private _meterProvider?: MeterProvider;
private _meterProvider?: ReturnType<typeof createMeterProvider>;
private _serviceName?: string;
private _configuration?: Partial<NodeSDKConfiguration>;

Expand Down Expand Up @@ -296,7 +296,7 @@ export class NodeSDK {
if (this._meterProviderConfig.reader) {
readers.push(this._meterProviderConfig.reader);
}
const meterProvider = new MeterProvider({
const meterProvider = createMeterProvider({
resource: this._resource,
views: this._meterProviderConfig?.views ?? [],
readers: readers,
Expand Down
Loading
Loading