Skip to content

Commit

Permalink
feat: add the UpDownCounter instrument (open-telemetry#1120)
Browse files Browse the repository at this point in the history
  • Loading branch information
mayurkale22 authored and dyladan committed Feb 18, 2021
1 parent 762efb4 commit 67e7567
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 7 deletions.
27 changes: 26 additions & 1 deletion api/src/metrics/Meter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,13 @@
* limitations under the License.
*/

import { MetricOptions, Counter, ValueRecorder, Observer } from './Metric';
import {
MetricOptions,
Counter,
ValueRecorder,
Observer,
UpDownCounter,
} from './Metric';

/**
* An interface to allow the recording metrics.
Expand All @@ -40,6 +46,25 @@ export interface Meter {
*/
createCounter(name: string, options?: MetricOptions): Counter;

/**
* Creates a new `UpDownCounter` metric. UpDownCounter is a synchronous
* instrument and very similar to Counter except that Add(increment)
* supports negative increments. It is generally useful for capturing changes
* in an amount of resources used, or any quantity that rises and falls
* during a request.
* Example uses for UpDownCounter:
* <ol>
* <li> count the number of active requests. </li>
* <li> count memory in use by instrumenting new and delete. </li>
* <li> count queue size by instrumenting enqueue and dequeue. </li>
* <li> count semaphore up and down operations. </li>
* </ol>
*
* @param name the name of the metric.
* @param [options] the metric options.
*/
createUpDownCounter(name: string, options?: MetricOptions): UpDownCounter;

/**
* Creates a new `Observer` metric.
* @param name the name of the metric.
Expand Down
14 changes: 8 additions & 6 deletions api/src/metrics/Metric.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,7 @@ export interface MetricOptions {
disabled?: boolean;

/**
* Asserts that this metric may only increase (e.g. time spent).
*/
monotonic?: boolean;

/**
* (ValueRecorder only, default true) Asserts that this metric will only accept
* (Measure only, default true) Asserts that this metric will only accept
* non-negative values (e.g. disk usage).
*/
absolute?: boolean;
Expand Down Expand Up @@ -125,6 +120,13 @@ export interface Counter extends UnboundMetric<BoundCounter> {
add(value: number, labels?: Labels): void;
}

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

export interface ValueRecorder extends UnboundMetric<BoundValueRecorder> {
/**
* Records the given value to this value recorder.
Expand Down
10 changes: 10 additions & 0 deletions api/src/metrics/NoopMeter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import {
Counter,
ValueRecorder,
Observer,
UpDownCounter,
} from './Metric';
import { BoundValueRecorder, BoundCounter } from './BoundInstrument';
import { CorrelationContext } from '../correlation_context/CorrelationContext';
Expand Down Expand Up @@ -53,6 +54,15 @@ export class NoopMeter implements Meter {
return NOOP_COUNTER_METRIC;
}

/**
* Returns a constant noop UpDownCounter.
* @param name the name of the metric.
* @param [options] the metric options.
*/
createUpDownCounter(name: string, options?: MetricOptions): UpDownCounter {
return NOOP_COUNTER_METRIC;
}

/**
* Returns constant noop observer.
* @param name the name of the metric.
Expand Down

0 comments on commit 67e7567

Please sign in to comment.