Skip to content

Commit

Permalink
feat(api-metrics): async instruments spec compliance (#2569)
Browse files Browse the repository at this point in the history
Co-authored-by: Valentin Marchaud <contact@vmarchaud.fr>
  • Loading branch information
legendecas and vmarchaud committed Nov 22, 2021
1 parent 9b5feb2 commit 5e9c7e1
Show file tree
Hide file tree
Showing 7 changed files with 23 additions and 64 deletions.
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
22 changes: 7 additions & 15 deletions experimental/packages/opentelemetry-api-metrics/src/NoopMeter.ts
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, _attributes: Attributes): void {}
}

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

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, attributes?: Attributes): 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,
attributes?: Attributes,
) => Observation;
}
export type ObservableBase = Record<never, never>;

/** 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 { Attributes } from './Metric';
* Interface that is being used in callback function for Observable Metric
*/
export interface ObservableResult {
observe(value: number, attributes: Attributes): void;
observe(value: number, attributes?: Attributes): void;
}

This file was deleted.

0 comments on commit 5e9c7e1

Please sign in to comment.