File tree Expand file tree Collapse file tree 12 files changed +71
-0
lines changed
packages/opentelemetry-exporter-metrics-otlp-http/src Expand file tree Collapse file tree 12 files changed +71
-0
lines changed Original file line number Diff line number Diff 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
Original file line number Diff line number Diff 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 :
Original file line number Diff line number Diff line change @@ -23,6 +23,7 @@ import { equalsCaseInsensitive } from './utils';
2323 */
2424export enum InstrumentType {
2525 COUNTER = 'COUNTER' ,
26+ GAUGE = 'GAUGE' ,
2627 HISTOGRAM = 'HISTOGRAM' ,
2728 UP_DOWN_COUNTER = 'UP_DOWN_COUNTER' ,
2829 OBSERVABLE_COUNTER = 'OBSERVABLE_COUNTER' ,
Original file line number Diff line number Diff line change @@ -36,6 +36,7 @@ import {
3636 AsyncWritableMetricStorage ,
3737 WritableMetricStorage ,
3838} from './state/WritableMetricStorage' ;
39+ import { Gauge } from './types' ;
3940
4041export 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 */
Original file line number Diff line number Diff line change @@ -25,27 +25,47 @@ import {
2525 ObservableUpDownCounter ,
2626 BatchObservableCallback ,
2727 Observable ,
28+ Attributes ,
2829} from '@opentelemetry/api' ;
2930import {
3031 createInstrumentDescriptor ,
3132 InstrumentType ,
3233} from './InstrumentDescriptor' ;
3334import {
3435 CounterInstrument ,
36+ GaugeInstrument ,
3537 HistogramInstrument ,
3638 ObservableCounterInstrument ,
3739 ObservableGaugeInstrument ,
3840 ObservableUpDownCounterInstrument ,
3941 UpDownCounterInstrument ,
4042} from './Instruments' ;
4143import { MeterSharedState } from './state/MeterSharedState' ;
44+ import { Gauge } from './types' ;
4245
4346/**
4447 * This class implements the {@link IMeter} interface.
4548 */
4649export 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 */
Original file line number Diff line number Diff 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 ;
Original file line number Diff line number Diff 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 ;
Original file line number Diff line number Diff line change 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
1718export type CommonReaderOptions = {
1819 timeoutMillis ?: number ;
@@ -23,3 +24,18 @@ export type CollectionOptions = CommonReaderOptions;
2324export type ShutdownOptions = CommonReaderOptions ;
2425
2526export 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+ }
Original file line number Diff line number Diff 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 }
Original file line number Diff line number Diff line change @@ -19,6 +19,7 @@ import * as assert from 'assert';
1919import * as sinon from 'sinon' ;
2020import {
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 (
You can’t perform that action at this time.
0 commit comments