Skip to content

Commit

Permalink
feature(views): move views registration to MeterProvider constructor (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
pichlermarc authored Jul 18, 2022
1 parent db32860 commit bec6c3c
Show file tree
Hide file tree
Showing 16 changed files with 419 additions and 377 deletions.
2 changes: 2 additions & 0 deletions experimental/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ All notable changes to experimental packages in this project will be documented

### :boom: Breaking Change

* feature(views): move views registration to MeterProvider constructor [#3066](https://github.com/open-telemetry/opentelemetry-js/pull/3066) @pichlermarc

### :rocket: (Enhancement)

### :bug: (Bug Fix)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import {
ExplicitBucketHistogramAggregation,
MeterProvider,
MetricReader,
View,
} from '@opentelemetry/sdk-metrics-base';
import { IKeyValue, IMetric, IResource } from '@opentelemetry/otlp-transformer';

Expand Down Expand Up @@ -59,7 +60,15 @@ export async function collect() {
}

export function setUp() {
meterProvider = new MeterProvider({ resource: testResource });
meterProvider = new MeterProvider({
resource: testResource,
views: [
new View({
aggregation: new ExplicitBucketHistogramAggregation([0, 100]),
instrumentName: 'int-histogram',
})
]
});
reader = new TestMetricReader();
meterProvider.addMetricReader(
reader
Expand Down Expand Up @@ -96,10 +105,7 @@ export function mockObservableGauge(
}

export function mockHistogram(): Histogram {
const name = 'int-histogram';
meterProvider.addView({ aggregation: new ExplicitBucketHistogramAggregation([0, 100]) });

return meter.createHistogram(name, {
return meter.createHistogram('int-histogram', {
description: 'sample histogram description',
valueType: ValueType.INT,
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import {
ensureExportMetricsServiceRequestIsSet, ensureHeadersContain,
ensureHistogramIsCorrect,
ensureObservableGaugeIsCorrect,
ensureWebResourceIsCorrect,
ensureWebResourceIsCorrect, HISTOGRAM_AGGREGATION_VIEW,
mockCounter,
mockHistogram,
mockObservableGauge,
Expand All @@ -47,7 +47,7 @@ describe('OTLPMetricExporter - web', () => {
let errorStub: sinon.SinonStub;

beforeEach(async () => {
setUp();
setUp([HISTOGRAM_AGGREGATION_VIEW]);
stubOpen = sinon.stub(XMLHttpRequest.prototype, 'open');
sinon.stub(XMLHttpRequest.prototype, 'send');
stubBeacon = sinon.stub(navigator, 'sendBeacon');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ import {
AggregationTemporality,
ExplicitBucketHistogramAggregation,
MeterProvider,
MetricReader
MetricReader,
View
} from '@opentelemetry/sdk-metrics-base';
import {
IExportMetricsServiceRequest,
Expand Down Expand Up @@ -61,6 +62,11 @@ class TestMetricReader extends MetricReader {
}
}

export const HISTOGRAM_AGGREGATION_VIEW = new View({
aggregation: new ExplicitBucketHistogramAggregation([0, 100]),
instrumentName: 'int-histogram',
});

const defaultResource = Resource.default().merge(new Resource({
service: 'ui',
version: 1,
Expand All @@ -78,8 +84,8 @@ export async function collect() {
return (await reader.collect())!;
}

export function setUp() {
meterProvider = new MeterProvider({ resource: defaultResource });
export function setUp(views?: View[]) {
meterProvider = new MeterProvider({ resource: defaultResource, views });
reader = new TestMetricReader();
meterProvider.addMetricReader(
reader
Expand Down Expand Up @@ -124,7 +130,6 @@ export function mockDoubleCounter(): Counter {
}



export function mockObservableCounter(
callback: (observableResult: ObservableResult) => void,
name = 'double-observable-counter'
Expand Down Expand Up @@ -158,18 +163,7 @@ export function mockObservableUpDownCounter(
}

export function mockHistogram(): Histogram {
const name = 'int-histogram';

meterProvider.addView({
aggregation: new ExplicitBucketHistogramAggregation([0, 100])
},
{
instrument: {
name: name
}
});

return meter.createHistogram(name, {
return meter.createHistogram('int-histogram', {
description: 'sample histogram description',
valueType: ValueType.INT,
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,10 @@ import {
mockCounter,
mockObservableGauge,
mockHistogram,
collect, shutdown, setUp,
collect,
shutdown,
setUp,
HISTOGRAM_AGGREGATION_VIEW,
} from '../metricsHelper';
import { MockedResponse } from './nodeHelpers';
import { AggregationTemporality, ResourceMetrics } from '@opentelemetry/sdk-metrics-base';
Expand All @@ -54,7 +57,7 @@ describe('OTLPMetricExporter - node with json over http', () => {
let metrics: ResourceMetrics;

beforeEach(async () => {
setUp();
setUp([HISTOGRAM_AGGREGATION_VIEW]);
});

afterEach(async () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ import {
AggregationTemporality,
ExplicitBucketHistogramAggregation,
MeterProvider,
MetricReader
MetricReader,
View
} from '@opentelemetry/sdk-metrics-base';
import { IExportMetricsServiceRequest, IKeyValue, IMetric } from '@opentelemetry/otlp-transformer';
import { Stream } from 'stream';
Expand Down Expand Up @@ -64,7 +65,15 @@ export async function collect() {
}

export function setUp() {
meterProvider = new MeterProvider({ resource: testResource });
meterProvider = new MeterProvider({
resource: testResource,
views: [
new View({
aggregation: new ExplicitBucketHistogramAggregation([0, 100]),
instrumentName: 'int-histogram',
})
]
});
reader = new TestMetricReader();
meterProvider.addMetricReader(
reader
Expand Down Expand Up @@ -102,7 +111,6 @@ export function mockObservableGauge(

export function mockHistogram(): Histogram {
const name = 'int-histogram';
meterProvider.addView({ aggregation: new ExplicitBucketHistogramAggregation([0, 100]) });

return meter.createHistogram(name, {
description: 'sample histogram description',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import {
ExplicitBucketHistogramAggregation,
SumAggregation,
Histogram,
View,
} from '@opentelemetry/sdk-metrics-base';
import * as sinon from 'sinon';
import { PrometheusSerializer } from '../src';
Expand All @@ -45,6 +46,7 @@ class TestMetricReader extends MetricReader {
}

async onForceFlush() {}

async onShutdown() {}
}

Expand All @@ -67,11 +69,12 @@ describe('PrometheusSerializer', () => {
describe('Singular', () => {
async function testSerializer(serializer: PrometheusSerializer) {
const reader = new TestMetricReader();
const meterProvider = new MeterProvider();
const meterProvider = new MeterProvider(
{
views: [new View({ aggregation: new SumAggregation(), instrumentName: '*' })]
}
);
meterProvider.addMetricReader(reader);
meterProvider.addView({
aggregation: new SumAggregation(),
});
const meter = meterProvider.getMeter('test');

const counter = meter.createCounter('test_total');
Expand Down Expand Up @@ -112,9 +115,13 @@ describe('PrometheusSerializer', () => {
describe('Histogram', () => {
async function testSerializer(serializer: PrometheusSerializer) {
const reader = new TestMetricReader();
const meterProvider = new MeterProvider();
const meterProvider = new MeterProvider({
views: [new View({
aggregation: new ExplicitBucketHistogramAggregation([1, 10, 100]),
instrumentName: '*'
})]
});
meterProvider.addMetricReader(reader);
meterProvider.addView({ aggregation: new ExplicitBucketHistogramAggregation([1, 10, 100]) });
const meter = meterProvider.getMeter('test');

const histogram = meter.createHistogram('test');
Expand Down Expand Up @@ -167,9 +174,10 @@ describe('PrometheusSerializer', () => {
describe('Singular', () => {
async function testSerializer(serializer: PrometheusSerializer) {
const reader = new TestMetricReader();
const meterProvider = new MeterProvider();
const meterProvider = new MeterProvider({
views: [new View({ aggregation: new SumAggregation(), instrumentName: '*' })]
});
meterProvider.addMetricReader(reader);
meterProvider.addView({ aggregation: new SumAggregation() });
const meter = meterProvider.getMeter('test');

const counter = meter.createCounter('test_total', {
Expand Down Expand Up @@ -216,9 +224,13 @@ describe('PrometheusSerializer', () => {
describe('with ExplicitBucketHistogramAggregation', () => {
async function testSerializer(serializer: PrometheusSerializer) {
const reader = new TestMetricReader();
const meterProvider = new MeterProvider();
const meterProvider = new MeterProvider({
views: [new View({
aggregation: new ExplicitBucketHistogramAggregation([1, 10, 100]),
instrumentName: '*'
})]
});
meterProvider.addMetricReader(reader);
meterProvider.addView({ aggregation: new ExplicitBucketHistogramAggregation([1, 10, 100]) });
const meter = meterProvider.getMeter('test');

const histogram = meter.createHistogram('test', {
Expand Down Expand Up @@ -267,9 +279,10 @@ describe('PrometheusSerializer', () => {
describe('validate against metric conventions', () => {
async function getCounterResult(name: string, serializer: PrometheusSerializer) {
const reader = new TestMetricReader();
const meterProvider = new MeterProvider();
const meterProvider = new MeterProvider({
views: [new View({ aggregation: new SumAggregation(), instrumentName: '*' })]
});
meterProvider.addMetricReader(reader);
meterProvider.addView({ aggregation: new SumAggregation() });
const meter = meterProvider.getMeter('test');

const counter = meter.createCounter(name);
Expand Down Expand Up @@ -306,9 +319,10 @@ describe('PrometheusSerializer', () => {
describe('serialize non-normalized values', () => {
async function testSerializer(serializer: PrometheusSerializer, name: string, fn: (counter: UpDownCounter) => void) {
const reader = new TestMetricReader();
const meterProvider = new MeterProvider();
const meterProvider = new MeterProvider({
views: [new View({ aggregation: new SumAggregation(), instrumentName: '*' })]
});
meterProvider.addMetricReader(reader);
meterProvider.addView({ aggregation: new SumAggregation() });
const meter = meterProvider.getMeter('test');

const counter = meter.createUpDownCounter(name);
Expand Down
13 changes: 13 additions & 0 deletions experimental/packages/opentelemetry-sdk-metrics-base/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,19 @@ async function batchObservableCallback(batchObservableResult) {
}
```

Views can be registered when instantiating a `MeterProvider`:

```js
const meterProvider = new MeterProvider({
views: [
// override the bucket boundaries on `my.histogram` to [0, 50, 100]
new View({ aggregation: new ExplicitBucketHistogramAggregation([0, 50, 100]), instrumentName: 'my.histogram'}),
// rename 'my.counter' to 'my.renamed.counter'
new View({ name: 'my.renamed.counter', instrumentName: 'my.counter'})
]
})
```

## Example

See [examples/prometheus](https://github.com/open-telemetry/opentelemetry-js/tree/main/experimental/examples/prometheus) for an end-to-end example, including exporting metrics.
Expand Down
Loading

0 comments on commit bec6c3c

Please sign in to comment.