Skip to content

Commit

Permalink
Merge 14ee0c3 into a96116d
Browse files Browse the repository at this point in the history
  • Loading branch information
llc1123 authored Apr 28, 2023
2 parents a96116d + 14ee0c3 commit 4701e04
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 75 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -114,10 +114,9 @@ export function sendWithXhr(
minDelay = DEFAULT_EXPORT_BACKOFF_MULTIPLIER * minDelay;

// retry after interval specified in Retry-After header
if (xhr.getResponseHeader('Retry-After')) {
retryTime = parseRetryAfterToMills(
xhr.getResponseHeader('Retry-After')!
);
const retryAfter = xhr.getResponseHeader('Retry-After');
if (retryAfter) {
retryTime = parseRetryAfterToMills(retryAfter);
} else {
// exponential backoff with jitter
retryTime = Math.round(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -218,9 +218,7 @@ export function configureCompression(
if (compression) {
return compression;
} else {
const definedCompression =
getEnv().OTEL_EXPORTER_OTLP_TRACES_COMPRESSION ||
getEnv().OTEL_EXPORTER_OTLP_COMPRESSION;
const definedCompression = getEnv().OTEL_EXPORTER_OTLP_COMPRESSION;
return definedCompression === CompressionAlgorithm.GZIP
? CompressionAlgorithm.GZIP
: CompressionAlgorithm.NONE;
Expand Down
20 changes: 11 additions & 9 deletions experimental/packages/otlp-exporter-base/src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,8 @@
*/

import { diag } from '@opentelemetry/api';
import { getEnv } from '@opentelemetry/core';
import { DEFAULT_ENVIRONMENT, getEnv } from '@opentelemetry/core';

const DEFAULT_TRACE_TIMEOUT = 10000;
export const DEFAULT_EXPORT_MAX_ATTEMPTS = 5;
export const DEFAULT_EXPORT_INITIAL_BACKOFF = 1000;
export const DEFAULT_EXPORT_MAX_BACKOFF = 5000;
Expand Down Expand Up @@ -73,7 +72,7 @@ export function appendRootPathToUrlIfNeeded(url: string): string {
}

/**
* Configure exporter trace timeout value from passed in value or environment variables
* Configure exporter timeout value from passed in value or environment variables
* @param timeoutMillis
* @returns timeout value in milliseconds
*/
Expand All @@ -83,7 +82,10 @@ export function configureExporterTimeout(
if (typeof timeoutMillis === 'number') {
if (timeoutMillis <= 0) {
// OTLP exporter configured timeout - using default value of 10000ms
return invalidTimeout(timeoutMillis, DEFAULT_TRACE_TIMEOUT);
return invalidTimeout(
timeoutMillis,
DEFAULT_ENVIRONMENT.OTEL_EXPORTER_OTLP_TIMEOUT
);
}
return timeoutMillis;
} else {
Expand All @@ -92,14 +94,14 @@ export function configureExporterTimeout(
}

function getExporterTimeoutFromEnv(): number {
const definedTimeout = Number(
getEnv().OTEL_EXPORTER_OTLP_TRACES_TIMEOUT ??
getEnv().OTEL_EXPORTER_OTLP_TIMEOUT
);
const definedTimeout = Number(getEnv().OTEL_EXPORTER_OTLP_TIMEOUT);

if (definedTimeout <= 0) {
// OTLP exporter configured timeout - using default value of 10000ms
return invalidTimeout(definedTimeout, DEFAULT_TRACE_TIMEOUT);
return invalidTimeout(
definedTimeout,
DEFAULT_ENVIRONMENT.OTEL_EXPORTER_OTLP_TIMEOUT
);
} else {
return definedTimeout;
}
Expand Down
38 changes: 9 additions & 29 deletions experimental/packages/otlp-exporter-base/test/node/util.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,50 +66,30 @@ describe('configureExporterTimeout', () => {
const exporterTimeout = configureExporterTimeout(9000);
assert.strictEqual(exporterTimeout, 9000);
});
it('should use default trace export timeout env variable value when timeoutMillis parameter is undefined', () => {
it('should use env value when timeoutMillis parameter is undefined', () => {
envSource.OTEL_EXPORTER_OTLP_TIMEOUT = '9000';
const exporterTimeout = configureExporterTimeout(undefined);
assert.strictEqual(exporterTimeout, 10000);
});
it('should use default trace export timeout env variable value when timeoutMillis parameter is negative', () => {
const exporterTimeout = configureExporterTimeout(-18000);
assert.strictEqual(exporterTimeout, 10000);
});
it('should use trace export timeout value defined in env', () => {
envSource.OTEL_EXPORTER_OTLP_TRACES_TIMEOUT = '15000';
const exporterTimeout = configureExporterTimeout(undefined);
assert.strictEqual(exporterTimeout, 15000);
delete envSource.OTEL_EXPORTER_OTLP_TRACES_TIMEOUT;
assert.strictEqual(exporterTimeout, 9000);
delete envSource.OTEL_EXPORTER_OTLP_TIMEOUT;
});
it('should use default trace export timeout env variable value when trace export timeout value defined in env is negative', () => {
envSource.OTEL_EXPORTER_OTLP_TRACES_TIMEOUT = '-15000';
it('should use default trace export timeout env variable value when both timeoutMillis parameter and env value are undefined', () => {
const exporterTimeout = configureExporterTimeout(undefined);
assert.strictEqual(exporterTimeout, 10000);
delete envSource.OTEL_EXPORTER_OTLP_TRACES_TIMEOUT;
});
it('should use default trace export timeout when timeoutMillis parameter is negative', () => {
const exporterTimeout = configureExporterTimeout(-15000);
assert.strictEqual(exporterTimeout, 10000);
});
it('should use timeoutMillis parameter over trace export timeout value defined in env', () => {
envSource.OTEL_EXPORTER_OTLP_TRACES_TIMEOUT = '11000';
const exporterTimeout = configureExporterTimeout(9000);
assert.strictEqual(exporterTimeout, 9000);
delete envSource.OTEL_EXPORTER_OTLP_TRACES_TIMEOUT;
});
it('should use default value when both timeoutMillis parameter and export timeout values defined in env are negative', () => {
envSource.OTEL_EXPORTER_OTLP_TRACES_TIMEOUT = '-11000';
envSource.OTEL_EXPORTER_OTLP_TIMEOUT = '-9000';
const exporterTimeout = configureExporterTimeout(-5000);
assert.strictEqual(exporterTimeout, 10000);
delete envSource.OTEL_EXPORTER_OTLP_TRACES_TIMEOUT;
delete envSource.OTEL_EXPORTER_OTLP_TIMEOUT;
});
it('should use default value export timeout value defined in env are negative', () => {
envSource.OTEL_EXPORTER_OTLP_TRACES_TIMEOUT = '-11000';
envSource.OTEL_EXPORTER_OTLP_TIMEOUT = '-9000';
const exporterTimeout = configureExporterTimeout(undefined);
assert.strictEqual(exporterTimeout, 10000);
delete envSource.OTEL_EXPORTER_OTLP_TRACES_TIMEOUT;
delete envSource.OTEL_EXPORTER_OTLP_TIMEOUT;
});
it('should warn user about invalid timeout', () => {
Expand Down Expand Up @@ -153,20 +133,20 @@ describe('configureCompression', () => {
);
});
it('should return gzip compression defined via env', () => {
envSource.OTEL_EXPORTER_OTLP_TRACES_COMPRESSION = 'gzip';
envSource.OTEL_EXPORTER_OTLP_COMPRESSION = 'gzip';
assert.strictEqual(
configureCompression(undefined),
CompressionAlgorithm.GZIP
);
delete envSource.OTEL_EXPORTER_OTLP_TRACES_COMPRESSION;
delete envSource.OTEL_EXPORTER_OTLP_COMPRESSION;
});
it('should return none for compression defined via env', () => {
envSource.OTEL_EXPORTER_OTLP_TRACES_COMPRESSION = 'none';
envSource.OTEL_EXPORTER_OTLP_COMPRESSION = 'none';
assert.strictEqual(
configureCompression(undefined),
CompressionAlgorithm.NONE
);
delete envSource.OTEL_EXPORTER_OTLP_TRACES_COMPRESSION;
delete envSource.OTEL_EXPORTER_OTLP_COMPRESSION;
});
it('should return none for compression when no compression is set', () => {
assert.strictEqual(
Expand Down
20 changes: 5 additions & 15 deletions experimental/packages/otlp-grpc-exporter-base/src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -180,9 +180,7 @@ export function configureSecurity(
}

function getSecurityFromEnv(): boolean {
const definedInsecure =
getEnv().OTEL_EXPORTER_OTLP_TRACES_INSECURE ||
getEnv().OTEL_EXPORTER_OTLP_INSECURE;
const definedInsecure = getEnv().OTEL_EXPORTER_OTLP_INSECURE;

if (definedInsecure) {
return definedInsecure.toLowerCase() === 'true';
Expand All @@ -204,9 +202,7 @@ export function useSecureConnection(): grpc.ChannelCredentials {
}

function retrieveRootCert(): Buffer | undefined {
const rootCertificate =
getEnv().OTEL_EXPORTER_OTLP_TRACES_CERTIFICATE ||
getEnv().OTEL_EXPORTER_OTLP_CERTIFICATE;
const rootCertificate = getEnv().OTEL_EXPORTER_OTLP_CERTIFICATE;

if (rootCertificate) {
try {
Expand All @@ -221,9 +217,7 @@ function retrieveRootCert(): Buffer | undefined {
}

function retrievePrivateKey(): Buffer | undefined {
const clientKey =
getEnv().OTEL_EXPORTER_OTLP_TRACES_CLIENT_KEY ||
getEnv().OTEL_EXPORTER_OTLP_CLIENT_KEY;
const clientKey = getEnv().OTEL_EXPORTER_OTLP_CLIENT_KEY;

if (clientKey) {
try {
Expand All @@ -238,9 +232,7 @@ function retrievePrivateKey(): Buffer | undefined {
}

function retrieveCertChain(): Buffer | undefined {
const clientChain =
getEnv().OTEL_EXPORTER_OTLP_TRACES_CLIENT_CERTIFICATE ||
getEnv().OTEL_EXPORTER_OTLP_CLIENT_CERTIFICATE;
const clientChain = getEnv().OTEL_EXPORTER_OTLP_CLIENT_CERTIFICATE;

if (clientChain) {
try {
Expand Down Expand Up @@ -278,9 +270,7 @@ export function configureCompression(
if (compression) {
return toGrpcCompression(compression);
} else {
const definedCompression =
getEnv().OTEL_EXPORTER_OTLP_TRACES_COMPRESSION ||
getEnv().OTEL_EXPORTER_OTLP_COMPRESSION;
const definedCompression = getEnv().OTEL_EXPORTER_OTLP_COMPRESSION;

return definedCompression === 'gzip'
? GrpcCompressionAlgorithm.GZIP
Expand Down
29 changes: 14 additions & 15 deletions experimental/packages/otlp-grpc-exporter-base/test/util.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,10 +106,10 @@ describe('utils - configureSecurity', () => {
assert.ok(credentials._isSecure() === true);
});
it('should return secure channel when endpoint contains https scheme - no matter insecure env settings,', () => {
envSource.OTEL_EXPORTER_OTLP_TRACES_INSECURE = 'true';
envSource.OTEL_EXPORTER_OTLP_INSECURE = 'true';
const credentials = configureSecurity(undefined, 'https://foo.bar');
assert.ok(credentials._isSecure() === true);
delete envSource.OTEL_EXPORTER_OTLP_TRACES_INSECURE;
delete envSource.OTEL_EXPORTER_OTLP_INSECURE;
});

it('should return insecure channel when endpoint contains http scheme and no insecure env settings', () => {
Expand All @@ -121,10 +121,10 @@ describe('utils - configureSecurity', () => {
assert.ok(credentials._isSecure() === true);
});
it('should return insecure channel when endpoint contains http scheme and insecure env set to false', () => {
envSource.OTEL_EXPORTER_OTLP_TRACES_INSECURE = 'false';
envSource.OTEL_EXPORTER_OTLP_INSECURE = 'false';
const credentials = configureSecurity(undefined, 'http://foo.bar');
assert.ok(credentials._isSecure() === false);
delete envSource.OTEL_EXPORTER_OTLP_TRACES_INSECURE;
delete envSource.OTEL_EXPORTER_OTLP_INSECURE;
});
it('should return insecure channel when endpoint contains http scheme and insecure env set to true', () => {
envSource.OTEL_EXPORTER_OTLP_INSECURE = 'true';
Expand All @@ -133,10 +133,10 @@ describe('utils - configureSecurity', () => {
delete envSource.OTEL_EXPORTER_OTLP_INSECURE;
});
it('should return secure channel when endpoint does not contain scheme and insecure env set to false', () => {
envSource.OTEL_EXPORTER_OTLP_TRACES_INSECURE = 'false';
envSource.OTEL_EXPORTER_OTLP_INSECURE = 'false';
const credentials = configureSecurity(undefined, 'foo.bar');
assert.ok(credentials._isSecure() === true);
delete envSource.OTEL_EXPORTER_OTLP_TRACES_INSECURE;
delete envSource.OTEL_EXPORTER_OTLP_INSECURE;
});
it('should return insecure channel when endpoint does not contain scheme and insecure env set to true', () => {
envSource.OTEL_EXPORTER_OTLP_INSECURE = 'true';
Expand All @@ -150,16 +150,15 @@ describe('useSecureConnection', () => {
const envSource = process.env;
it('should return secure connection using all credentials', () => {
envSource.OTEL_EXPORTER_OTLP_CERTIFICATE = './test/certs/ca.crt';
envSource.OTEL_EXPORTER_OTLP_TRACES_CLIENT_KEY = './test/certs/client.key';
envSource.OTEL_EXPORTER_OTLP_TRACES_CLIENT_CERTIFICATE =
'./test/certs/client.crt';
envSource.OTEL_EXPORTER_OTLP_CLIENT_KEY = './test/certs/client.key';
envSource.OTEL_EXPORTER_OTLP_CLIENT_CERTIFICATE = './test/certs/client.crt';

const credentials = useSecureConnection();
assert.ok(credentials._isSecure() === true);

delete envSource.OTEL_EXPORTER_OTLP_CERTIFICATE;
delete envSource.OTEL_EXPORTER_OTLP_TRACES_CLIENT_KEY;
delete envSource.OTEL_EXPORTER_OTLP_TRACES_CLIENT_CERTIFICATE;
delete envSource.OTEL_EXPORTER_OTLP_CLIENT_KEY;
delete envSource.OTEL_EXPORTER_OTLP_CLIENT_CERTIFICATE;
});
it('should return secure connection using only root certificate', () => {
envSource.OTEL_EXPORTER_OTLP_CERTIFICATE = './test/certs/ca.crt';
Expand Down Expand Up @@ -192,20 +191,20 @@ describe('configureCompression', () => {
);
});
it('should return gzip compression defined via env', () => {
envSource.OTEL_EXPORTER_OTLP_TRACES_COMPRESSION = 'gzip';
envSource.OTEL_EXPORTER_OTLP_COMPRESSION = 'gzip';
assert.strictEqual(
configureCompression(undefined),
GrpcCompressionAlgorithm.GZIP
);
delete envSource.OTEL_EXPORTER_OTLP_TRACES_COMPRESSION;
delete envSource.OTEL_EXPORTER_OTLP_COMPRESSION;
});
it('should return none for compression defined via env', () => {
envSource.OTEL_EXPORTER_OTLP_TRACES_COMPRESSION = 'none';
envSource.OTEL_EXPORTER_OTLP_COMPRESSION = 'none';
assert.strictEqual(
configureCompression(undefined),
GrpcCompressionAlgorithm.NONE
);
delete envSource.OTEL_EXPORTER_OTLP_TRACES_COMPRESSION;
delete envSource.OTEL_EXPORTER_OTLP_COMPRESSION;
});
it('should return none for compression when no compression is set', () => {
assert.strictEqual(
Expand Down

0 comments on commit 4701e04

Please sign in to comment.