Skip to content

Commit

Permalink
feat(api-metrics): remove bind/unbind and bound instruments (#2559)
Browse files Browse the repository at this point in the history
Co-authored-by: Valentin Marchaud <contact@vmarchaud.fr>
Co-authored-by: Daniel Dyla <dyladan@users.noreply.github.com>
  • Loading branch information
3 people committed Nov 2, 2021
1 parent c307949 commit 5bb8322
Show file tree
Hide file tree
Showing 21 changed files with 114 additions and 290 deletions.
6 changes: 3 additions & 3 deletions examples/otlp-exporter-node/metrics.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ const histogram = meter.createHistogram('test_histogram', {
const labels = { pid: process.pid, environment: 'staging' };

setInterval(() => {
requestCounter.bind(labels).add(1);
upDownCounter.bind(labels).add(Math.random() > 0.5 ? 1 : -1);
histogram.bind(labels).record(Math.random());
requestCounter.add(1, labels);
upDownCounter.add(Math.random() > 0.5 ? 1 : -1, labels);
histogram.record(Math.random(), labels);
}, 1000);
4 changes: 2 additions & 2 deletions examples/prometheus/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,6 @@ const upDownCounter = meter.createUpDownCounter('test_up_down_counter', {
const labels = { pid: process.pid, environment: 'staging' };

setInterval(() => {
requestCounter.bind(labels).add(1);
upDownCounter.bind(labels).add(Math.random() > 0.5 ? 1 : -1);
requestCounter.add(1, labels);
upDownCounter.add(Math.random() > 0.5 ? 1 : -1, labels);
}, 1000);
4 changes: 2 additions & 2 deletions examples/tracer-web/examples/metrics/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ function startMetrics() {
const labels = { pid: process.pid, environment: 'staging' };

interval = setInterval(() => {
requestCounter.bind(labels).add(1);
upDownCounter.bind(labels).add(Math.random() > 0.5 ? 1 : -1);
requestCounter.add(1, labels);
upDownCounter.add(Math.random() > 0.5 ? 1 : -1, labels);
}, 1000);
}

Expand Down
104 changes: 16 additions & 88 deletions experimental/packages/opentelemetry-api-metrics/src/NoopMeter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import { BatchObserverResult } from './types/BatchObserverResult';
import { Meter } from './types/Meter';
import {
MetricOptions,
UnboundMetric,
Labels,
Counter,
Histogram,
Expand All @@ -28,11 +27,6 @@ import {
ObservableCounter,
ObservableUpDownCounter,
} from './types/Metric';
import {
BoundHistogram,
BoundCounter,
BoundObservableBase,
} from './types/BoundInstrument';
import { ObservableResult } from './types/ObservableResult';
import { Observation } from './types/Observation';

Expand Down Expand Up @@ -67,7 +61,7 @@ export class NoopMeter implements Meter {
* @param [options] the metric options.
*/
createUpDownCounter(_name: string, _options?: MetricOptions): UpDownCounter {
return NOOP_COUNTER_METRIC;
return NOOP_UP_DOWN_COUNTER_METRIC;
}

/**
Expand Down Expand Up @@ -124,59 +118,21 @@ export class NoopMeter implements Meter {
}
}

export class NoopMetric<T> implements UnboundMetric<T> {
private readonly _instrument: T;

constructor(instrument: T) {
this._instrument = instrument;
}
export class NoopMetric {}

/**
* Returns a Bound Instrument associated with specified Labels.
* It is recommended to keep a reference to the Bound Instrument instead of
* always calling this method for every operations.
* @param labels key-values pairs that are associated with a specific metric
* that you want to record.
*/
bind(_labels: Labels): T {
return this._instrument;
}

/**
* Removes the Binding from the metric, if it is present.
* @param labels key-values pairs that are associated with a specific metric.
*/
unbind(_labels: Labels): void {
return;
}

/**
* Clears all timeseries from the Metric.
*/
clear(): void {
return;
}
export class NoopCounterMetric extends NoopMetric implements Counter {
add(_value: number, _labels: Labels): void {}
}

export class NoopCounterMetric
extends NoopMetric<BoundCounter>
implements Counter {
add(value: number, labels: Labels): void {
this.bind(labels).add(value);
}
export class NoopUpDownCounterMetric extends NoopMetric implements UpDownCounter {
add(_value: number, _labels: Labels): void {}
}

export class NoopHistogramMetric
extends NoopMetric<BoundHistogram>
implements Histogram {
record(value: number, labels: Labels): void {
this.bind(labels).record(value);
}
export class NoopHistogramMetric extends NoopMetric implements Histogram {
record(_value: number, _labels: Labels): void {}
}

export class NoopObservableBaseMetric
extends NoopMetric<BoundObservableBase>
implements ObservableBase {
export class NoopObservableBaseMetric extends NoopMetric implements ObservableBase {
observation(): Observation {
return {
observable: this as ObservableBase,
Expand All @@ -187,42 +143,14 @@ export class NoopObservableBaseMetric

export class NoopBatchObserver {}

export class NoopBoundCounter implements BoundCounter {
add(_value: number): void {
return;
}
}

export class NoopBoundHistogram implements BoundHistogram {
record(_value: number, _baggage?: unknown, _spanContext?: unknown): void {
return;
}
}
export const NOOP_METER = new NoopMeter();

export class NoopBoundObservableBase implements BoundObservableBase {
update(_value: number): void {}
}
export const NOOP_COUNTER_METRIC = new NoopCounterMetric();
export const NOOP_UP_DOWN_COUNTER_METRIC = new NoopUpDownCounterMetric();
export const NOOP_HISTOGRAM_METRIC = new NoopHistogramMetric();

export const NOOP_METER = new NoopMeter();
export const NOOP_BOUND_COUNTER = new NoopBoundCounter();
export const NOOP_COUNTER_METRIC = new NoopCounterMetric(NOOP_BOUND_COUNTER);

export const NOOP_BOUND_HISTOGRAM = new NoopBoundHistogram();
export const NOOP_HISTOGRAM_METRIC = new NoopHistogramMetric(
NOOP_BOUND_HISTOGRAM
);

export const NOOP_BOUND_OBSERVABLE_BASE = new NoopBoundObservableBase();
export const NOOP_OBSERVABLE_GAUGE_METRIC = new NoopObservableBaseMetric(
NOOP_BOUND_OBSERVABLE_BASE
);

export const NOOP_OBSERVABLE_UP_DOWN_COUNTER_METRIC = new NoopObservableBaseMetric(
NOOP_BOUND_OBSERVABLE_BASE
);

export const NOOP_OBSERVABLE_COUNTER_METRIC = new NoopObservableBaseMetric(
NOOP_BOUND_OBSERVABLE_BASE
);
export const NOOP_OBSERVABLE_GAUGE_METRIC = new NoopObservableBaseMetric();
export const NOOP_OBSERVABLE_UP_DOWN_COUNTER_METRIC = new NoopObservableBaseMetric();
export const NOOP_OBSERVABLE_COUNTER_METRIC = new NoopObservableBaseMetric();

export const NOOP_BATCH_OBSERVER = new NoopBatchObserver();
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
export * from './NoopMeter';
export * from './NoopMeterProvider';
export * from './types/BatchObserverResult';
export * from './types/BoundInstrument';
export * from './types/Meter';
export * from './types/MeterProvider';
export * from './types/Metric';
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,6 @@
* limitations under the License.
*/

import {
BoundObservableBase,
BoundCounter,
BoundHistogram,
} from './BoundInstrument';
import {
Observation,
} from './Observation';
Expand Down Expand Up @@ -88,38 +83,6 @@ export enum AggregationTemporality {
AGGREGATION_TEMPORALITY_CUMULATIVE,
}

/**
* Metric represents a base class for different types of metric
* pre aggregations.
*/
export interface Metric {
/**
* Clears all bound instruments from the Metric.
*/
clear(): void;
}

/**
* UnboundMetric represents a base class for different types of metric
* pre aggregations without label value bound yet.
*/
export interface UnboundMetric<T> extends Metric {
/**
* Returns a Instrument associated with specified Labels.
* It is recommended to keep a reference to the Instrument instead of always
* calling this method for every operations.
* @param labels key-values pairs that are associated with a specific metric
* that you want to record.
*/
bind(labels: Labels): T;

/**
* Removes the Instrument from the metric, if it is present.
* @param labels key-values pairs that are associated with a specific metric.
*/
unbind(labels: Labels): void;
}

/**
* Counter is the most common synchronous instrument. This instrument supports
* an `Add(increment)` function for reporting a sum, and is restricted to
Expand All @@ -135,31 +98,32 @@ export interface UnboundMetric<T> extends Metric {
* <li> count the number of 5xx errors. </li>
* <ol>
*/
export interface Counter extends UnboundMetric<BoundCounter> {
export interface Counter {
/**
* Adds the given value to the current value. Values cannot be negative.
*/
add(value: number, labels?: Labels): void;
}

export interface UpDownCounter extends UnboundMetric<BoundCounter> {
export interface UpDownCounter {
/**
* Adds the given value to the current value. Values can be negative.
*/
add(value: number, labels?: Labels): void;
}

export interface Histogram extends UnboundMetric<BoundHistogram> {
export interface Histogram {
/**
* Records the given value to this histogram.
*/
record(value: number, labels?: Labels): void;
}

/** Base interface for the Observable metrics. */
export interface ObservableBase extends UnboundMetric<BoundObservableBase> {
export interface ObservableBase {
observation: (
value: number
value: number,
labels?: Labels,
) => Observation;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@
import * as assert from 'assert';
import {
NoopMeterProvider,
NOOP_BOUND_COUNTER,
NOOP_BOUND_HISTOGRAM,
NOOP_COUNTER_METRIC,
NOOP_HISTOGRAM_METRIC,
} from '../../src';
Expand All @@ -30,20 +28,16 @@ describe('NoopMeter', () => {
const labels = {};

// ensure NoopMetric does not crash.
counter.bind(labels).add(1);
counter.unbind(labels);
counter.add(1, labels);

// ensure the correct noop const is returned
assert.strictEqual(counter, NOOP_COUNTER_METRIC);
assert.strictEqual(counter.bind(labels), NOOP_BOUND_COUNTER);
counter.clear();

const histogram = meter.createHistogram('some-name');
histogram.bind(labels).record(1);
histogram.record(1, labels);

// ensure the correct noop const is returned
assert.strictEqual(histogram, NOOP_HISTOGRAM_METRIC);
assert.strictEqual(histogram.bind(labels), NOOP_BOUND_HISTOGRAM);

const options = {
component: 'tests',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,6 @@ const counter = meter.createCounter('metric_name', {
});
counter.add(10, { pid: process.pid });

// Record data using Instrument: It is recommended to keep a reference to the Bound Instrument instead of
// always calling `bind()` method for every operations.
const boundCounter = counter.bind({ pid: process.pid });
boundCounter.add(10);

// .. some other work
```

Expand Down
Loading

0 comments on commit 5bb8322

Please sign in to comment.