1414 * limitations under the License.
1515 */
1616
17- import * as types from '@opentelemetry/api' ;
17+ import * as api from '@opentelemetry/api' ;
1818import { Resource } from '@opentelemetry/resources' ;
1919import {
2020 BoundCounter ,
@@ -30,11 +30,11 @@ import { hashLabels } from './Utils';
3030
3131/** This is a SDK implementation of {@link Metric} interface. */
3232export abstract class Metric < T extends BaseBoundInstrument >
33- implements types . Metric < T > {
33+ implements api . Metric < T > {
3434 protected readonly _monotonic : boolean ;
3535 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 ;
3838 private readonly _descriptor : MetricDescriptor ;
3939 private readonly _instruments : Map < string , T > = new Map ( ) ;
4040
@@ -58,7 +58,7 @@ export abstract class Metric<T extends BaseBoundInstrument>
5858 * @param labels key-values pairs that are associated with a specific metric
5959 * that you want to record.
6060 */
61- bind ( labels : types . Labels ) : T {
61+ bind ( labels : api . Labels ) : T {
6262 const hash = hashLabels ( labels ) ;
6363 if ( this . _instruments . has ( hash ) ) return this . _instruments . get ( hash ) ! ;
6464
@@ -71,7 +71,7 @@ export abstract class Metric<T extends BaseBoundInstrument>
7171 * Removes the Instrument from the metric, if it is present.
7272 * @param labels key-values pairs that are associated with a specific metric.
7373 */
74- unbind ( labels : types . Labels ) : void {
74+ unbind ( labels : api . Labels ) : void {
7575 this . _instruments . delete ( hashLabels ( labels ) ) ;
7676 }
7777
@@ -102,12 +102,12 @@ export abstract class Metric<T extends BaseBoundInstrument>
102102 } ;
103103 }
104104
105- protected abstract _makeInstrument ( labels : types . Labels ) : T ;
105+ protected abstract _makeInstrument ( labels : api . Labels ) : T ;
106106}
107107
108108/** This is a SDK implementation of Counter Metric. */
109109export class CounterMetric extends Metric < BoundCounter >
110- implements Pick < types . MetricUtils , 'add' > {
110+ implements Pick < api . MetricUtils , 'add' > {
111111 constructor (
112112 name : string ,
113113 options : MetricOptions ,
@@ -116,7 +116,7 @@ export class CounterMetric extends Metric<BoundCounter>
116116 ) {
117117 super ( name , options , MetricKind . COUNTER , resource ) ;
118118 }
119- protected _makeInstrument ( labels : types . Labels ) : BoundCounter {
119+ protected _makeInstrument ( labels : api . Labels ) : BoundCounter {
120120 return new BoundCounter (
121121 labels ,
122122 this . _disabled ,
@@ -134,13 +134,13 @@ export class CounterMetric extends Metric<BoundCounter>
134134 * @param labels key-values pairs that are associated with a specific metric
135135 * that you want to record.
136136 */
137- add ( value : number , labels : types . Labels ) {
137+ add ( value : number , labels : api . Labels ) {
138138 this . bind ( labels ) . add ( value ) ;
139139 }
140140}
141141
142142export class MeasureMetric extends Metric < BoundMeasure >
143- implements Pick < types . MetricUtils , 'record' > {
143+ implements Pick < api . MetricUtils , 'record' > {
144144 protected readonly _absolute : boolean ;
145145
146146 constructor (
@@ -153,7 +153,7 @@ export class MeasureMetric extends Metric<BoundMeasure>
153153
154154 this . _absolute = options . absolute !== undefined ? options . absolute : true ; // Absolute default is true
155155 }
156- protected _makeInstrument ( labels : types . Labels ) : BoundMeasure {
156+ protected _makeInstrument ( labels : api . Labels ) : BoundMeasure {
157157 return new BoundMeasure (
158158 labels ,
159159 this . _disabled ,
@@ -165,15 +165,15 @@ export class MeasureMetric extends Metric<BoundMeasure>
165165 ) ;
166166 }
167167
168- record ( value : number , labels : types . Labels ) {
168+ record ( value : number , labels : api . Labels ) {
169169 this . bind ( labels ) . record ( value ) ;
170170 }
171171}
172172
173173/** This is a SDK implementation of Observer Metric. */
174174export 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 ( ) ;
177177
178178 constructor (
179179 name : string ,
@@ -184,7 +184,7 @@ export class ObserverMetric extends Metric<BoundObserver>
184184 super ( name , options , MetricKind . OBSERVER , resource ) ;
185185 }
186186
187- protected _makeInstrument ( labels : types . Labels ) : BoundObserver {
187+ protected _makeInstrument ( labels : api . Labels ) : BoundObserver {
188188 return new BoundObserver (
189189 labels ,
190190 this . _disabled ,
@@ -196,7 +196,7 @@ export class ObserverMetric extends Metric<BoundObserver>
196196 }
197197
198198 getMetricRecord ( ) : MetricRecord [ ] {
199- this . _observerResult . observers . forEach ( ( callback , labels ) => {
199+ this . _observerResult . callbackObservers . forEach ( ( callback , labels ) => {
200200 const instrument = this . bind ( labels ) ;
201201 instrument . update ( callback ( ) ) ;
202202 } ) ;
@@ -207,7 +207,13 @@ export class ObserverMetric extends Metric<BoundObserver>
207207 * Sets a callback where user can observe value for certain labels
208208 * @param callback
209209 */
210- setCallback ( callback : ( observerResult : types . ObserverResult ) => void ) : void {
210+ setCallback ( callback : ( observerResult : api . ObserverResult ) => void ) : void {
211211 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+ } ) ;
212218 }
213219}
0 commit comments