@@ -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,24 @@ 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 ) ;
60+ logger . info ( `Found an agent on ${ config . agentHost } : ${ config . agentPort } , proceeding to announce request.` ) ;
6061 ctx . transitionTo ( 'unannounced' ) ;
6162 return ;
6263 }
6364
6465 logger . debug (
65- `No Instana host agent is running on ${ agentHost } :${ agentOpts . port } : ${ localhostCheckErr } . Trying the default ` +
66- 'gateway next.'
66+ `No Instana host agent is running on ${ config . agentHost } :${ config . agentPort } : ${ localhostCheckErr } .` +
67+ ' Trying the default gateway next.'
6768 ) ;
6869
6970 readDefaultGateway ( function onReadDefaultGateway ( getDefaultGatewayErr , defaultGateway ) {
7071 if ( getDefaultGatewayErr ) {
7172 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 (
73+ `The Instana host agent cannot be reached via ${ config . agentHost } :${ config . agentPort } ` +
74+ 'and the default gateway cannot be determined. ' +
75+ `Details: Error for the connection attempt: ${ safelyExtractErrorMessage (
7476 localhostCheckErr
7577 ) } ; error for determining the gateway: ${ safelyExtractErrorMessage (
7678 getDefaultGatewayErr
@@ -84,14 +86,16 @@ function enter(ctx) {
8486
8587 checkHost ( defaultGateway , function onCheckHostDefaultGateway ( defaultGatewayCheckErr ) {
8688 if ( ! defaultGatewayCheckErr ) {
87- setAgentHost ( defaultGateway ) ;
89+ config . agentHost = defaultGateway ;
90+ logger . info ( `Found an agent on ${ config . agentHost } :${ config . agentPort } , proceeding to announce request.` ) ;
8891 ctx . transitionTo ( 'unannounced' ) ;
8992 return ;
9093 }
9194
9295 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: ` +
96+ `The Instana host agent can neither be reached via ${ config . agentHost } :${ config . agentPort } ` +
97+ `nor via the default gateway ${ defaultGateway } :${ config . agentPort } . ` +
98+ 'Details: Error for the first attempt: ' +
9599 `${ safelyExtractErrorMessage ( localhostCheckErr ) } ; error for the second attempt: ${ safelyExtractErrorMessage (
96100 defaultGatewayCheckErr
97101 ) } . The Instana host agent might not be ready yet, scheduling another attempt to establish a connection ` +
@@ -139,7 +143,7 @@ function checkHost(host, cb) {
139143 req = http . request (
140144 {
141145 host,
142- port : agentOpts . port ,
146+ port : config . agentPort ,
143147 path : '/' ,
144148 agent : http . agent ,
145149 method : 'GET' ,
@@ -155,7 +159,7 @@ function checkHost(host, cb) {
155159 res . resume ( ) ;
156160 handleCallback (
157161 new Error (
158- `The attempt to connect to the Instana host agent on ${ host } :${ agentOpts . port } has failed with an ` +
162+ `The attempt to connect to the Instana host agent on ${ host } :${ config . agentPort } has failed with an ` +
159163 `unexpected status code. Expected HTTP 200 but received: ${ res . statusCode } `
160164 )
161165 ) ;
@@ -165,16 +169,16 @@ function checkHost(host, cb) {
165169 } catch ( e ) {
166170 handleCallback (
167171 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 } `
172+ `The attempt to connect to the Instana host agent on ${ host } :${ config . agentPort } ` +
173+ `has failed with the following error: ${ e . message } `
170174 )
171175 ) ;
172176 return ;
173177 }
174178
175179 req . on ( 'timeout' , function onTimeout ( ) {
176180 handleCallback (
177- new Error ( `The attempt to connect to the Instana host agent on ${ host } :${ agentOpts . port } has timed out` )
181+ new Error ( `The attempt to connect to the Instana host agent on ${ host } :${ config . agentPort } has timed out` )
178182 ) ;
179183 } ) ;
180184
@@ -184,8 +188,8 @@ function checkHost(host, cb) {
184188 req . on ( 'error' , err => {
185189 handleCallback (
186190 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 } `
191+ `The attempt to connect to the Instana host agent on ${ host } :${ config . agentPort } ` +
192+ `has failed with the following error: ${ err . message } `
189193 )
190194 ) ;
191195 } ) ;
@@ -219,8 +223,8 @@ function handleResponse(host, res, cb) {
219223 } catch ( e ) {
220224 cb (
221225 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 ` +
226+ `The attempt to connect to the Instana host agent on ${ host } :${ config . agentPort } has failed, the response ` +
227+ `cannot be parsed as JSON. The party listening on ${ host } :${ config . agentPort } does not seem to be an ` +
224228 `Instana host agent. Full response: ${ responsePayload } `
225229 )
226230 ) ;
@@ -231,9 +235,9 @@ function handleResponse(host, res, cb) {
231235 } else {
232236 cb (
233237 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 } `
238+ `The attempt to connect to the Instana host agent on ${ host } :${ config . agentPort } ` +
239+ ' has failed, the response did not have a "version" property. The party listening on ' +
240+ `${ host } : ${ config . agentPort } does not seem to be an Instana host agent. Full response: ${ responsePayload } `
237241 )
238242 ) ;
239243 }
@@ -253,14 +257,6 @@ function safelyExtractErrorMessage(error) {
253257 return error ;
254258}
255259
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-
264260module . exports = {
265261 init,
266262 enter,
0 commit comments