Nick's Blog

Newrelic Nodejs agent failed to connect to collector

Today I was working on connect my nodejs agent from GKE cluster into newrelic. I was having trouble to connect and get the following error

{"v":0,"level":30,"name":"newrelic","hostname":"app-frontend-5fb974b675-trccz","pid":1,"time":"2019-04-25T01:20:40.971Z","msg":"Using New Relic for Node.js. Agent version: 4.13.1; Node version: v10.15.3."}
{"v":0,"level":30,"name":"newrelic","hostname":"app-frontend-5fb974b675-trccz","pid":1,"time":"2019-04-25T01:20:42.155Z","msg":"Starting New Relic for Node.js connectionprocess."}
{"v":0,"level":50,"name":"newrelic","hostname":"app-frontend-5fb974b675-trccz","pid":1,"time":"2019-04-25T01:20:45.655Z","msg":"The New Relic collector rejected this agent.","component":"collector_api"}
{"v":0,"level":50,"name":"newrelic","hostname":"app-frontend-5fb974b675-trccz","pid":1,"time":"2019-04-25T01:20:45.656Z","msg":"New Relic for Node.js halted startup due to an error:","stack":"Error: Failed to connect to collector
at onConnect (/srv/app-frontend/node_modules/newrelic/lib/agent.js:163:18)
at retry (/srv/app-frontend/node_modules/newrelic/lib/collector/api.js:108:14)
at onPreConnect (/srv/app-frontend/node_modules/newrelic/lib/collector/api.js:157:14)
at Immediate.setImmediate (/srv/app-frontend/node_modules/newrelic/lib/collector/parse-response.js:62:24)
at ZoneDelegate../node_modules/zone.js/dist/zone-node.js.ZoneDelegate.invokeTask (/srv/app-frontend/dist/server/webpack:/node_modules/zone.js/dist/zone-node.js:423:1)
at Zone../node_modules/zone.js/dist/zone-node.js.Zone.runTask (/srv/app-frontend/dist/server/webpack:/node_modules/zone.js/dist/zone-node.js:195:1)
at ./node_modules/zone.js/dist/zone-node.js.ZoneTask.invokeTask (/srv/app-frontend/dist/server/webpack:/node_modules/zone.js/dist/zone-node.js:498:1)
at Immediate.ZoneTask.invoke (/srv/app-frontend/dist/server/webpack:/node_modules/zone.js/dist/zone-node.js:487:1)
at Immediate.timer [as _onImmediate] (/srv/app-frontend/dist/server/webpack:/node_modules/zone.js/dist/zone-node.js:2185:1)
at runCallback (timers.js:705:18)","message":"Failed to connect to collector"}

After the troubleshooting, I realised there are at least 2 factors that would affect the agent being connected.

1. Outbound firewall rules: if you have a enterprise firewall sitting in the front of your application, you need to open 80 and 443 for certain IP addresses (refer to official website)

Networks
For U.S. region accounts:
50.31.164.0/24
162.247.240.0/22

For EU region accounts:
185.221.84.0/22
Ports
TCP 80
TCP 443

Endpoints​​
For U.S. region accounts:
collector*.newrelic.com

For EU region accounts:
collector*.eu01.nr-data.net:443

2. The license needs to be correct and in place. This was the actual problem I experiencing. I use kubernetes secret to store the actual license inside of an environment variable. In this case, I forgot to use base64 to encode. And I endup getting garbage text inside of the environment.

NEW_RELIC_LICENSE_KEY=#garbage text#

So I did the base64 for my license

cat tmp.txt | base64

After the change newrelic agent is able to connect.

Apart from that, there is also one more thing may affect this is the nodejs newrelic agent version needs to up to date. Because official confirmed certain version is having connection issue, for more detail, please check this out. https://discuss.newrelic.com/t/node-connectivity-issue-affecting-versions-4-11-to-5-2-1/72172