Skip to content

Commit c6dab2a

Browse files
authored
refactor: change metrics export data model to match OTLP protos (#2809)
* refactor: match metrics export model with proto * refactor: get rid of MetricsData * fix: don't filter empty instrumentation library metrics
1 parent 82192b5 commit c6dab2a

28 files changed

+91
-189
lines changed

experimental/packages/opentelemetry-sdk-metrics-base/src/Meter.ts

+8-6
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ import { Counter, Histogram, UpDownCounter } from './Instruments';
2121
import { MeterProviderSharedState } from './state/MeterProviderSharedState';
2222
import { MultiMetricStorage } from './state/MultiWritableMetricStorage';
2323
import { SyncMetricStorage } from './state/SyncMetricStorage';
24-
import { MetricData } from './export/MetricData';
24+
import { InstrumentationLibraryMetrics } from './export/MetricData';
2525
import { isNotNullish } from './utils';
2626
import { MetricCollectorHandle } from './state/MetricCollector';
2727
import { HrTime } from '@opentelemetry/api';
@@ -128,16 +128,18 @@ export class Meter implements metrics.Meter {
128128
* @param collectionTime the HrTime at which the collection was initiated.
129129
* @returns the list of {@link MetricData} collected.
130130
*/
131-
async collect(collector: MetricCollectorHandle, collectionTime: HrTime): Promise<MetricData[]> {
132-
const result = await Promise.all(this._metricStorageRegistry.getStorages().map(metricStorage => {
131+
async collect(collector: MetricCollectorHandle, collectionTime: HrTime): Promise<InstrumentationLibraryMetrics> {
132+
const metricData = await Promise.all(this._metricStorageRegistry.getStorages().map(metricStorage => {
133133
return metricStorage.collect(
134134
collector,
135135
this._meterProviderSharedState.metricCollectors,
136-
this._meterProviderSharedState.resource,
137-
this._instrumentationLibrary,
138136
this._meterProviderSharedState.sdkStartTime,
139137
collectionTime);
140138
}));
141-
return result.filter(isNotNullish);
139+
140+
return {
141+
instrumentationLibrary: this._instrumentationLibrary,
142+
metrics: metricData.filter(isNotNullish),
143+
};
142144
}
143145
}

experimental/packages/opentelemetry-sdk-metrics-base/src/aggregator/Drop.ts

-4
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,6 @@
1515
*/
1616

1717
import { HrTime } from '@opentelemetry/api';
18-
import { InstrumentationLibrary } from '@opentelemetry/core';
19-
import { Resource } from '@opentelemetry/resources';
2018
import { MetricData } from '../export/MetricData';
2119
import { InstrumentDescriptor } from '../InstrumentDescriptor';
2220
import { Maybe } from '../utils';
@@ -43,8 +41,6 @@ export class DropAggregator implements Aggregator<undefined> {
4341
}
4442

4543
toMetricData(
46-
_resource: Resource,
47-
_instrumentationLibrary: InstrumentationLibrary,
4844
_instrumentDescriptor: InstrumentDescriptor,
4945
_accumulationByAttributes: AccumulationRecord<undefined>[],
5046
_startTime: HrTime,

experimental/packages/opentelemetry-sdk-metrics-base/src/aggregator/Histogram.ts

-6
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,6 @@ import {
2222
Histogram,
2323
} from './types';
2424
import { HistogramMetricData, PointDataType } from '../export/MetricData';
25-
import { Resource } from '@opentelemetry/resources';
26-
import { InstrumentationLibrary } from '@opentelemetry/core';
2725
import { HrTime } from '@opentelemetry/api';
2826
import { InstrumentDescriptor } from '../InstrumentDescriptor';
2927
import { Maybe } from '../utils';
@@ -138,15 +136,11 @@ export class HistogramAggregator implements Aggregator<HistogramAccumulation> {
138136
}
139137

140138
toMetricData(
141-
resource: Resource,
142-
instrumentationLibrary: InstrumentationLibrary,
143139
metricDescriptor: InstrumentDescriptor,
144140
accumulationByAttributes: AccumulationRecord<HistogramAccumulation>[],
145141
startTime: HrTime,
146142
endTime: HrTime): Maybe<HistogramMetricData> {
147143
return {
148-
resource,
149-
instrumentationLibrary,
150144
instrumentDescriptor: metricDescriptor,
151145
pointDataType: PointDataType.HISTOGRAM,
152146
pointData: accumulationByAttributes.map(([attributes, accumulation]) => {

experimental/packages/opentelemetry-sdk-metrics-base/src/aggregator/LastValue.ts

+1-6
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,7 @@
1616

1717
import { LastValue, AggregatorKind, Aggregator, Accumulation, AccumulationRecord } from './types';
1818
import { HrTime } from '@opentelemetry/api';
19-
import { hrTime, hrTimeToMicroseconds, InstrumentationLibrary } from '@opentelemetry/core';
20-
import { Resource } from '@opentelemetry/resources';
19+
import { hrTime, hrTimeToMicroseconds } from '@opentelemetry/core';
2120
import { PointDataType, SingularMetricData } from '../export/MetricData';
2221
import { InstrumentDescriptor } from '../InstrumentDescriptor';
2322
import { Maybe } from '../utils';
@@ -67,15 +66,11 @@ export class LastValueAggregator implements Aggregator<LastValueAccumulation> {
6766
}
6867

6968
toMetricData(
70-
resource: Resource,
71-
instrumentationLibrary: InstrumentationLibrary,
7269
instrumentDescriptor: InstrumentDescriptor,
7370
accumulationByAttributes: AccumulationRecord<LastValueAccumulation>[],
7471
startTime: HrTime,
7572
endTime: HrTime): Maybe<SingularMetricData> {
7673
return {
77-
resource,
78-
instrumentationLibrary,
7974
instrumentDescriptor,
8075
pointDataType: PointDataType.SINGULAR,
8176
pointData: accumulationByAttributes.map(([attributes, accumulation]) => {

experimental/packages/opentelemetry-sdk-metrics-base/src/aggregator/Sum.ts

-6
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@
1616

1717
import { Sum, AggregatorKind, Aggregator, Accumulation, AccumulationRecord } from './types';
1818
import { HrTime } from '@opentelemetry/api';
19-
import { InstrumentationLibrary } from '@opentelemetry/core';
20-
import { Resource } from '@opentelemetry/resources';
2119
import { PointDataType, SingularMetricData } from '../export/MetricData';
2220
import { InstrumentDescriptor } from '../InstrumentDescriptor';
2321
import { Maybe } from '../utils';
@@ -57,15 +55,11 @@ export class SumAggregator implements Aggregator<SumAccumulation> {
5755
}
5856

5957
toMetricData(
60-
resource: Resource,
61-
instrumentationLibrary: InstrumentationLibrary,
6258
instrumentDescriptor: InstrumentDescriptor,
6359
accumulationByAttributes: AccumulationRecord<SumAccumulation>[],
6460
startTime: HrTime,
6561
endTime: HrTime): Maybe<SingularMetricData> {
6662
return {
67-
resource,
68-
instrumentationLibrary,
6963
instrumentDescriptor,
7064
pointDataType: PointDataType.SINGULAR,
7165
pointData: accumulationByAttributes.map(([attributes, accumulation]) => {

experimental/packages/opentelemetry-sdk-metrics-base/src/aggregator/types.ts

+1-5
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@
1616

1717
import { HrTime } from '@opentelemetry/api';
1818
import { Attributes } from '@opentelemetry/api-metrics';
19-
import { InstrumentationLibrary } from '@opentelemetry/core';
20-
import { Resource } from '@opentelemetry/resources';
2119
import { MetricData } from '../export/MetricData';
2220
import { InstrumentDescriptor } from '../InstrumentDescriptor';
2321
import { Maybe } from '../utils';
@@ -116,9 +114,7 @@ export interface Aggregator<T> {
116114
* @param endTime the end time of the metric data.
117115
* @return the {@link MetricData} that this {@link Aggregator} will produce.
118116
*/
119-
toMetricData(resource: Resource,
120-
instrumentationLibrary: InstrumentationLibrary,
121-
instrumentDescriptor: InstrumentDescriptor,
117+
toMetricData(instrumentDescriptor: InstrumentDescriptor,
122118
accumulationByAttributes: AccumulationRecord<T>[],
123119
startTime: HrTime,
124120
endTime: HrTime): Maybe<MetricData>;

experimental/packages/opentelemetry-sdk-metrics-base/src/export/MetricData.ts

+15-16
Original file line numberDiff line numberDiff line change
@@ -25,46 +25,45 @@ import { Histogram } from '../aggregator/types';
2525
* Basic metric data fields.
2626
*/
2727
export interface BaseMetricData {
28-
/**
29-
* Resource associated with metric telemetry.
30-
*/
31-
readonly resource: Resource;
32-
/**
33-
* InstrumentationLibrary which created the metric instrument.
34-
*/
35-
readonly instrumentationLibrary: InstrumentationLibrary;
36-
/**
37-
* InstrumentDescriptor which describes the metric instrument.
38-
*/
3928
readonly instrumentDescriptor: InstrumentDescriptor;
4029
/**
4130
* PointDataType of the metric instrument.
4231
*/
43-
readonly pointDataType: PointDataType,
32+
readonly pointDataType: PointDataType;
4433
}
4534

4635
/**
4736
* Represents a metric data aggregated by either a LastValueAggregation or
4837
* SumAggregation.
4938
*/
5039
export interface SingularMetricData extends BaseMetricData {
51-
readonly pointDataType: PointDataType.SINGULAR,
52-
readonly pointData: PointData<number>[],
40+
readonly pointDataType: PointDataType.SINGULAR;
41+
readonly pointData: PointData<number>[];
5342
}
5443

5544
/**
5645
* Represents a metric data aggregated by a HistogramAggregation.
5746
*/
5847
export interface HistogramMetricData extends BaseMetricData {
59-
readonly pointDataType: PointDataType.HISTOGRAM,
60-
readonly pointData: PointData<Histogram>[],
48+
readonly pointDataType: PointDataType.HISTOGRAM;
49+
readonly pointData: PointData<Histogram>[];
6150
}
6251

6352
/**
6453
* Represents an aggregated metric data.
6554
*/
6655
export type MetricData = SingularMetricData | HistogramMetricData;
6756

57+
export interface InstrumentationLibraryMetrics {
58+
instrumentationLibrary: InstrumentationLibrary;
59+
metrics: MetricData[];
60+
}
61+
62+
export interface ResourceMetrics {
63+
resource: Resource;
64+
instrumentationLibraryMetrics: InstrumentationLibraryMetrics[];
65+
}
66+
6867
/**
6968
* The aggregated point data type.
7069
*/

experimental/packages/opentelemetry-sdk-metrics-base/src/export/MetricExporter.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
*/
1616

1717
import { AggregationTemporality } from './AggregationTemporality';
18-
import { MetricData } from './MetricData';
18+
import { ResourceMetrics } from './MetricData';
1919
import {
2020
ExportResult,
2121
ExportResultCode,
@@ -26,7 +26,7 @@ import {
2626

2727
export interface PushMetricExporter {
2828

29-
export(batch: MetricData[], resultCallback: (result: ExportResult) => void): void;
29+
export(metrics: ResourceMetrics, resultCallback: (result: ExportResult) => void): void;
3030

3131
forceFlush(): Promise<void>;
3232

@@ -39,7 +39,7 @@ export interface PushMetricExporter {
3939
export class ConsoleMetricExporter implements PushMetricExporter {
4040
protected _shutdown = true;
4141

42-
export(_batch: MetricData[], resultCallback: (result: ExportResult) => void) {
42+
export(metrics: ResourceMetrics, resultCallback: (result: ExportResult) => void) {
4343
return resultCallback({
4444
code: ExportResultCode.FAILED,
4545
error: new Error('Method not implemented')

experimental/packages/opentelemetry-sdk-metrics-base/src/export/MetricProducer.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,11 @@
1414
* limitations under the License.
1515
*/
1616

17-
import { MetricData } from './MetricData';
17+
import { ResourceMetrics } from './MetricData';
1818

1919
/**
2020
* This is a public interface that represent an export state of a MetricReader.
2121
*/
2222
export interface MetricProducer {
23-
collect(): Promise<MetricData[]>;
23+
collect(): Promise<ResourceMetrics>;
2424
}

experimental/packages/opentelemetry-sdk-metrics-base/src/export/MetricReader.ts

+5-4
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,9 @@
1717
import * as api from '@opentelemetry/api';
1818
import { AggregationTemporality } from './AggregationTemporality';
1919
import { MetricProducer } from './MetricProducer';
20-
import { MetricData } from './MetricData';
21-
import { callWithTimeout } from '../utils';
20+
import { ResourceMetrics } from './MetricData';
21+
import { callWithTimeout, Maybe } from '../utils';
22+
2223

2324
export type ReaderOptions = {
2425
timeoutMillis?: number
@@ -91,15 +92,15 @@ export abstract class MetricReader {
9192
/**
9293
* Collect all metrics from the associated {@link MetricProducer}
9394
*/
94-
async collect(options?: ReaderCollectionOptions): Promise<MetricData[]> {
95+
async collect(options?: ReaderCollectionOptions): Promise<Maybe<ResourceMetrics>> {
9596
if (this._metricProducer === undefined) {
9697
throw new Error('MetricReader is not bound to a MetricProducer');
9798
}
9899

99100
// Subsequent invocations to collect are not allowed. SDKs SHOULD return some failure for these calls.
100101
if (this._shutdown) {
101102
api.diag.warn('Collection is not allowed after shutdown');
102-
return [];
103+
return undefined;
103104
}
104105

105106
// No timeout if timeoutMillis is undefined or null.

experimental/packages/opentelemetry-sdk-metrics-base/src/export/PeriodicExportingMetricReader.ts

+5
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,11 @@ export class PeriodicExportingMetricReader extends MetricReader {
6363

6464
private async _runOnce(): Promise<void> {
6565
const metrics = await this.collect({});
66+
67+
if (metrics === undefined) {
68+
return;
69+
}
70+
6671
return new Promise((resolve, reject) => {
6772
this._exporter.export(metrics, result => {
6873
if (result.code !== ExportResultCode.SUCCESS) {

experimental/packages/opentelemetry-sdk-metrics-base/src/state/AsyncMetricStorage.ts

-6
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,6 @@ import {
2424
} from '../InstrumentDescriptor';
2525
import { AttributesProcessor } from '../view/AttributesProcessor';
2626
import { MetricStorage } from './MetricStorage';
27-
import { InstrumentationLibrary } from '@opentelemetry/core';
28-
import { Resource } from '@opentelemetry/resources';
2927
import { MetricData } from '../export/MetricData';
3028
import { DeltaMetricProcessor } from './DeltaMetricProcessor';
3129
import { TemporalMetricProcessor } from './TemporalMetricProcessor';
@@ -72,8 +70,6 @@ export class AsyncMetricStorage<T extends Maybe<Accumulation>> extends MetricSto
7270
async collect(
7371
collector: MetricCollectorHandle,
7472
collectors: MetricCollectorHandle[],
75-
resource: Resource,
76-
instrumentationLibrary: InstrumentationLibrary,
7773
sdkStartTime: HrTime,
7874
collectionTime: HrTime,
7975
): Promise<Maybe<MetricData>> {
@@ -87,8 +83,6 @@ export class AsyncMetricStorage<T extends Maybe<Accumulation>> extends MetricSto
8783
return this._temporalMetricStorage.buildMetrics(
8884
collector,
8985
collectors,
90-
resource,
91-
instrumentationLibrary,
9286
this._instrumentDescriptor,
9387
accumulations,
9488
sdkStartTime,

experimental/packages/opentelemetry-sdk-metrics-base/src/state/MetricCollector.ts

+8-5
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616

1717
import { hrTime } from '@opentelemetry/core';
1818
import { AggregationTemporality } from '../export/AggregationTemporality';
19-
import { MetricData } from '../export/MetricData';
19+
import { ResourceMetrics } from '../export/MetricData';
2020
import { MetricProducer } from '../export/MetricProducer';
2121
import { MetricReader } from '../export/MetricReader';
2222
import { MeterProviderSharedState } from './MeterProviderSharedState';
@@ -32,12 +32,15 @@ export class MetricCollector implements MetricProducer {
3232
this.aggregatorTemporality = this._metricReader.getPreferredAggregationTemporality();
3333
}
3434

35-
async collect(): Promise<MetricData[]> {
35+
async collect(): Promise<ResourceMetrics> {
3636
const collectionTime = hrTime();
37-
const results = await Promise.all(this._sharedState.meters
38-
.map(meter => meter.collect(this, collectionTime)));
37+
const instrumentationLibraryMetrics = (await Promise.all(this._sharedState.meters
38+
.map(meter => meter.collect(this, collectionTime))));
3939

40-
return results.reduce((cumulation, current) => cumulation.concat(current), []);
40+
return {
41+
resource: this._sharedState.resource,
42+
instrumentationLibraryMetrics,
43+
};
4144
}
4245

4346
/**

experimental/packages/opentelemetry-sdk-metrics-base/src/state/MetricStorage.ts

-4
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,6 @@
1515
*/
1616

1717
import { HrTime } from '@opentelemetry/api';
18-
import { InstrumentationLibrary } from '@opentelemetry/core';
19-
import { Resource } from '@opentelemetry/resources';
2018
import { MetricData } from '../export/MetricData';
2119
import { Maybe } from '../utils';
2220
import { MetricCollectorHandle } from './MetricCollector';
@@ -40,8 +38,6 @@ export abstract class MetricStorage {
4038
abstract collect(
4139
collector: MetricCollectorHandle,
4240
collectors: MetricCollectorHandle[],
43-
resource: Resource,
44-
instrumentationLibrary: InstrumentationLibrary,
4541
sdkStartTime: HrTime,
4642
collectionTime: HrTime,
4743
): Promise<Maybe<MetricData>>;

experimental/packages/opentelemetry-sdk-metrics-base/src/state/SyncMetricStorage.ts

-6
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,6 @@ import {
2525
} from '../InstrumentDescriptor';
2626
import { AttributesProcessor } from '../view/AttributesProcessor';
2727
import { MetricStorage } from './MetricStorage';
28-
import { InstrumentationLibrary } from '@opentelemetry/core';
29-
import { Resource } from '@opentelemetry/resources';
3028
import { MetricData } from '../export/MetricData';
3129
import { DeltaMetricProcessor } from './DeltaMetricProcessor';
3230
import { TemporalMetricProcessor } from './TemporalMetricProcessor';
@@ -66,8 +64,6 @@ export class SyncMetricStorage<T extends Maybe<Accumulation>> extends MetricStor
6664
async collect(
6765
collector: MetricCollectorHandle,
6866
collectors: MetricCollectorHandle[],
69-
resource: Resource,
70-
instrumentationLibrary: InstrumentationLibrary,
7167
sdkStartTime: HrTime,
7268
collectionTime: HrTime,
7369
): Promise<Maybe<MetricData>> {
@@ -76,8 +72,6 @@ export class SyncMetricStorage<T extends Maybe<Accumulation>> extends MetricStor
7672
return this._temporalMetricStorage.buildMetrics(
7773
collector,
7874
collectors,
79-
resource,
80-
instrumentationLibrary,
8175
this._instrumentDescriptor,
8276
accumulations,
8377
sdkStartTime,

0 commit comments

Comments
 (0)