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
* refactor(sdk-trace-base)!: remove `BasicTracerProvider.addSpanProcessor` API in favor of constructor options. [#5134](https://github.com/open-telemetry/opentelemetry-js/pull/5134) @david-luna

### :rocket: (Enhancement)
Expand Down
52 changes: 27 additions & 25 deletions doc/metrics.md
Original file line number Diff line number Diff line change
Expand Up @@ -397,17 +397,20 @@ Below an example is given how you can define explicit buckets for a histogram.

```typescript
// Define view for the histogram metric
const histogramView = new View({
aggregation: new ExplicitBucketHistogramAggregation([0, 1, 5, 10, 15, 20, 25, 30]),
const histogramView: ViewOptions = {
aggregation: {
type: AggregationType.EXPLICIT_BUCKET_HISTOGRAM,
boundaries: [0, 1, 5, 10, 15, 20, 25, 30]
},
instrumentName: 'http.server.duration',
instrumentType: InstrumentType.HISTOGRAM,
});
};

// Note, the instrumentName is the same as the name that has been passed for
// the Meter#createHistogram function

// Create an instance of the metric provider
const meterProvider = new MeterProvider({
const meterProvider = createMeterProvider({
views: [
histogramView
]
Expand Down Expand Up @@ -441,20 +444,20 @@ instruments with a specific name:
The following view drops all instruments that are associated with a meter named `pubsub`:

```typescript
const dropView = new View({
aggregation: new DropAggregation(),
const dropView: ViewOptions = {
aggregation: { type: AggregationType.DROP },
meterName: 'pubsub',
});
};
```

Alternatively, you can also drop instruments with a specific instrument name,
for example, all instruments of which the name starts with `http`:

```typescript
const dropView = new View({
aggregation: new DropAggregation(),
const dropView: ViewOptions = {
aggregation: { type: AggregationType.DROP },
instrumentName: 'http*',
});
};
```

### Customizing the metric attributes of instrument
Expand All @@ -467,12 +470,12 @@ In the example below will drop all attributes except attribute `environment` for
all instruments.

```typescript
new View({
const attributeLimitingView: ViewOptions = {
// only export the attribute 'environment'
attributeKeys: ['environment'],
attributeProcesssors: [createAllowListAttributesProcessor(['environment'])],
// apply the view to all instruments
instrumentName: '*',
})
};
```

## Exporting measurements
Expand All @@ -497,22 +500,24 @@ to use the Prometheus exporter `PrometheusExporter` which is included in the

```typescript
const { PrometheusExporter } = require('@opentelemetry/exporter-prometheus');
const { MeterProvider } = require('@opentelemetry/sdk-metrics');
const { createMetricReader } = require('@opentelemetry/sdk-metrics');

// Add your port to the Prometheus options
const options = { port: 9464 };
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 = createMeterProvider({
readers: [exporter]
});

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

// Now, start recording data
const counter = meter.createCounter('metric_name', {
description: 'Example of a counter'
});
counter.add(10, { pid: process.pid });
counter.add(10, {pid: process.pid});
```

In the above example the instantiated `PrometheusExporter` is configured to expose
Expand All @@ -534,19 +539,16 @@ The example below shows how you can configure OpenTelemetry JavaScript to use
the OTLP exporter using http/protobuf.

```typescript
const { MeterProvider, PeriodicExportingMetricReader } = require('@opentelemetry/sdk-metrics');
const { createMeterProvider, PeriodicExportingMetricReader } = require('@opentelemetry/sdk-metrics');
const { OTLPMetricExporter } = require('@opentelemetry/exporter-metrics-otlp-proto');
const collectorOptions = {
url: '<opentelemetry-collector-url>', // url is optional and can be omitted - default is http://localhost:4318/v1/metrics
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 = createMeterProvider({
readers: [createPeriodicExportingMetricReader({ exporter: metricExporter, exportIntervalMillis: 1000 })],
});

// Now, start recording data
const meter = meterProvider.getMeter('example-meter');
Expand Down
15 changes: 8 additions & 7 deletions examples/opentelemetry-web/examples/metrics/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const { DiagConsoleLogger, DiagLogLevel, diag, metrics } = require('@opentelemetry/api');
const { OTLPMetricExporter } = require('@opentelemetry/exporter-metrics-otlp-http');
const { MeterProvider, PeriodicExportingMetricReader } = require('@opentelemetry/sdk-metrics');
const { createMeterProvider, createPeriodicExportingMetricReader } = require('@opentelemetry/sdk-metrics');

// Optional and only needed to see the internal diagnostic logging (during development)
diag.setLogger(new DiagConsoleLogger(), DiagLogLevel.DEBUG);
Expand All @@ -18,13 +18,14 @@ function stopMetrics() {
function startMetrics() {
console.log('STARTING METRICS');

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

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

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

Expand Down
39 changes: 17 additions & 22 deletions examples/otlp-exporter-node/metrics.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,11 @@ const { DiagConsoleLogger, DiagLogLevel, diag } = require('@opentelemetry/api');
const { OTLPMetricExporter } = require('@opentelemetry/exporter-metrics-otlp-http');
// const { OTLPMetricExporter } = require('@opentelemetry/exporter-metrics-otlp-grpc');
// const { OTLPMetricExporter } = require('@opentelemetry/exporter-metrics-otlp-proto');
// const { ConsoleMetricExporter } = require('@opentelemetry/sdk-metrics');
// const { createConsoleMetricExporter } = require('@opentelemetry/sdk-metrics');
const {
ExponentialHistogramAggregation,
MeterProvider,
PeriodicExportingMetricReader,
View,
createMeterProvider,
createPeriodicExportingMetricReader,
AggregationType,
} = require('@opentelemetry/sdk-metrics');
const { Resource } = require('@opentelemetry/resources');
const {
Expand All @@ -25,30 +24,26 @@ const metricExporter = new OTLPMetricExporter({
// },
});

// Define view for the exponential histogram metric
const expHistogramView = new View({
aggregation: new ExponentialHistogramAggregation(),
// Note, the instrumentName is the same as the name that has been passed for
// the Meter#createHistogram function for exponentialHistogram.
instrumentName: 'test_exponential_histogram',
});

// Create an instance of the metric provider
const meterProvider = new MeterProvider({
const meterProvider = createMeterProvider({
readers: [createPeriodicExportingMetricReader({
exporter: metricExporter,
// exporter: createConsoleMetricExporter(),
exportIntervalMillis: 1000,
})],
resource: new Resource({
[SEMRESATTRS_SERVICE_NAME]: 'basic-metric-service',
}),
views: [expHistogramView],
// Define view for the exponential histogram metric
views: [{
aggregation: { type: AggregationType.EXPONENTIAL_HISTOGRAM },
// Note, the instrumentName is the same as the name that has been passed for
// the Meter#createHistogram function for exponentialHistogram.
instrumentName: 'test_exponential_histogram',
}],
});

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
27 changes: 15 additions & 12 deletions experimental/examples/opencensus-shim/setup.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ const {
NodeTracerProvider,
BatchSpanProcessor,
} = require('@opentelemetry/sdk-trace-node');
const { MeterProvider } = require('@opentelemetry/sdk-metrics');
const { createMeterProvider } = require('@opentelemetry/sdk-metrics');
const {
OTLPTraceExporter,
} = require('@opentelemetry/exporter-trace-otlp-grpc');
Expand Down Expand Up @@ -58,17 +58,20 @@ module.exports = function setup(serviceName) {
});
tracerProvider.register();

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

metrics.setGlobalMeterProvider(meterProvider);

// Start OpenCensus tracing
Expand Down
7 changes: 4 additions & 3 deletions experimental/examples/prometheus/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use strict';

const { DiagConsoleLogger, DiagLogLevel, diag } = require('@opentelemetry/api');
const { MeterProvider } = require('@opentelemetry/sdk-metrics');
const { MeterProvider, createMeterProvider} = require('@opentelemetry/sdk-metrics');
const { PrometheusExporter } = require('@opentelemetry/exporter-prometheus');

// Optional and only needed to see the internal diagnostic logging (during development)
Expand All @@ -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 = createMeterProvider({
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 @@ -27,7 +27,7 @@ The OTLPMetricsExporter in Node expects the URL to only be the hostname. It will
options that work with trace also work with metrics.

```js
const { MeterProvider, PeriodicExportingMetricReader } = require('@opentelemetry/sdk-metrics');
const { createPeriodicExportingMetricReader, createMeterProvider } = require('@opentelemetry/sdk-metrics');
const { OTLPMetricExporter } = require('@opentelemetry/exporter-metrics-otlp-grpc');
const collectorOptions = {
// url is optional and can be omitted - default is http://localhost:4317
Expand All @@ -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 = createMeterProvider({
readers: [
createPeriodicExportingMetricReader({
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 @@ -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 @@ -26,7 +26,7 @@ the [Collector Trace Exporter for web and node][trace-exporter-url].
The OTLPMetricExporter in Web expects the endpoint to end in `/v1/metrics`.

```js
import { MeterProvider, PeriodicExportingMetricReader } from '@opentelemetry/sdk-metrics';
import { createMeterProvider, createPeriodicExportingMetricReader } from '@opentelemetry/sdk-metrics';
import { OTLPMetricExporter } from '@opentelemetry/exporter-metrics-otlp-http';

const collectorOptions = {
Expand All @@ -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 = createMeterProvider({
readers: [
createPeriodicExportingMetricReader({
exporter: metricExporter,
exportIntervalMillis: 1000,
})
]
});

// Now, start recording data
const meter = meterProvider.getMeter('example-meter');
Expand All @@ -51,19 +53,21 @@ counter.add(10, { 'key': 'value' });
## Metrics in Node

```js
const { MeterProvider, PeriodicExportingMetricReader } = require('@opentelemetry/sdk-metrics');
const { createMeterProvider, createPeriodicExportingMetricReader } = require('@opentelemetry/sdk-metrics');
const { OTLPMetricExporter } = require('@opentelemetry/exporter-metrics-otlp-http');
const collectorOptions = {
url: '<opentelemetry-collector-url>', // url is optional and can be omitted - default is http://localhost:4318/v1/metrics
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 = createMeterProvider({
readers: [
createPeriodicExportingMetricReader({
exporter: metricExporter,
exportIntervalMillis: 1000,
})
]
});

// Now, start recording data
const meter = meterProvider.getMeter('example-meter');
Expand Down
Loading
Loading