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

feature(views): move views registration to MeterProvider constructor #3066

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
a310e37
feature(views): move views registration to MeterProvider constructor
pichlermarc Jun 29, 2022
a44657e
feature(views): introduce UserView class.
pichlermarc Jul 4, 2022
4834310
feature(views): add tests for UserView.
pichlermarc Jul 4, 2022
b8535a3
feature(views): remove View class.
pichlermarc Jul 4, 2022
ebe4d53
feature(views): rename UserView -> View.
pichlermarc Jul 4, 2022
6c511ab
feature(views): update tests.
pichlermarc Jul 4, 2022
84c3e85
feature(views): collapse options into one.
pichlermarc Jul 5, 2022
a937ffc
feature(views): verify that at least one selector option is used.
pichlermarc Jul 5, 2022
b48efde
feature(views): undo accidental re-format.
pichlermarc Jul 5, 2022
7a91b9d
feature(views): cleanup.
pichlermarc Jul 5, 2022
389521e
fix(changelog): add changelog entry.
pichlermarc Jul 5, 2022
38122b7
Merge branch 'main' into meterprovider-constructor-views
pichlermarc Jul 7, 2022
bf7caab
fix(changelog): move changelog entry.
pichlermarc Jul 7, 2022
59e69ae
fix(views): address review comments.
pichlermarc Jul 8, 2022
dc2b657
fix(views): rename isSelectorProvided -> isSelectorNotProvided
pichlermarc Jul 8, 2022
7892706
fix(views): amend documentation.
pichlermarc Jul 8, 2022
600a49c
Merge remote-tracking branch 'upstream/main' into meterprovider-const…
pichlermarc Jul 12, 2022
dd13da0
fix(meterprovider): address review comments.
pichlermarc Jul 13, 2022
87271e8
docs(sdk-metrics-base): remove empty lines.
pichlermarc Jul 13, 2022
da511f3
Merge remote-tracking branch 'upstream/main' into meterprovider-const…
pichlermarc Jul 15, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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