Skip to content

Commit

Permalink
feat: direct calling of metric instruments (#507)
Browse files Browse the repository at this point in the history
* feat: direct calling of metric instruments

* fix: add interface in types->metric

* fix: linting

* fix: linting & docs

* fix: add overloads for record in measure metric

* fix: linting

* fix: linting

* fix: linting
  • Loading branch information
xiao-lix authored and mayurkale22 committed Nov 12, 2019
1 parent 5792593 commit 668c3aa
Show file tree
Hide file tree
Showing 5 changed files with 107 additions and 18 deletions.
43 changes: 36 additions & 7 deletions packages/opentelemetry-core/src/metrics/NoopMeter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import {
Meter,
Metric,
MetricOptions,
MetricUtils,
MeasureHandle,
SpanContext,
LabelSet,
Expand Down Expand Up @@ -110,6 +111,38 @@ export class NoopMetric<T> implements Metric<T> {
}
}

export class NoopCounterMetric extends NoopMetric<CounterHandle>
implements Pick<MetricUtils, 'add'> {
add(value: number, labelSet: LabelSet) {
this.getHandle(labelSet).add(value);
}
}

export class NoopGaugeMetric extends NoopMetric<GaugeHandle>
implements Pick<MetricUtils, 'set'> {
set(value: number, labelSet: LabelSet) {
this.getHandle(labelSet).set(value);
}
}

export class NoopMeasureMetric extends NoopMetric<MeasureHandle>
implements Pick<MetricUtils, 'record'> {
record(
value: number,
labelSet: LabelSet,
distContext?: DistributedContext,
spanContext?: SpanContext
) {
if (typeof distContext === 'undefined') {
this.getHandle(labelSet).record(value);
} else if (typeof spanContext === 'undefined') {
this.getHandle(labelSet).record(value, distContext);
} else {
this.getHandle(labelSet).record(value, distContext, spanContext);
}
}
}

export class NoopCounterHandle implements CounterHandle {
add(value: number): void {
return;
Expand All @@ -133,16 +166,12 @@ export class NoopMeasureHandle implements MeasureHandle {
}

export const NOOP_GAUGE_HANDLE = new NoopGaugeHandle();
export const NOOP_GAUGE_METRIC = new NoopMetric<GaugeHandle>(NOOP_GAUGE_HANDLE);
export const NOOP_GAUGE_METRIC = new NoopGaugeMetric(NOOP_GAUGE_HANDLE);

export const NOOP_COUNTER_HANDLE = new NoopCounterHandle();
export const NOOP_COUNTER_METRIC = new NoopMetric<CounterHandle>(
NOOP_COUNTER_HANDLE
);
export const NOOP_COUNTER_METRIC = new NoopCounterMetric(NOOP_COUNTER_HANDLE);

export const NOOP_MEASURE_HANDLE = new NoopMeasureHandle();
export const NOOP_MEASURE_METRIC = new NoopMetric<MeasureHandle>(
NOOP_MEASURE_HANDLE
);
export const NOOP_MEASURE_METRIC = new NoopMeasureMetric(NOOP_MEASURE_HANDLE);

export const NOOP_LABEL_SET = {} as LabelSet;
9 changes: 0 additions & 9 deletions packages/opentelemetry-metrics/src/LabelSet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,3 @@ export class LabelSet implements types.LabelSet {
this.labels = labels;
}
}

/**
* Type guard to remove nulls from arrays
*
* @param value value to be checked for null equality
*/
export function notNull<T>(value: T | null): value is T {
return value !== null;
}
24 changes: 22 additions & 2 deletions packages/opentelemetry-metrics/src/Metric.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,8 @@ export abstract class Metric<T extends BaseHandle> implements types.Metric<T> {
}

/** This is a SDK implementation of Counter Metric. */
export class CounterMetric extends Metric<CounterHandle> {
export class CounterMetric extends Metric<CounterHandle>
implements Pick<types.MetricUtils, 'add'> {
constructor(
name: string,
options: MetricOptions,
Expand All @@ -145,10 +146,20 @@ export class CounterMetric extends Metric<CounterHandle> {
this._onUpdate
);
}

/**
* 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.
*/
add(value: number, labelSet: types.LabelSet) {
this.getHandle(labelSet).add(value);
}
}

/** This is a SDK implementation of Gauge Metric. */
export class GaugeMetric extends Metric<GaugeHandle> {
export class GaugeMetric extends Metric<GaugeHandle>
implements Pick<types.MetricUtils, 'set'> {
constructor(
name: string,
options: MetricOptions,
Expand All @@ -172,4 +183,13 @@ export class GaugeMetric extends Metric<GaugeHandle> {
this._onUpdate
);
}

/**
* 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.
*/
set(value: number, labelSet: types.LabelSet) {
this.getHandle(labelSet).set(value);
}
}
16 changes: 16 additions & 0 deletions packages/opentelemetry-metrics/test/Meter.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,14 @@ describe('Meter', () => {
assert.ok(counter instanceof Metric);
});

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);
counter.add(10, labelSet);
assert.strictEqual(counter.getHandle(labelSet)['_data'], 20);
});

describe('.getHandle()', () => {
it('should create a counter handle', () => {
const counter = meter.createCounter('name') as CounterMetric;
Expand Down Expand Up @@ -229,6 +237,14 @@ describe('Meter', () => {
assert.ok(gauge instanceof Metric);
});

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);
gauge.set(250, labelSet);
assert.strictEqual(gauge.getHandle(labelSet)['_data'], 250);
});

describe('.getHandle()', () => {
it('should create a gauge handle', () => {
const gauge = meter.createGauge('name') as GaugeMetric;
Expand Down
33 changes: 33 additions & 0 deletions packages/opentelemetry-types/src/metrics/Metric.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@
* limitations under the License.
*/

import { DistributedContext } from '../distributed_context/DistributedContext';
import { SpanContext } from '../trace/span_context';

/**
* Options needed for metric creation
*/
Expand Down Expand Up @@ -99,6 +102,36 @@ export interface Metric<T> {
setCallback(fn: () => void): void;
}

export interface MetricUtils {
/**
* Adds the given value to the current value. Values cannot be negative.
*/
add(value: number, labelSet: LabelSet): void;

/**
* Sets the given value. Values can be negative.
*/
set(value: number, labelSet: LabelSet): void;

/**
* Records the given value to this measure.
*/
record(value: number, labelSet: LabelSet): void;

record(
value: number,
labelSet: LabelSet,
distContext: DistributedContext
): void;

record(
value: number,
labelSet: LabelSet,
distContext: DistributedContext,
spanContext: SpanContext
): void;
}

/**
* key-value pairs passed by the user.
*/
Expand Down

0 comments on commit 668c3aa

Please sign in to comment.