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(metrics-exporters): configure temporality via env var #3305

Merged
Show file tree
Hide file tree
Changes from 3 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
1 change: 1 addition & 0 deletions experimental/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ All notable changes to experimental packages in this project will be documented
### :rocket: (Enhancement)

* feat(sdk-node): configure trace exporter with environment variables [#3143](https://github.com/open-telemetry/opentelemetry-js/pull/3143) @svetlanabrennan
* feat(metrics-exporters): configure temporality via environment variable [#3305](https://github.com/open-telemetry/opentelemetry-js/pull/3305) @pichlermarc

### :bug: (Bug Fix)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
*/

import {
defaultOptions,
OTLPMetricExporterBase,
OTLPMetricExporterOptions
} from '@opentelemetry/exporter-metrics-otlp-http';
Expand All @@ -33,7 +32,7 @@ import { createExportMetricsServiceRequest, IExportMetricsServiceRequest } from

class OTLPMetricExporterProxy extends OTLPGRPCExporterNodeBase<ResourceMetrics, IExportMetricsServiceRequest> {

constructor(config: OTLPGRPCExporterConfigNode & OTLPMetricExporterOptions= defaultOptions) {
constructor(config?: OTLPGRPCExporterConfigNode & OTLPMetricExporterOptions) {
legendecas marked this conversation as resolved.
Show resolved Hide resolved
super(config);
const headers = baggageUtils.parseKeyPairsIntoRecord(getEnv().OTEL_EXPORTER_OTLP_METRICS_HEADERS);
this.metadata ||= new Metadata();
Expand Down Expand Up @@ -73,7 +72,7 @@ class OTLPMetricExporterProxy extends OTLPGRPCExporterNodeBase<ResourceMetrics,
* OTLP-gRPC metric exporter
*/
export class OTLPMetricExporter extends OTLPMetricExporterBase<OTLPMetricExporterProxy>{
constructor(config: OTLPGRPCExporterConfigNode & OTLPMetricExporterOptions = defaultOptions) {
constructor(config?: OTLPGRPCExporterConfigNode & OTLPMetricExporterOptions) {
super(new OTLPMetricExporterProxy(config), config);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,23 @@
* limitations under the License.
*/

import { ExportResult } from '@opentelemetry/core';
import {
ExportResult,
getEnv
} from '@opentelemetry/core';
import {
AggregationTemporality,
AggregationTemporalitySelector,
InstrumentType,
PushMetricExporter,
ResourceMetrics
} from '@opentelemetry/sdk-metrics';
import { defaultOptions, OTLPMetricExporterOptions } from './OTLPMetricExporterOptions';
import {
OTLPMetricExporterOptions
} from './OTLPMetricExporterOptions';
import { OTLPExporterBase } from '@opentelemetry/otlp-exporter-base';
import { IExportMetricsServiceRequest } from '@opentelemetry/otlp-transformer';
import { diag } from '@opentelemetry/api';

export const CumulativeTemporalitySelector: AggregationTemporalitySelector = () => AggregationTemporality.CUMULATIVE;

Expand All @@ -41,14 +47,33 @@ export const DeltaTemporalitySelector: AggregationTemporalitySelector = (instrum
}
};

function chooseTemporalitySelector(temporalityPreference?: AggregationTemporality): AggregationTemporalitySelector {
if (temporalityPreference === AggregationTemporality.DELTA) {
function chooseTemporalitySelectorFromEnvironment() {
const env = getEnv();
const configuredTemporality = env.OTEL_EXPORTER_OTLP_METRICS_TEMPORALITY_PREFERENCE.trim().toLowerCase();

if (configuredTemporality === 'cumulative') {
return CumulativeTemporalitySelector;
}
if (configuredTemporality === 'delta') {
return DeltaTemporalitySelector;
}

diag.warn(`OTEL_EXPORTER_OTLP_METRICS_TEMPORALITY_PREFERENCE is set to '${env.OTEL_EXPORTER_OTLP_METRICS_TEMPORALITY_PREFERENCE}', but only 'cumulative' and 'delta' are allowed. Using default ('cumulative') instead.`);
return CumulativeTemporalitySelector;
}

function chooseTemporalitySelector(temporalityPreference?: AggregationTemporality): AggregationTemporalitySelector {
// Directly passed preference has priority.
if (temporalityPreference != null) {
if (temporalityPreference === AggregationTemporality.DELTA) {
return DeltaTemporalitySelector;
}
return CumulativeTemporalitySelector;
}

return chooseTemporalitySelectorFromEnvironment();
}

export class OTLPMetricExporterBase<T extends OTLPExporterBase<OTLPMetricExporterOptions,
ResourceMetrics,
IExportMetricsServiceRequest>>
Expand All @@ -57,9 +82,9 @@ implements PushMetricExporter {
protected _aggregationTemporalitySelector: AggregationTemporalitySelector;

constructor(exporter: T,
config: OTLPMetricExporterOptions = defaultOptions) {
config?: OTLPMetricExporterOptions) {
this._otlpExporter = exporter;
this._aggregationTemporalitySelector = chooseTemporalitySelector(config.temporalityPreference);
this._aggregationTemporalitySelector = chooseTemporalitySelector(config?.temporalityPreference);
}

export(metrics: ResourceMetrics, resultCallback: (result: ExportResult) => void): void {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,3 @@ import { OTLPExporterConfigBase } from '@opentelemetry/otlp-exporter-base';
export interface OTLPMetricExporterOptions extends OTLPExporterConfigBase {
temporalityPreference?: AggregationTemporality
}
export const defaultExporterTemporality = AggregationTemporality.CUMULATIVE;
export const defaultOptions = {temporalityPreference: defaultExporterTemporality};
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

import { ResourceMetrics } from '@opentelemetry/sdk-metrics';
import { baggageUtils, getEnv } from '@opentelemetry/core';
import { defaultOptions, OTLPMetricExporterOptions } from '../../OTLPMetricExporterOptions';
import { OTLPMetricExporterOptions } from '../../OTLPMetricExporterOptions';
import { OTLPMetricExporterBase } from '../../OTLPMetricExporterBase';
import {
OTLPExporterBrowserBase,
Expand All @@ -31,7 +31,7 @@ const DEFAULT_COLLECTOR_URL = `http://localhost:4318/${DEFAULT_COLLECTOR_RESOURC

class OTLPExporterBrowserProxy extends OTLPExporterBrowserBase<ResourceMetrics, IExportMetricsServiceRequest> {

constructor(config: OTLPMetricExporterOptions & OTLPExporterConfigBase = defaultOptions) {
constructor(config?: OTLPMetricExporterOptions & OTLPExporterConfigBase) {
super(config);
this._headers = Object.assign(
this._headers,
Expand Down Expand Up @@ -60,7 +60,7 @@ class OTLPExporterBrowserProxy extends OTLPExporterBrowserBase<ResourceMetrics,
* Collector Metric Exporter for Web
*/
export class OTLPMetricExporter extends OTLPMetricExporterBase<OTLPExporterBrowserProxy> {
constructor(config: OTLPExporterConfigBase & OTLPMetricExporterOptions = defaultOptions) {
constructor(config?: OTLPExporterConfigBase & OTLPMetricExporterOptions) {
super(new OTLPExporterBrowserProxy(config), config);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

import { ResourceMetrics } from '@opentelemetry/sdk-metrics';
import { getEnv, baggageUtils} from '@opentelemetry/core';
import { defaultOptions, OTLPMetricExporterOptions } from '../../OTLPMetricExporterOptions';
import { OTLPMetricExporterOptions } from '../../OTLPMetricExporterOptions';
import { OTLPMetricExporterBase } from '../../OTLPMetricExporterBase';
import {
OTLPExporterNodeBase,
Expand All @@ -31,7 +31,7 @@ const DEFAULT_COLLECTOR_URL = `http://localhost:4318/${DEFAULT_COLLECTOR_RESOURC

class OTLPExporterNodeProxy extends OTLPExporterNodeBase<ResourceMetrics, IExportMetricsServiceRequest> {

constructor(config: OTLPExporterNodeConfigBase & OTLPMetricExporterOptions = defaultOptions) {
constructor(config?: OTLPExporterNodeConfigBase & OTLPMetricExporterOptions) {
super(config);
this.headers = Object.assign(
this.headers,
Expand Down Expand Up @@ -60,7 +60,7 @@ class OTLPExporterNodeProxy extends OTLPExporterNodeBase<ResourceMetrics, IExpor
* Collector Metric Exporter for Node
*/
export class OTLPMetricExporter extends OTLPMetricExporterBase<OTLPExporterNodeProxy> {
constructor(config: OTLPExporterNodeConfigBase & OTLPMetricExporterOptions = defaultOptions) {
constructor(config?: OTLPExporterNodeConfigBase & OTLPMetricExporterOptions) {
super(new OTLPExporterNodeProxy(config), config);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,22 +20,34 @@ import {
} from '@opentelemetry/sdk-metrics';
import * as assert from 'assert';
import * as sinon from 'sinon';
import { collect, mockCounter, mockObservableGauge, setUp, shutdown } from '../metricsHelper';
import { OTLPExporterBase, OTLPExporterConfigBase } from '@opentelemetry/otlp-exporter-base';
import {
collect,
mockCounter,
mockObservableGauge,
setUp,
shutdown
} from '../metricsHelper';
import {
OTLPExporterBase,
OTLPExporterConfigBase
} from '@opentelemetry/otlp-exporter-base';
import { IExportMetricsServiceRequest } from '@opentelemetry/otlp-transformer';

type CollectorExporterConfig = OTLPExporterConfigBase;
class OTLPMetricExporter extends OTLPExporterBase<
CollectorExporterConfig,

class OTLPMetricExporter extends OTLPExporterBase<CollectorExporterConfig,
ResourceMetrics,
IExportMetricsServiceRequest
> {
IExportMetricsServiceRequest> {
onInit() {}

onShutdown() {}

send() {}

getDefaultUrl(config: CollectorExporterConfig) {
return config.url || '';
}

convert(metrics: ResourceMetrics[]): IExportMetricsServiceRequest {
return { resourceMetrics: [] };
}
Expand Down Expand Up @@ -125,7 +137,7 @@ describe('OTLPMetricExporter - common', () => {
describe('when exporter is shutdown', () => {
it(
'should not export anything but return callback with code' +
' "FailedNotRetryable"',
' "FailedNotRetryable"',
async () => {
await collectorExporter.shutdown();
spySend.resetHistory();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,35 +15,49 @@
*/


import { diag, DiagLogger } from '@opentelemetry/api';
import {
diag,
DiagLogger
} from '@opentelemetry/api';
import * as core from '@opentelemetry/core';
import * as assert from 'assert';
import * as http from 'http';
import * as sinon from 'sinon';
import {
CumulativeTemporalitySelector,
DeltaTemporalitySelector,
OTLPMetricExporterOptions
} from '../../src';

import {
OTLPMetricExporter
} from '../../src/platform/node';
import {
collect,
ensureCounterIsCorrect,
ensureExportMetricsServiceRequestIsSet,
ensureObservableGaugeIsCorrect,
ensureHistogramIsCorrect,
ensureObservableGaugeIsCorrect,
HISTOGRAM_AGGREGATION_VIEW,
mockCounter,
mockObservableGauge,
mockHistogram,
collect,
shutdown,
mockObservableGauge,
setUp,
HISTOGRAM_AGGREGATION_VIEW,
shutdown,
} from '../metricsHelper';
import { MockedResponse } from './nodeHelpers';
import { AggregationTemporality, ResourceMetrics } from '@opentelemetry/sdk-metrics';
import { Stream, PassThrough } from 'stream';
import { OTLPExporterError, OTLPExporterNodeConfigBase } from '@opentelemetry/otlp-exporter-base';
import {
AggregationTemporality,
ResourceMetrics
} from '@opentelemetry/sdk-metrics';
import {
PassThrough,
Stream
} from 'stream';
import {
OTLPExporterError,
OTLPExporterNodeConfigBase
} from '@opentelemetry/otlp-exporter-base';
import { IExportMetricsServiceRequest } from '@opentelemetry/otlp-transformer';

let fakeRequest: PassThrough;
Expand Down Expand Up @@ -190,6 +204,69 @@ describe('OTLPMetricExporter - node with json over http', () => {
envSource.OTEL_EXPORTER_OTLP_METRICS_HEADERS = '';
envSource.OTEL_EXPORTER_OTLP_HEADERS = '';
});
it('should use delta temporality defined via env', () => {
{
envSource.OTEL_EXPORTER_OTLP_METRICS_TEMPORALITY_PREFERENCE = 'delta';
legendecas marked this conversation as resolved.
Show resolved Hide resolved
const exporter = new OTLPMetricExporter();
assert.strictEqual(exporter['_aggregationTemporalitySelector'], DeltaTemporalitySelector);
}
{
envSource.OTEL_EXPORTER_OTLP_METRICS_TEMPORALITY_PREFERENCE = 'DELTA';
const exporter = new OTLPMetricExporter();
assert.strictEqual(exporter['_aggregationTemporalitySelector'], DeltaTemporalitySelector);
}
{
envSource.OTEL_EXPORTER_OTLP_METRICS_TEMPORALITY_PREFERENCE = 'DeLTa';
const exporter = new OTLPMetricExporter();
assert.strictEqual(exporter['_aggregationTemporalitySelector'], DeltaTemporalitySelector);
}
{
envSource.OTEL_EXPORTER_OTLP_METRICS_TEMPORALITY_PREFERENCE = 'delta ';
const exporter = new OTLPMetricExporter();
assert.strictEqual(exporter['_aggregationTemporalitySelector'], DeltaTemporalitySelector);
}
});
it('should use cumulative temporality defined via env', () => {
{
envSource.OTEL_EXPORTER_OTLP_METRICS_TEMPORALITY_PREFERENCE = 'cumulative';
const exporter = new OTLPMetricExporter();
assert.strictEqual(exporter['_aggregationTemporalitySelector'], CumulativeTemporalitySelector);
}
{
envSource.OTEL_EXPORTER_OTLP_METRICS_TEMPORALITY_PREFERENCE = 'CUMULATIVE';
const exporter = new OTLPMetricExporter();
assert.strictEqual(exporter['_aggregationTemporalitySelector'], CumulativeTemporalitySelector);
}
{
envSource.OTEL_EXPORTER_OTLP_METRICS_TEMPORALITY_PREFERENCE = 'CuMULaTIvE';
const exporter = new OTLPMetricExporter();
assert.strictEqual(exporter['_aggregationTemporalitySelector'], CumulativeTemporalitySelector);
}
{
envSource.OTEL_EXPORTER_OTLP_METRICS_TEMPORALITY_PREFERENCE = 'cumulative ';
const exporter = new OTLPMetricExporter();
assert.strictEqual(exporter['_aggregationTemporalitySelector'], CumulativeTemporalitySelector);
}
});
it('should configure cumulative temporality with invalid value in env', () => {
{
envSource.OTEL_EXPORTER_OTLP_METRICS_TEMPORALITY_PREFERENCE = 'invalid';
const exporter = new OTLPMetricExporter();
assert.strictEqual(exporter['_aggregationTemporalitySelector'], CumulativeTemporalitySelector);
}
{
envSource.OTEL_EXPORTER_OTLP_METRICS_TEMPORALITY_PREFERENCE = ' ';
const exporter = new OTLPMetricExporter();
assert.strictEqual(exporter['_aggregationTemporalitySelector'], CumulativeTemporalitySelector);
}
});
it('should respect explicit config over environment variable', () => {
envSource.OTEL_EXPORTER_OTLP_METRICS_TEMPORALITY_PREFERENCE = 'cumulative';
const exporter = new OTLPMetricExporter({
temporalityPreference: AggregationTemporality.DELTA
});
assert.strictEqual(exporter['_aggregationTemporalitySelector'], DeltaTemporalitySelector);
});
});

describe('export', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
*/

import {
defaultOptions,
OTLPMetricExporterOptions
} from '@opentelemetry/exporter-metrics-otlp-http';
import { ServiceClientType, OTLPProtoExporterNodeBase } from '@opentelemetry/otlp-proto-exporter-base';
Expand All @@ -34,7 +33,7 @@ const DEFAULT_COLLECTOR_URL = `http://localhost:4318/${DEFAULT_COLLECTOR_RESOURC

class OTLPMetricExporterNodeProxy extends OTLPProtoExporterNodeBase<ResourceMetrics, IExportMetricsServiceRequest> {

constructor(config: OTLPExporterNodeConfigBase & OTLPMetricExporterOptions = defaultOptions) {
constructor(config?: OTLPExporterNodeConfigBase & OTLPMetricExporterOptions) {
super(config);
this.headers = Object.assign(
this.headers,
Expand Down Expand Up @@ -64,7 +63,7 @@ class OTLPMetricExporterNodeProxy extends OTLPProtoExporterNodeBase<ResourceMetr
}

export class OTLPMetricExporter extends OTLPMetricExporterBase<OTLPMetricExporterNodeProxy> {
constructor(config: OTLPExporterNodeConfigBase & OTLPMetricExporterOptions = defaultOptions) {
constructor(config?: OTLPExporterNodeConfigBase & OTLPMetricExporterOptions) {
super(new OTLPMetricExporterNodeProxy(config), config);
}
}
2 changes: 2 additions & 0 deletions packages/opentelemetry-core/src/utils/environment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ export type ENVIRONMENT = {
OTEL_EXPORTER_OTLP_PROTOCOL?: string,
OTEL_EXPORTER_OTLP_TRACES_PROTOCOL?: string,
OTEL_EXPORTER_OTLP_METRICS_PROTOCOL?: string,
OTEL_EXPORTER_OTLP_METRICS_TEMPORALITY_PREFERENCE?: string
} & ENVIRONMENT_NUMBERS &
ENVIRONMENT_LISTS;

Expand Down Expand Up @@ -178,6 +179,7 @@ export const DEFAULT_ENVIRONMENT: Required<ENVIRONMENT> = {
OTEL_EXPORTER_OTLP_PROTOCOL: 'http/protobuf',
OTEL_EXPORTER_OTLP_TRACES_PROTOCOL: 'http/protobuf',
OTEL_EXPORTER_OTLP_METRICS_PROTOCOL: 'http/protobuf',
OTEL_EXPORTER_OTLP_METRICS_TEMPORALITY_PREFERENCE: 'cumulative'
};

/**
Expand Down