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(api-metrics): async instruments spec compliance #2569

Merged
merged 10 commits into from
Nov 22, 2021
6 changes: 3 additions & 3 deletions examples/metrics/metrics/observer.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,14 @@ const meter = new MeterProvider({
interval: 2000,
}).getMeter('example-meter');

meter.createObservableGauge('cpu_core_usage', {
description: 'Example of a sync observable gauge with callback',
}, async (observableResult) => { // this callback is called once per each interval
meter.createObservableGauge('cpu_core_usage', async (observableResult) => { // this callback is called once per each interval
await new Promise((resolve) => {
setTimeout(() => { resolve(); }, 50);
});
observableResult.observe(getRandomValue(), { core: '1' });
observableResult.observe(getRandomValue(), { core: '2' });
}, {
description: 'Example of a sync observable gauge with callback',
});

function getRandomValue() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ import {
ObservableUpDownCounter,
} from './types/Metric';
import { ObservableResult } from './types/ObservableResult';
import { Observation } from './types/Observation';

/**
* NoopMeter is a noop implementation of the {@link Meter} interface. It reuses
Expand Down Expand Up @@ -66,41 +65,41 @@ export class NoopMeter implements Meter {
/**
* Returns a constant noop observable gauge.
* @param name the name of the metric.
* @param callback the observable gauge callback
* @param [options] the metric options.
* @param [callback] the observable gauge callback
*/
createObservableGauge(
_name: string,
_callback: (observableResult: ObservableResult) => void,
_options?: MetricOptions,
_callback?: (observableResult: ObservableResult) => void
): ObservableGauge {
return NOOP_OBSERVABLE_GAUGE_METRIC;
}

/**
* Returns a constant noop observable counter.
* @param name the name of the metric.
* @param callback the observable counter callback
* @param [options] the metric options.
* @param [callback] the observable counter callback
*/
createObservableCounter(
_name: string,
_callback: (observableResult: ObservableResult) => void,
_options?: MetricOptions,
_callback?: (observableResult: ObservableResult) => void
): ObservableCounter {
return NOOP_OBSERVABLE_COUNTER_METRIC;
}

/**
* Returns a constant noop up down observable counter.
* @param name the name of the metric.
* @param callback the up down observable counter callback
* @param [options] the metric options.
* @param [callback] the up down observable counter callback
*/
createObservableUpDownCounter(
_name: string,
_callback: (observableResult: ObservableResult) => void,
_options?: MetricOptions,
_callback?: (observableResult: ObservableResult) => void
): ObservableUpDownCounter {
return NOOP_OBSERVABLE_UP_DOWN_COUNTER_METRIC;
}
Expand All @@ -120,14 +119,7 @@ export class NoopHistogramMetric extends NoopMetric implements Histogram {
record(_value: number, _labels: Labels): void {}
}

export class NoopObservableBaseMetric extends NoopMetric implements ObservableBase {
observation(): Observation {
return {
observable: this as ObservableBase,
value: 0,
};
}
}
export class NoopObservableBaseMetric extends NoopMetric implements ObservableBase {}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does NoopMetric serve any purpose here or anywhere?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm, the only use case can be observable instanceof NoopMetric. However, there are no API spec constraints on class hierarchy here, maybe we can remove it.


export const NOOP_METER = new NoopMeter();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ export * from './NoopMeterProvider';
export * from './types/Meter';
export * from './types/MeterProvider';
export * from './types/Metric';
export * from './types/Observation';
export * from './types/ObservableResult';

import { MetricsAPI } from './api/metrics';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,36 +81,36 @@ export interface Meter {
/**
* Creates a new `ObservableGauge` metric.
* @param name the name of the metric.
* @param callback the observable callback
* @param [options] the metric options.
* @param [callback] the observable callback
*/
createObservableGauge(
name: string,
options?: MetricOptions,
callback?: (observableResult: ObservableResult) => void
callback: (observableResult: ObservableResult) => void,
options?: MetricOptions
): ObservableGauge;

/**
* Creates a new `ObservableCounter` metric.
* @param name the name of the metric.
* @param callback the observable callback
* @param [options] the metric options.
* @param [callback] the observable callback
*/
createObservableCounter(
name: string,
options?: MetricOptions,
callback?: (observableResult: ObservableResult) => void
callback: (observableResult: ObservableResult) => void,
options?: MetricOptions
): ObservableCounter;

/**
* Creates a new `ObservableUpDownCounter` metric.
* @param name the name of the metric.
* @param callback the observable callback
* @param [options] the metric options.
* @param [callback] the observable callback
*/
createObservableUpDownCounter(
name: string,
options?: MetricOptions,
callback?: (observableResult: ObservableResult) => void
callback: (observableResult: ObservableResult) => void,
options?: MetricOptions
): ObservableUpDownCounter;
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,6 @@
* limitations under the License.
*/

import {
Observation,
} from './Observation';

/**
* Options needed for metric creation
*/
Expand Down Expand Up @@ -112,13 +108,10 @@ export interface Histogram {
record(value: number, labels?: Labels): void;
}

// ObservableBase has to be an Object but for now there is no field or method
// declared.
/** Base interface for the Observable metrics. */
export interface ObservableBase {
observation: (
value: number,
labels?: Labels,
) => Observation;
}
export type ObservableBase = Record<never, never>;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Record<never, never> only allows the empty object correct?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It declares an object type, and it can be extended to have any keys. The reason I use Record here is that the linter rules disallow empty interface interface ObservableBase {}.


/** Base interface for the ObservableGauge metrics. */
export type ObservableGauge = ObservableBase;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,5 @@ import { Labels } from './Metric';
* Interface that is being used in callback function for Observable Metric
*/
export interface ObservableResult {
observe(value: number, labels: Labels): void;
observe(value: number, labels?: Labels): void;
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,11 @@ export function mockObservableGauge(
meter['_metrics'].get(name) ||
meter.createObservableGauge(
name,
callback,
{
description: 'sample observable gauge description',
valueType: ValueType.DOUBLE,
},
callback
}
);
metric.clear();
metric.bind({});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,11 +83,11 @@ export function mockObservableGauge(
meter['_metrics'].get(name) ||
meter.createObservableGauge(
name,
callback,
{
description: 'sample observable gauge description',
valueType: ValueType.DOUBLE,
},
callback
}
);
metric.clear();
metric.bind({});
Expand All @@ -102,11 +102,11 @@ export function mockObservableCounter(
meter['_metrics'].get(name) ||
meter.createObservableCounter(
name,
callback,
{
description: 'sample observable counter description',
valueType: ValueType.DOUBLE,
},
callback
}
);
metric.clear();
metric.bind({});
Expand All @@ -121,11 +121,11 @@ export function mockObservableUpDownCounter(
meter['_metrics'].get(name) ||
meter.createObservableUpDownCounter(
name,
callback,
{
description: 'sample observable up down counter description',
valueType: ValueType.DOUBLE,
},
callback
}
);
metric.clear();
metric.bind({});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,11 @@ export function mockObservableGauge(
meter['_metrics'].get(name) ||
meter.createObservableGauge(
name,
callback,
{
description: 'sample observable gauge description',
valueType: ValueType.DOUBLE,
},
callback
}
);
metric.clear();
metric.bind({});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -286,14 +286,14 @@ describe('PrometheusExporter', () => {

meter.createObservableGauge(
'metric_observable_gauge',
{
description: 'a test description',
},
(observableResult: ObservableResult) => {
observableResult.observe(getCpuUsage(), {
pid: String(123),
core: '1',
});
},
{
description: 'a test description',
}
);

Expand Down Expand Up @@ -477,13 +477,13 @@ describe('PrometheusExporter', () => {

meter.createObservableCounter(
'metric_observable_counter',
{
description: 'a test description',
},
(observableResult: ObservableResult) => {
observableResult.observe(getValue(), {
key1: 'labelValue1',
});
},
{
description: 'a test description',
}
);

Expand Down Expand Up @@ -517,13 +517,13 @@ describe('PrometheusExporter', () => {

meter.createObservableUpDownCounter(
'metric_observable_up_down_counter',
{
description: 'a test description',
},
(observableResult: ObservableResult) => {
observableResult.observe(getValue(), {
key1: 'labelValue1',
});
},
{
description: 'a test description',
}
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,6 @@ describe('PrometheusSerializer', () => {
}).getMeter('test');
const observableGauge = meter.createObservableGauge(
'test',
{},
observableResult => {
observableResult.observe(1, labels);
}
Expand All @@ -128,7 +127,6 @@ describe('PrometheusSerializer', () => {
}).getMeter('test');
const observableGauge = meter.createObservableGauge(
'test',
{},
observableResult => {
observableResult.observe(1, labels);
}
Expand Down Expand Up @@ -306,11 +304,11 @@ describe('PrometheusSerializer', () => {
const processor = new PrometheusLabelsBatcher();
const observableGauge = meter.createObservableGauge(
'test',
{
description: 'foobar',
},
observableResult => {
observableResult.observe(1, labels);
},
{
description: 'foobar',
}
) as ObservableGaugeMetric;
await meter.collect();
Expand Down
Loading