-
Notifications
You must be signed in to change notification settings - Fork 821
/
MetricCollector.ts
110 lines (97 loc) · 3.53 KB
/
MetricCollector.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
/*
* Copyright The OpenTelemetry Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { millisToHrTime } from '@opentelemetry/core';
import { AggregationTemporalitySelector } from '../export/AggregationSelector';
import { CollectionResult, ScopeMetrics } from '../export/MetricData';
import { MetricProducer, MetricCollectOptions } from '../export/MetricProducer';
import { MetricReader } from '../export/MetricReader';
import { InstrumentType } from '../InstrumentDescriptor';
import { ForceFlushOptions, ShutdownOptions } from '../types';
import { MeterProviderSharedState } from './MeterProviderSharedState';
/**
* An internal opaque interface that the MetricReader receives as
* MetricProducer. It acts as the storage key to the internal metric stream
* state for each MetricReader.
*/
export class MetricCollector implements MetricProducer {
constructor(
private _sharedState: MeterProviderSharedState,
private _metricReader: MetricReader
) {}
async collect(options?: MetricCollectOptions): Promise<CollectionResult> {
const collectionTime = millisToHrTime(Date.now());
const scopeMetrics: ScopeMetrics[] = [];
const errors: unknown[] = [];
const meterCollectionPromises = Array.from(
this._sharedState.meterSharedStates.values()
).map(async meterSharedState => {
const current = await meterSharedState.collect(
this,
collectionTime,
options
);
// only add scope metrics if available
if (current?.scopeMetrics != null) {
scopeMetrics.push(current.scopeMetrics);
}
// only add errors if available
if (current?.errors != null) {
errors.push(...current.errors);
}
});
await Promise.all(meterCollectionPromises);
return {
resourceMetrics: {
resource: this._sharedState.resource,
scopeMetrics: scopeMetrics,
},
errors: errors,
};
}
/**
* Delegates for MetricReader.forceFlush.
*/
async forceFlush(options?: ForceFlushOptions): Promise<void> {
await this._metricReader.forceFlush(options);
}
/**
* Delegates for MetricReader.shutdown.
*/
async shutdown(options?: ShutdownOptions): Promise<void> {
await this._metricReader.shutdown(options);
}
selectAggregationTemporality(instrumentType: InstrumentType) {
return this._metricReader.selectAggregationTemporality(instrumentType);
}
selectAggregation(instrumentType: InstrumentType) {
return this._metricReader.selectAggregation(instrumentType);
}
/**
* Select the cardinality limit for the given {@link InstrumentType} for this
* collector.
*/
selectCardinalityLimit(instrumentType: InstrumentType): number {
return this._metricReader.selectCardinalityLimit?.(instrumentType) ?? 2000;
}
}
/**
* An internal interface for MetricCollector. Exposes the necessary
* information for metric collection.
*/
export interface MetricCollectorHandle {
selectAggregationTemporality: AggregationTemporalitySelector;
selectCardinalityLimit(instrumentType: InstrumentType): number;
}