Skip to content

Commit e0f076d

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

File tree

3 files changed

+51
-1
lines changed

3 files changed

+51
-1
lines changed

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

+18
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,
@@ -104,6 +106,16 @@ function chooseTemporalitySelector(
104106
return chooseTemporalitySelectorFromEnvironment();
105107
}
106108

109+
function chooseAggregationSelector(
110+
config: OTLPMetricExporterOptions | undefined
111+
) {
112+
if (config?.aggregationSelector) {
113+
return config.aggregationSelector;
114+
} else {
115+
return (_instrumentType: any) => Aggregation.Default();
116+
}
117+
}
118+
107119
export class OTLPMetricExporterBase<
108120
T extends OTLPExporterBase<
109121
OTLPMetricExporterOptions,
@@ -114,9 +126,11 @@ export class OTLPMetricExporterBase<
114126
{
115127
public _otlpExporter: T;
116128
private _aggregationTemporalitySelector: AggregationTemporalitySelector;
129+
private _aggregationSelector: AggregationSelector;
117130

118131
constructor(exporter: T, config?: OTLPMetricExporterOptions) {
119132
this._otlpExporter = exporter;
133+
this._aggregationSelector = chooseAggregationSelector(config);
120134
this._aggregationTemporalitySelector = chooseTemporalitySelector(
121135
config?.temporalityPreference
122136
);
@@ -137,6 +151,10 @@ export class OTLPMetricExporterBase<
137151
return Promise.resolve();
138152
}
139153

154+
selectAggregation(instrumentType: InstrumentType): Aggregation {
155+
return this._aggregationSelector(instrumentType);
156+
}
157+
140158
selectAggregationTemporality(
141159
instrumentType: InstrumentType
142160
): 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)