Skip to content

Commit 3c60f6b

Browse files
mayurkale22dyladan
authored andcommitted
feat: add the UpDownCounter instrument (open-telemetry#1120)
1 parent 93e9b9b commit 3c60f6b

File tree

3 files changed

+44
-7
lines changed

3 files changed

+44
-7
lines changed

api/src/metrics/Meter.ts

+26-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,13 @@
1414
* limitations under the License.
1515
*/
1616

17-
import { MetricOptions, Counter, ValueRecorder, Observer } from './Metric';
17+
import {
18+
MetricOptions,
19+
Counter,
20+
ValueRecorder,
21+
Observer,
22+
UpDownCounter,
23+
} from './Metric';
1824

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

49+
/**
50+
* Creates a new `UpDownCounter` metric. UpDownCounter is a synchronous
51+
* instrument and very similar to Counter except that Add(increment)
52+
* supports negative increments. It is generally useful for capturing changes
53+
* in an amount of resources used, or any quantity that rises and falls
54+
* during a request.
55+
* Example uses for UpDownCounter:
56+
* <ol>
57+
* <li> count the number of active requests. </li>
58+
* <li> count memory in use by instrumenting new and delete. </li>
59+
* <li> count queue size by instrumenting enqueue and dequeue. </li>
60+
* <li> count semaphore up and down operations. </li>
61+
* </ol>
62+
*
63+
* @param name the name of the metric.
64+
* @param [options] the metric options.
65+
*/
66+
createUpDownCounter(name: string, options?: MetricOptions): UpDownCounter;
67+
4368
/**
4469
* Creates a new `Observer` metric.
4570
* @param name the name of the metric.

api/src/metrics/Metric.ts

+8-6
Original file line numberDiff line numberDiff line change
@@ -48,12 +48,7 @@ export interface MetricOptions {
4848
disabled?: boolean;
4949

5050
/**
51-
* Asserts that this metric may only increase (e.g. time spent).
52-
*/
53-
monotonic?: boolean;
54-
55-
/**
56-
* (ValueRecorder only, default true) Asserts that this metric will only accept
51+
* (Measure only, default true) Asserts that this metric will only accept
5752
* non-negative values (e.g. disk usage).
5853
*/
5954
absolute?: boolean;
@@ -125,6 +120,13 @@ export interface Counter extends UnboundMetric<BoundCounter> {
125120
add(value: number, labels?: Labels): void;
126121
}
127122

123+
export interface UpDownCounter extends UnboundMetric<BoundCounter> {
124+
/**
125+
* Adds the given value to the current value. Values can be negative.
126+
*/
127+
add(value: number, labels?: Labels): void;
128+
}
129+
128130
export interface ValueRecorder extends UnboundMetric<BoundValueRecorder> {
129131
/**
130132
* Records the given value to this value recorder.

api/src/metrics/NoopMeter.ts

+10
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import {
2222
Counter,
2323
ValueRecorder,
2424
Observer,
25+
UpDownCounter,
2526
} from './Metric';
2627
import { BoundValueRecorder, BoundCounter } from './BoundInstrument';
2728
import { CorrelationContext } from '../correlation_context/CorrelationContext';
@@ -53,6 +54,15 @@ export class NoopMeter implements Meter {
5354
return NOOP_COUNTER_METRIC;
5455
}
5556

57+
/**
58+
* Returns a constant noop UpDownCounter.
59+
* @param name the name of the metric.
60+
* @param [options] the metric options.
61+
*/
62+
createUpDownCounter(name: string, options?: MetricOptions): UpDownCounter {
63+
return NOOP_COUNTER_METRIC;
64+
}
65+
5666
/**
5767
* Returns constant noop observer.
5868
* @param name the name of the metric.

0 commit comments

Comments
 (0)