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

fix(exporter-collector): all http export requests should share same a… #1863

Merged
merged 6 commits into from
Jan 25, 2021
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
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import { CollectorExporterBase } from '../../CollectorExporterBase';
import { CollectorExporterNodeConfigBase } from './types';
import * as collectorTypes from '../../types';
import { parseHeaders } from '../../util';
import { sendWithHttp } from './util';
import { createHttpAgent, sendWithHttp } from './util';

/**
* Collector Metric Exporter abstract base class
Expand All @@ -36,26 +36,15 @@ export abstract class CollectorExporterNodeBase<
> {
DEFAULT_HEADERS: Record<string, string> = {};
headers: Record<string, string>;
keepAlive: boolean = true;
httpAgentOptions: http.AgentOptions | https.AgentOptions = {};
agent: http.Agent | https.Agent | undefined;
constructor(config: CollectorExporterNodeConfigBase = {}) {
super(config);
if ((config as any).metadata) {
this.logger.warn('Metadata cannot be set when using http');
}
this.headers =
parseHeaders(config.headers, this.logger) || this.DEFAULT_HEADERS;
if (typeof config.keepAlive === 'boolean') {
this.keepAlive = config.keepAlive;
}
if (config.httpAgentOptions) {
if (!this.keepAlive) {
this.logger.warn(
'httpAgentOptions is used only when keepAlive is true'
);
}
this.httpAgentOptions = Object.assign({}, config.httpAgentOptions);
}
this.agent = createHttpAgent(this.logger, config);
}

onInit(_config: CollectorExporterNodeConfigBase): void {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ import * as http from 'http';
import * as https from 'https';
import * as collectorTypes from '../../types';
import { CollectorExporterNodeBase } from './CollectorExporterNodeBase';
import { CollectorExporterNodeConfigBase } from '.';
import { Logger } from '@opentelemetry/api';

/**
* Sends data using http
Expand Down Expand Up @@ -46,16 +48,10 @@ export function sendWithHttp<ExportItem, ServiceRequest>(
'Content-Type': contentType,
...collector.headers,
},
agent: collector.agent,
Flarna marked this conversation as resolved.
Show resolved Hide resolved
};

const request = parsedUrl.protocol === 'http:' ? http.request : https.request;
const Agent = parsedUrl.protocol === 'http:' ? http.Agent : https.Agent;
if (collector.keepAlive) {
options.agent = new Agent({
...collector.httpAgentOptions,
keepAlive: true,
});
}

const req = request(options, (res: http.IncomingMessage) => {
let data = '';
Expand All @@ -81,3 +77,25 @@ export function sendWithHttp<ExportItem, ServiceRequest>(
req.write(data);
req.end();
}

export function createHttpAgent(
logger: Logger,
config: CollectorExporterNodeConfigBase
): http.Agent | https.Agent | undefined {
if (config.httpAgentOptions && config.keepAlive === false) {
logger.warn('httpAgentOptions is used only when keepAlive is true');
return undefined;
}

if (config.keepAlive === false || !config.url) return undefined;

try {
const parsedUrl = new url.URL(config.url as string);
const httpAgentOptions = config.httpAgentOptions ?? {};
const Agent = parsedUrl.protocol === 'http:' ? http.Agent : https.Agent;
return new Agent({ keepAlive: true, ...httpAgentOptions });
} catch {
logger.error('collector exporter failed to create http agent');
vmarchaud marked this conversation as resolved.
Show resolved Hide resolved
return undefined;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,19 @@ describe('CollectorTraceExporter - node with json over http', () => {
});
});

it('different http export request should use the same agent', done => {
collectorExporter.export(spans, () => {});
collectorExporter.export(spans, () => {});

setTimeout(() => {
const [firstExportAgent, secondExportAgent] = spyRequest.args.map(
a => a[0].agent
);
assert.strictEqual(firstExportAgent, secondExportAgent);
done();
});
});

it('should successfully send the spans', done => {
collectorExporter.export(spans, () => {});

Expand Down