Skip to content

Commit e1fe39d

Browse files
apply review suggestions
Co-authored-by: Marc Pichler <[email protected]>
1 parent 215dcbd commit e1fe39d

File tree

10 files changed

+52
-80
lines changed

10 files changed

+52
-80
lines changed

experimental/packages/otlp-exporter-base/src/configuration/convert-legacy-node-http-options.ts

Lines changed: 6 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -32,24 +32,13 @@ function convertLegacyAgentOptions(
3232
return config.httpAgentOptions;
3333
}
3434

35-
// populate keepAlive for use with new settings
36-
if (config?.keepAlive != null) {
37-
if (config.httpAgentOptions != null) {
38-
if (config.httpAgentOptions.keepAlive == null) {
39-
// specific setting is not set, populate with non-specific setting.
40-
config.httpAgentOptions.keepAlive = config.keepAlive;
41-
}
42-
// do nothing, use specific setting otherwise
43-
} else {
44-
// populate specific option if AgentOptions does not exist.
45-
config.httpAgentOptions = {
46-
keepAlive: config.keepAlive,
47-
};
48-
}
35+
let legacy = config.httpAgentOptions;
36+
if (config.keepAlive != null) {
37+
legacy = { keepAlive: config.keepAlive, ...legacy };
4938
}
5039

51-
if (config.httpAgentOptions != null) {
52-
return httpAgentFactoryFromOptions(config.httpAgentOptions);
40+
if (legacy != null) {
41+
return httpAgentFactoryFromOptions(legacy);
5342
} else {
5443
return undefined;
5544
}
@@ -80,7 +69,7 @@ export function convertLegacyHttpOptions(
8069
concurrencyLimit: config.concurrencyLimit,
8170
timeoutMillis: config.timeoutMillis,
8271
compression: config.compression,
83-
agent: convertLegacyAgentOptions(config),
72+
agentFactory: convertLegacyAgentOptions(config),
8473
},
8574
getHttpConfigurationFromEnvironment(signalIdentifier, signalResourcePath),
8675
getHttpConfigurationDefaults(requiredHeaders, signalResourcePath)

experimental/packages/otlp-exporter-base/src/configuration/otlp-http-configuration.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ export type HttpAgentFactory =
3434
export interface OtlpHttpConfiguration extends OtlpSharedConfiguration {
3535
url: string;
3636
headers: () => Record<string, string>;
37-
agent: HttpAgentFactory;
37+
agentFactory: HttpAgentFactory;
3838
}
3939

4040
function mergeHeaders(
@@ -81,8 +81,8 @@ export function httpAgentFactoryFromOptions(
8181
options: http.AgentOptions | https.AgentOptions
8282
): HttpAgentFactory {
8383
return async protocol => {
84-
const { Agent } =
85-
protocol === 'http:' ? await import('http') : await import('https');
84+
const module = protocol === 'http:' ? import('http') : import('https');
85+
const { Agent } = await module;
8686
return new Agent(options);
8787
};
8888
}
@@ -112,10 +112,10 @@ export function mergeOtlpHttpConfigurationWithDefaults(
112112
validateUserProvidedUrl(userProvidedConfiguration.url) ??
113113
fallbackConfiguration.url ??
114114
defaultConfiguration.url,
115-
agent:
116-
userProvidedConfiguration.agent ??
117-
fallbackConfiguration.agent ??
118-
defaultConfiguration.agent,
115+
agentFactory:
116+
userProvidedConfiguration.agentFactory ??
117+
fallbackConfiguration.agentFactory ??
118+
defaultConfiguration.agentFactory,
119119
};
120120
}
121121

@@ -127,6 +127,6 @@ export function getHttpConfigurationDefaults(
127127
...getSharedConfigurationDefaults(),
128128
headers: () => requiredHeaders,
129129
url: 'http://localhost:4318/' + signalResourcePath,
130-
agent: httpAgentFactoryFromOptions({ keepAlive: true }),
130+
agentFactory: httpAgentFactoryFromOptions({ keepAlive: true }),
131131
};
132132
}

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

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -14,21 +14,19 @@
1414
* limitations under the License.
1515
*/
1616

17-
import type {
18-
HttpRequestParameters,
19-
sendWithHttp,
20-
} from './http-transport-types';
17+
import type { HttpRequestParameters } from './http-transport-types';
2118

2219
// NOTE: do not change these type imports to actual imports. Doing so WILL break `@opentelemetry/instrumentation-http`,
2320
// as they'd be imported before the http/https modules can be wrapped.
2421
import type * as https from 'https';
2522
import type * as http from 'http';
2623
import { ExportResponse } from '../export-response';
2724
import { IExporterTransport } from '../exporter-transport';
25+
import { sendWithHttp } from './http-transport-utils';
2826

2927
interface Utils {
3028
agent: http.Agent | https.Agent;
31-
send: sendWithHttp;
29+
request: typeof http.request | typeof https.request;
3230
}
3331

3432
class HttpExporterTransport implements IExporterTransport {
@@ -37,10 +35,11 @@ class HttpExporterTransport implements IExporterTransport {
3735
constructor(private _parameters: HttpRequestParameters) {}
3836

3937
async send(data: Uint8Array, timeoutMillis: number): Promise<ExportResponse> {
40-
const { agent, send } = await this._loadUtils();
38+
const { agent, request } = await this._loadUtils();
4139

4240
return new Promise<ExportResponse>(resolve => {
43-
send(
41+
sendWithHttp(
42+
request,
4443
this._parameters,
4544
agent,
4645
data,
@@ -60,23 +59,25 @@ class HttpExporterTransport implements IExporterTransport {
6059
let utils = this._utils;
6160

6261
if (utils === null) {
63-
// Lazy import to ensure that http/https is not required before instrumentations can wrap it.
64-
const imported = await import('./http-transport-utils.js');
65-
62+
const protocol = new URL(this._parameters.url).protocol;
6663
utils = this._utils = {
67-
agent: await this._parameters.agent(
68-
new URL(this._parameters.url).protocol
69-
),
70-
// @ts-expect-error dynamic imports are never transpiled, but named exports only work when
71-
// dynamically importing an esm file, and the utils file might be transpiled to cjs
72-
send: imported.sendWithHttp ?? imported.default.sendWithHttp,
64+
agent: await this._parameters.agentFactory(protocol),
65+
request: await requestFactory(protocol),
7366
};
7467
}
7568

7669
return utils;
7770
}
7871
}
7972

73+
async function requestFactory(
74+
protocol: string
75+
): Promise<typeof http.request | typeof https.request> {
76+
const module = protocol === 'http:' ? import('http') : import('https');
77+
const { request } = await module;
78+
return request;
79+
}
80+
8081
export function createHttpExporterTransport(
8182
parameters: HttpRequestParameters
8283
): IExporterTransport {

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

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -14,22 +14,11 @@
1414
* limitations under the License.
1515
*/
1616

17-
import type * as http from 'http';
18-
import type * as https from 'https';
19-
import { ExportResponse } from '../export-response';
2017
import { HttpAgentFactory } from '../configuration/otlp-http-configuration';
2118

22-
export type sendWithHttp = (
23-
params: HttpRequestParameters,
24-
agent: http.Agent | https.Agent,
25-
data: Uint8Array,
26-
onDone: (response: ExportResponse) => void,
27-
timeoutMillis: number
28-
) => void;
29-
3019
export interface HttpRequestParameters {
3120
url: string;
3221
headers: () => Record<string, string>;
3322
compression: 'gzip' | 'none';
34-
agent: HttpAgentFactory;
23+
agentFactory: HttpAgentFactory;
3524
}

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16-
import * as http from 'http';
17-
import * as https from 'https';
16+
import type * as http from 'http';
17+
import type * as https from 'https';
1818
import * as zlib from 'zlib';
1919
import { Readable } from 'stream';
2020
import { HttpRequestParameters } from './http-transport-types';
@@ -27,13 +27,15 @@ import { OTLPExporterError } from '../types';
2727

2828
/**
2929
* Sends data using http
30+
* @param requestFunction
3031
* @param params
3132
* @param agent
3233
* @param data
3334
* @param onDone
3435
* @param timeoutMillis
3536
*/
3637
export function sendWithHttp(
38+
request: typeof https.request | typeof http.request,
3739
params: HttpRequestParameters,
3840
agent: http.Agent | https.Agent,
3941
data: Uint8Array,
@@ -53,8 +55,6 @@ export function sendWithHttp(
5355
agent: agent,
5456
};
5557

56-
const request = parsedUrl.protocol === 'http:' ? http.request : https.request;
57-
5858
const req = request(options, (res: http.IncomingMessage) => {
5959
const responseData: Buffer[] = [];
6060
res.on('data', chunk => responseData.push(chunk));

experimental/packages/otlp-exporter-base/test/common/configuration/otlp-http-configuration.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ describe('mergeOtlpHttpConfigurationWithDefaults', function () {
2828
compression: 'none',
2929
concurrencyLimit: 2,
3030
headers: () => ({ 'User-Agent': 'default-user-agent' }),
31-
agent: () => null!,
31+
agentFactory: () => null!,
3232
};
3333

3434
describe('headers', function () {

experimental/packages/otlp-exporter-base/test/node/configuration/convert-legacy-node-otlp-http-options.test.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ describe('convertLegacyHttpOptions', function () {
5252
);
5353

5454
// assert
55-
assert.strictEqual(options.agent, factory);
55+
assert.strictEqual(options.agentFactory, factory);
5656
});
5757

5858
it('should keep specific keepAlive', async () => {
@@ -65,7 +65,7 @@ describe('convertLegacyHttpOptions', function () {
6565
'v1/signal',
6666
{}
6767
);
68-
const agent = (await options.agent('https:')) as https.Agent;
68+
const agent = (await options.agentFactory('https:')) as https.Agent;
6969

7070
// assert
7171
assert.ok(agent.options.keepAlive);
@@ -85,7 +85,7 @@ describe('convertLegacyHttpOptions', function () {
8585
'v1/signal',
8686
{}
8787
);
88-
const agent = (await options.agent('https:')) as https.Agent;
88+
const agent = (await options.agentFactory('https:')) as https.Agent;
8989

9090
// assert
9191
assert.ok(agent.options.keepAlive);

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

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ describe('HttpExporterTransport', function () {
5858
url: 'http://localhost:8080',
5959
headers: () => ({}),
6060
compression: 'none',
61-
agent: () => new http.Agent(),
61+
agentFactory: () => new http.Agent(),
6262
});
6363

6464
// act
@@ -95,7 +95,7 @@ describe('HttpExporterTransport', function () {
9595
url: 'http://jocajhost:8080',
9696
headers: () => ({}),
9797
compression: 'none',
98-
agent: protocol => {
98+
agentFactory: protocol => {
9999
assert.strictEqual(protocol, 'http:');
100100
return new SedAgent();
101101
},
@@ -124,7 +124,7 @@ describe('HttpExporterTransport', function () {
124124
url: 'http://localhost:8080',
125125
headers: () => ({}),
126126
compression: 'none',
127-
agent: () => new http.Agent(),
127+
agentFactory: () => new http.Agent(),
128128
});
129129

130130
// act
@@ -148,7 +148,7 @@ describe('HttpExporterTransport', function () {
148148
url: 'http://localhost:8080',
149149
headers: () => ({}),
150150
compression: 'none',
151-
agent: () => new http.Agent(),
151+
agentFactory: () => new http.Agent(),
152152
});
153153

154154
const result = await transport.send(sampleRequestData, 1000);
@@ -173,7 +173,7 @@ describe('HttpExporterTransport', function () {
173173
url: 'http://localhost:8080',
174174
headers: () => ({}),
175175
compression: 'none',
176-
agent: () => new http.Agent(),
176+
agentFactory: () => new http.Agent(),
177177
});
178178

179179
// act
@@ -210,7 +210,7 @@ describe('HttpExporterTransport', function () {
210210
url: 'http://localhost:8080',
211211
headers: () => ({}),
212212
compression: 'none',
213-
agent: () => new http.Agent(),
213+
agentFactory: () => new http.Agent(),
214214
});
215215

216216
// act
@@ -243,7 +243,7 @@ describe('HttpExporterTransport', function () {
243243
url: 'http://localhost:8080',
244244
headers: () => ({}),
245245
compression: 'none',
246-
agent: () => new http.Agent(),
246+
agentFactory: () => new http.Agent(),
247247
});
248248

249249
// act
@@ -264,7 +264,7 @@ describe('HttpExporterTransport', function () {
264264
url: 'http://example.test',
265265
headers: () => ({}),
266266
compression: 'none',
267-
agent: () => new http.Agent(),
267+
agentFactory: () => new http.Agent(),
268268
});
269269

270270
// act
@@ -314,7 +314,7 @@ describe('HttpExporterTransport', function () {
314314
url: 'http://localhost:8080',
315315
headers: () => ({ foo: 'foo-value', bar: 'bar-value' }),
316316
compression: 'none',
317-
agent: () => new http.Agent(),
317+
agentFactory: () => new http.Agent(),
318318
});
319319

320320
// assert
@@ -363,7 +363,7 @@ describe('HttpExporterTransport', function () {
363363
url: 'http://localhost:8080',
364364
headers: () => ({ foo: 'foo-value', bar: 'bar-value' }),
365365
compression: 'gzip',
366-
agent: () => new http.Agent(),
366+
agentFactory: () => new http.Agent(),
367367
});
368368

369369
// act

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ describe('createOtlpHttpExportDelegate', function () {
5555
const delegate = createOtlpHttpExportDelegate(
5656
{
5757
url: 'http://localhost:8083',
58-
agent: () => new http.Agent(),
58+
agentFactory: () => new http.Agent(),
5959
compression: 'none',
6060
concurrencyLimit: 30,
6161
headers: () => ({}),

experimental/packages/otlp-exporter-base/tsconfig.json

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,5 @@
1919
{
2020
"path": "../otlp-transformer"
2121
}
22-
],
23-
"ts-node": {
24-
"experimentalResolver": true,
25-
"compilerOptions": {
26-
"module": "commonjs",
27-
"moduleResolution": "node10"
28-
}
29-
}
22+
]
3023
}

0 commit comments

Comments
 (0)