Skip to content

Commit 39e9a09

Browse files
authored
refactor(otlp-exporter-base): fix eslint warnigns (#5391)
1 parent b5885e9 commit 39e9a09

File tree

2 files changed

+35
-21
lines changed

2 files changed

+35
-21
lines changed

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

Lines changed: 33 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -26,33 +26,23 @@ import type * as http from 'http';
2626
import { ExportResponse } from '../export-response';
2727
import { IExporterTransport } from '../exporter-transport';
2828

29+
interface Utils {
30+
agent: http.Agent | https.Agent;
31+
send: sendWithHttp;
32+
}
33+
2934
class HttpExporterTransport implements IExporterTransport {
30-
private _send: sendWithHttp | null = null;
31-
private _agent: http.Agent | https.Agent | null = null;
35+
private _utils: Utils | null = null;
3236

3337
constructor(private _parameters: HttpRequestParameters) {}
3438

3539
async send(data: Uint8Array, timeoutMillis: number): Promise<ExportResponse> {
36-
if (this._send == null) {
37-
// Lazy require to ensure that http/https is not required before instrumentations can wrap it.
38-
const {
39-
sendWithHttp,
40-
createHttpAgent,
41-
// eslint-disable-next-line @typescript-eslint/no-var-requires
42-
} = require('./http-transport-utils');
43-
this._agent = createHttpAgent(
44-
this._parameters.url,
45-
this._parameters.agentOptions
46-
);
47-
this._send = sendWithHttp;
48-
}
40+
const { agent, send } = this._loadUtils();
4941

5042
return new Promise<ExportResponse>(resolve => {
51-
// this will always be defined
52-
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
53-
this._send?.(
43+
send(
5444
this._parameters,
55-
this._agent!,
45+
agent,
5646
data,
5747
result => {
5848
resolve(result);
@@ -61,9 +51,33 @@ class HttpExporterTransport implements IExporterTransport {
6151
);
6252
});
6353
}
54+
6455
shutdown() {
6556
// intentionally left empty, nothing to do.
6657
}
58+
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-var-requires
68+
} = require('./http-transport-utils');
69+
70+
utils = this._utils = {
71+
agent: createHttpAgent(
72+
this._parameters.url,
73+
this._parameters.agentOptions
74+
),
75+
send: sendWithHttp,
76+
};
77+
}
78+
79+
return utils;
80+
}
6781
}
6882

6983
export function createHttpExporterTransport(

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,10 +92,10 @@ export function sendWithHttp(
9292
error: new Error('Request Timeout'),
9393
});
9494
});
95-
req.on('error', (error: Error | any) => {
95+
req.on('error', (error: Error) => {
9696
onDone({
9797
status: 'failure',
98-
error: error,
98+
error,
9999
});
100100
});
101101

0 commit comments

Comments
 (0)