microsoft / ApplicationInsights-node.js

Microsoft Application Insights SDK for Node.js

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

ApplicationInsights:Invalid JSON config file when using `APPLICATIONINSIGHTS_CONNECTION_STRING`

kashw2 opened this issue · comments

Environment:
OS: NixOS 23.05 (Linux)
Node: 18.17.1
applicationinsights version: 2.9.0

After upgrading the applicationinsights package in my project from 2.7.3 to 2.8.0 and then to 2.9.0 when running my NodeJS application using the APPLICATIONINSIGHTS_CONNECTION_STRING variable as done prior to 2.8.0 I get the warning:

[1] ApplicationInsights:Invalid JSON config file:  [
[1]   SyntaxError: Unexpected end of JSON input
[1]       at JSON.parse (<anonymous>)
[1]       at r._loadJsonFile (/home/keanu/Documents/Development/redacted/dist/redacted.js:2:5417)
[1]       at new r (/home/keanu/Documents/Development/redacted/dist/redacted.js:2:4957)
[1]       at r.getInstance (/home/keanu/Documents/Development/redacted/dist/redacted.js:2:5038)
[1]       at /home/keanu/Documents/Development/redacted/dist/redacted.js:7:72232
[1]       at /home/keanu/Documents/Development/redacted/dist/redacted.js:1:263
[1]       at /home/keanu/Documents/Development/redacted/dist/redacted.js:12:32841
[1]       at /home/keanu/Documents/Development/redacted/dist/redacted.js:1:263
[1]       at /home/keanu/Documents/Development/redacted/dist/redacted.js:43:26129
[1]       at /home/keanu/Documents/Development/redacted/dist/redacted.js:1:263
[1] ]

the warning is not present during build and goes away if i provide the applicationinsights.json however this functionality looks to be opt-in via the APPLICATIONINSIGHTS_CONFIGURATION_FILE variable which I've not set.

When the application is running after the warning, the live metrics page doesnt load and reports no telemetry is being received, this works again as expected when downgrading to 2.7.3.

My application insights initialisation looks like the following:

applicationInsights.setup()
  .setSendLiveMetrics(true)
  .setAutoCollectConsole(true, true)
  .setAutoCollectDependencies(true)
  .setAutoCollectExceptions(true)
  .setAutoCollectHeartbeat(true)
  .setAutoCollectPerformance(true)
  .setAutoCollectPreAggregatedMetrics(true)
  .setAutoDependencyCorrelation(true)
  .start();

@kashw2 are you bundling your code with the applicationinsights SDK code?, the code always try to read the file, and by default it will read the applicationinsighs.json that is part of the actual npm package, I suppose this could be a problem if there is some bundling involved

image

Hi, yes I'm currently using esbuild to bundle. Something odd that I only just noticed when reproducing this issue again was that the location that it was requiring the applicationinsights.json file to be was actually not part of the application directory itself and was in the parent directory.

The project I work on is apart of a monorepo with the project in question having a layout like:

myProject
|
------ V1
|
------ V2

When reproducing this issue now, it actually asks for the applicationinsights.json file to be present in the myProject directory instead of the V2 directory that I am working on and building out of.

I'm actually experiencing the same issue. It's asking for the config file located outside my project directory. I have downgraded to version 2.7.3 and the error has disappeared.

I'm trying to isolate the changes that triggered the error and will update once I have found it.

Hi, yes I'm currently using esbuild to bundle. Something odd that I only just noticed when reproducing this issue again was that the location that it was requiring the applicationinsights.json file to be was actually not part of the application directory itself and was in the parent directory.

The project I work on is apart of a monorepo with the project in question having a layout like:

myProject
|
------ V1
|
------ V2

When reproducing this issue now, it actually asks for the applicationinsights.json file to be present in the myProject directory instead of the V2 directory that I am working on and building out of.

Experiencing something similar - did you find a workaround? @kashw2

We have been able to overwrite using: process.env.APPLICATIONINSIGHTS_CONFIGURATION_CONTENT = '{}'

per docs:

Alternatively, instead of using a configuration file, you can specify the entire content of the JSON configuration via the APPLICATIONINSIGHTS_CONFIGURATION_CONTENT environment variable .

Just got a chance to check the solution from @jakereifer.

While this does remove the warning during build, the issue of telemetry data not being sent to Application Insights and viewable in the Live Metrics pane still persists. Is this also the case when you attempted this?

@kashw2 this still sends telem to app insights for me - we're also using @vscode/extension-telemetry which depends on applicationinsights. not sure if that changes anything

@kashw2 are you bundling your code with the applicationinsights SDK code?, the code always try to read the file, and by default it will read the applicationinsighs.json that is part of the actual npm package, I suppose this could be a problem if there is some bundling involved

@hectorhdzg: This is definitely a problem for us. We are also bundling the code (with esbuild) and unless we explicitly exclude applicationinsights from bundling we get this error message.

And for us it also looks in a folder which is one level above from where our application is in. Maybe this could even be used as an attack vector, if a file is placed there, to divert the telemetry data to a different place.

How a file from the source code is dynamically loaded during runtime in

let configFileName = "applicationinsights.json";
let rootPath = path.join(__dirname, "../../"); // Root of applicationinsights folder (__dirname = ../out/Library)
let tempDir = path.join(rootPath, configFileName); // default
really looks like a bug to me. This could be fixed e.g. by instead requiring that file statically and using that instead. With this in place bundlers could also handle this correctly.

In #1241 and #1242 I have two proposed fixes for this. The first one being more minimal, the second one more elaborate.