Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
feat(otlp-exporter-base): add retrying transport
  • Loading branch information
pichlermarc committed May 31, 2024
commit e8635df5f9bb791516f57ca55c7b02ffdc48dd6c
Original file line number Diff line number Diff line change
Expand Up @@ -87,15 +87,15 @@ describe('OTLPLogExporter', () => {
it('should include user-agent header by default', () => {
const exporter = new OTLPLogExporter();
assert.strictEqual(
exporter['_transport']['_parameters']['headers']['User-Agent'],
exporter['_transport']['_transport']['_parameters']['headers']['User-Agent'],
`OTel-OTLP-Exporter-JavaScript/${VERSION}`
);
});

it('should use headers defined via env', () => {
envSource.OTEL_EXPORTER_OTLP_LOGS_HEADERS = 'foo=bar';
const exporter = new OTLPLogExporter();
assert.strictEqual(exporter['_transport']['_parameters']['headers']['foo'], 'bar');
assert.strictEqual(exporter['_transport']['_transport']['_parameters']['headers']['foo'], 'bar');
delete envSource.OTEL_EXPORTER_OTLP_LOGS_HEADERS;
});

Expand All @@ -115,8 +115,8 @@ describe('OTLPLogExporter', () => {
foo: 'constructor',
},
});
assert.strictEqual(exporter['_transport']['_parameters']['headers']['foo'], 'constructor');
assert.strictEqual(exporter['_transport']['_parameters']['headers']['bar'], 'foo');
assert.strictEqual(exporter['_transport']['_transport']['_parameters']['headers']['foo'], 'constructor');
assert.strictEqual(exporter['_transport']['_transport']['_parameters']['headers']['bar'], 'foo');
envSource.OTEL_EXPORTER_OTLP_HEADERS = '';
});
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -156,22 +156,22 @@ describe('OTLPLogExporter - node with proto over http', () => {
it('should include user-agent header by default', () => {
const exporter = new OTLPLogExporter();
assert.strictEqual(
exporter['_transport']['_parameters']['headers']['User-Agent'],
exporter['_transport']['_transport']['_parameters']['headers']['User-Agent'],
`OTel-OTLP-Exporter-JavaScript/${VERSION}`
);
});
it('should use headers defined via env', () => {
envSource.OTEL_EXPORTER_OTLP_LOGS_HEADERS = 'foo=bar';
const exporter = new OTLPLogExporter();
assert.strictEqual(exporter['_transport']['_parameters']['headers']['foo'], 'bar');
assert.strictEqual(exporter['_transport']['_transport']['_parameters']['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_LOGS_HEADERS = 'foo=boo';
const exporter = new OTLPLogExporter();
assert.strictEqual(exporter['_transport']['_parameters']['headers']['foo'], 'boo');
assert.strictEqual(exporter['_transport']['_parameters']['headers']['bar'], 'foo');
assert.strictEqual(exporter['_transport']['_transport']['_parameters']['headers']['foo'], 'boo');
assert.strictEqual(exporter['_transport']['_transport']['_parameters']['headers']['bar'], 'foo');
envSource.OTEL_EXPORTER_OTLP_LOGS_HEADERS = '';
envSource.OTEL_EXPORTER_OTLP_HEADERS = '';
});
Expand All @@ -182,8 +182,8 @@ describe('OTLPLogExporter - node with proto over http', () => {
foo: 'constructor',
},
});
assert.strictEqual(exporter['_transport']['_parameters']['headers']['foo'], 'constructor');
assert.strictEqual(exporter['_transport']['_parameters']['headers']['bar'], 'foo');
assert.strictEqual(exporter['_transport']['_transport']['_parameters']['headers']['foo'], 'constructor');
assert.strictEqual(exporter['_transport']['_transport']['_parameters']['headers']['bar'], 'foo');
envSource.OTEL_EXPORTER_OTLP_HEADERS = '';
});
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -170,22 +170,22 @@ describe('OTLPTraceExporter - node with json over http', () => {
it('should use headers defined via env', () => {
envSource.OTEL_EXPORTER_OTLP_HEADERS = 'foo=bar';
const exporter = new OTLPTraceExporter();
assert.strictEqual(exporter['_transport']['_parameters']['headers']['foo'], 'bar');
assert.strictEqual(exporter['_transport']['_transport']['_parameters']['headers']['foo'], 'bar');
envSource.OTEL_EXPORTER_OTLP_HEADERS = '';
});
it('should include user agent in header', () => {
const exporter = new OTLPTraceExporter();
assert.strictEqual(
exporter['_transport']['_parameters']['headers']['User-Agent'],
exporter['_transport']['_transport']['_parameters']['headers']['User-Agent'],
`OTel-OTLP-Exporter-JavaScript/${VERSION}`
);
});
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_TRACES_HEADERS = 'foo=boo';
const exporter = new OTLPTraceExporter();
assert.strictEqual(exporter['_transport']['_parameters']['headers']['foo'], 'boo');
assert.strictEqual(exporter['_transport']['_parameters']['headers']['bar'], 'foo');
assert.strictEqual(exporter['_transport']['_transport']['_parameters']['headers']['foo'], 'boo');
assert.strictEqual(exporter['_transport']['_transport']['_parameters']['headers']['bar'], 'foo');
envSource.OTEL_EXPORTER_OTLP_TRACES_HEADERS = '';
envSource.OTEL_EXPORTER_OTLP_HEADERS = '';
});
Expand All @@ -196,8 +196,8 @@ describe('OTLPTraceExporter - node with json over http', () => {
foo: 'constructor',
},
});
assert.strictEqual(exporter['_transport']['_parameters']['headers']['foo'], 'constructor');
assert.strictEqual(exporter['_transport']['_parameters']['headers']['bar'], 'foo');
assert.strictEqual(exporter['_transport']['_transport']['_parameters']['headers']['foo'], 'constructor');
assert.strictEqual(exporter['_transport']['_transport']['_parameters']['headers']['bar'], 'foo');
envSource.OTEL_EXPORTER_OTLP_HEADERS = '';
});
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ describe('OTLPTraceExporter - node with proto over http', () => {
const exporter = new OTLPTraceExporter();
it('should include user agent in header', () => {
assert.strictEqual(
exporter['_transport']['_parameters']['headers']['User-Agent'],
exporter['_transport']['_transport']['_parameters']['headers']['User-Agent'],
`OTel-OTLP-Exporter-JavaScript/${VERSION}`
);
});
Expand Down Expand Up @@ -169,15 +169,15 @@ describe('OTLPTraceExporter - node with proto over http', () => {
it('should use headers defined via env', () => {
envSource.OTEL_EXPORTER_OTLP_HEADERS = 'foo=bar';
const exporter = new OTLPTraceExporter();
assert.strictEqual(exporter['_transport']['_parameters']['headers']['foo'], 'bar');
assert.strictEqual(exporter['_transport']['_transport']['_parameters']['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_TRACES_HEADERS = 'foo=boo';
const exporter = new OTLPTraceExporter();
assert.strictEqual(exporter['_transport']['_parameters']['headers']['foo'], 'boo');
assert.strictEqual(exporter['_transport']['_parameters']['headers']['bar'], 'foo');
assert.strictEqual(exporter['_transport']['_transport']['_parameters']['headers']['foo'], 'boo');
assert.strictEqual(exporter['_transport']['_transport']['_parameters']['headers']['bar'], 'foo');
envSource.OTEL_EXPORTER_OTLP_TRACES_HEADERS = '';
envSource.OTEL_EXPORTER_OTLP_HEADERS = '';
});
Expand All @@ -188,8 +188,8 @@ describe('OTLPTraceExporter - node with proto over http', () => {
foo: 'constructor',
},
});
assert.strictEqual(exporter['_transport']['_parameters']['headers']['foo'], 'constructor');
assert.strictEqual(exporter['_transport']['_parameters']['headers']['bar'], 'foo');
assert.strictEqual(exporter['_transport']['_transport']['_parameters']['headers']['foo'], 'constructor');
assert.strictEqual(exporter['_transport']['_transport']['_parameters']['headers']['bar'], 'foo');
envSource.OTEL_EXPORTER_OTLP_HEADERS = '';
});
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -343,15 +343,15 @@ describe('OTLPMetricExporter - node with json over http', () => {
envSource.OTEL_EXPORTER_OTLP_HEADERS = 'foo=bar';
const exporter = new OTLPMetricExporter();
assert.strictEqual(
exporter._otlpExporter['_transport']['_parameters']['headers']['foo'],
exporter._otlpExporter['_transport']['_transport']['_parameters']['headers']['foo'],
'bar'
);
envSource.OTEL_EXPORTER_OTLP_HEADERS = '';
});
it('should include user agent in header', () => {
const exporter = new OTLPMetricExporter();
assert.strictEqual(
exporter._otlpExporter['_transport']['_parameters']['headers']['User-Agent'],
exporter._otlpExporter['_transport']['_transport']['_parameters']['headers']['User-Agent'],
`OTel-OTLP-Exporter-JavaScript/${VERSION}`
);
});
Expand All @@ -360,11 +360,11 @@ describe('OTLPMetricExporter - node with json over http', () => {
envSource.OTEL_EXPORTER_OTLP_METRICS_HEADERS = 'foo=boo';
const exporter = new OTLPMetricExporter();
assert.strictEqual(
exporter._otlpExporter['_transport']['_parameters']['headers']['foo'],
exporter._otlpExporter['_transport']['_transport']['_parameters']['headers']['foo'],
'boo'
);
assert.strictEqual(
exporter._otlpExporter['_transport']['_parameters']['headers']['bar'],
exporter._otlpExporter['_transport']['_transport']['_parameters']['headers']['bar'],
'foo'
);
envSource.OTEL_EXPORTER_OTLP_METRICS_HEADERS = '';
Expand All @@ -378,11 +378,11 @@ describe('OTLPMetricExporter - node with json over http', () => {
},
});
assert.strictEqual(
exporter._otlpExporter['_transport']['_parameters']['headers']['foo'],
exporter._otlpExporter['_transport']['_transport']['_parameters']['headers']['foo'],
'constructor'
);
assert.strictEqual(
exporter._otlpExporter['_transport']['_parameters']['headers']['bar'],
exporter._otlpExporter['_transport']['_transport']['_parameters']['headers']['bar'],
'foo'
);
envSource.OTEL_EXPORTER_OTLP_HEADERS = '';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ describe('OTLPMetricExporter - node with proto over http', () => {
const exporter = new OTLPMetricExporter();
it('should include user agent in header', () => {
assert.strictEqual(
exporter._otlpExporter['_transport']['_parameters']['headers']['User-Agent'],
exporter._otlpExporter['_transport']['_transport']['_parameters']['headers']['User-Agent'],
`OTel-OTLP-Exporter-JavaScript/${VERSION}`
);
});
Expand Down Expand Up @@ -181,7 +181,7 @@ describe('OTLPMetricExporter - node with proto over http', () => {
envSource.OTEL_EXPORTER_OTLP_HEADERS = 'foo=bar';
const exporter = new OTLPMetricExporter();
assert.strictEqual(
exporter._otlpExporter['_transport']['_parameters']['headers']['foo'],
exporter._otlpExporter['_transport']['_transport']['_parameters']['headers']['foo'],
'bar'
);
envSource.OTEL_EXPORTER_OTLP_HEADERS = '';
Expand All @@ -191,11 +191,11 @@ describe('OTLPMetricExporter - node with proto over http', () => {
envSource.OTEL_EXPORTER_OTLP_METRICS_HEADERS = 'foo=boo';
const exporter = new OTLPMetricExporter();
assert.strictEqual(
exporter._otlpExporter['_transport']['_parameters']['headers']['foo'],
exporter._otlpExporter['_transport']['_transport']['_parameters']['headers']['foo'],
'boo'
);
assert.strictEqual(
exporter._otlpExporter['_transport']['_parameters']['headers']['bar'],
exporter._otlpExporter['_transport']['_transport']['_parameters']['headers']['bar'],
'foo'
);
envSource.OTEL_EXPORTER_OTLP_METRICS_HEADERS = '';
Expand All @@ -209,10 +209,10 @@ describe('OTLPMetricExporter - node with proto over http', () => {
},
});
assert.strictEqual(
exporter._otlpExporter['_transport']['_parameters']['headers']['foo'],
exporter._otlpExporter['_transport']['_transport']['_parameters']['headers']['foo'],
'constructor'
);
assert.strictEqual(exporter._otlpExporter['_transport']['_parameters']['headers']['bar'], 'foo');
assert.strictEqual(exporter._otlpExporter['_transport']['_transport']['_parameters']['headers']['bar'], 'foo');
envSource.OTEL_EXPORTER_OTLP_HEADERS = '';
});
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import { ISerializer } from '@opentelemetry/otlp-transformer';
import { IExporterTransport } from '../../exporter-transport';
import { createHttpExporterTransport } from './http-exporter-transport';
import { OTLPExporterError } from '../../types';
import { createRetryingTransport } from '../../retryable-transport';

/**
* Collector Metric Exporter abstract base class
Expand Down Expand Up @@ -65,16 +66,18 @@ export abstract class OTLPExporterNodeBase<
getEnv().OTEL_EXPORTER_OTLP_HEADERS
);

this._transport = createHttpExporterTransport({
agentOptions: config.httpAgentOptions ?? { keepAlive: true },
compression: configureCompression(config.compression),
headers: Object.assign(
{},
nonSignalSpecificHeaders,
signalSpecificHeaders
),
url: this.url,
timeoutMillis: this.timeoutMillis,
this._transport = createRetryingTransport({
transport: createHttpExporterTransport({
agentOptions: config.httpAgentOptions ?? { keepAlive: true },
compression: configureCompression(config.compression),
headers: Object.assign(
{},
nonSignalSpecificHeaders,
signalSpecificHeaders
),
url: this.url,
timeoutMillis: this.timeoutMillis,
}),
});
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/*
* Copyright The OpenTelemetry Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import { IExporterTransport } from './exporter-transport';
import { ExportResponse } from './export-response';

const MAX_ATTEMPTS = 5;
const INITIAL_BACKOFF = 1000;
const MAX_BACKOFF = 5000;
const BACKOFF_MULTIPLIER = 1.5;

class RetryingTransport implements IExporterTransport {
constructor(private _transport: IExporterTransport) {}

private retry(data: Uint8Array, inMillis: number): Promise<ExportResponse> {
return new Promise((resolve, reject) => {
setTimeout(() => {
this._transport.send(data).then(resolve, reject);
}, inMillis);
});
}

async send(data: Uint8Array): Promise<ExportResponse> {
let result = await this._transport.send(data);
let attempts = MAX_ATTEMPTS;
let nextBackoff = INITIAL_BACKOFF;

// TODO: I'm not 100% sure this is correct, please review in-depth.
while (result.status === 'retryable' && attempts > 0) {
attempts--;
const upperBound = Math.min(nextBackoff, MAX_BACKOFF);
const backoff = Math.random() * upperBound;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IIUC this is an iterative way of implementing exponential backoff (with 1,5 as exponent). What I do not fully get is this multiplication with Math.rand() 🤔

With this randomness in place the waiting time between retries may not increase. We may even get small values consuming the reties very fast.

  • Maybe we need also a lowerBound?
  • Or maybe its even simpler if just use Math.min(nextBackoff, MAX_BACKOFF)? The progression is [1000, 1500, 2250, 3375, 5000, 5000 ...]

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah yes you're right, that does not make any sense. 🤦 I think I was trying to implement a jitter but then failed to follow through on it and ended up with this thing. I think I fixed it in in f1577c0.

It takes the min of nextBackoff and MAX_BACKOFF and then adds some jitter [-0.2ms, +0.2ms] to it. Would appreciate you having another look 👀

nextBackoff = nextBackoff * BACKOFF_MULTIPLIER;
result = await this.retry(data, result.retryInMillis ?? backoff);
}

return result;
}

shutdown() {
return this._transport.shutdown();
}
}

/**
* Creates an Exporter Transport that retries on 'retryable' response.
*/
export function createRetryingTransport(options: {
// Underlying transport to wrap.
transport: IExporterTransport;
}): IExporterTransport {
return new RetryingTransport(options.transport);
}
Loading