17
17
import * as api from '@opentelemetry/api' ;
18
18
import { ConsoleLogger , InstrumentationLibrary } from '@opentelemetry/core' ;
19
19
import { Resource } from '@opentelemetry/resources' ;
20
- import { BatchObserverMetric } from './BatchObserverMetric ' ;
20
+ import { BatchObserver } from './BatchObserver ' ;
21
21
import { BaseBoundInstrument } from './BoundInstrument' ;
22
22
import { Processor } from './export/Processor' ;
23
- import { MetricKind } from './export/types' ;
24
23
import { UpDownCounterMetric } from './UpDownCounterMetric' ;
25
24
import { CounterMetric } from './CounterMetric' ;
26
25
import { UpDownSumObserverMetric } from './UpDownSumObserverMetric' ;
@@ -38,6 +37,7 @@ import { NoopExporter } from './export/NoopExporter';
38
37
*/
39
38
export class Meter implements api . Meter {
40
39
private readonly _logger : api . Logger ;
40
+ private readonly _batchObservers : BatchObserver [ ] = [ ] ;
41
41
private readonly _metrics = new Map < string , Metric < BaseBoundInstrument > > ( ) ;
42
42
private readonly _processor : Processor ;
43
43
private readonly _resource : Resource ;
@@ -258,36 +258,20 @@ export class Meter implements api.Meter {
258
258
}
259
259
260
260
/**
261
- * Creates a new batch observer metric.
262
- * @param name the name of the metric.
261
+ * Creates a new batch observer.
263
262
* @param callback the batch observer callback
264
- * @param [options] the metric batch options.
263
+ * @param [options] the batch options.
265
264
*/
266
265
createBatchObserver (
267
- name : string ,
268
266
callback : ( observerResult : api . BatchObserverResult ) => void ,
269
- options : api . BatchMetricOptions = { }
270
- ) : api . BatchObserver {
271
- if ( ! this . _isValidName ( name ) ) {
272
- this . _logger . warn (
273
- `Invalid metric name ${ name } . Defaulting to noop metric implementation.`
274
- ) ;
275
- return api . NOOP_BATCH_OBSERVER_METRIC ;
276
- }
277
- const opt : api . BatchMetricOptions = {
267
+ options : api . BatchObserverOptions = { }
268
+ ) : BatchObserver {
269
+ const opt : api . BatchObserverOptions = {
278
270
logger : this . _logger ,
279
- ...DEFAULT_METRIC_OPTIONS ,
280
271
...options ,
281
272
} ;
282
- const batchObserver = new BatchObserverMetric (
283
- name ,
284
- opt ,
285
- this . _processor ,
286
- this . _resource ,
287
- this . _instrumentationLibrary ,
288
- callback
289
- ) ;
290
- this . _registerMetric ( name , batchObserver ) ;
273
+ const batchObserver = new BatchObserver ( opt , callback ) ;
274
+ this . _batchObservers . push ( batchObserver ) ;
291
275
return batchObserver ;
292
276
}
293
277
@@ -300,27 +284,15 @@ export class Meter implements api.Meter {
300
284
*/
301
285
async collect ( ) : Promise < void > {
302
286
// call batch observers first
303
- const batchObservers = Array . from ( this . _metrics . values ( ) )
304
- . filter ( metric => {
305
- return metric . getKind ( ) === MetricKind . BATCH_OBSERVER ;
306
- } )
307
- . map ( metric => {
308
- return metric . getMetricRecord ( ) ;
309
- } ) ;
310
- await Promise . all ( batchObservers ) . then ( records => {
311
- records . forEach ( metrics => {
312
- metrics . forEach ( metric => this . _processor . process ( metric ) ) ;
313
- } ) ;
287
+ const observations = this . _batchObservers . map ( observer => {
288
+ return observer . collect ( ) ;
314
289
} ) ;
290
+ await Promise . all ( observations ) ;
315
291
316
292
// after this all remaining metrics can be run
317
- const metrics = Array . from ( this . _metrics . values ( ) )
318
- . filter ( metric => {
319
- return metric . getKind ( ) !== MetricKind . BATCH_OBSERVER ;
320
- } )
321
- . map ( metric => {
322
- return metric . getMetricRecord ( ) ;
323
- } ) ;
293
+ const metrics = Array . from ( this . _metrics . values ( ) ) . map ( metric => {
294
+ return metric . getMetricRecord ( ) ;
295
+ } ) ;
324
296
325
297
await Promise . all ( metrics ) . then ( records => {
326
298
records . forEach ( metrics => {
0 commit comments