1717import * as api from '@opentelemetry/api' ;
1818import { ConsoleLogger , InstrumentationLibrary } from '@opentelemetry/core' ;
1919import { Resource } from '@opentelemetry/resources' ;
20- import { BatchObserverMetric } from './BatchObserverMetric ' ;
20+ import { BatchObserver } from './BatchObserver ' ;
2121import { BaseBoundInstrument } from './BoundInstrument' ;
2222import { Processor } from './export/Processor' ;
23- import { MetricKind } from './export/types' ;
2423import { UpDownCounterMetric } from './UpDownCounterMetric' ;
2524import { CounterMetric } from './CounterMetric' ;
2625import { UpDownSumObserverMetric } from './UpDownSumObserverMetric' ;
@@ -38,6 +37,7 @@ import { NoopExporter } from './export/NoopExporter';
3837 */
3938export class Meter implements api . Meter {
4039 private readonly _logger : api . Logger ;
40+ private readonly _batchObservers : BatchObserver [ ] = [ ] ;
4141 private readonly _metrics = new Map < string , Metric < BaseBoundInstrument > > ( ) ;
4242 private readonly _processor : Processor ;
4343 private readonly _resource : Resource ;
@@ -258,36 +258,20 @@ export class Meter implements api.Meter {
258258 }
259259
260260 /**
261- * Creates a new batch observer metric.
262- * @param name the name of the metric.
261+ * Creates a new batch observer.
263262 * @param callback the batch observer callback
264- * @param [options] the metric batch options.
263+ * @param [options] the batch options.
265264 */
266265 createBatchObserver (
267- name : string ,
268266 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 = {
278270 logger : this . _logger ,
279- ...DEFAULT_METRIC_OPTIONS ,
280271 ...options ,
281272 } ;
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 ) ;
291275 return batchObserver ;
292276 }
293277
@@ -300,27 +284,15 @@ export class Meter implements api.Meter {
300284 */
301285 async collect ( ) : Promise < void > {
302286 // 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 ( ) ;
314289 } ) ;
290+ await Promise . all ( observations ) ;
315291
316292 // 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+ } ) ;
324296
325297 await Promise . all ( metrics ) . then ( records => {
326298 records . forEach ( metrics => {
0 commit comments