Skip to content

Commit

Permalink
feat(otlp-exporter): cleanup.
Browse files Browse the repository at this point in the history
  • Loading branch information
pichlermarc committed Mar 30, 2022
1 parent 9617b90 commit 9066ce1
Show file tree
Hide file tree
Showing 10 changed files with 54 additions and 54 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,9 @@

import { otlpTypes } from '@opentelemetry/exporter-trace-otlp-http';
import {
defaultExporterTemporality,
defaultOptions,
OTLPMetricExporterBase,
OTLPMetricExporterBase, OTLPMetricExporterOptions,
toOTLPExportMetricServiceRequest
} from '@opentelemetry/exporter-metrics-otlp-http';
import { AggregationTemporality, ResourceMetrics } from '@opentelemetry/sdk-metrics-base-wip';
Expand All @@ -32,23 +33,19 @@ import { Metadata } from '@grpc/grpc-js';

const DEFAULT_COLLECTOR_URL = 'localhost:4317';

export interface OTLPMetricExporterOptions extends OTLPExporterConfigNode {
aggregationTemporality: AggregationTemporality
}

class OTLPMetricExporterProxy extends OTLPExporterNodeBase<ResourceMetrics,
otlpTypes.opentelemetryProto.collector.metrics.v1.ExportMetricsServiceRequest> {
protected readonly _aggregationTemporality: AggregationTemporality;

constructor(config: OTLPMetricExporterOptions = defaultOptions) {
constructor(config: OTLPExporterConfigNode & OTLPMetricExporterOptions= defaultOptions) {
super(config);
this.metadata ||= new Metadata();
const headers = baggageUtils.parseKeyPairsIntoRecord(getEnv().OTEL_EXPORTER_OTLP_METRICS_HEADERS);
for (const [k, v] of Object.entries(headers)) {
this.metadata.set(k, v);
}
this._aggregationTemporality = config.aggregationTemporality;

this._aggregationTemporality = config.aggregationTemporality ?? defaultExporterTemporality;
}

getServiceProtoPath(): string {
Expand Down Expand Up @@ -82,7 +79,7 @@ class OTLPMetricExporterProxy extends OTLPExporterNodeBase<ResourceMetrics,
* OTLP-gRPC metric exporter
*/
export class OTLPMetricExporter extends OTLPMetricExporterBase<OTLPMetricExporterProxy>{
constructor(config: OTLPMetricExporterOptions = defaultOptions) {
constructor(config: OTLPExporterConfigNode & OTLPMetricExporterOptions = defaultOptions) {
super(new OTLPMetricExporterProxy(config), config);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@ describe('OTLPMetricExporter - node (getDefaultUrl)', () => {
it('should default to localhost', done => {
const collectorExporter = new OTLPMetricExporter();
setTimeout(() => {
assert.strictEqual(collectorExporter.otlpExporter.url, 'localhost:4317');
assert.strictEqual(collectorExporter._otlpExporter.url, 'localhost:4317');
done();
});
});
Expand All @@ -271,7 +271,7 @@ describe('OTLPMetricExporter - node (getDefaultUrl)', () => {
aggregationTemporality: AggregationTemporality.CUMULATIVE
});
setTimeout(() => {
assert.strictEqual(collectorExporter.otlpExporter.url, 'foo.bar.com');
assert.strictEqual(collectorExporter._otlpExporter.url, 'foo.bar.com');
done();
});
});
Expand All @@ -283,7 +283,7 @@ describe('when configuring via environment', () => {
envSource.OTEL_EXPORTER_OTLP_ENDPOINT = 'http://foo.bar';
const collectorExporter = new OTLPMetricExporter();
assert.strictEqual(
collectorExporter.otlpExporter.url,
collectorExporter._otlpExporter.url,
'foo.bar'
);
envSource.OTEL_EXPORTER_OTLP_ENDPOINT = '';
Expand All @@ -293,7 +293,7 @@ describe('when configuring via environment', () => {
envSource.OTEL_EXPORTER_OTLP_METRICS_ENDPOINT = 'http://foo.metrics';
const collectorExporter = new OTLPMetricExporter();
assert.strictEqual(
collectorExporter.otlpExporter.url,
collectorExporter._otlpExporter.url,
'foo.metrics'
);
envSource.OTEL_EXPORTER_OTLP_ENDPOINT = '';
Expand All @@ -302,7 +302,7 @@ describe('when configuring via environment', () => {
it('should use headers defined via env', () => {
envSource.OTEL_EXPORTER_OTLP_HEADERS = 'foo=bar';
const collectorExporter = new OTLPMetricExporter();
assert.deepStrictEqual(collectorExporter.otlpExporter.metadata?.get('foo'), ['bar']);
assert.deepStrictEqual(collectorExporter._otlpExporter.metadata?.get('foo'), ['bar']);
envSource.OTEL_EXPORTER_OTLP_HEADERS = '';
});
it('should override global headers config with signal headers defined via env', () => {
Expand All @@ -315,9 +315,9 @@ describe('when configuring via environment', () => {
metadata,
aggregationTemporality: AggregationTemporality.CUMULATIVE
});
assert.deepStrictEqual(collectorExporter.otlpExporter.metadata?.get('foo'), ['boo']);
assert.deepStrictEqual(collectorExporter.otlpExporter.metadata?.get('bar'), ['foo']);
assert.deepStrictEqual(collectorExporter.otlpExporter.metadata?.get('goo'), ['lol']);
assert.deepStrictEqual(collectorExporter._otlpExporter.metadata?.get('foo'), ['boo']);
assert.deepStrictEqual(collectorExporter._otlpExporter.metadata?.get('bar'), ['foo']);
assert.deepStrictEqual(collectorExporter._otlpExporter.metadata?.get('goo'), ['lol']);
envSource.OTEL_EXPORTER_OTLP_METRICS_HEADERS = '';
envSource.OTEL_EXPORTER_OTLP_HEADERS = '';
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,27 +15,29 @@
*/

import { ExportResult } from '@opentelemetry/core';
import { AggregationTemporality, ResourceMetrics, PushMetricExporter } from '@opentelemetry/sdk-metrics-base-wip';
import { AggregationTemporality, PushMetricExporter, ResourceMetrics } from '@opentelemetry/sdk-metrics-base-wip';
import { OTLPExporterBase, otlpTypes } from '@opentelemetry/exporter-trace-otlp-http';
import { defaultOptions, OTLPMetricExporterOptions } from './OTLPMetricExporterOptions';

export class OTLPMetricExporterBase<T extends OTLPExporterBase<OTLPMetricExporterOptions, ResourceMetrics, otlpTypes.opentelemetryProto.collector.metrics.v1.ExportMetricsServiceRequest>>
export class OTLPMetricExporterBase<T extends OTLPExporterBase<OTLPMetricExporterOptions,
ResourceMetrics,
otlpTypes.opentelemetryProto.collector.metrics.v1.ExportMetricsServiceRequest>>
implements PushMetricExporter {
public otlpExporter: T;
public _otlpExporter: T;
protected _preferredAggregationTemporality: AggregationTemporality;

constructor(exporter: T,
config: OTLPMetricExporterOptions = defaultOptions) {
this.otlpExporter = exporter;
this._preferredAggregationTemporality = config.aggregationTemporality;
this._otlpExporter = exporter;
this._preferredAggregationTemporality = config.aggregationTemporality ?? AggregationTemporality.CUMULATIVE;
}

export(metrics: ResourceMetrics, resultCallback: (result: ExportResult) => void): void {
this.otlpExporter.export([metrics], resultCallback);
this._otlpExporter.export([metrics], resultCallback);
}

async shutdown(): Promise<void> {
await this.otlpExporter.shutdown();
await this._otlpExporter.shutdown();
}

forceFlush(): Promise<void> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import { otlpTypes } from '@opentelemetry/exporter-trace-otlp-http';
import { AggregationTemporality } from '@opentelemetry/sdk-metrics-base-wip';

export interface OTLPMetricExporterOptions extends otlpTypes.OTLPExporterConfigBase {
aggregationTemporality: AggregationTemporality
aggregationTemporality?: AggregationTemporality
}

export const defaultOptions = {aggregationTemporality: AggregationTemporality.CUMULATIVE};
export const defaultExporterTemporality = AggregationTemporality.CUMULATIVE;
export const defaultOptions = {aggregationTemporality: defaultExporterTemporality};
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,13 @@ import {
} from '@opentelemetry/exporter-trace-otlp-http';
import { toOTLPExportMetricServiceRequest } from '../../transformMetrics';
import { baggageUtils, getEnv } from '@opentelemetry/core';
import { defaultOptions, OTLPMetricExporterOptions } from '../../OTLPMetricExporterOptions';
import { defaultExporterTemporality, defaultOptions, OTLPMetricExporterOptions } from '../../OTLPMetricExporterOptions';
import { OTLPMetricExporterBase } from '../../OTLPMetricExporterBase';

const DEFAULT_COLLECTOR_RESOURCE_PATH = '/v1/metrics';
const DEFAULT_COLLECTOR_URL = `http://localhost:4318${DEFAULT_COLLECTOR_RESOURCE_PATH}`;

export class OTLPExporterBrowserProxy extends OTLPExporterBrowserBase<ResourceMetrics,
class OTLPExporterBrowserProxy extends OTLPExporterBrowserBase<ResourceMetrics,
otlpTypes.opentelemetryProto.collector.metrics.v1.ExportMetricsServiceRequest> {
protected readonly _aggregationTemporality: AggregationTemporality;

Expand All @@ -40,7 +40,7 @@ export class OTLPExporterBrowserProxy extends OTLPExporterBrowserBase<ResourceMe
getEnv().OTEL_EXPORTER_OTLP_METRICS_HEADERS
)
);
this._aggregationTemporality = config.aggregationTemporality;
this._aggregationTemporality = config.aggregationTemporality ?? defaultExporterTemporality;
}

getDefaultUrl(config: otlpTypes.OTLPExporterConfigBase): string {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,13 @@ import {
} from '@opentelemetry/exporter-trace-otlp-http';
import { toOTLPExportMetricServiceRequest } from '../../transformMetrics';
import { getEnv, baggageUtils} from '@opentelemetry/core';
import { defaultOptions, OTLPMetricExporterOptions } from '../../OTLPMetricExporterOptions';
import { defaultExporterTemporality, defaultOptions, OTLPMetricExporterOptions } from '../../OTLPMetricExporterOptions';
import { OTLPMetricExporterBase } from '../../OTLPMetricExporterBase';

const DEFAULT_COLLECTOR_RESOURCE_PATH = '/v1/metrics';
const DEFAULT_COLLECTOR_URL = `http://localhost:4318${DEFAULT_COLLECTOR_RESOURCE_PATH}`;

export class OTLPExporterNodeProxy extends OTLPExporterNodeBase<ResourceMetrics,
class OTLPExporterNodeProxy extends OTLPExporterNodeBase<ResourceMetrics,
otlpTypes.opentelemetryProto.collector.metrics.v1.ExportMetricsServiceRequest> {
protected readonly _aggregationTemporality: AggregationTemporality;

Expand All @@ -41,7 +41,7 @@ export class OTLPExporterNodeProxy extends OTLPExporterNodeBase<ResourceMetrics,
getEnv().OTEL_EXPORTER_OTLP_METRICS_HEADERS
)
);
this._aggregationTemporality = config.aggregationTemporality;
this._aggregationTemporality = config.aggregationTemporality ?? defaultExporterTemporality;
}

convert(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -399,7 +399,7 @@ describe('OTLPMetricExporter - web', () => {
envSource.OTEL_EXPORTER_OTLP_ENDPOINT = 'http://foo.bar/v1/metrics';
const collectorExporter = new OTLPMetricExporter();
assert.strictEqual(
collectorExporter.otlpExporter.url,
collectorExporter._otlpExporter.url,
envSource.OTEL_EXPORTER_OTLP_ENDPOINT
);
envSource.OTEL_EXPORTER_OTLP_ENDPOINT = '';
Expand All @@ -408,7 +408,7 @@ describe('OTLPMetricExporter - web', () => {
envSource.OTEL_EXPORTER_OTLP_ENDPOINT = 'http://foo.bar';
const collectorExporter = new OTLPMetricExporter();
assert.strictEqual(
collectorExporter.otlpExporter.url,
collectorExporter._otlpExporter.url,
`${envSource.OTEL_EXPORTER_OTLP_ENDPOINT}/v1/metrics`
);
envSource.OTEL_EXPORTER_OTLP_ENDPOINT = '';
Expand All @@ -418,7 +418,7 @@ describe('OTLPMetricExporter - web', () => {
envSource.OTEL_EXPORTER_OTLP_METRICS_ENDPOINT = 'http://foo.metrics';
const collectorExporter = new OTLPMetricExporter();
assert.strictEqual(
collectorExporter.otlpExporter.url,
collectorExporter._otlpExporter.url,
envSource.OTEL_EXPORTER_OTLP_METRICS_ENDPOINT
);
envSource.OTEL_EXPORTER_OTLP_ENDPOINT = '';
Expand All @@ -431,7 +431,7 @@ describe('OTLPMetricExporter - web', () => {
aggregationTemporality: AggregationTemporality.CUMULATIVE
});
// @ts-expect-error access internal property for testing
assert.strictEqual(collectorExporter.otlpExporter._headers.foo, 'bar');
assert.strictEqual(collectorExporter._otlpExporter._headers.foo, 'bar');
envSource.OTEL_EXPORTER_OTLP_HEADERS = '';
});
it('should override global headers config with signal headers defined via env', () => {
Expand All @@ -442,9 +442,9 @@ describe('OTLPMetricExporter - web', () => {
aggregationTemporality: AggregationTemporality.CUMULATIVE
});
// @ts-expect-error access internal property for testing
assert.strictEqual(collectorExporter.otlpExporter._headers.foo, 'boo');
assert.strictEqual(collectorExporter._otlpExporter._headers.foo, 'boo');
// @ts-expect-error access internal property for testing
assert.strictEqual(collectorExporter.otlpExporter._headers.bar, 'foo');
assert.strictEqual(collectorExporter._otlpExporter._headers.bar, 'foo');
envSource.OTEL_EXPORTER_OTLP_METRICS_HEADERS = '';
envSource.OTEL_EXPORTER_OTLP_HEADERS = '';
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ describe('OTLPMetricExporter - node with json over http', () => {
envSource.OTEL_EXPORTER_OTLP_ENDPOINT = 'http://foo.bar/v1/metrics';
const collectorExporter = new OTLPMetricExporter();
assert.strictEqual(
collectorExporter.otlpExporter.url,
collectorExporter._otlpExporter.url,
envSource.OTEL_EXPORTER_OTLP_ENDPOINT
);
envSource.OTEL_EXPORTER_OTLP_ENDPOINT = '';
Expand All @@ -116,7 +116,7 @@ describe('OTLPMetricExporter - node with json over http', () => {
envSource.OTEL_EXPORTER_OTLP_ENDPOINT = 'http://foo.bar';
const collectorExporter = new OTLPMetricExporter();
assert.strictEqual(
collectorExporter.otlpExporter.url,
collectorExporter._otlpExporter.url,
`${envSource.OTEL_EXPORTER_OTLP_ENDPOINT}/v1/metrics`
);
envSource.OTEL_EXPORTER_OTLP_ENDPOINT = '';
Expand All @@ -126,7 +126,7 @@ describe('OTLPMetricExporter - node with json over http', () => {
envSource.OTEL_EXPORTER_OTLP_METRICS_ENDPOINT = 'http://foo.metrics';
const collectorExporter = new OTLPMetricExporter();
assert.strictEqual(
collectorExporter.otlpExporter.url,
collectorExporter._otlpExporter.url,
envSource.OTEL_EXPORTER_OTLP_METRICS_ENDPOINT
);
envSource.OTEL_EXPORTER_OTLP_ENDPOINT = '';
Expand All @@ -135,15 +135,15 @@ describe('OTLPMetricExporter - node with json over http', () => {
it('should use headers defined via env', () => {
envSource.OTEL_EXPORTER_OTLP_HEADERS = 'foo=bar';
const collectorExporter = new OTLPMetricExporter();
assert.strictEqual(collectorExporter.otlpExporter.headers.foo, 'bar');
assert.strictEqual(collectorExporter._otlpExporter.headers.foo, 'bar');
envSource.OTEL_EXPORTER_OTLP_HEADERS = '';
});
it('should override global headers config with signal headers defined via env', () => {
envSource.OTEL_EXPORTER_OTLP_HEADERS = 'foo=bar,bar=foo';
envSource.OTEL_EXPORTER_OTLP_METRICS_HEADERS = 'foo=boo';
const collectorExporter = new OTLPMetricExporter();
assert.strictEqual(collectorExporter.otlpExporter.headers.foo, 'boo');
assert.strictEqual(collectorExporter.otlpExporter.headers.bar, 'foo');
assert.strictEqual(collectorExporter._otlpExporter.headers.foo, 'boo');
assert.strictEqual(collectorExporter._otlpExporter.headers.bar, 'foo');
envSource.OTEL_EXPORTER_OTLP_METRICS_HEADERS = '';
envSource.OTEL_EXPORTER_OTLP_HEADERS = '';
});
Expand Down Expand Up @@ -322,7 +322,7 @@ describe('OTLPMetricExporter - node with json over http', () => {
const collectorExporter = new OTLPMetricExporter();
setTimeout(() => {
assert.strictEqual(
collectorExporter.otlpExporter.url,
collectorExporter._otlpExporter.url,
'http://localhost:4318/v1/metrics'
);
done();
Expand All @@ -336,7 +336,7 @@ describe('OTLPMetricExporter - node with json over http', () => {
aggregationTemporality: AggregationTemporality.CUMULATIVE
});
setTimeout(() => {
assert.strictEqual(collectorExporter.otlpExporter.url, url);
assert.strictEqual(collectorExporter._otlpExporter.url, url);
done();
});
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import {
appendResourcePathToUrlIfNotPresent,
} from '@opentelemetry/exporter-trace-otlp-http';
import {
defaultExporterTemporality,
defaultOptions,
OTLPMetricExporterOptions,
toOTLPExportMetricServiceRequest
Expand All @@ -33,7 +34,7 @@ const DEFAULT_COLLECTOR_RESOURCE_PATH = '/v1/metrics';
const DEFAULT_COLLECTOR_URL = `http://localhost:4318${DEFAULT_COLLECTOR_RESOURCE_PATH}`;


export class OTLPMetricExporterNodeProxy extends OTLPExporterNodeBase<ResourceMetrics,
class OTLPMetricExporterNodeProxy extends OTLPExporterNodeBase<ResourceMetrics,
otlpTypes.opentelemetryProto.collector.metrics.v1.ExportMetricsServiceRequest> {
protected readonly _aggregationTemporality: AggregationTemporality;

Expand All @@ -45,7 +46,7 @@ export class OTLPMetricExporterNodeProxy extends OTLPExporterNodeBase<ResourceMe
getEnv().OTEL_EXPORTER_OTLP_METRICS_HEADERS
)
);
this._aggregationTemporality = config.aggregationTemporality;
this._aggregationTemporality = config.aggregationTemporality ?? defaultExporterTemporality;
}

convert(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ describe('OTLPMetricExporter - node with proto over http', () => {
envSource.OTEL_EXPORTER_OTLP_ENDPOINT = 'http://foo.bar/v1/metrics';
const collectorExporter = new OTLPMetricExporter();
assert.strictEqual(
collectorExporter.otlpExporter.url,
collectorExporter._otlpExporter.url,
envSource.OTEL_EXPORTER_OTLP_ENDPOINT
);
envSource.OTEL_EXPORTER_OTLP_ENDPOINT = '';
Expand All @@ -68,7 +68,7 @@ describe('OTLPMetricExporter - node with proto over http', () => {
envSource.OTEL_EXPORTER_OTLP_ENDPOINT = 'http://foo.bar';
const collectorExporter = new OTLPMetricExporter();
assert.strictEqual(
collectorExporter.otlpExporter.url,
collectorExporter._otlpExporter.url,
`${envSource.OTEL_EXPORTER_OTLP_ENDPOINT}/v1/metrics`
);
envSource.OTEL_EXPORTER_OTLP_ENDPOINT = '';
Expand All @@ -78,7 +78,7 @@ describe('OTLPMetricExporter - node with proto over http', () => {
envSource.OTEL_EXPORTER_OTLP_METRICS_ENDPOINT = 'http://foo.metrics';
const collectorExporter = new OTLPMetricExporter();
assert.strictEqual(
collectorExporter.otlpExporter.url,
collectorExporter._otlpExporter.url,
envSource.OTEL_EXPORTER_OTLP_METRICS_ENDPOINT
);
envSource.OTEL_EXPORTER_OTLP_ENDPOINT = '';
Expand All @@ -87,15 +87,15 @@ describe('OTLPMetricExporter - node with proto over http', () => {
it('should use headers defined via env', () => {
envSource.OTEL_EXPORTER_OTLP_HEADERS = 'foo=bar';
const collectorExporter = new OTLPMetricExporter();
assert.strictEqual(collectorExporter.otlpExporter.headers.foo, 'bar');
assert.strictEqual(collectorExporter._otlpExporter.headers.foo, 'bar');
envSource.OTEL_EXPORTER_OTLP_HEADERS = '';
});
it('should override global headers config with signal headers defined via env', () => {
envSource.OTEL_EXPORTER_OTLP_HEADERS = 'foo=bar,bar=foo';
envSource.OTEL_EXPORTER_OTLP_METRICS_HEADERS = 'foo=boo';
const collectorExporter = new OTLPMetricExporter();
assert.strictEqual(collectorExporter.otlpExporter.headers.foo, 'boo');
assert.strictEqual(collectorExporter.otlpExporter.headers.bar, 'foo');
assert.strictEqual(collectorExporter._otlpExporter.headers.foo, 'boo');
assert.strictEqual(collectorExporter._otlpExporter.headers.bar, 'foo');
envSource.OTEL_EXPORTER_OTLP_METRICS_HEADERS = '';
envSource.OTEL_EXPORTER_OTLP_HEADERS = '';
});
Expand Down

0 comments on commit 9066ce1

Please sign in to comment.