Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Custom .trackRequest and .trackDependency not appearing in Application Insights telemetry data #368

Closed
jem-Solytica opened this issue Feb 2, 2018 · 3 comments
Labels
Milestone

Comments

@jem-Solytica
Copy link

When using Azure Functions with the applicationinsights package (1.0.1), the custom client.trackRequest() events seem to not be submitted to App Insights. After having issues with custom code, I am unable to get any results when querying the "request" in application insights analytics, but see the other telemetry data (metrics, events, exceptions, etc.). I have also tried the example code at: https://docs.microsoft.com/en-us/azure/azure-functions/functions-monitoring. But even a new function app with just the example code does not cause the custom request items to show up in analytics.

No difference in outcome whether I use the base integration (adding the APPINSIGHTS_INSTRUMENTATIONKEY app setting), or a custom setup chain within the function. I have tried including/excluding the .start() from the setup chain, and various true/false settings on the setup chain.

@jem-Solytica
Copy link
Author

jem-Solytica commented Feb 6, 2018

It appears that either numerous examples are wrong, or there's a missing type cast in both the .trackRequest() and .trackDependency() module.

The issue is that the .resultCode property must be of type string in both instances, but the examples indicate using numeric values.

When the result codes are changed to strings (encapsulated in double or single quotes) in the .trackDependency() and .trackRequest() calls, the errors go away, and the telemetry can be seen in the Application Insights queries.

Here is the test code:

/*
 * File: index.js
 * Author: Jason E. McCollough 
 * Copyright: (c) 2018, Solytica Corp.
 * Description: Test file to debug application insights issues with custom requests and custom dependencies not being reported
 * Version: 1802.1
 * License: MIT
 */

function doAI() {
  console.log('Test function starting!');
  let appInsights = require('applicationinsights');

  appInsights.setup("<add_your_key>")
      .setAutoDependencyCorrelation(false)
      .setAutoCollectRequests(false)
      .setAutoCollectPerformance(false)
      .setAutoCollectExceptions(false)
      .setAutoCollectDependencies(false)
      .setAutoCollectConsole(false)
      .setUseDiskRetryCaching(false)
      .setInternalLogging(true, true); //we don't call start because we don't want any auto-collection
  let aiClient = appInsights.defaultClient;

  aiClient.trackTrace({message: "TEST: add .setInternalLogging() to setup chain"});  //send a specific trace, so it's easier to manually correlate the items below

  //  console.log(JSON.stringify(aiClient,null,2));  //review the full client settings, etc.

  aiClient.trackEvent({name: "my custom event", properties: {customProperty: "custom property value"}});
  aiClient.trackException({exception: new Error("handled exceptions can be logged with this method")});
  aiClient.trackMetric({name: "custom metric", value: 3});
  aiClient.trackTrace({message: "trace message"});
  aiClient.trackDependency({target:"http://dbname", name:"select customers proc", data:"SELECT * FROM Customers", duration:231, resultCode:0, success: true, dependencyTypeName: "ZSQL"});
  aiClient.trackRequest({name:"GET /customers", url:"http://myserver/customers", duration:309, resultCode: 200, success:true});

  aiClient.flush(); //avoid waiting for the maxBatchIntervalMs to complete
  console.log('Finished Test');
}

doAI();

Here are the errors:
For the .trackDependency() call:

$ time node index.js
Test function starting!
Finished Test
ApplicationInsights:Sender [ { host: 'dc.services.visualstudio.com',
    port: null,
    path: '/v2/track',
    method: 'POST',
    withCredentials: false,
    headers: 
     { 'Content-Type': 'application/x-json-stream',
       'Content-Encoding': 'gzip',
       'Content-Length': 1290 } } ]
ApplicationInsights:Sender [ '{"itemsReceived":7,"itemsAccepted":5,"errors":[{"index":5,"statusCode":400,"message":"106: Field \'resultCode\' on type \'RemoteDependencyData\' is of incorrect type. Expected: string, Actual: 0"},{"index":6,"statusCode":400,"message":"106: Field \'responseCode\' on type \'RequestData\' is of incorrect type. Expected: string, Actual: 200"}]}' ]

real    0m0.933s
user    0m0.160s
sys     0m0.008s

and, for the .trackRequest() call:

$ time node index.js
Test function starting!
Finished Test
ApplicationInsights:Sender [ { host: 'dc.services.visualstudio.com',
    port: null,
    path: '/v2/track',
    method: 'POST',
    withCredentials: false,
    headers: 
     { 'Content-Type': 'application/x-json-stream',
       'Content-Encoding': 'gzip',
       'Content-Length': 1289 } } ]
ApplicationInsights:Sender [ '{"itemsReceived":7,"itemsAccepted":6,"errors":[{"index":6,"statusCode":400,"message":"106: Field \'responseCode\' on type \'RequestData\' is of incorrect type. Expected: string, Actual: 200"}]}' ]

real    0m1.142s
user    0m0.164s
sys     0m0.004s

Following changing the type of the responseCode properties:

$ time node index.js
Test function starting!
Finished Test
ApplicationInsights:Sender [ { host: 'dc.services.visualstudio.com',
    port: null,
    path: '/v2/track',
    method: 'POST',
    withCredentials: false,
    headers: 
     { 'Content-Type': 'application/x-json-stream',
       'Content-Encoding': 'gzip',
       'Content-Length': 1292 } } ]
ApplicationInsights:Sender [ '{"itemsReceived":7,"itemsAccepted":7,"errors":[]}' ]

real    0m0.861s
user    0m0.148s
sys     0m0.020s

@jem-Solytica jem-Solytica changed the title Custom .trackRequest not being submitted Custom .trackRequest and .trackDependency not appearing in Application Insights telemetry data Feb 6, 2018
@OsvaldoRosado
Copy link
Member

It looks like those code samples are indeed incorrect. The typescript type for RequestTelemetry demands this field be a string. See:
https://github.com/Microsoft/ApplicationInsights-node.js/blob/develop/Declarations/Contracts/TelemetryTypes/RequestTelemetry.ts#L31

If typescript was being used, this mismatch should result in a compile time error. That being said, we should add an internal cast here because of the incorrect code samples

OsvaldoRosado added a commit that referenced this issue Feb 8, 2018
@OsvaldoRosado OsvaldoRosado added this to the 1.0.2 milestone Feb 8, 2018
OsvaldoRosado added a commit that referenced this issue Feb 21, 2018
* Fix issue #362

* Fix issue #358

* Fix issue #346

* Fix issue #344

* Address issue #368

* Address issue #365

* Add additional logging and fix stability issues with retry caching

* Bump package.json

* Add calls to ICACLS for windows

* Address PR feedback

* Fix unit tests in Node < 0.11.12

* Fix more unit tests for Node < 0.11.12

* Fix again
@OsvaldoRosado
Copy link
Member

This should be fixed now in 1.0.2. Thanks for reporting!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants