Skip to content

Commit

Permalink
feat(add-aggregation-selector-option-to-otlp-metric-exporter)
Browse files Browse the repository at this point in the history
  • Loading branch information
AkselAllas committed Jan 17, 2024
1 parent ae0a3c5 commit 0fad7d0
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 1 deletion.
2 changes: 2 additions & 0 deletions experimental/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ All notable changes to experimental packages in this project will be documented

### :rocket: (Enhancement)

* feat(exporter-metrics-otlp-http) [#4409](https://github.com/open-telemetry/opentelemetry-js/pull/4409) @AkselAllas

### :bug: (Bug Fix)

* fix(instrumentation): use caret range on import-in-the-middle [#4380](https://github.com/open-telemetry/opentelemetry-js/pull/4380) @pichlermarc
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?.aggregationSelector) {
return config.aggregationSelector;
} else {
return (_instrumentType: any) => Aggregation.Default();
}
}

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;
aggregationSelector?: 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,32 @@ 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({
aggregationSelector: _instrumentType => aggregation,
});
assert.equal(
exporter.selectAggregation(InstrumentType.COUNTER),
aggregation
);
});

it('aggregationSelector returns the default aggregation preference when nothing is supplied', () => {
const exporter = new OTLPMetricExporter({
temporalityPreference: AggregationTemporalityPreference.CUMULATIVE,
aggregationSelector: _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

0 comments on commit 0fad7d0

Please sign in to comment.