Skip to content

Commit 00a06f5

Browse files
committed
fix(otlp-exporter-base): remove usage of require in esm/esnext builds
1 parent 7e30af4 commit 00a06f5

1 file changed

Lines changed: 21 additions & 19 deletions

File tree

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

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,12 @@ interface Utils {
3333

3434
class HttpExporterTransport implements IExporterTransport {
3535
private _utils: Utils | null = null;
36+
private _loadingUtils: Promise<Utils> | null = null;
3637

3738
constructor(private _parameters: HttpRequestParameters) {}
3839

3940
async send(data: Uint8Array, timeoutMillis: number): Promise<ExportResponse> {
40-
const { agent, send } = this._loadUtils();
41+
const { agent, send } = await this._loadUtils();
4142

4243
return new Promise<ExportResponse>(resolve => {
4344
send(
@@ -56,27 +57,28 @@ class HttpExporterTransport implements IExporterTransport {
5657
// intentionally left empty, nothing to do.
5758
}
5859

59-
private _loadUtils(): Utils {
60-
let utils = this._utils;
61-
62-
if (utils === null) {
63-
// Lazy require to ensure that http/https is not required before instrumentations can wrap it.
64-
const {
65-
sendWithHttp,
66-
createHttpAgent,
67-
// eslint-disable-next-line @typescript-eslint/no-require-imports
68-
} = require('./http-transport-utils');
60+
private async _loadUtils(): Promise<Utils> {
61+
if (this._utils) {
62+
return this._utils;
63+
}
6964

70-
utils = this._utils = {
71-
agent: createHttpAgent(
72-
this._parameters.url,
73-
this._parameters.agentOptions
74-
),
75-
send: sendWithHttp,
76-
};
65+
if (this._loadingUtils) {
66+
return this._loadingUtils;
7767
}
7868

79-
return utils;
69+
// Lazy require to ensure that http/https is not required before instrumentations can wrap it.
70+
return (this._loadingUtils = import('./http-transport-utils').then(
71+
({ sendWithHttp, createHttpAgent }) => {
72+
this._loadingUtils = null;
73+
return (this._utils = {
74+
agent: createHttpAgent(
75+
this._parameters.url,
76+
this._parameters.agentOptions
77+
),
78+
send: sendWithHttp,
79+
});
80+
}
81+
));
8082
}
8183
}
8284

0 commit comments

Comments
 (0)