Skip to content

Commit 396b6d1

Browse files
committed
feat(add-aggregation-selector-option-to-otlp-metric-exporter)
1 parent ae0a3c5 commit 396b6d1

File tree

4 files changed

+54
-1
lines changed

4 files changed

+54
-1
lines changed

experimental/CHANGELOG.md

+2
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ All notable changes to experimental packages in this project will be documented
1313

1414
### :rocket: (Enhancement)
1515

16+
* feat(exporter-metrics-otlp-http) [#4409](https://github.com/open-telemetry/opentelemetry-js/pull/4409) @AkselAllas
17+
1618
### :bug: (Bug Fix)
1719

1820
* fix(instrumentation): use caret range on import-in-the-middle [#4380](https://github.com/open-telemetry/opentelemetry-js/pull/4380) @pichlermarc

experimental/packages/opentelemetry-exporter-metrics-otlp-http/src/OTLPMetricExporterBase.ts

+19
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ import {
2121
InstrumentType,
2222
PushMetricExporter,
2323
ResourceMetrics,
24+
Aggregation,
25+
AggregationSelector,
2426
} from '@opentelemetry/sdk-metrics';
2527
import {
2628
AggregationTemporalityPreference,
@@ -29,6 +31,7 @@ import {
2931
import { OTLPExporterBase } from '@opentelemetry/otlp-exporter-base';
3032
import { IExportMetricsServiceRequest } from '@opentelemetry/otlp-transformer';
3133
import { diag } from '@opentelemetry/api';
34+
import { DEFAULT_AGGREGATION_SELECTOR } from '@opentelemetry/sdk-metrics/src/export/AggregationSelector';
3235

3336
export const CumulativeTemporalitySelector: AggregationTemporalitySelector =
3437
() => AggregationTemporality.CUMULATIVE;
@@ -104,6 +107,16 @@ function chooseTemporalitySelector(
104107
return chooseTemporalitySelectorFromEnvironment();
105108
}
106109

110+
function chooseAggregationSelector(
111+
config: OTLPMetricExporterOptions | undefined
112+
) {
113+
if (config?.aggregationSelector) {
114+
return config.aggregationSelector;
115+
} else {
116+
return DEFAULT_AGGREGATION_SELECTOR;
117+
}
118+
}
119+
107120
export class OTLPMetricExporterBase<
108121
T extends OTLPExporterBase<
109122
OTLPMetricExporterOptions,
@@ -114,9 +127,11 @@ export class OTLPMetricExporterBase<
114127
{
115128
public _otlpExporter: T;
116129
private _aggregationTemporalitySelector: AggregationTemporalitySelector;
130+
private _aggregationSelector: AggregationSelector;
117131

118132
constructor(exporter: T, config?: OTLPMetricExporterOptions) {
119133
this._otlpExporter = exporter;
134+
this._aggregationSelector = chooseAggregationSelector(config);
120135
this._aggregationTemporalitySelector = chooseTemporalitySelector(
121136
config?.temporalityPreference
122137
);
@@ -137,6 +152,10 @@ export class OTLPMetricExporterBase<
137152
return Promise.resolve();
138153
}
139154

155+
selectAggregation(instrumentType: InstrumentType): Aggregation {
156+
return this._aggregationSelector(instrumentType);
157+
}
158+
140159
selectAggregationTemporality(
141160
instrumentType: InstrumentType
142161
): AggregationTemporality {

experimental/packages/opentelemetry-exporter-metrics-otlp-http/src/OTLPMetricExporterOptions.ts

+5-1
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,16 @@
1515
*/
1616

1717
import { OTLPExporterConfigBase } from '@opentelemetry/otlp-exporter-base';
18-
import { AggregationTemporality } from '@opentelemetry/sdk-metrics';
18+
import {
19+
AggregationTemporality,
20+
AggregationSelector,
21+
} from '@opentelemetry/sdk-metrics';
1922

2023
export interface OTLPMetricExporterOptions extends OTLPExporterConfigBase {
2124
temporalityPreference?:
2225
| AggregationTemporalityPreference
2326
| AggregationTemporality;
27+
aggregationSelector?: AggregationSelector;
2428
}
2529

2630
export enum AggregationTemporalityPreference {

experimental/packages/opentelemetry-exporter-metrics-otlp-http/test/node/CollectorMetricExporter.test.ts

+28
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,9 @@ import {
4343
} from '../metricsHelper';
4444
import { MockedResponse } from './nodeHelpers';
4545
import {
46+
Aggregation,
4647
AggregationTemporality,
48+
ExplicitBucketHistogramAggregation,
4749
InstrumentType,
4850
ResourceMetrics,
4951
} from '@opentelemetry/sdk-metrics';
@@ -218,6 +220,32 @@ describe('OTLPMetricExporter - node with json over http', () => {
218220
});
219221
});
220222

223+
describe('aggregation', () => {
224+
it('aggregationSelector calls the selector supplied to the constructor', () => {
225+
const aggregation = new ExplicitBucketHistogramAggregation([
226+
0, 100, 100000,
227+
]);
228+
const exporter = new OTLPMetricExporter({
229+
aggregationSelector: _instrumentType => aggregation,
230+
});
231+
assert.equal(
232+
exporter.selectAggregation(InstrumentType.COUNTER),
233+
aggregation
234+
);
235+
});
236+
237+
it('aggregationSelector returns the default aggregation preference when nothing is supplied', () => {
238+
const exporter = new OTLPMetricExporter({
239+
temporalityPreference: AggregationTemporalityPreference.CUMULATIVE,
240+
aggregationSelector: _instrumentType => Aggregation.Default(),
241+
});
242+
assert.equal(
243+
exporter.selectAggregation(InstrumentType.COUNTER),
244+
Aggregation.Default()
245+
);
246+
});
247+
});
248+
221249
describe('when configuring via environment', () => {
222250
const envSource = process.env;
223251
it('should use url defined in env that ends with root path and append version and signal path', () => {

0 commit comments

Comments
 (0)