Skip to content

Commit 928796d

Browse files
authored
feat(sdk-metrics): added synchronous gauge to SDK (#4565)
* feat(instrumentation): added synchronous gauge to SDK * fixup! feat(instrumentation): added synchronous gauge to SDK * fixup! feat(instrumentation): added synchronous gauge to SDK * fixup! feat(instrumentation): added synchronous gauge to SDK * fixup! feat(instrumentation): added synchronous gauge to SDK * fixup! feat(instrumentation): added synchronous gauge to SDK
1 parent d66e1d7 commit 928796d

File tree

12 files changed

+71
-0
lines changed

12 files changed

+71
-0
lines changed

experimental/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ and AnyValueMap types [#4575](https://github.com/open-telemetry/opentelemetry-js
2626

2727
### :rocket: (Enhancement)
2828

29+
* feat(metrics): added synchronous gauge to SDK [#4565](https://github.com/open-telemetry/opentelemetry-js/pull/4565) @clintonb
2930
* feat(opentelemetry-instrumentation-xhr): optionally ignore network events [#4571](https://github.com/open-telemetry/opentelemetry-js/pull/4571/) @mustafahaddara
3031
* refactor(instr-http): use exported strings for semconv. [#4573](https://github.com/open-telemetry/opentelemetry-js/pull/4573/) @JamieDanielson
3132
* perf(instrumentation-http): remove obvious temp allocations [#4576](https://github.com/open-telemetry/opentelemetry-js/pull/4576) @Samuron

experimental/packages/opentelemetry-exporter-metrics-otlp-http/src/OTLPMetricExporterBase.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ export const DeltaTemporalitySelector: AggregationTemporalitySelector = (
4141
switch (instrumentType) {
4242
case InstrumentType.COUNTER:
4343
case InstrumentType.OBSERVABLE_COUNTER:
44+
case InstrumentType.GAUGE:
4445
case InstrumentType.HISTOGRAM:
4546
case InstrumentType.OBSERVABLE_GAUGE:
4647
return AggregationTemporality.DELTA;
@@ -57,6 +58,7 @@ export const LowMemoryTemporalitySelector: AggregationTemporalitySelector = (
5758
case InstrumentType.COUNTER:
5859
case InstrumentType.HISTOGRAM:
5960
return AggregationTemporality.DELTA;
61+
case InstrumentType.GAUGE:
6062
case InstrumentType.UP_DOWN_COUNTER:
6163
case InstrumentType.OBSERVABLE_UP_DOWN_COUNTER:
6264
case InstrumentType.OBSERVABLE_COUNTER:

packages/sdk-metrics/src/InstrumentDescriptor.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import { equalsCaseInsensitive } from './utils';
2323
*/
2424
export enum InstrumentType {
2525
COUNTER = 'COUNTER',
26+
GAUGE = 'GAUGE',
2627
HISTOGRAM = 'HISTOGRAM',
2728
UP_DOWN_COUNTER = 'UP_DOWN_COUNTER',
2829
OBSERVABLE_COUNTER = 'OBSERVABLE_COUNTER',

packages/sdk-metrics/src/Instruments.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ import {
3636
AsyncWritableMetricStorage,
3737
WritableMetricStorage,
3838
} from './state/WritableMetricStorage';
39+
import { Gauge } from './types';
3940

4041
export class SyncInstrument {
4142
constructor(
@@ -110,6 +111,18 @@ export class CounterInstrument extends SyncInstrument implements Counter {
110111
}
111112
}
112113

114+
/**
115+
* The class implements {@link Gauge} interface.
116+
*/
117+
export class GaugeInstrument extends SyncInstrument implements Gauge {
118+
/**
119+
* Records a measurement.
120+
*/
121+
record(value: number, attributes?: MetricAttributes, ctx?: Context): void {
122+
this._record(value, attributes, ctx);
123+
}
124+
}
125+
113126
/**
114127
* The class implements {@link Histogram} interface.
115128
*/

packages/sdk-metrics/src/Meter.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,27 +25,47 @@ import {
2525
ObservableUpDownCounter,
2626
BatchObservableCallback,
2727
Observable,
28+
Attributes,
2829
} from '@opentelemetry/api';
2930
import {
3031
createInstrumentDescriptor,
3132
InstrumentType,
3233
} from './InstrumentDescriptor';
3334
import {
3435
CounterInstrument,
36+
GaugeInstrument,
3537
HistogramInstrument,
3638
ObservableCounterInstrument,
3739
ObservableGaugeInstrument,
3840
ObservableUpDownCounterInstrument,
3941
UpDownCounterInstrument,
4042
} from './Instruments';
4143
import { MeterSharedState } from './state/MeterSharedState';
44+
import { Gauge } from './types';
4245

4346
/**
4447
* This class implements the {@link IMeter} interface.
4548
*/
4649
export class Meter implements IMeter {
4750
constructor(private _meterSharedState: MeterSharedState) {}
4851

52+
/**
53+
* Create a {@link Gauge} instrument.
54+
* @experimental
55+
*/
56+
createGauge<AttributesTypes extends Attributes = Attributes>(
57+
name: string,
58+
options?: MetricOptions
59+
): Gauge<AttributesTypes> {
60+
const descriptor = createInstrumentDescriptor(
61+
name,
62+
InstrumentType.GAUGE,
63+
options
64+
);
65+
const storage = this._meterSharedState.registerMetricStorage(descriptor);
66+
return new GaugeInstrument(storage, descriptor);
67+
}
68+
4969
/**
5070
* Create a {@link Histogram} instrument.
5171
*/

packages/sdk-metrics/src/aggregator/ExponentialHistogram.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -580,6 +580,7 @@ export class ExponentialHistogramAggregator
580580

581581
// determine if instrument allows negative values.
582582
const allowsNegativeValues =
583+
descriptor.type === InstrumentType.GAUGE ||
583584
descriptor.type === InstrumentType.UP_DOWN_COUNTER ||
584585
descriptor.type === InstrumentType.OBSERVABLE_GAUGE ||
585586
descriptor.type === InstrumentType.OBSERVABLE_UP_DOWN_COUNTER;

packages/sdk-metrics/src/aggregator/Histogram.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,7 @@ export class HistogramAggregator implements Aggregator<HistogramAccumulation> {
231231

232232
// determine if instrument allows negative values.
233233
const allowsNegativeValues =
234+
descriptor.type === InstrumentType.GAUGE ||
234235
descriptor.type === InstrumentType.UP_DOWN_COUNTER ||
235236
descriptor.type === InstrumentType.OBSERVABLE_GAUGE ||
236237
descriptor.type === InstrumentType.OBSERVABLE_UP_DOWN_COUNTER;

packages/sdk-metrics/src/types.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16+
import { Context, MetricAttributes } from '@opentelemetry/api';
1617

1718
export type CommonReaderOptions = {
1819
timeoutMillis?: number;
@@ -23,3 +24,18 @@ export type CollectionOptions = CommonReaderOptions;
2324
export type ShutdownOptions = CommonReaderOptions;
2425

2526
export type ForceFlushOptions = CommonReaderOptions;
27+
28+
/**
29+
* @experimental
30+
*
31+
* This is intentionally not using the API's type as it's only available from @opentelemetry/api 1.9.0 and up.
32+
* In SDK 2.0 we'll be able to bump the minimum API version and remove this workaround.
33+
*/
34+
export interface Gauge<
35+
AttributesTypes extends MetricAttributes = MetricAttributes,
36+
> {
37+
/**
38+
* Records a measurement. Value of the measurement must not be negative.
39+
*/
40+
record(value: number, attributes?: AttributesTypes, context?: Context): void;
41+
}

packages/sdk-metrics/src/view/Aggregation.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,7 @@ export class DefaultAggregation extends Aggregation {
182182
case InstrumentType.OBSERVABLE_UP_DOWN_COUNTER: {
183183
return SUM_AGGREGATION;
184184
}
185+
case InstrumentType.GAUGE:
185186
case InstrumentType.OBSERVABLE_GAUGE: {
186187
return LAST_VALUE_AGGREGATION;
187188
}

packages/sdk-metrics/test/Meter.test.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import * as assert from 'assert';
1919
import * as sinon from 'sinon';
2020
import {
2121
CounterInstrument,
22+
GaugeInstrument,
2223
HistogramInstrument,
2324
ObservableCounterInstrument,
2425
ObservableGaugeInstrument,
@@ -88,6 +89,18 @@ describe('Meter', () => {
8889
});
8990
});
9091

92+
describe('createGauge', () => {
93+
testWithNames('Gauge', name => {
94+
const meterSharedState = new MeterSharedState(
95+
new MeterProviderSharedState(defaultResource),
96+
defaultInstrumentationScope
97+
);
98+
const meter = new Meter(meterSharedState);
99+
const Gauge = meter.createGauge(name);
100+
assert(Gauge instanceof GaugeInstrument);
101+
});
102+
});
103+
91104
describe('createObservableCounter', () => {
92105
testWithNames('ObservableCounter', name => {
93106
const meterSharedState = new MeterSharedState(

packages/sdk-metrics/test/export/ConsoleMetricExporter.test.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,7 @@ describe('ConsoleMetricExporter', () => {
160160
switch (instrumentType) {
161161
case InstrumentType.COUNTER:
162162
case InstrumentType.OBSERVABLE_COUNTER:
163+
case InstrumentType.GAUGE:
163164
case InstrumentType.HISTOGRAM:
164165
case InstrumentType.OBSERVABLE_GAUGE:
165166
return AggregationTemporality.DELTA;

packages/sdk-metrics/test/export/utils.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ const instrumentTypes = [
2929
InstrumentType.UP_DOWN_COUNTER,
3030
InstrumentType.OBSERVABLE_UP_DOWN_COUNTER,
3131
InstrumentType.HISTOGRAM,
32+
InstrumentType.GAUGE,
3233
InstrumentType.OBSERVABLE_GAUGE,
3334
];
3435

0 commit comments

Comments
 (0)