@@ -26,33 +26,23 @@ import type * as http from 'http';
2626import { ExportResponse } from '../export-response' ;
2727import { IExporterTransport } from '../exporter-transport' ;
2828
29+ interface Utils {
30+ agent : http . Agent | https . Agent ;
31+ send : sendWithHttp ;
32+ }
33+
2934class 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
6983export function createHttpExporterTransport (
0 commit comments