Skip to content

Commit e0dafe0

Browse files
fix(otlp-exporter-base): remove brackets from IPv6 hostname in HTTP transport (#6632)
Signed-off-by: Raphael Torquato <raphael.martini@live.com>
1 parent f804c93 commit e0dafe0

4 files changed

Lines changed: 52 additions & 10 deletions

File tree

experimental/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ For notes on migrating to 2.x / 0.200.x see [the upgrade guide](doc/upgrade-to-2
5252

5353
### :bug: Bug Fixes
5454

55+
* fix(otlp-exporter-base): fix IPv6 address handling by passing URL object to http.request [#5768](https://github.com/open-telemetry/opentelemetry-js/issues/5768) @raphaeltorquat0
5556
* fix(instrumentation-xml-http-request): avoid unwrapping `XMLHttpRequest` API when disabling [#6611](https://github.com/open-telemetry/opentelemetry-js/pull/6611) @david-luna
5657
* fix(instrumentation-fetch): tolerate non-writable `globalThis.fetch` and fix premature `_isEnabled` / `_isFetchPatched` flips in `enable()` @brunorodmoreira
5758
* fix(instrumentation-xhr): resolve relative URLs before matching `ignoreUrls` [#6551](https://github.com/open-telemetry/opentelemetry-js/pull/6551) @Maximiliano-Zeballos

experimental/packages/otlp-exporter-base/src/transport/http-transport-utils.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,15 +54,12 @@ export function sendWithHttp(
5454
}
5555

5656
const options: http.RequestOptions | https.RequestOptions = {
57-
hostname: parsedUrl.hostname,
58-
port: parsedUrl.port,
59-
path: parsedUrl.pathname,
6057
method: 'POST',
6158
headers,
6259
agent,
6360
};
6461

65-
const req = request(options, (res: http.IncomingMessage) => {
62+
const req = request(parsedUrl, options, (res: http.IncomingMessage) => {
6663
const responseData: Buffer[] = [];
6764
let responseSize = 0;
6865
res.on('data', (chunk: Buffer) => {

experimental/packages/otlp-exporter-base/test/node/http-exporter-transport.test.ts

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,47 @@ describe('HttpExporterTransport', function () {
106106
);
107107
});
108108

109+
it('returns success when sending to IPv6 localhost address', async function () {
110+
// arrange
111+
const expectedResponseData = Buffer.from([4, 5, 6]);
112+
server = http.createServer((_, res) => {
113+
res.statusCode = 200;
114+
res.write(expectedResponseData);
115+
res.end();
116+
});
117+
118+
// Listen on IPv6 localhost - skip test if IPv6 is not available
119+
try {
120+
await new Promise<void>((resolve, reject) => {
121+
server!.listen(0, '::1', () => resolve());
122+
server!.once('error', reject);
123+
});
124+
} catch (err: unknown) {
125+
if ((err as NodeJS.ErrnoException).code === 'EADDRNOTAVAIL') {
126+
this.skip();
127+
}
128+
throw err;
129+
}
130+
const port = (server!.address() as any).port;
131+
132+
const transport = createHttpExporterTransport({
133+
url: `http://[::1]:${port}`,
134+
headers: async () => ({}),
135+
compression: 'none',
136+
agentFactory: () => new http.Agent(),
137+
});
138+
139+
// act
140+
const result = await transport.send(sampleRequestData, 1000);
141+
142+
// assert
143+
assert.strictEqual(result.status, 'success');
144+
assert.deepEqual(
145+
(result as ExportResponseSuccess).data,
146+
expectedResponseData
147+
);
148+
});
149+
109150
it('returns retryable on retryable status', async function () {
110151
//arrange
111152
server = http.createServer((_, res) => {

experimental/packages/otlp-exporter-base/test/node/http-transport-utils.test.ts

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,17 @@ describe('compressAndSend', function () {
2626
});
2727

2828
describe('sendWithHttp', function () {
29-
const requestFn: typeof http.request = (
30-
opts: any,
31-
cb: any
29+
let sentUserAgent: string;
30+
31+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
32+
const requestFn: any = (
33+
url: URL,
34+
opts: http.RequestOptions,
35+
cb?: (res: http.IncomingMessage) => void
3236
): http.ClientRequest => {
33-
sentUserAgent = opts.headers['User-Agent'];
34-
return http.request(opts, cb).destroy();
37+
sentUserAgent = (opts.headers as Record<string, string>)['User-Agent'];
38+
return http.request(url, opts, cb).destroy();
3539
};
36-
let sentUserAgent: string;
3740

3841
beforeEach(function () {
3942
sentUserAgent = '';

0 commit comments

Comments
 (0)