1616
1717import * as metrics from '@opentelemetry/api-metrics' ;
1818import { InstrumentationLibrary } from '@opentelemetry/core' ;
19- import { createInstrumentDescriptor , InstrumentDescriptor , InstrumentType } from './InstrumentDescriptor' ;
19+ import { createInstrumentDescriptor , InstrumentType } from './InstrumentDescriptor' ;
2020import { CounterInstrument , HistogramInstrument , UpDownCounterInstrument } from './Instruments' ;
2121import { MeterProviderSharedState } from './state/MeterProviderSharedState' ;
22- import { MultiMetricStorage } from './state/MultiWritableMetricStorage' ;
23- import { SyncMetricStorage } from './state/SyncMetricStorage' ;
24- import { InstrumentationLibraryMetrics } from './export/MetricData' ;
25- import { isNotNullish } from './utils' ;
26- import { MetricCollectorHandle } from './state/MetricCollector' ;
27- import { HrTime } from '@opentelemetry/api' ;
28- import { AsyncMetricStorage } from './state/AsyncMetricStorage' ;
29- import { WritableMetricStorage } from './state/WritableMetricStorage' ;
30- import { MetricStorageRegistry } from './state/MetricStorageRegistry' ;
22+ import { MeterSharedState } from './state/MeterSharedState' ;
3123
3224/**
3325 * This class implements the {@link metrics.Meter} interface.
3426 */
3527export class Meter implements metrics . Meter {
36- private _metricStorageRegistry = new MetricStorageRegistry ( ) ;
28+ private _meterSharedState : MeterSharedState ;
3729
38- constructor ( private _meterProviderSharedState : MeterProviderSharedState , private _instrumentationLibrary : InstrumentationLibrary ) {
39- this . _meterProviderSharedState . meters . push ( this ) ;
30+ constructor ( meterProviderSharedState : MeterProviderSharedState , instrumentationLibrary : InstrumentationLibrary ) {
31+ this . _meterSharedState = meterProviderSharedState . getMeterSharedState ( instrumentationLibrary ) ;
4032 }
4133
4234 /**
4335 * Create a {@link metrics.Histogram} instrument.
4436 */
4537 createHistogram ( name : string , options ?: metrics . HistogramOptions ) : metrics . Histogram {
4638 const descriptor = createInstrumentDescriptor ( name , InstrumentType . HISTOGRAM , options ) ;
47- const storage = this . _registerMetricStorage ( descriptor ) ;
39+ const storage = this . _meterSharedState . registerMetricStorage ( descriptor ) ;
4840 return new HistogramInstrument ( storage , descriptor ) ;
4941 }
5042
@@ -53,7 +45,7 @@ export class Meter implements metrics.Meter {
5345 */
5446 createCounter ( name : string , options ?: metrics . CounterOptions ) : metrics . Counter {
5547 const descriptor = createInstrumentDescriptor ( name , InstrumentType . COUNTER , options ) ;
56- const storage = this . _registerMetricStorage ( descriptor ) ;
48+ const storage = this . _meterSharedState . registerMetricStorage ( descriptor ) ;
5749 return new CounterInstrument ( storage , descriptor ) ;
5850 }
5951
@@ -62,7 +54,7 @@ export class Meter implements metrics.Meter {
6254 */
6355 createUpDownCounter ( name : string , options ?: metrics . UpDownCounterOptions ) : metrics . UpDownCounter {
6456 const descriptor = createInstrumentDescriptor ( name , InstrumentType . UP_DOWN_COUNTER , options ) ;
65- const storage = this . _registerMetricStorage ( descriptor ) ;
57+ const storage = this . _meterSharedState . registerMetricStorage ( descriptor ) ;
6658 return new UpDownCounterInstrument ( storage , descriptor ) ;
6759 }
6860
@@ -75,7 +67,7 @@ export class Meter implements metrics.Meter {
7567 options ?: metrics . ObservableGaugeOptions ,
7668 ) : void {
7769 const descriptor = createInstrumentDescriptor ( name , InstrumentType . OBSERVABLE_GAUGE , options ) ;
78- this . _registerAsyncMetricStorage ( descriptor , callback ) ;
70+ this . _meterSharedState . registerAsyncMetricStorage ( descriptor , callback ) ;
7971 }
8072
8173 /**
@@ -87,7 +79,7 @@ export class Meter implements metrics.Meter {
8779 options ?: metrics . ObservableCounterOptions ,
8880 ) : void {
8981 const descriptor = createInstrumentDescriptor ( name , InstrumentType . OBSERVABLE_COUNTER , options ) ;
90- this . _registerAsyncMetricStorage ( descriptor , callback ) ;
82+ this . _meterSharedState . registerAsyncMetricStorage ( descriptor , callback ) ;
9183 }
9284
9385 /**
@@ -99,47 +91,6 @@ export class Meter implements metrics.Meter {
9991 options ?: metrics . ObservableUpDownCounterOptions ,
10092 ) : void {
10193 const descriptor = createInstrumentDescriptor ( name , InstrumentType . OBSERVABLE_UP_DOWN_COUNTER , options ) ;
102- this . _registerAsyncMetricStorage ( descriptor , callback ) ;
103- }
104-
105- private _registerMetricStorage ( descriptor : InstrumentDescriptor ) : WritableMetricStorage {
106- const views = this . _meterProviderSharedState . viewRegistry . findViews ( descriptor , this . _instrumentationLibrary ) ;
107- const storages = views . map ( view => this . _metricStorageRegistry . register ( SyncMetricStorage . create ( view , descriptor ) ) )
108- . filter ( isNotNullish ) ;
109-
110- if ( storages . length === 1 ) {
111- return storages [ 0 ] ;
112- }
113-
114- // This will be a no-op WritableMetricStorage when length is null.
115- return new MultiMetricStorage ( storages ) ;
116- }
117-
118- private _registerAsyncMetricStorage ( descriptor : InstrumentDescriptor , callback : metrics . ObservableCallback ) {
119- const views = this . _meterProviderSharedState . viewRegistry . findViews ( descriptor , this . _instrumentationLibrary ) ;
120- views . forEach ( view => {
121- this . _metricStorageRegistry . register ( AsyncMetricStorage . create ( view , descriptor , callback ) ) ;
122- } ) ;
123- }
124-
125- /**
126- * @internal
127- * @param collector opaque handle of {@link MetricCollector} which initiated the collection.
128- * @param collectionTime the HrTime at which the collection was initiated.
129- * @returns the list of {@link MetricData} collected.
130- */
131- async collect ( collector : MetricCollectorHandle , collectionTime : HrTime ) : Promise < InstrumentationLibraryMetrics > {
132- const metricData = await Promise . all ( this . _metricStorageRegistry . getStorages ( ) . map ( metricStorage => {
133- return metricStorage . collect (
134- collector ,
135- this . _meterProviderSharedState . metricCollectors ,
136- this . _meterProviderSharedState . sdkStartTime ,
137- collectionTime ) ;
138- } ) ) ;
139-
140- return {
141- instrumentationLibrary : this . _instrumentationLibrary ,
142- metrics : metricData . filter ( isNotNullish ) ,
143- } ;
94+ this . _meterSharedState . registerAsyncMetricStorage ( descriptor , callback ) ;
14495 }
14596}
0 commit comments