Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(exporter-metrics-otlp-http): add option to set the exporter aggregation preference #4409

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions experimental/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ All notable changes to experimental packages in this project will be documented

### :rocket: (Enhancement)

* feat(exporter-metrics-otlp-http): add option to set the exporter aggregation preference [#4409](https://github.com/open-telemetry/opentelemetry-js/pull/4409) @AkselAllas

### :bug: (Bug Fix)

### :books: (Refine Doc)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ import {
InstrumentType,
PushMetricExporter,
ResourceMetrics,
Aggregation,
AggregationSelector,
} from '@opentelemetry/sdk-metrics';
import {
AggregationTemporalityPreference,
Expand Down Expand Up @@ -104,6 +106,16 @@ function chooseTemporalitySelector(
return chooseTemporalitySelectorFromEnvironment();
}

function chooseAggregationSelector(
config: OTLPMetricExporterOptions | undefined
) {
if (config?.aggregationPreference) {
return config.aggregationPreference;
} else {
return (_instrumentType: any) => Aggregation.Default();
pichlermarc marked this conversation as resolved.
Show resolved Hide resolved
}
}

export class OTLPMetricExporterBase<
T extends OTLPExporterBase<
OTLPMetricExporterOptions,
Expand All @@ -114,9 +126,11 @@ export class OTLPMetricExporterBase<
{
public _otlpExporter: T;
private _aggregationTemporalitySelector: AggregationTemporalitySelector;
private _aggregationSelector: AggregationSelector;

constructor(exporter: T, config?: OTLPMetricExporterOptions) {
this._otlpExporter = exporter;
this._aggregationSelector = chooseAggregationSelector(config);
this._aggregationTemporalitySelector = chooseTemporalitySelector(
config?.temporalityPreference
);
Expand All @@ -137,6 +151,10 @@ export class OTLPMetricExporterBase<
return Promise.resolve();
}

selectAggregation(instrumentType: InstrumentType): Aggregation {
return this._aggregationSelector(instrumentType);
}

selectAggregationTemporality(
instrumentType: InstrumentType
): AggregationTemporality {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,16 @@
*/

import { OTLPExporterConfigBase } from '@opentelemetry/otlp-exporter-base';
import { AggregationTemporality } from '@opentelemetry/sdk-metrics';
import {
AggregationTemporality,
AggregationSelector,
} from '@opentelemetry/sdk-metrics';

export interface OTLPMetricExporterOptions extends OTLPExporterConfigBase {
temporalityPreference?:
| AggregationTemporalityPreference
| AggregationTemporality;
aggregationPreference?: AggregationSelector;
}

export enum AggregationTemporalityPreference {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,9 @@ import {
} from '../metricsHelper';
import { MockedResponse } from './nodeHelpers';
import {
Aggregation,
AggregationTemporality,
ExplicitBucketHistogramAggregation,
InstrumentType,
ResourceMetrics,
} from '@opentelemetry/sdk-metrics';
Expand Down Expand Up @@ -218,6 +220,31 @@ describe('OTLPMetricExporter - node with json over http', () => {
});
});

describe('aggregation', () => {
it('aggregationSelector calls the selector supplied to the constructor', () => {
const aggregation = new ExplicitBucketHistogramAggregation([
0, 100, 100000,
]);
const exporter = new OTLPMetricExporter({
aggregationPreference: _instrumentType => aggregation,
});
assert.equal(
exporter.selectAggregation(InstrumentType.COUNTER),
aggregation
);
});

it('aggregationSelector returns the default aggregation preference when nothing is supplied', () => {
const exporter = new OTLPMetricExporter({
aggregationPreference: _instrumentType => Aggregation.Default(),
});
assert.equal(
exporter.selectAggregation(InstrumentType.COUNTER),
Aggregation.Default()
);
});
});

describe('when configuring via environment', () => {
const envSource = process.env;
it('should use url defined in env that ends with root path and append version and signal path', () => {
Expand Down