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

feat(otlp-exporter-base): support CONNECT proxies #5054

Closed
Show file tree
Hide file tree
Changes from all 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 @@ -50,7 +50,9 @@ function mergeHeaders(
return Object.assign(headers, requiredHeaders);
}

function validateUserProvidedUrl(url: string | undefined): string | undefined {
export function validateUserProvidedUrl(
url: string | undefined
): string | undefined {
if (url == null) {
return undefined;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import { convertLegacyAgentOptions } from './convert-legacy-agent-options';
import {
getHttpConfigurationDefaults,
mergeOtlpHttpConfigurationWithDefaults,
validateUserProvidedUrl,
} from '../../configuration/otlp-http-configuration';
import { getHttpConfigurationFromEnvironment } from '../../configuration/otlp-http-env-configuration';

Expand Down Expand Up @@ -75,6 +76,7 @@ export abstract class OTLPExporterNodeBase<
compression: actualConfig.compression,
headers: actualConfig.headers,
url: actualConfig.url,
proxy: validateUserProvidedUrl(config.proxy),
}),
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,7 @@ class HttpExporterTransport implements IExporterTransport {
createHttpAgent,
// eslint-disable-next-line @typescript-eslint/no-var-requires
} = require('./http-transport-utils');
this._agent = createHttpAgent(
this._parameters.url,
this._parameters.agentOptions
);
this._agent = createHttpAgent(this._parameters);
this._send = sendWithHttp;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,5 @@ export interface HttpRequestParameters {
headers: Record<string, string>;
compression: 'gzip' | 'none';
agentOptions: http.AgentOptions | https.AgentOptions;
proxy?: string;
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import * as http from 'http';
import * as https from 'https';
import * as zlib from 'zlib';
import { TcpNetConnectOpts, Socket } from 'net';
import { Readable } from 'stream';
import { HttpRequestParameters } from './http-transport-types';
import { ExportResponse } from '../../export-response';
Expand Down Expand Up @@ -52,6 +53,7 @@ export function sendWithHttp(
...params.headers,
},
agent: agent,
timeout: timeoutMillis,
};

const request = parsedUrl.protocol === 'http:' ? http.request : https.request;
Expand Down Expand Up @@ -81,13 +83,7 @@ export function sendWithHttp(
});
});

req.setTimeout(timeoutMillis, () => {
req.destroy();
onDone({
status: 'failure',
error: new Error('Request Timeout'),
});
});
req.on('timeout', () => req.destroy(new Error('Request Timeout')));
req.on('error', (error: Error | any) => {
onDone({
status: 'failure',
Expand Down Expand Up @@ -138,11 +134,56 @@ function readableFromUint8Array(buff: string | Uint8Array): Readable {
return readable;
}

export function createHttpAgent(
rawUrl: string,
agentOptions: http.AgentOptions | https.AgentOptions
) {
const parsedUrl = new URL(rawUrl);
export function createHttpAgent(params: HttpRequestParameters) {
const parsedUrl = new URL(params.url);
const Agent = parsedUrl.protocol === 'http:' ? http.Agent : https.Agent;
return new Agent(agentOptions);

if (!params.proxy) {
return new Agent(params.agentOptions);
}

const parsedProxy = new URL(params.proxy);

const headers: http.OutgoingHttpHeaders = {};
if (parsedProxy.username) {
const basic = Buffer.from(
`${parsedProxy.username}:${parsedProxy.password}`
).toString('base64');
headers['Proxy-Authorization'] = `Basic ${basic}`;
}

const request =
parsedProxy.protocol === 'http:' ? http.request : https.request;

class ProxyAgent extends Agent {
constructor() {
super({ keepAlive: true, ...params.agentOptions });
}

createConnection(
options: TcpNetConnectOpts,
callback: (err: Error | null, conn?: Socket | null) => void
) {
const req = request({
method: 'CONNECT',
hostname: parsedProxy.hostname,
port: parsedProxy.port,
headers,
path: `${options.host || parsedUrl.hostname}:${options.port}`,
timeout: options.timeout,
})
.on('connect', (res, conn) => {
if (res.statusCode === 200) {
callback(null, conn);
} else {
callback(new OTLPExporterError(res.statusMessage, res.statusCode));
}
})
.end();

req.on('error', callback);
req.on('timeout', () => req.destroy(new Error('Request Timeout')));
}
}
return new ProxyAgent();
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import { OTLPExporterConfigBase } from '../../types';
export interface OTLPExporterNodeConfigBase extends OTLPExporterConfigBase {
keepAlive?: boolean;
compression?: CompressionAlgorithm;
proxy?: string;
httpAgentOptions?: http.AgentOptions | https.AgentOptions;
}

Expand Down
Loading
Loading