14
14
* limitations under the License.
15
15
*/
16
16
17
- import * as types from '@opentelemetry/api' ;
17
+ import * as api from '@opentelemetry/api' ;
18
18
import { Resource } from '@opentelemetry/resources' ;
19
19
import {
20
20
BoundCounter ,
@@ -30,11 +30,11 @@ import { hashLabels } from './Utils';
30
30
31
31
/** This is a SDK implementation of {@link Metric} interface. */
32
32
export abstract class Metric < T extends BaseBoundInstrument >
33
- implements types . Metric < T > {
33
+ implements api . Metric < T > {
34
34
protected readonly _monotonic : boolean ;
35
35
protected readonly _disabled : boolean ;
36
- protected readonly _valueType : types . ValueType ;
37
- protected readonly _logger : types . Logger ;
36
+ protected readonly _valueType : api . ValueType ;
37
+ protected readonly _logger : api . Logger ;
38
38
private readonly _descriptor : MetricDescriptor ;
39
39
private readonly _instruments : Map < string , T > = new Map ( ) ;
40
40
@@ -58,7 +58,7 @@ export abstract class Metric<T extends BaseBoundInstrument>
58
58
* @param labels key-values pairs that are associated with a specific metric
59
59
* that you want to record.
60
60
*/
61
- bind ( labels : types . Labels ) : T {
61
+ bind ( labels : api . Labels ) : T {
62
62
const hash = hashLabels ( labels ) ;
63
63
if ( this . _instruments . has ( hash ) ) return this . _instruments . get ( hash ) ! ;
64
64
@@ -71,7 +71,7 @@ export abstract class Metric<T extends BaseBoundInstrument>
71
71
* Removes the Instrument from the metric, if it is present.
72
72
* @param labels key-values pairs that are associated with a specific metric.
73
73
*/
74
- unbind ( labels : types . Labels ) : void {
74
+ unbind ( labels : api . Labels ) : void {
75
75
this . _instruments . delete ( hashLabels ( labels ) ) ;
76
76
}
77
77
@@ -102,12 +102,12 @@ export abstract class Metric<T extends BaseBoundInstrument>
102
102
} ;
103
103
}
104
104
105
- protected abstract _makeInstrument ( labels : types . Labels ) : T ;
105
+ protected abstract _makeInstrument ( labels : api . Labels ) : T ;
106
106
}
107
107
108
108
/** This is a SDK implementation of Counter Metric. */
109
109
export class CounterMetric extends Metric < BoundCounter >
110
- implements Pick < types . MetricUtils , 'add' > {
110
+ implements Pick < api . MetricUtils , 'add' > {
111
111
constructor (
112
112
name : string ,
113
113
options : MetricOptions ,
@@ -116,7 +116,7 @@ export class CounterMetric extends Metric<BoundCounter>
116
116
) {
117
117
super ( name , options , MetricKind . COUNTER , resource ) ;
118
118
}
119
- protected _makeInstrument ( labels : types . Labels ) : BoundCounter {
119
+ protected _makeInstrument ( labels : api . Labels ) : BoundCounter {
120
120
return new BoundCounter (
121
121
labels ,
122
122
this . _disabled ,
@@ -134,13 +134,13 @@ export class CounterMetric extends Metric<BoundCounter>
134
134
* @param labels key-values pairs that are associated with a specific metric
135
135
* that you want to record.
136
136
*/
137
- add ( value : number , labels : types . Labels ) {
137
+ add ( value : number , labels : api . Labels ) {
138
138
this . bind ( labels ) . add ( value ) ;
139
139
}
140
140
}
141
141
142
142
export class MeasureMetric extends Metric < BoundMeasure >
143
- implements Pick < types . MetricUtils , 'record' > {
143
+ implements Pick < api . MetricUtils , 'record' > {
144
144
protected readonly _absolute : boolean ;
145
145
146
146
constructor (
@@ -153,7 +153,7 @@ export class MeasureMetric extends Metric<BoundMeasure>
153
153
154
154
this . _absolute = options . absolute !== undefined ? options . absolute : true ; // Absolute default is true
155
155
}
156
- protected _makeInstrument ( labels : types . Labels ) : BoundMeasure {
156
+ protected _makeInstrument ( labels : api . Labels ) : BoundMeasure {
157
157
return new BoundMeasure (
158
158
labels ,
159
159
this . _disabled ,
@@ -165,15 +165,15 @@ export class MeasureMetric extends Metric<BoundMeasure>
165
165
) ;
166
166
}
167
167
168
- record ( value : number , labels : types . Labels ) {
168
+ record ( value : number , labels : api . Labels ) {
169
169
this . bind ( labels ) . record ( value ) ;
170
170
}
171
171
}
172
172
173
173
/** This is a SDK implementation of Observer Metric. */
174
174
export class ObserverMetric extends Metric < BoundObserver >
175
- implements Pick < types . MetricUtils , 'setCallback' > {
176
- private _observerResult : types . ObserverResult = new ObserverResult ( ) ;
175
+ implements Pick < api . MetricUtils , 'setCallback' > {
176
+ private _observerResult = new ObserverResult ( ) ;
177
177
178
178
constructor (
179
179
name : string ,
@@ -184,7 +184,7 @@ export class ObserverMetric extends Metric<BoundObserver>
184
184
super ( name , options , MetricKind . OBSERVER , resource ) ;
185
185
}
186
186
187
- protected _makeInstrument ( labels : types . Labels ) : BoundObserver {
187
+ protected _makeInstrument ( labels : api . Labels ) : BoundObserver {
188
188
return new BoundObserver (
189
189
labels ,
190
190
this . _disabled ,
@@ -196,7 +196,7 @@ export class ObserverMetric extends Metric<BoundObserver>
196
196
}
197
197
198
198
getMetricRecord ( ) : MetricRecord [ ] {
199
- this . _observerResult . observers . forEach ( ( callback , labels ) => {
199
+ this . _observerResult . callbackObservers . forEach ( ( callback , labels ) => {
200
200
const instrument = this . bind ( labels ) ;
201
201
instrument . update ( callback ( ) ) ;
202
202
} ) ;
@@ -207,7 +207,13 @@ export class ObserverMetric extends Metric<BoundObserver>
207
207
* Sets a callback where user can observe value for certain labels
208
208
* @param callback
209
209
*/
210
- setCallback ( callback : ( observerResult : types . ObserverResult ) => void ) : void {
210
+ setCallback ( callback : ( observerResult : api . ObserverResult ) => void ) : void {
211
211
callback ( this . _observerResult ) ;
212
+ this . _observerResult . observers . forEach ( ( observer , labels ) => {
213
+ observer . subscribe ( value => {
214
+ const instrument = this . bind ( labels ) ;
215
+ instrument . update ( value ) ;
216
+ } ) ;
217
+ } ) ;
212
218
}
213
219
}
0 commit comments