From 8ba87686843459614996228fc24b89df49cd8006 Mon Sep 17 00:00:00 2001 From: Joshua Stupplebeen Date: Thu, 19 Dec 2019 09:59:44 -0700 Subject: [PATCH] chore: rename Metric Handle to Bound Instrument (#634) --- examples/prometheus/index.js | 8 +- getting-started/README.md | 22 +- .../monitored-example/monitoring.js | 10 +- .../src/metrics/NoopMeter.ts | 84 ++++--- .../test/metrics/NoopMeter.test.ts | 32 +-- .../src/types.ts | 4 +- .../README.md | 8 +- .../test/prometheus.test.ts | 33 ++- packages/opentelemetry-metrics/README.md | 12 +- .../src/{Handle.ts => BoundInstrument.ts} | 25 ++- packages/opentelemetry-metrics/src/Meter.ts | 12 +- packages/opentelemetry-metrics/src/Metric.ts | 73 +++--- packages/opentelemetry-metrics/src/index.ts | 2 +- .../opentelemetry-metrics/test/Meter.test.ts | 212 +++++++++--------- .../test/export/ConsoleMetricExporter.test.ts | 8 +- packages/opentelemetry-types/src/index.ts | 2 +- .../metrics/{Handle.ts => BoundInstrument.ts} | 10 +- .../opentelemetry-types/src/metrics/Meter.ts | 18 +- .../opentelemetry-types/src/metrics/Metric.ts | 18 +- 19 files changed, 310 insertions(+), 283 deletions(-) rename packages/opentelemetry-metrics/src/{Handle.ts => BoundInstrument.ts} (78%) rename packages/opentelemetry-types/src/metrics/{Handle.ts => BoundInstrument.ts} (89%) diff --git a/examples/prometheus/index.js b/examples/prometheus/index.js index 16ac24943be..166dff8e94f 100644 --- a/examples/prometheus/index.js +++ b/examples/prometheus/index.js @@ -46,10 +46,10 @@ setInterval(() => { currentMonotonicGaugeValue += Math.random(); - monotonicCounter.getHandle(labels).add(1); - nonMonotonicCounter.getHandle(labels).add(Math.random() > 0.5 ? 1 : -1); - monotonicGauge.getHandle(labels).set(currentMonotonicGaugeValue); + monotonicCounter.bind(labels).add(1); + nonMonotonicCounter.bind(labels).add(Math.random() > 0.5 ? 1 : -1); + monotonicGauge.bind(labels).set(currentMonotonicGaugeValue); nonMonotonicGauge - .getHandle(labels) + .bind(labels) .set(Math.random() > 0.5 ? Math.random() * 10 : -Math.random() * 10); }, 1000); diff --git a/getting-started/README.md b/getting-started/README.md index 01c0a139107..f48ba912f7c 100644 --- a/getting-started/README.md +++ b/getting-started/README.md @@ -227,17 +227,17 @@ const requestCount = meter.createCounter("requests", { description: "Count all incoming requests" }); -const handles = new Map(); +const instruments = new Map(); module.exports.countAllRequests = () => { return (req, res, next) => { - if (!handles.has(req.path)) { + if (!instruments.has(req.path)) { const labelSet = meter.labels({ route: req.path }); - const handle = requestCount.getHandle(labelSet); - handles.set(req.path, handle); + const instrument = requestCount.bind(labelSet); + instruments.set(req.path, instrument); } - handles.get(req.path).add(1); + instruments.get(req.path).add(1); next(); }; }; @@ -253,7 +253,7 @@ app.use(countAllRequests()); Now, when we make requests to our service our meter will count all requests. -**Note**: Creating a new `labelSet` and `handle` on every request is not ideal as creating the `labelSet` can often be an expensive operation. This is why handles are created and stored in a `Map` according to the route key. +**Note**: Creating a new `labelSet` and `instrument` on every request is not ideal as creating the `labelSet` can often be an expensive operation. This is why instruments are created and stored in a `Map` according to the route key. #### Initialize and register a metrics exporter Counting metrics is only useful if we can export them somewhere that we can see them. For this, we're going to use prometheus. Creating and registering a metrics exporter is much like the tracing exporter above. First we will need to install the prometheus exporter. @@ -287,17 +287,17 @@ const requestCount = meter.createCounter("requests", { description: "Count all incoming requests" }); -const handles = new Map(); +const instruments = new Map(); module.exports.countAllRequests = () => { return (req, res, next) => { - if (!handles.has(req.path)) { + if (!instruments.has(req.path)) { const labelSet = meter.labels({ route: req.path }); - const handle = requestCount.getHandle(labelSet); - handles.set(req.path, handle); + const instrument = requestCount.bind(labelSet); + instruments.set(req.path, instrument); } - handles.get(req.path).add(1); + instruments.get(req.path).add(1); next(); }; }; diff --git a/getting-started/monitored-example/monitoring.js b/getting-started/monitored-example/monitoring.js index 070d9d01ef8..2135073cc9a 100644 --- a/getting-started/monitored-example/monitoring.js +++ b/getting-started/monitored-example/monitoring.js @@ -22,17 +22,17 @@ const requestCount = meter.createCounter("requests", { description: "Count all incoming requests" }); -const handles = new Map(); +const instruments = new Map(); module.exports.countAllRequests = () => { return (req, res, next) => { - if (!handles.has(req.path)) { + if (!instruments.has(req.path)) { const labelSet = meter.labels({ route: req.path }); - const handle = requestCount.getHandle(labelSet); - handles.set(req.path, handle); + const instrument = requestCount.bind(labelSet); + instruments.set(req.path, instrument); } - handles.get(req.path).add(1); + instruments.get(req.path).add(1); next(); }; }; diff --git a/packages/opentelemetry-core/src/metrics/NoopMeter.ts b/packages/opentelemetry-core/src/metrics/NoopMeter.ts index 6f2cb99507e..da551ce7412 100644 --- a/packages/opentelemetry-core/src/metrics/NoopMeter.ts +++ b/packages/opentelemetry-core/src/metrics/NoopMeter.ts @@ -15,14 +15,14 @@ */ import { - CounterHandle, + CounterInstrument, DistributedContext, - GaugeHandle, + GaugeInstrument, Meter, Metric, MetricOptions, MetricUtils, - MeasureHandle, + MeasureInstrument, SpanContext, LabelSet, Labels, @@ -40,7 +40,10 @@ export class NoopMeter implements Meter { * @param name the name of the metric. * @param [options] the metric options. */ - createMeasure(name: string, options?: MetricOptions): Metric { + createMeasure( + name: string, + options?: MetricOptions + ): Metric { return NOOP_MEASURE_METRIC; } @@ -49,7 +52,10 @@ export class NoopMeter implements Meter { * @param name the name of the metric. * @param [options] the metric options. */ - createCounter(name: string, options?: MetricOptions): Metric { + createCounter( + name: string, + options?: MetricOptions + ): Metric { return NOOP_COUNTER_METRIC; } @@ -58,7 +64,7 @@ export class NoopMeter implements Meter { * @param name the name of the metric. * @param [options] the metric options. */ - createGauge(name: string, options?: MetricOptions): Metric { + createGauge(name: string, options?: MetricOptions): Metric { return NOOP_GAUGE_METRIC; } @@ -68,33 +74,33 @@ export class NoopMeter implements Meter { } export class NoopMetric implements Metric { - private readonly _handle: T; + private readonly _instrument: T; - constructor(handle: T) { - this._handle = handle; + constructor(instrument: T) { + this._instrument = instrument; } /** - * Returns a Handle associated with specified LabelSet. - * It is recommended to keep a reference to the Handle instead of always + * Returns an Instrument associated with specified LabelSet. + * It is recommended to keep a reference to the Instrument instead of always * calling this method for every operations. - * @param labels the canonicalized LabelSet used to associate with this metric handle. + * @param labels the canonicalized LabelSet used to associate with this metric instrument. */ - getHandle(labels: LabelSet): T { - return this._handle; + bind(labels: LabelSet): T { + return this._instrument; } /** - * Returns a Handle for a metric with all labels not set. + * Returns an Instrument for a metric with all labels not set. */ - getDefaultHandle(): T { - return this._handle; + getDefaultBound(): T { + return this._instrument; } /** - * Removes the Handle from the metric, if it is present. - * @param labels the canonicalized LabelSet used to associate with this metric handle. + * Removes the Instrument from the metric, if it is present. + * @param labels the canonicalized LabelSet used to associate with this metric instrument. */ - removeHandle(labels: LabelSet): void { + unbind(labels: LabelSet): void { // @todo: implement this method return; } @@ -111,21 +117,21 @@ export class NoopMetric implements Metric { } } -export class NoopCounterMetric extends NoopMetric +export class NoopCounterMetric extends NoopMetric implements Pick { add(value: number, labelSet: LabelSet) { - this.getHandle(labelSet).add(value); + this.bind(labelSet).add(value); } } -export class NoopGaugeMetric extends NoopMetric +export class NoopGaugeMetric extends NoopMetric implements Pick { set(value: number, labelSet: LabelSet) { - this.getHandle(labelSet).set(value); + this.bind(labelSet).set(value); } } -export class NoopMeasureMetric extends NoopMetric +export class NoopMeasureMetric extends NoopMetric implements Pick { record( value: number, @@ -134,28 +140,28 @@ export class NoopMeasureMetric extends NoopMetric spanContext?: SpanContext ) { if (typeof distContext === 'undefined') { - this.getHandle(labelSet).record(value); + this.bind(labelSet).record(value); } else if (typeof spanContext === 'undefined') { - this.getHandle(labelSet).record(value, distContext); + this.bind(labelSet).record(value, distContext); } else { - this.getHandle(labelSet).record(value, distContext, spanContext); + this.bind(labelSet).record(value, distContext, spanContext); } } } -export class NoopCounterHandle implements CounterHandle { +export class NoopCounterInstrument implements CounterInstrument { add(value: number): void { return; } } -export class NoopGaugeHandle implements GaugeHandle { +export class NoopGaugeInstrument implements GaugeInstrument { set(value: number): void { return; } } -export class NoopMeasureHandle implements MeasureHandle { +export class NoopMeasureInstrument implements MeasureInstrument { record( value: number, distContext?: DistributedContext, @@ -165,13 +171,17 @@ export class NoopMeasureHandle implements MeasureHandle { } } -export const NOOP_GAUGE_HANDLE = new NoopGaugeHandle(); -export const NOOP_GAUGE_METRIC = new NoopGaugeMetric(NOOP_GAUGE_HANDLE); +export const NOOP_GAUGE_INSTRUMENT = new NoopGaugeInstrument(); +export const NOOP_GAUGE_METRIC = new NoopGaugeMetric(NOOP_GAUGE_INSTRUMENT); -export const NOOP_COUNTER_HANDLE = new NoopCounterHandle(); -export const NOOP_COUNTER_METRIC = new NoopCounterMetric(NOOP_COUNTER_HANDLE); +export const NOOP_COUNTER_INSTRUMENT = new NoopCounterInstrument(); +export const NOOP_COUNTER_METRIC = new NoopCounterMetric( + NOOP_COUNTER_INSTRUMENT +); -export const NOOP_MEASURE_HANDLE = new NoopMeasureHandle(); -export const NOOP_MEASURE_METRIC = new NoopMeasureMetric(NOOP_MEASURE_HANDLE); +export const NOOP_MEASURE_INSTRUMENT = new NoopMeasureInstrument(); +export const NOOP_MEASURE_METRIC = new NoopMeasureMetric( + NOOP_MEASURE_INSTRUMENT +); export const NOOP_LABEL_SET = {} as LabelSet; diff --git a/packages/opentelemetry-core/test/metrics/NoopMeter.test.ts b/packages/opentelemetry-core/test/metrics/NoopMeter.test.ts index 38294975f45..2569d10d04a 100644 --- a/packages/opentelemetry-core/test/metrics/NoopMeter.test.ts +++ b/packages/opentelemetry-core/test/metrics/NoopMeter.test.ts @@ -17,11 +17,11 @@ import * as assert from 'assert'; import { NoopMeter, - NOOP_GAUGE_HANDLE, + NOOP_GAUGE_INSTRUMENT, NOOP_GAUGE_METRIC, - NOOP_COUNTER_HANDLE, + NOOP_COUNTER_INSTRUMENT, NOOP_COUNTER_METRIC, - NOOP_MEASURE_HANDLE, + NOOP_MEASURE_INSTRUMENT, NOOP_MEASURE_METRIC, } from '../../src/metrics/NoopMeter'; import { Labels } from '@opentelemetry/types'; @@ -37,20 +37,20 @@ describe('NoopMeter', () => { counter.setCallback(() => { assert.fail('callback occurred'); }); - counter.getHandle(labelSet).add(1); - counter.getDefaultHandle().add(1); - counter.removeHandle(labelSet); + counter.bind(labelSet).add(1); + counter.getDefaultBound().add(1); + counter.unbind(labelSet); // ensure the correct noop const is returned assert.strictEqual(counter, NOOP_COUNTER_METRIC); - assert.strictEqual(counter.getHandle(labelSet), NOOP_COUNTER_HANDLE); - assert.strictEqual(counter.getDefaultHandle(), NOOP_COUNTER_HANDLE); + assert.strictEqual(counter.bind(labelSet), NOOP_COUNTER_INSTRUMENT); + assert.strictEqual(counter.getDefaultBound(), NOOP_COUNTER_INSTRUMENT); counter.clear(); const measure = meter.createMeasure('some-name'); - measure.getDefaultHandle().record(1); - measure.getDefaultHandle().record(1, { key: { value: 'value' } }); - measure.getDefaultHandle().record( + measure.getDefaultBound().record(1); + measure.getDefaultBound().record(1, { key: { value: 'value' } }); + measure.getDefaultBound().record( 1, { key: { value: 'value' } }, { @@ -61,16 +61,16 @@ describe('NoopMeter', () => { // ensure the correct noop const is returned assert.strictEqual(measure, NOOP_MEASURE_METRIC); - assert.strictEqual(measure.getDefaultHandle(), NOOP_MEASURE_HANDLE); - assert.strictEqual(measure.getHandle(labelSet), NOOP_MEASURE_HANDLE); + assert.strictEqual(measure.getDefaultBound(), NOOP_MEASURE_INSTRUMENT); + assert.strictEqual(measure.bind(labelSet), NOOP_MEASURE_INSTRUMENT); const gauge = meter.createGauge('some-name'); - gauge.getDefaultHandle().set(1); + gauge.getDefaultBound().set(1); // ensure the correct noop const is returned assert.strictEqual(gauge, NOOP_GAUGE_METRIC); - assert.strictEqual(gauge.getDefaultHandle(), NOOP_GAUGE_HANDLE); - assert.strictEqual(gauge.getHandle(labelSet), NOOP_GAUGE_HANDLE); + assert.strictEqual(gauge.getDefaultBound(), NOOP_GAUGE_INSTRUMENT); + assert.strictEqual(gauge.bind(labelSet), NOOP_GAUGE_INSTRUMENT); const options = { component: 'tests', diff --git a/packages/opentelemetry-exporter-collector/src/types.ts b/packages/opentelemetry-exporter-collector/src/types.ts index 1e5cb13b3d2..d62d8c00f93 100644 --- a/packages/opentelemetry-exporter-collector/src/types.ts +++ b/packages/opentelemetry-exporter-collector/src/types.ts @@ -119,8 +119,8 @@ export interface LibraryInfo { /** * A pointer from the current span to another span in the same trace or in a * different trace. For example, this can be used in batching operations, where - * a single batch handler processes multiple requests from different traces or - * when the handler receives a request from a different project. + * a single batch instrument processes multiple requests from different traces or + * when the instrument receives a request from a different project. */ export interface Link { /** diff --git a/packages/opentelemetry-exporter-prometheus/README.md b/packages/opentelemetry-exporter-prometheus/README.md index 4e7353e5e45..619574c760f 100644 --- a/packages/opentelemetry-exporter-prometheus/README.md +++ b/packages/opentelemetry-exporter-prometheus/README.md @@ -36,10 +36,10 @@ meter.addExporter(exporter); const counter = meter.createCounter('metric_name'); counter.add(10, meter.labels({ [key]: 'value' })); -// Record data using Handle: It is recommended to keep a reference to the Handle instead of -// always calling `getHandle()` method for every operations. -const handle = counter.getHandle(meter.labels({ [key]: 'value' })); -handle.add(10); +// Record data using Instrument: It is recommended to keep a reference to the Instrument instead of +// always calling `bind()` method for every operations. +const instrument = counter.bind(meter.labels({ [key]: 'value' })); +instrument.add(10); // .. some other work diff --git a/packages/opentelemetry-exporter-prometheus/test/prometheus.test.ts b/packages/opentelemetry-exporter-prometheus/test/prometheus.test.ts index 28ba07d4a14..8fb8d030f01 100644 --- a/packages/opentelemetry-exporter-prometheus/test/prometheus.test.ts +++ b/packages/opentelemetry-exporter-prometheus/test/prometheus.test.ts @@ -178,15 +178,15 @@ describe('PrometheusExporter', () => { const counter = meter.createCounter('counter', { description: 'a test description', labelKeys: ['key1'], - }) as CounterMetric; + }); - const handle = counter.getHandle(meter.labels({ key1: 'labelValue1' })); - handle.add(10); + const instrument = counter.bind(meter.labels({ key1: 'labelValue1' })); + instrument.add(10); exporter.export(meter.getMetrics(), () => { // This is to test the special case where counters are destroyed // and recreated in the exporter in order to get around prom-client's // aggregation and use ours. - handle.add(10); + instrument.add(10); exporter.export(meter.getMetrics(), () => { http .get('http://localhost:9464/metrics', res => { @@ -220,8 +220,8 @@ describe('PrometheusExporter', () => { labelKeys: ['key1'], }) as GaugeMetric; - const handle = gauge.getHandle(meter.labels({ key1: 'labelValue1' })); - handle.set(10); + const instrument = gauge.bind(meter.labels({ key1: 'labelValue1' })); + instrument.set(10); exporter.export([gauge.get()!], () => { http .get('http://localhost:9464/metrics', res => { @@ -243,7 +243,7 @@ describe('PrometheusExporter', () => { }); }); - it('should export a multiple aggregations', done => { + it('should export multiple aggregations', done => { const gauge = meter.createGauge('gauge', { description: 'a test description', labelKeys: ['gaugeKey1'], @@ -254,8 +254,8 @@ describe('PrometheusExporter', () => { labelKeys: ['counterKey1'], }) as CounterMetric; - gauge.getHandle(meter.labels({ key1: 'labelValue1' })).set(10); - counter.getHandle(meter.labels({ key1: 'labelValue1' })).add(10); + gauge.bind(meter.labels({ key1: 'labelValue1' })).set(10); + counter.bind(meter.labels({ key1: 'labelValue1' })).add(10); exporter.export([gauge.get()!, counter.get()!], () => { http .get('http://localhost:9464/metrics', res => { @@ -301,8 +301,8 @@ describe('PrometheusExporter', () => { it('should add a description if missing', done => { const gauge = meter.createGauge('gauge') as GaugeMetric; - const handle = gauge.getHandle(meter.labels({ key1: 'labelValue1' })); - handle.set(10); + const instrument = gauge.bind(meter.labels({ key1: 'labelValue1' })); + instrument.set(10); exporter.export([gauge.get()!], () => { http .get('http://localhost:9464/metrics', res => { @@ -326,8 +326,8 @@ describe('PrometheusExporter', () => { it('should sanitize names', done => { const gauge = meter.createGauge('gauge.bad-name') as GaugeMetric; - const handle = gauge.getHandle(meter.labels({ key1: 'labelValue1' })); - handle.set(10); + const instrument = gauge.bind(meter.labels({ key1: 'labelValue1' })); + instrument.set(10); exporter.export([gauge.get()!], () => { http .get('http://localhost:9464/metrics', res => { @@ -354,10 +354,9 @@ describe('PrometheusExporter', () => { description: 'a test description', monotonic: false, labelKeys: ['key1'], - }) as CounterMetric; + }); - const handle = counter.getHandle(meter.labels({ key1: 'labelValue1' })); - handle.add(20); + counter.bind(meter.labels({ key1: 'labelValue1' })).add(20); exporter.export(meter.getMetrics(), () => { http .get('http://localhost:9464/metrics', res => { @@ -385,7 +384,7 @@ describe('PrometheusExporter', () => { beforeEach(() => { meter = new Meter(); gauge = meter.createGauge('gauge') as GaugeMetric; - gauge.getHandle(meter.labels({ key1: 'labelValue1' })).set(10); + gauge.bind(meter.labels({ key1: 'labelValue1' })).set(10); }); afterEach(done => { diff --git a/packages/opentelemetry-metrics/README.md b/packages/opentelemetry-metrics/README.md index 4e3747eba75..b6f5388c516 100644 --- a/packages/opentelemetry-metrics/README.md +++ b/packages/opentelemetry-metrics/README.md @@ -31,9 +31,9 @@ const counter = meter.createCounter('metric_name', { const labels = meter.labels({ pid: process.pid }); -// Create a Handle associated with specified label values. -const handle = counter.getHandle(labels); -handle.add(10); +// Create a Instrument associated with specified label values. +const instrument = counter.bind(labels); +instrument.add(10); ``` ### Gauge @@ -52,9 +52,9 @@ const gauge = meter.createGauge('metric_name', { const labels = meter.labels({ pid: process.pid }); -// Create a Handle associated with specified label values. -const handle = gauge.getHandle(labels); -handle.set(10); // Set to 10 +// Create a Instrument associated with specified label values. +const instrument = gauge.bind(labels); +instrument.set(10); // Set to 10 ``` See [examples/prometheus](https://github.com/open-telemetry/opentelemetry-js/tree/master/examples/prometheus) for a short example. diff --git a/packages/opentelemetry-metrics/src/Handle.ts b/packages/opentelemetry-metrics/src/BoundInstrument.ts similarity index 78% rename from packages/opentelemetry-metrics/src/Handle.ts rename to packages/opentelemetry-metrics/src/BoundInstrument.ts index 335493bffb0..8483b3af2fa 100644 --- a/packages/opentelemetry-metrics/src/Handle.ts +++ b/packages/opentelemetry-metrics/src/BoundInstrument.ts @@ -18,10 +18,10 @@ import * as types from '@opentelemetry/types'; import { TimeSeries } from './export/types'; /** - * This class represent the base to handle, which is responsible for generating + * This class represent the base to instrument, which is responsible for generating * the TimeSeries. */ -export class BaseHandle { +export class BaseInstrument { protected _data = 0; protected _labelSet: types.LabelSet; @@ -32,7 +32,7 @@ export class BaseHandle { /** * Returns the TimeSeries with one or more Point. * - * @param timestamp The time at which the handle is recorded. + * @param timestamp The time at which the instrument is recorded. * @returns The TimeSeries. */ getTimeSeries(timestamp: types.HrTime): TimeSeries { @@ -46,10 +46,11 @@ export class BaseHandle { } /** - * CounterHandle allows the SDK to observe/record a single metric event. The - * value of single handle in the `Counter` associated with specified LabelSet. + * CounterInstrument allows the SDK to observe/record a single metric event. The + * value of single instrument in the `Counter` associated with specified LabelSet. */ -export class CounterHandle extends BaseHandle implements types.CounterHandle { +export class CounterInstrument extends BaseInstrument + implements types.CounterInstrument { constructor( labelSet: types.LabelSet, private readonly _disabled: boolean, @@ -86,10 +87,11 @@ export class CounterHandle extends BaseHandle implements types.CounterHandle { } /** - * GaugeHandle allows the SDK to observe/record a single metric event. The - * value of single handle in the `Gauge` associated with specified LabelSet. + * GaugeInstrument allows the SDK to observe/record a single metric event. The + * value of single instrument in the `Gauge` associated with specified LabelSet. */ -export class GaugeHandle extends BaseHandle implements types.GaugeHandle { +export class GaugeInstrument extends BaseInstrument + implements types.GaugeInstrument { constructor( labelSet: types.LabelSet, private readonly _disabled: boolean, @@ -127,9 +129,10 @@ export class GaugeHandle extends BaseHandle implements types.GaugeHandle { } /** - * MeasureHandle is an implementation of the {@link MeasureHandle} interface. + * MeasureInstrument is an implementation of the {@link MeasureInstrument} interface. */ -export class MeasureHandle extends BaseHandle implements types.MeasureHandle { +export class MeasureInstrument extends BaseInstrument + implements types.MeasureInstrument { record( value: number, distContext?: types.DistributedContext, diff --git a/packages/opentelemetry-metrics/src/Meter.ts b/packages/opentelemetry-metrics/src/Meter.ts index ba2b74cd566..10be5301905 100644 --- a/packages/opentelemetry-metrics/src/Meter.ts +++ b/packages/opentelemetry-metrics/src/Meter.ts @@ -21,7 +21,7 @@ import { NOOP_GAUGE_METRIC, NOOP_MEASURE_METRIC, } from '@opentelemetry/core'; -import { BaseHandle } from './Handle'; +import { BaseInstrument } from './BoundInstrument'; import { Metric, CounterMetric, GaugeMetric } from './Metric'; import { MetricOptions, @@ -39,7 +39,7 @@ import { ExportResult } from '@opentelemetry/base'; */ export class Meter implements types.Meter { private readonly _logger: types.Logger; - private readonly _metrics = new Map>(); + private readonly _metrics = new Map>(); private readonly _exporters: MetricExporter[] = []; readonly labels = Meter.labels; @@ -59,7 +59,7 @@ export class Meter implements types.Meter { createMeasure( name: string, options?: types.MetricOptions - ): types.Metric { + ): types.Metric { if (!this._isValidName(name)) { this._logger.warn( `Invalid metric name ${name}. Defaulting to noop metric implementation.` @@ -80,7 +80,7 @@ export class Meter implements types.Meter { createCounter( name: string, options?: types.MetricOptions - ): types.Metric { + ): types.Metric { if (!this._isValidName(name)) { this._logger.warn( `Invalid metric name ${name}. Defaulting to noop metric implementation.` @@ -112,7 +112,7 @@ export class Meter implements types.Meter { createGauge( name: string, options?: types.MetricOptions - ): types.Metric { + ): types.Metric { if (!this._isValidName(name)) { this._logger.warn( `Invalid metric name ${name}. Defaulting to noop metric implementation.` @@ -197,7 +197,7 @@ export class Meter implements types.Meter { * @param name The name of the metric. * @param metric The metric to register. */ - private _registerMetric( + private _registerMetric( name: string, metric: Metric ): void { diff --git a/packages/opentelemetry-metrics/src/Metric.ts b/packages/opentelemetry-metrics/src/Metric.ts index 78a6a212c4a..a64a4b262c7 100644 --- a/packages/opentelemetry-metrics/src/Metric.ts +++ b/packages/opentelemetry-metrics/src/Metric.ts @@ -16,7 +16,11 @@ import * as types from '@opentelemetry/types'; import { hrTime } from '@opentelemetry/core'; -import { CounterHandle, GaugeHandle, BaseHandle } from './Handle'; +import { + CounterInstrument, + GaugeInstrument, + BaseInstrument, +} from './BoundInstrument'; import { MetricOptions } from './types'; import { ReadableMetric, @@ -25,13 +29,14 @@ import { } from './export/types'; /** This is a SDK implementation of {@link Metric} interface. */ -export abstract class Metric implements types.Metric { +export abstract class Metric + implements types.Metric { protected readonly _monotonic: boolean; protected readonly _disabled: boolean; protected readonly _valueType: types.ValueType; protected readonly _logger: types.Logger; private readonly _metricDescriptor: MetricDescriptor; - private readonly _handles: Map = new Map(); + private readonly _instruments: Map = new Map(); constructor( private readonly _name: string, @@ -46,42 +51,42 @@ export abstract class Metric implements types.Metric { } /** - * Returns a Handle associated with specified LabelSet. - * It is recommended to keep a reference to the Handle instead of always + * Returns an Instrument associated with specified LabelSet. + * It is recommended to keep a reference to the Instrument instead of always * calling this method for each operation. - * @param labelSet the canonicalized LabelSet used to associate with this metric handle. + * @param labelSet the canonicalized LabelSet used to associate with this metric instrument. */ - getHandle(labelSet: types.LabelSet): T { - if (this._handles.has(labelSet.identifier)) - return this._handles.get(labelSet.identifier)!; + bind(labelSet: types.LabelSet): T { + if (this._instruments.has(labelSet.identifier)) + return this._instruments.get(labelSet.identifier)!; - const handle = this._makeHandle(labelSet); - this._handles.set(labelSet.identifier, handle); - return handle; + const instrument = this._makeInstrument(labelSet); + this._instruments.set(labelSet.identifier, instrument); + return instrument; } /** - * Returns a Handle for a metric with all labels not set. + * Returns a Instrument for a metric with all labels not set. */ - getDefaultHandle(): T { + getDefaultBound(): T { // @todo: implement this method this._logger.error('not implemented yet'); throw new Error('not implemented yet'); } /** - * Removes the Handle from the metric, if it is present. - * @param labelSet the canonicalized LabelSet used to associate with this metric handle. + * Removes the Instrument from the metric, if it is present. + * @param labelSet the canonicalized LabelSet used to associate with this metric instrument. */ - removeHandle(labelSet: types.LabelSet): void { - this._handles.delete(labelSet.identifier); + unbind(labelSet: types.LabelSet): void { + this._instruments.delete(labelSet.identifier); } /** - * Clears all Handles from the Metric. + * Clears all Instruments from the Metric. */ clear(): void { - this._handles.clear(); + this._instruments.clear(); } setCallback(fn: () => void): void { @@ -96,13 +101,13 @@ export abstract class Metric implements types.Metric { * Metric. */ get(): ReadableMetric | null { - if (this._handles.size === 0) return null; + if (this._instruments.size === 0) return null; const timestamp = hrTime(); return { descriptor: this._metricDescriptor, - timeseries: Array.from(this._handles, ([_, handle]) => - handle.getTimeSeries(timestamp) + timeseries: Array.from(this._instruments, ([_, instrument]) => + instrument.getTimeSeries(timestamp) ), }; } @@ -118,11 +123,11 @@ export abstract class Metric implements types.Metric { }; } - protected abstract _makeHandle(labelSet: types.LabelSet): T; + protected abstract _makeInstrument(labelSet: types.LabelSet): T; } /** This is a SDK implementation of Counter Metric. */ -export class CounterMetric extends Metric +export class CounterMetric extends Metric implements Pick { constructor( name: string, @@ -137,8 +142,8 @@ export class CounterMetric extends Metric : MetricDescriptorType.COUNTER_INT64 ); } - protected _makeHandle(labelSet: types.LabelSet): CounterHandle { - return new CounterHandle( + protected _makeInstrument(labelSet: types.LabelSet): CounterInstrument { + return new CounterInstrument( labelSet, this._disabled, this._monotonic, @@ -151,15 +156,15 @@ export class CounterMetric extends Metric /** * Adds the given value to the current value. Values cannot be negative. * @param value the value to add. - * @param labelSet the canonicalized LabelSet used to associate with this metric's handle. + * @param labelSet the canonicalized LabelSet used to associate with this metric's instrument. */ add(value: number, labelSet: types.LabelSet) { - this.getHandle(labelSet).add(value); + this.bind(labelSet).add(value); } } /** This is a SDK implementation of Gauge Metric. */ -export class GaugeMetric extends Metric +export class GaugeMetric extends Metric implements Pick { constructor( name: string, @@ -174,8 +179,8 @@ export class GaugeMetric extends Metric : MetricDescriptorType.GAUGE_INT64 ); } - protected _makeHandle(labelSet: types.LabelSet): GaugeHandle { - return new GaugeHandle( + protected _makeInstrument(labelSet: types.LabelSet): GaugeInstrument { + return new GaugeInstrument( labelSet, this._disabled, this._monotonic, @@ -188,9 +193,9 @@ export class GaugeMetric extends Metric /** * Sets the given value. Values can be negative. * @param value the new value. - * @param labelSet the canonicalized LabelSet used to associate with this metric's handle. + * @param labelSet the canonicalized LabelSet used to associate with this metric's instrument. */ set(value: number, labelSet: types.LabelSet) { - this.getHandle(labelSet).set(value); + this.bind(labelSet).set(value); } } diff --git a/packages/opentelemetry-metrics/src/index.ts b/packages/opentelemetry-metrics/src/index.ts index 71f30b58c44..0cd212f0b5b 100644 --- a/packages/opentelemetry-metrics/src/index.ts +++ b/packages/opentelemetry-metrics/src/index.ts @@ -14,7 +14,7 @@ * limitations under the License. */ -export * from './Handle'; +export * from './BoundInstrument'; export * from './Meter'; export * from './Metric'; export * from './export/ConsoleMetricExporter'; diff --git a/packages/opentelemetry-metrics/test/Meter.test.ts b/packages/opentelemetry-metrics/test/Meter.test.ts index 903fa64f1f8..0197dc165b0 100644 --- a/packages/opentelemetry-metrics/test/Meter.test.ts +++ b/packages/opentelemetry-metrics/test/Meter.test.ts @@ -79,26 +79,26 @@ describe('Meter', () => { it('should be able to call add() directly on counter', () => { const counter = meter.createCounter('name') as CounterMetric; counter.add(10, labelSet); - assert.strictEqual(counter.getHandle(labelSet)['_data'], 10); + assert.strictEqual(counter.bind(labelSet)['_data'], 10); counter.add(10, labelSet); - assert.strictEqual(counter.getHandle(labelSet)['_data'], 20); + assert.strictEqual(counter.bind(labelSet)['_data'], 20); }); - describe('.getHandle()', () => { - it('should create a counter handle', () => { + describe('.bind()', () => { + it('should create a counter instrument', () => { const counter = meter.createCounter('name') as CounterMetric; - const handle = counter.getHandle(labelSet); - handle.add(10); - assert.strictEqual(handle['_data'], 10); - handle.add(10); - assert.strictEqual(handle['_data'], 20); + const instrument = counter.bind(labelSet); + instrument.add(10); + assert.strictEqual(instrument['_data'], 10); + instrument.add(10); + assert.strictEqual(instrument['_data'], 20); }); it('should return the timeseries', () => { const counter = meter.createCounter('name') as CounterMetric; - const handle = counter.getHandle(labelSet); - handle.add(20); - assert.deepStrictEqual(handle.getTimeSeries(hrTime), { + const instrument = counter.bind(labelSet); + instrument.add(20); + assert.deepStrictEqual(instrument.getTimeSeries(hrTime), { labelValues: [{ value: 'value1' }, { value: 'value2' }], points: [{ value: 20, timestamp: hrTime }], }); @@ -106,78 +106,78 @@ describe('Meter', () => { it('should add positive values by default', () => { const counter = meter.createCounter('name') as CounterMetric; - const handle = counter.getHandle(labelSet); - handle.add(10); - assert.strictEqual(handle['_data'], 10); - handle.add(-100); - assert.strictEqual(handle['_data'], 10); + const instrument = counter.bind(labelSet); + instrument.add(10); + assert.strictEqual(instrument['_data'], 10); + instrument.add(-100); + assert.strictEqual(instrument['_data'], 10); }); - it('should not add the handle data when disabled', () => { + it('should not add the instrument data when disabled', () => { const counter = meter.createCounter('name', { disabled: true, }) as CounterMetric; - const handle = counter.getHandle(labelSet); - handle.add(10); - assert.strictEqual(handle['_data'], 0); + const instrument = counter.bind(labelSet); + instrument.add(10); + assert.strictEqual(instrument['_data'], 0); }); it('should add negative value when monotonic is set to false', () => { const counter = meter.createCounter('name', { monotonic: false, }) as CounterMetric; - const handle = counter.getHandle(labelSet); - handle.add(-10); - assert.strictEqual(handle['_data'], -10); + const instrument = counter.bind(labelSet); + instrument.add(-10); + assert.strictEqual(instrument['_data'], -10); }); - it('should return same handle on same label values', () => { + it('should return same instrument on same label values', () => { const counter = meter.createCounter('name') as CounterMetric; - const handle = counter.getHandle(labelSet); - handle.add(10); - const handle1 = counter.getHandle(labelSet); - handle1.add(10); - assert.strictEqual(handle['_data'], 20); - assert.strictEqual(handle, handle1); + const instrument = counter.bind(labelSet); + instrument.add(10); + const instrument1 = counter.bind(labelSet); + instrument1.add(10); + assert.strictEqual(instrument['_data'], 20); + assert.strictEqual(instrument, instrument1); }); }); - describe('.removeHandle()', () => { - it('should remove a counter handle', () => { + describe('.unbind()', () => { + it('should remove a counter instrument', () => { const counter = meter.createCounter('name') as CounterMetric; - const handle = counter.getHandle(labelSet); - assert.strictEqual(counter['_handles'].size, 1); - counter.removeHandle(labelSet); - assert.strictEqual(counter['_handles'].size, 0); - const handle1 = counter.getHandle(labelSet); - assert.strictEqual(counter['_handles'].size, 1); - assert.notStrictEqual(handle, handle1); + const instrument = counter.bind(labelSet); + assert.strictEqual(counter['_instruments'].size, 1); + counter.unbind(labelSet); + assert.strictEqual(counter['_instruments'].size, 0); + const instrument1 = counter.bind(labelSet); + assert.strictEqual(counter['_instruments'].size, 1); + assert.notStrictEqual(instrument, instrument1); }); - it('should not fail when removing non existing handle', () => { + it('should not fail when removing non existing instrument', () => { const counter = meter.createCounter('name'); - counter.removeHandle(new LabelSet('', {})); + counter.unbind(new LabelSet('', {})); }); - it('should clear all handles', () => { + it('should clear all instruments', () => { const counter = meter.createCounter('name') as CounterMetric; - counter.getHandle(labelSet); - assert.strictEqual(counter['_handles'].size, 1); + counter.bind(labelSet); + assert.strictEqual(counter['_instruments'].size, 1); counter.clear(); - assert.strictEqual(counter['_handles'].size, 0); + assert.strictEqual(counter['_instruments'].size, 0); }); }); describe('.registerMetric()', () => { it('skip already registered Metric', () => { const counter1 = meter.createCounter('name1') as CounterMetric; - counter1.getHandle(labelSet).add(10); + counter1.bind(labelSet).add(10); // should skip below metric const counter2 = meter.createCounter('name1', { valueType: types.ValueType.INT, }) as CounterMetric; - counter2.getHandle(labelSet).add(500); + counter2.bind(labelSet).add(500); assert.strictEqual(meter.getMetrics().length, 1); const [{ descriptor, timeseries }] = meter.getMetrics(); @@ -240,19 +240,19 @@ describe('Meter', () => { it('should be able to call set() directly on gauge', () => { const gauge = meter.createGauge('name') as GaugeMetric; gauge.set(10, labelSet); - assert.strictEqual(gauge.getHandle(labelSet)['_data'], 10); + assert.strictEqual(gauge.bind(labelSet)['_data'], 10); gauge.set(250, labelSet); - assert.strictEqual(gauge.getHandle(labelSet)['_data'], 250); + assert.strictEqual(gauge.bind(labelSet)['_data'], 250); }); - describe('.getHandle()', () => { - it('should create a gauge handle', () => { + describe('.bind()', () => { + it('should create a gauge instrument', () => { const gauge = meter.createGauge('name') as GaugeMetric; - const handle = gauge.getHandle(labelSet); - handle.set(10); - assert.strictEqual(handle['_data'], 10); - handle.set(250); - assert.strictEqual(handle['_data'], 250); + const instrument = gauge.bind(labelSet); + instrument.set(10); + assert.strictEqual(instrument['_data'], 10); + instrument.set(250); + assert.strictEqual(instrument['_data'], 250); }); it('should return the timeseries', () => { @@ -261,9 +261,9 @@ describe('Meter', () => { const k2 = 'k2'; const labels = { [k1]: 'v1', [k2]: 'v2' }; const LabelSet2 = new LabelSet('|#k1:v1,k2:v2', labels); - const handle = gauge.getHandle(LabelSet2); - handle.set(150); - assert.deepStrictEqual(handle.getTimeSeries(hrTime), { + const instrument = gauge.bind(LabelSet2); + instrument.set(150); + assert.deepStrictEqual(instrument.getTimeSeries(hrTime), { labelValues: [{ value: 'v1' }, { value: 'v2' }], points: [{ value: 150, timestamp: hrTime }], }); @@ -271,65 +271,65 @@ describe('Meter', () => { it('should go up and down by default', () => { const gauge = meter.createGauge('name') as GaugeMetric; - const handle = gauge.getHandle(labelSet); - handle.set(10); - assert.strictEqual(handle['_data'], 10); - handle.set(-100); - assert.strictEqual(handle['_data'], -100); + const instrument = gauge.bind(labelSet); + instrument.set(10); + assert.strictEqual(instrument['_data'], 10); + instrument.set(-100); + assert.strictEqual(instrument['_data'], -100); }); - it('should not set the handle data when disabled', () => { + it('should not set the instrument data when disabled', () => { const gauge = meter.createGauge('name', { disabled: true, }) as GaugeMetric; - const handle = gauge.getHandle(labelSet); - handle.set(10); - assert.strictEqual(handle['_data'], 0); + const instrument = gauge.bind(labelSet); + instrument.set(10); + assert.strictEqual(instrument['_data'], 0); }); it('should not set negative value when monotonic is set to true', () => { const gauge = meter.createGauge('name', { monotonic: true, }) as GaugeMetric; - const handle = gauge.getHandle(labelSet); - handle.set(-10); - assert.strictEqual(handle['_data'], 0); + const instrument = gauge.bind(labelSet); + instrument.set(-10); + assert.strictEqual(instrument['_data'], 0); }); - it('should return same handle on same label values', () => { + it('should return same instrument on same label values', () => { const gauge = meter.createGauge('name') as GaugeMetric; - const handle = gauge.getHandle(labelSet); - handle.set(10); - const handle1 = gauge.getHandle(labelSet); - handle1.set(10); - assert.strictEqual(handle['_data'], 10); - assert.strictEqual(handle, handle1); + const instrument = gauge.bind(labelSet); + instrument.set(10); + const instrument1 = gauge.bind(labelSet); + instrument1.set(10); + assert.strictEqual(instrument['_data'], 10); + assert.strictEqual(instrument, instrument1); }); }); - describe('.removeHandle()', () => { - it('should remove the gauge handle', () => { + describe('.unbind()', () => { + it('should remove the gauge instrument', () => { const gauge = meter.createGauge('name') as GaugeMetric; - const handle = gauge.getHandle(labelSet); - assert.strictEqual(gauge['_handles'].size, 1); - gauge.removeHandle(labelSet); - assert.strictEqual(gauge['_handles'].size, 0); - const handle1 = gauge.getHandle(labelSet); - assert.strictEqual(gauge['_handles'].size, 1); - assert.notStrictEqual(handle, handle1); + const instrument = gauge.bind(labelSet); + assert.strictEqual(gauge['_instruments'].size, 1); + gauge.unbind(labelSet); + assert.strictEqual(gauge['_instruments'].size, 0); + const instrument1 = gauge.bind(labelSet); + assert.strictEqual(gauge['_instruments'].size, 1); + assert.notStrictEqual(instrument, instrument1); }); - it('should not fail when removing non existing handle', () => { + it('should not fail when removing non existing instrument', () => { const gauge = meter.createGauge('name'); - gauge.removeHandle(new LabelSet('', {})); + gauge.unbind(new LabelSet('', {})); }); - it('should clear all handles', () => { + it('should clear all instruments', () => { const gauge = meter.createGauge('name') as GaugeMetric; - gauge.getHandle(labelSet); - assert.strictEqual(gauge['_handles'].size, 1); + gauge.bind(labelSet); + assert.strictEqual(gauge['_instruments'].size, 1); gauge.clear(); - assert.strictEqual(gauge['_handles'].size, 0); + assert.strictEqual(gauge['_instruments'].size, 0); }); }); @@ -391,8 +391,8 @@ describe('Meter', () => { labelKeys: [key], }); const labelSet = meter.labels({ [key]: 'counter-value' }); - const handle = counter.getHandle(labelSet); - handle.add(10.45); + const instrument = counter.bind(labelSet); + instrument.add(10.45); assert.strictEqual(meter.getMetrics().length, 1); const [{ descriptor, timeseries }] = meter.getMetrics(); @@ -423,8 +423,8 @@ describe('Meter', () => { valueType: types.ValueType.INT, }); const labelSet = meter.labels({ [key]: 'counter-value' }); - const handle = counter.getHandle(labelSet); - handle.add(10.45); + const instrument = counter.bind(labelSet); + instrument.add(10.45); assert.strictEqual(meter.getMetrics().length, 1); const [{ descriptor, timeseries }] = meter.getMetrics(); @@ -455,8 +455,8 @@ describe('Meter', () => { }); const labelSet1 = meter.labels({ [key]: 'gauge-value1' }); const labelSet2 = meter.labels({ [key]: 'gauge-value2' }); - gauge.getHandle(labelSet1).set(200.34); - gauge.getHandle(labelSet2).set(-10.67); + gauge.bind(labelSet1).set(200.34); + gauge.bind(labelSet2).set(-10.67); assert.strictEqual(meter.getMetrics().length, 1); const [{ descriptor, timeseries }] = meter.getMetrics(); @@ -498,8 +498,8 @@ describe('Meter', () => { }); const labelSet1 = meter.labels({ [key]: 'gauge-value1' }); const labelSet2 = meter.labels({ [key]: 'gauge-value2' }); - gauge.getHandle(labelSet1).set(200.34); - gauge.getHandle(labelSet2).set(-10.67); + gauge.bind(labelSet1).set(200.34); + gauge.bind(labelSet2).set(-10.67); assert.strictEqual(meter.getMetrics().length, 1); const [{ descriptor, timeseries }] = meter.getMetrics(); @@ -557,13 +557,13 @@ describe('Meter', () => { }); meter.addExporter(exporter); - const gauge = meter.createGauge('name') as GaugeMetric; + const gauge = meter.createGauge('name'); const labelSet = meter.labels({ value: 'value1', value2: 'value2' }); - gauge.getHandle(labelSet).set(20); + gauge.bind(labelSet).set(20); }); it('should export a counter when it is updated', done => { - const counter = meter.createCounter('name') as CounterMetric; + const counter = meter.createCounter('name'); const exporter = new NoopExporter(); exporter.on('export', metrics => { assert.equal(metrics[0].descriptor.name, 'name'); @@ -581,7 +581,7 @@ describe('Meter', () => { meter.addExporter(exporter); const labelSet = meter.labels({ value: 'value1', value2: 'value2' }); - counter.getHandle(labelSet).add(20); + counter.bind(labelSet).add(20); }); }); }); diff --git a/packages/opentelemetry-metrics/test/export/ConsoleMetricExporter.test.ts b/packages/opentelemetry-metrics/test/export/ConsoleMetricExporter.test.ts index ac4d12794d5..929db6d3aa4 100644 --- a/packages/opentelemetry-metrics/test/export/ConsoleMetricExporter.test.ts +++ b/packages/opentelemetry-metrics/test/export/ConsoleMetricExporter.test.ts @@ -16,7 +16,7 @@ import * as assert from 'assert'; import * as sinon from 'sinon'; -import { ConsoleMetricExporter, Meter, GaugeMetric } from '../../src'; +import { ConsoleMetricExporter, Meter } from '../../src'; describe('ConsoleMetricExporter', () => { let consoleExporter: ConsoleMetricExporter; @@ -41,11 +41,11 @@ describe('ConsoleMetricExporter', () => { const gauge = meter.createGauge('gauge', { description: 'a test description', labelKeys: ['key1', 'key2'], - }) as GaugeMetric; - const handle = gauge.getHandle( + }); + const instrument = gauge.bind( meter.labels({ key1: 'labelValue1', key2: 'labelValue2' }) ); - handle.set(10); + instrument.set(10); const [descriptor, timeseries] = spyConsole.args; assert.deepStrictEqual(descriptor, [ { description: 'a test description', name: 'gauge' }, diff --git a/packages/opentelemetry-types/src/index.ts b/packages/opentelemetry-types/src/index.ts index 6e90dd16906..97a8409bf9a 100644 --- a/packages/opentelemetry-types/src/index.ts +++ b/packages/opentelemetry-types/src/index.ts @@ -20,7 +20,7 @@ export * from './context/propagation/BinaryFormat'; export * from './context/propagation/HttpTextFormat'; export * from './distributed_context/DistributedContext'; export * from './distributed_context/EntryValue'; -export * from './metrics/Handle'; +export * from './metrics/BoundInstrument'; export * from './metrics/Meter'; export * from './metrics/Metric'; export * from './trace/attributes'; diff --git a/packages/opentelemetry-types/src/metrics/Handle.ts b/packages/opentelemetry-types/src/metrics/BoundInstrument.ts similarity index 89% rename from packages/opentelemetry-types/src/metrics/Handle.ts rename to packages/opentelemetry-types/src/metrics/BoundInstrument.ts index ecd325d7e80..b416d3b0751 100644 --- a/packages/opentelemetry-types/src/metrics/Handle.ts +++ b/packages/opentelemetry-types/src/metrics/BoundInstrument.ts @@ -17,8 +17,8 @@ import { DistributedContext } from '../distributed_context/DistributedContext'; import { SpanContext } from '../trace/span_context'; -/** A Handle for Counter Metric. */ -export interface CounterHandle { +/** An Instrument for Counter Metric. */ +export interface CounterInstrument { /** * Adds the given value to the current value. Values cannot be negative. * @param value the value to add. @@ -26,8 +26,8 @@ export interface CounterHandle { add(value: number): void; } -/** A Handle for Gauge Metric. */ -export interface GaugeHandle { +/** An Instrument for Gauge Metric. */ +export interface GaugeInstrument { /** * Sets the given value. Values can be negative. * @param value the new value. @@ -36,7 +36,7 @@ export interface GaugeHandle { } /** Measure to report instantaneous measurement of a value. */ -export interface MeasureHandle { +export interface MeasureInstrument { /** * Records the given value to this measure. * @param value the measurement to record. diff --git a/packages/opentelemetry-types/src/metrics/Meter.ts b/packages/opentelemetry-types/src/metrics/Meter.ts index dd377f1e77a..6454e02c88e 100644 --- a/packages/opentelemetry-types/src/metrics/Meter.ts +++ b/packages/opentelemetry-types/src/metrics/Meter.ts @@ -15,7 +15,11 @@ */ import { Metric, MetricOptions, Labels, LabelSet } from './Metric'; -import { CounterHandle, GaugeHandle, MeasureHandle } from './Handle'; +import { + CounterInstrument, + GaugeInstrument, + MeasureInstrument, +} from './BoundInstrument'; /** * An interface to allow the recording metrics. @@ -30,7 +34,10 @@ export interface Meter { * @param name the name of the metric. * @param [options] the metric options. */ - createMeasure(name: string, options?: MetricOptions): Metric; + createMeasure( + name: string, + options?: MetricOptions + ): Metric; /** * Creates a new counter metric. Generally, this kind of metric when the @@ -39,7 +46,10 @@ export interface Meter { * @param name the name of the metric. * @param [options] the metric options. */ - createCounter(name: string, options?: MetricOptions): Metric; + createCounter( + name: string, + options?: MetricOptions + ): Metric; // TODO: Measurements can have a long or double type. However, it looks like // the metric timeseries API (according to spec) accepts values instead of @@ -57,7 +67,7 @@ export interface Meter { * @param name the name of the metric. * @param [options] the metric options. */ - createGauge(name: string, options?: MetricOptions): Metric; + createGauge(name: string, options?: MetricOptions): Metric; /** * Provide a pre-computed re-useable LabelSet by diff --git a/packages/opentelemetry-types/src/metrics/Metric.ts b/packages/opentelemetry-types/src/metrics/Metric.ts index a1e18e4fadc..dffdbe4241c 100644 --- a/packages/opentelemetry-types/src/metrics/Metric.ts +++ b/packages/opentelemetry-types/src/metrics/Metric.ts @@ -73,23 +73,23 @@ export enum ValueType { */ export interface Metric { /** - * Returns a Handle associated with specified LabelSet. - * It is recommended to keep a reference to the Handle instead of always + * Returns a Instrument associated with specified LabelSet. + * It is recommended to keep a reference to the Instrument instead of always * calling this method for every operations. - * @param labels the canonicalized LabelSet used to associate with this metric handle. + * @param labels the canonicalized LabelSet used to associate with this metric instrument. */ - getHandle(labels: LabelSet): T; + bind(labels: LabelSet): T; /** - * Returns a Handle for a metric with all labels not set. + * Returns a Instrument for a metric with all labels not set. */ - getDefaultHandle(): T; + getDefaultBound(): T; /** - * Removes the Handle from the metric, if it is present. - * @param labels the canonicalized LabelSet used to associate with this metric handle. + * Removes the Instrument from the metric, if it is present. + * @param labels the canonicalized LabelSet used to associate with this metric instrument. */ - removeHandle(labels: LabelSet): void; + unbind(labels: LabelSet): void; /** * Clears all timeseries from the Metric.