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

Use http keep-alive in collector exporter #1661

Merged
merged 8 commits into from
Dec 14, 2020
Merged
Show file tree
Hide file tree
Changes from 2 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 @@ -33,13 +33,17 @@ export abstract class CollectorExporterNodeBase<
> {
DEFAULT_HEADERS: Record<string, string> = {};
headers: Record<string, string>;
keepAlive: boolean = false;
srikanthccv marked this conversation as resolved.
Show resolved Hide resolved
srikanthccv marked this conversation as resolved.
Show resolved Hide resolved
constructor(config: CollectorExporterConfigBase = {}) {
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 (config.keepAlive) {
srikanthccv marked this conversation as resolved.
Show resolved Hide resolved
this.keepAlive = true;
}
}

onInit(_config: CollectorExporterConfigBase): void {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ export function sendWithHttp<ExportItem, ServiceRequest>(
): void {
const parsedUrl = new url.URL(collector.url);

const options = {
const options: http.RequestOptions | https.RequestOptions = {
hostname: parsedUrl.hostname,
port: parsedUrl.port,
path: parsedUrl.pathname,
Expand All @@ -49,6 +49,12 @@ export function sendWithHttp<ExportItem, ServiceRequest>(
};

const request = parsedUrl.protocol === 'http:' ? http.request : https.request;
const Agent = parsedUrl.protocol === 'http:' ? http.Agent : https.Agent;
if (collector.keepAlive) {
srikanthccv marked this conversation as resolved.
Show resolved Hide resolved
const keepAliveAgent = new Agent({ keepAlive: true });
srikanthccv marked this conversation as resolved.
Show resolved Hide resolved
options.agent = keepAliveAgent;
srikanthccv marked this conversation as resolved.
Show resolved Hide resolved
}

const req = request(options, (res: http.IncomingMessage) => {
if (res.statusCode && res.statusCode < 299) {
collector.logger.debug(`statusCode: ${res.statusCode}`);
Expand Down
1 change: 1 addition & 0 deletions packages/opentelemetry-exporter-collector/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -339,6 +339,7 @@ export interface CollectorExporterConfigBase {
serviceName?: string;
attributes?: Attributes;
url?: string;
keepAlive?: boolean;
srikanthccv marked this conversation as resolved.
Show resolved Hide resolved
}

/**
Expand Down