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 committed Jul 4, 2022
1 parent 6dd6af3 commit a310e37
Show file tree
Hide file tree
Showing 9 changed files with 312 additions and 213 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,19 @@ export async function collect() {
}

export function setUp() {
meterProvider = new MeterProvider({ resource: testResource });
meterProvider = new MeterProvider({
resource: testResource,
views: [{
view: {
aggregation: new ExplicitBucketHistogramAggregation([0, 100])
},
selector: {
instrument: {
name: 'int-histogram'
}
}
}]
});
reader = new TestMetricReader();
meterProvider.addMetricReader(
reader
Expand Down Expand Up @@ -96,10 +108,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,7 @@ import {
AggregationTemporality,
ExplicitBucketHistogramAggregation,
MeterProvider,
MetricReader
MetricReader, ViewRegistrationOptions
} from '@opentelemetry/sdk-metrics-base';
import {
IExportMetricsServiceRequest,
Expand Down Expand Up @@ -61,6 +61,17 @@ class TestMetricReader extends MetricReader {
}
}

export const HISTOGRAM_AGGREGATION_VIEW = {
view: {
aggregation: new ExplicitBucketHistogramAggregation([0, 100])
},
selector: {
instrument: {
name: 'int-histogram'
}
}
};

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

export function setUp() {
meterProvider = new MeterProvider({ resource: defaultResource });
export function setUp(views?: ViewRegistrationOptions[]) {
meterProvider = new MeterProvider({ resource: defaultResource, views });
reader = new TestMetricReader();
meterProvider.addMetricReader(
reader
Expand Down Expand Up @@ -158,18 +169,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,7 @@ 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 +54,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 @@ -64,7 +64,19 @@ export async function collect() {
}

export function setUp() {
meterProvider = new MeterProvider({ resource: testResource });
meterProvider = new MeterProvider({
resource: testResource,
views: [{
view: {
aggregation: new ExplicitBucketHistogramAggregation([0, 100])
},
selector: {
instrument: {
name: 'int-histogram'
}
}
}]
});
reader = new TestMetricReader();
meterProvider.addMetricReader(
reader
Expand Down Expand Up @@ -102,7 +114,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 @@ -45,6 +45,7 @@ class TestMetricReader extends MetricReader {
}

async onForceFlush() {}

async onShutdown() {}
}

Expand All @@ -67,11 +68,12 @@ describe('PrometheusSerializer', () => {
describe('Singular', () => {
async function testSerializer(serializer: PrometheusSerializer) {
const reader = new TestMetricReader();
const meterProvider = new MeterProvider();
const meterProvider = new MeterProvider(
{
views: [{ view: { aggregation: new SumAggregation() } }]
}
);
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 +114,10 @@ describe('PrometheusSerializer', () => {
describe('Histogram', () => {
async function testSerializer(serializer: PrometheusSerializer) {
const reader = new TestMetricReader();
const meterProvider = new MeterProvider();
const meterProvider = new MeterProvider({
views: [{ view: { aggregation: new ExplicitBucketHistogramAggregation([1, 10, 100]) } }]
});
meterProvider.addMetricReader(reader);
meterProvider.addView({ aggregation: new ExplicitBucketHistogramAggregation([1, 10, 100]) });
const meter = meterProvider.getMeter('test');

const histogram = meter.createHistogram('test');
Expand All @@ -139,11 +142,11 @@ describe('PrometheusSerializer', () => {
assert.strictEqual(
result,
`test_count{foo1="bar1",foo2="bar2"} 1 ${mockedHrTimeMs}\n` +
`test_sum{foo1="bar1",foo2="bar2"} 5 ${mockedHrTimeMs}\n` +
`test_bucket{foo1="bar1",foo2="bar2",le="1"} 0 ${mockedHrTimeMs}\n` +
`test_bucket{foo1="bar1",foo2="bar2",le="10"} 1 ${mockedHrTimeMs}\n` +
`test_bucket{foo1="bar1",foo2="bar2",le="100"} 1 ${mockedHrTimeMs}\n` +
`test_bucket{foo1="bar1",foo2="bar2",le="+Inf"} 1 ${mockedHrTimeMs}\n`
`test_sum{foo1="bar1",foo2="bar2"} 5 ${mockedHrTimeMs}\n` +
`test_bucket{foo1="bar1",foo2="bar2",le="1"} 0 ${mockedHrTimeMs}\n` +
`test_bucket{foo1="bar1",foo2="bar2",le="10"} 1 ${mockedHrTimeMs}\n` +
`test_bucket{foo1="bar1",foo2="bar2",le="100"} 1 ${mockedHrTimeMs}\n` +
`test_bucket{foo1="bar1",foo2="bar2",le="+Inf"} 1 ${mockedHrTimeMs}\n`
);
});

Expand All @@ -153,11 +156,11 @@ describe('PrometheusSerializer', () => {
assert.strictEqual(
result,
'test_count{foo1="bar1",foo2="bar2"} 1\n' +
'test_sum{foo1="bar1",foo2="bar2"} 5\n' +
'test_bucket{foo1="bar1",foo2="bar2",le="1"} 0\n' +
'test_bucket{foo1="bar1",foo2="bar2",le="10"} 1\n' +
'test_bucket{foo1="bar1",foo2="bar2",le="100"} 1\n' +
'test_bucket{foo1="bar1",foo2="bar2",le="+Inf"} 1\n'
'test_sum{foo1="bar1",foo2="bar2"} 5\n' +
'test_bucket{foo1="bar1",foo2="bar2",le="1"} 0\n' +
'test_bucket{foo1="bar1",foo2="bar2",le="10"} 1\n' +
'test_bucket{foo1="bar1",foo2="bar2",le="100"} 1\n' +
'test_bucket{foo1="bar1",foo2="bar2",le="+Inf"} 1\n'
);
});
});
Expand All @@ -167,9 +170,10 @@ describe('PrometheusSerializer', () => {
describe('Singular', () => {
async function testSerializer(serializer: PrometheusSerializer) {
const reader = new TestMetricReader();
const meterProvider = new MeterProvider();
const meterProvider = new MeterProvider({
views: [{ view: { aggregation: new SumAggregation() } }]
});
meterProvider.addMetricReader(reader);
meterProvider.addView({ aggregation: new SumAggregation() });
const meter = meterProvider.getMeter('test');

const counter = meter.createCounter('test_total', {
Expand All @@ -194,9 +198,9 @@ describe('PrometheusSerializer', () => {
assert.strictEqual(
result,
'# HELP test_total foobar\n' +
'# TYPE test_total counter\n' +
`test_total{val="1"} 1 ${mockedHrTimeMs}\n` +
`test_total{val="2"} 1 ${mockedHrTimeMs}\n`
'# TYPE test_total counter\n' +
`test_total{val="1"} 1 ${mockedHrTimeMs}\n` +
`test_total{val="2"} 1 ${mockedHrTimeMs}\n`
);
});

Expand All @@ -206,19 +210,20 @@ describe('PrometheusSerializer', () => {
assert.strictEqual(
result,
'# HELP test_total foobar\n' +
'# TYPE test_total counter\n' +
'test_total{val="1"} 1\n' +
'test_total{val="2"} 1\n'
'# TYPE test_total counter\n' +
'test_total{val="1"} 1\n' +
'test_total{val="2"} 1\n'
);
});
});

describe('with ExplicitBucketHistogramAggregation', () => {
async function testSerializer(serializer: PrometheusSerializer) {
const reader = new TestMetricReader();
const meterProvider = new MeterProvider();
const meterProvider = new MeterProvider({
views: [{ view: { aggregation: new ExplicitBucketHistogramAggregation([1, 10, 100]) } }]
});
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 @@ -246,19 +251,19 @@ describe('PrometheusSerializer', () => {
assert.strictEqual(
result,
'# HELP test foobar\n' +
'# TYPE test histogram\n' +
`test_count{val="1"} 3 ${mockedHrTimeMs}\n` +
`test_sum{val="1"} 175 ${mockedHrTimeMs}\n` +
`test_bucket{val="1",le="1"} 0 ${mockedHrTimeMs}\n` +
`test_bucket{val="1",le="10"} 1 ${mockedHrTimeMs}\n` +
`test_bucket{val="1",le="100"} 2 ${mockedHrTimeMs}\n` +
`test_bucket{val="1",le="+Inf"} 3 ${mockedHrTimeMs}\n` +
`test_count{val="2"} 1 ${mockedHrTimeMs}\n` +
`test_sum{val="2"} 5 ${mockedHrTimeMs}\n` +
`test_bucket{val="2",le="1"} 0 ${mockedHrTimeMs}\n` +
`test_bucket{val="2",le="10"} 1 ${mockedHrTimeMs}\n` +
`test_bucket{val="2",le="100"} 1 ${mockedHrTimeMs}\n` +
`test_bucket{val="2",le="+Inf"} 1 ${mockedHrTimeMs}\n`
'# TYPE test histogram\n' +
`test_count{val="1"} 3 ${mockedHrTimeMs}\n` +
`test_sum{val="1"} 175 ${mockedHrTimeMs}\n` +
`test_bucket{val="1",le="1"} 0 ${mockedHrTimeMs}\n` +
`test_bucket{val="1",le="10"} 1 ${mockedHrTimeMs}\n` +
`test_bucket{val="1",le="100"} 2 ${mockedHrTimeMs}\n` +
`test_bucket{val="1",le="+Inf"} 3 ${mockedHrTimeMs}\n` +
`test_count{val="2"} 1 ${mockedHrTimeMs}\n` +
`test_sum{val="2"} 5 ${mockedHrTimeMs}\n` +
`test_bucket{val="2",le="1"} 0 ${mockedHrTimeMs}\n` +
`test_bucket{val="2",le="10"} 1 ${mockedHrTimeMs}\n` +
`test_bucket{val="2",le="100"} 1 ${mockedHrTimeMs}\n` +
`test_bucket{val="2",le="+Inf"} 1 ${mockedHrTimeMs}\n`
);
});
});
Expand All @@ -267,9 +272,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: [{ view: { aggregation: new SumAggregation() } }]
});
meterProvider.addMetricReader(reader);
meterProvider.addView({ aggregation: new SumAggregation() });
const meter = meterProvider.getMeter('test');

const counter = meter.createCounter(name);
Expand Down Expand Up @@ -306,9 +312,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: [{ view: { aggregation: new SumAggregation() } }]
});
meterProvider.addMetricReader(reader);
meterProvider.addView({ aggregation: new SumAggregation() });
const meter = meterProvider.getMeter('test');

const counter = meter.createUpDownCounter(name);
Expand Down Expand Up @@ -392,13 +399,13 @@ describe('PrometheusSerializer', () => {
assert.strictEqual(
result,
'test_total{' +
'backslash="\u005c\u005c",' +
'doubleQuote="\u005c\u0022",' +
'lineFeed="\u005c\u006e",' +
'backslashN="\u005c\u005c\u006e",' +
'backslashDoubleQuote="\u005c\u005c\u005c\u0022",' +
'backslashLineFeed="\u005c\u005c\u005c\u006e"' +
`} 1 ${mockedHrTimeMs}\n`
'backslash="\u005c\u005c",' +
'doubleQuote="\u005c\u0022",' +
'lineFeed="\u005c\u006e",' +
'backslashN="\u005c\u005c\u006e",' +
'backslashDoubleQuote="\u005c\u005c\u005c\u0022",' +
'backslashLineFeed="\u005c\u005c\u005c\u006e"' +
`} 1 ${mockedHrTimeMs}\n`
);
});

Expand Down
Loading

0 comments on commit a310e37

Please sign in to comment.