@@ -9,17 +9,20 @@ const { callbackify } = require('util');
99const { atMostOnce } = require ( '@instana/core' ) . util ;
1010const { http } = require ( '@instana/core' ) . uninstrumentedHttp ;
1111
12- const agentOpts = require ( '../agent/opts' ) ;
1312const defaultGatewayParser = require ( './defaultGatewayParser' ) ;
1413const readDefaultGateway = callbackify ( defaultGatewayParser . parseProcSelfNetRouteFile ) ;
1514
1615/** @type {import('@instana/core/src/core').GenericLogger } */
1716let logger ;
1817
18+ /** @type {import('@instana/collector/src/types/collector').CollectorConfig } */
19+ let config ;
1920/**
20- * @param {import('@instana/core /src/config ').InstanaConfig } config
21+ * @param {import('@instana/collector /src/types/collector ').CollectorConfig } _config
2122 */
22- const init = config => {
23+ const init = _config => {
24+ config = _config ;
25+
2326 logger = config . logger ;
2427 defaultGatewayParser . init ( config ) ;
2528} ;
@@ -52,25 +55,23 @@ const retryTimeoutMillis = process.env.INSTANA_RETRY_AGENT_CONNECTION_IN_MS
5255 * @param {import('./').AnnounceCycleContext } ctx
5356 */
5457function enter ( ctx ) {
55- const agentHost = agentOpts . host ;
56-
57- checkHost ( agentHost , function onCheckHost ( localhostCheckErr ) {
58+ checkHost ( config . agentHost , function onCheckHost ( localhostCheckErr ) {
5859 if ( ! localhostCheckErr ) {
59- setAgentHost ( agentHost ) ;
6060 ctx . transitionTo ( 'unannounced' ) ;
6161 return ;
6262 }
6363
6464 logger . debug (
65- `No Instana host agent is running on ${ agentHost } :${ agentOpts . port } : ${ localhostCheckErr } . Trying the default ` +
66- 'gateway next.'
65+ `No Instana host agent is running on ${ config . agentHost } :${ config . agentPort } : ${ localhostCheckErr } .` +
66+ ' Trying the default gateway next.'
6767 ) ;
6868
6969 readDefaultGateway ( function onReadDefaultGateway ( getDefaultGatewayErr , defaultGateway ) {
7070 if ( getDefaultGatewayErr ) {
7171 logger . warn (
72- `The Instana host agent cannot be reached via ${ agentHost } :${ agentOpts . port } and the default gateway ` +
73- `cannot be determined. Details: Error for the connection attempt: ${ safelyExtractErrorMessage (
72+ `The Instana host agent cannot be reached via ${ config . agentHost } :${ config . agentPort } ` +
73+ 'and the default gateway cannot be determined. ' +
74+ `Details: Error for the connection attempt: ${ safelyExtractErrorMessage (
7475 localhostCheckErr
7576 ) } ; error for determining the gateway: ${ safelyExtractErrorMessage (
7677 getDefaultGatewayErr
@@ -84,14 +85,15 @@ function enter(ctx) {
8485
8586 checkHost ( defaultGateway , function onCheckHostDefaultGateway ( defaultGatewayCheckErr ) {
8687 if ( ! defaultGatewayCheckErr ) {
87- setAgentHost ( defaultGateway ) ;
88+ config . agentHost = defaultGateway ;
8889 ctx . transitionTo ( 'unannounced' ) ;
8990 return ;
9091 }
9192
9293 logger . warn (
93- `The Instana host agent can neither be reached via ${ agentHost } :${ agentOpts . port } nor via the default ` +
94- `gateway ${ defaultGateway } :${ agentOpts . port } . Details: Error for the first attempt: ` +
94+ `The Instana host agent can neither be reached via ${ config . agentHost } :${ config . agentPort } ` +
95+ `nor via the default gateway ${ defaultGateway } :${ config . agentPort } . ` +
96+ 'Details: Error for the first attempt: ' +
9597 `${ safelyExtractErrorMessage ( localhostCheckErr ) } ; error for the second attempt: ${ safelyExtractErrorMessage (
9698 defaultGatewayCheckErr
9799 ) } . The Instana host agent might not be ready yet, scheduling another attempt to establish a connection ` +
@@ -139,7 +141,7 @@ function checkHost(host, cb) {
139141 req = http . request (
140142 {
141143 host,
142- port : agentOpts . port ,
144+ port : config . agentPort ,
143145 path : '/' ,
144146 agent : http . agent ,
145147 method : 'GET' ,
@@ -155,7 +157,7 @@ function checkHost(host, cb) {
155157 res . resume ( ) ;
156158 handleCallback (
157159 new Error (
158- `The attempt to connect to the Instana host agent on ${ host } :${ agentOpts . port } has failed with an ` +
160+ `The attempt to connect to the Instana host agent on ${ host } :${ config . agentPort } has failed with an ` +
159161 `unexpected status code. Expected HTTP 200 but received: ${ res . statusCode } `
160162 )
161163 ) ;
@@ -165,16 +167,16 @@ function checkHost(host, cb) {
165167 } catch ( e ) {
166168 handleCallback (
167169 new Error (
168- `The attempt to connect to the Instana host agent on ${ host } :${ agentOpts . port } has failed with the following ` +
169- `error: ${ e . message } `
170+ `The attempt to connect to the Instana host agent on ${ host } :${ config . agentPort } ` +
171+ `has failed with the following error: ${ e . message } `
170172 )
171173 ) ;
172174 return ;
173175 }
174176
175177 req . on ( 'timeout' , function onTimeout ( ) {
176178 handleCallback (
177- new Error ( `The attempt to connect to the Instana host agent on ${ host } :${ agentOpts . port } has timed out` )
179+ new Error ( `The attempt to connect to the Instana host agent on ${ host } :${ config . agentPort } has timed out` )
178180 ) ;
179181 } ) ;
180182
@@ -184,8 +186,8 @@ function checkHost(host, cb) {
184186 req . on ( 'error' , err => {
185187 handleCallback (
186188 new Error (
187- `The attempt to connect to the Instana host agent on ${ host } :${ agentOpts . port } has failed with the following ` +
188- `error: ${ err . message } `
189+ `The attempt to connect to the Instana host agent on ${ host } :${ config . agentPort } ` +
190+ `has failed with the following error: ${ err . message } `
189191 )
190192 ) ;
191193 } ) ;
@@ -219,8 +221,8 @@ function handleResponse(host, res, cb) {
219221 } catch ( e ) {
220222 cb (
221223 new Error (
222- `The attempt to connect to the Instana host agent on ${ host } :${ agentOpts . port } has failed, the response ` +
223- `cannot be parsed as JSON. The party listening on ${ host } :${ agentOpts . port } does not seem to be an ` +
224+ `The attempt to connect to the Instana host agent on ${ host } :${ config . agentPort } has failed, the response ` +
225+ `cannot be parsed as JSON. The party listening on ${ host } :${ config . agentPort } does not seem to be an ` +
224226 `Instana host agent. Full response: ${ responsePayload } `
225227 )
226228 ) ;
@@ -231,9 +233,9 @@ function handleResponse(host, res, cb) {
231233 } else {
232234 cb (
233235 new Error (
234- `The attempt to connect to the Instana host agent on ${ host } :${ agentOpts . port } has failed, the response did ` +
235- ` not have a "version" property. The party listening on ${ host } : ${ agentOpts . port } does not seem to be an ` +
236- `Instana host agent. Full response: ${ responsePayload } `
236+ `The attempt to connect to the Instana host agent on ${ host } :${ config . agentPort } ` +
237+ ' has failed, the response did not have a "version" property. The party listening on ' +
238+ `${ host } : ${ config . agentPort } does not seem to be an Instana host agent. Full response: ${ responsePayload } `
237239 )
238240 ) ;
239241 }
@@ -253,14 +255,6 @@ function safelyExtractErrorMessage(error) {
253255 return error ;
254256}
255257
256- /**
257- * @param {string } host
258- */
259- function setAgentHost ( host ) {
260- logger . info ( `Found an agent on ${ host } :${ agentOpts . port } , proceeding to announce request.` ) ;
261- agentOpts . host = host ;
262- }
263-
264258module . exports = {
265259 init,
266260 enter,
0 commit comments