question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[BUG] trackMetric does not track stdDev nor sum

See original GitHub issue

When tracking a (custom) metric, the standard deviation is not tracked (as valueStdDev) and valueSum is identical to value. Additionally, when min-value equals 1,

Steps to Reproduce

  • OS/Browser: Google Chrome 93.0.4577.82 (64-bit), Windows 10
  • SDK Version [e.g. 22]: javascript:2.7.0
  • How you initialized the SDK:
<!DOCTYPE html>
<html>
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
  <script src="https://js.monitor.azure.com/scripts/b/ai.2.7.0.min.js"></script>
  <script>
  
  let msg = { config: {
    "instrumentationKey": "<<>>",
    "appId": "Debug Metrics"
}}
  
let init = new Microsoft.ApplicationInsights.ApplicationInsights(msg);
let appInsights = init.loadAppInsights();  
  </script>
</head>
<body>
	<script>
	appInsights.trackMetric({name: 'my_metric', average: 1, sampleCount: 2, min: 1, max: 2, stdDev: 1.0});
	appInsights.flush();
	</script>
</body>
</html>

When viewing the tracked metric in Application Insights on portal.azure.com, the values do not match what was provided.

Metric in Application Insights log on portal.azure.com

Expected behavior

  1. It should be possible to track the sum.
  2. When retrieving the metric in the Application Insights logs on portal.azure.com, the value, valueCount, valueSum, valueMin, valueMax, and valueStdDev should match (within some reasonable precision) to the values submitted from the JS script.

Additional context The documentation on appInsights.trackMetric is entirely missing (for the JS-part), and this is my best guess for actually getting something through. A lot of other attempts (e.g. trackMetric('name',value) didn’t even raise an error – but nothing appeared to be sent).

I have further found, that the second argument to trackMetric can be used to send additional properties (i.e. customDimensions), such as appInsights.trackMetric({name: 'bacon', average: 5, sampleCount: 6, min: 1, max: 15}, {PageName: "foo", "PageUrl": "/", "questionable_field": "zap" });

Issue Analytics

  • State:open
  • Created 2 years ago
  • Comments:11 (9 by maintainers)

github_iconTop GitHub Comments

1reaction
MSNevcommented, Oct 28, 2021

Ok, the issue with the average / sampleCount is because there is a different calculation between what the SDK takes and how the backend “processes” the values.

Simplistically, the backend expects the total “value” of the metric and the number of events “count” and from these it calculates the “average” value / count and if the min is smaller or the max is larger then the passed values are replaced with this calculation.

So, the fix is problematic… As technically the SDK should pass the value as the (average * (sampleCount || 1)) instead of just the average. However, doing this will probably break someone as now the reported value won’t be the value of the average passed (if the sampleCount is > 1)… Will think about this overnight.

0reactions
MSNevcommented, Dec 6, 2021

Well crap!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Get-Metric in Azure Monitor Application Insights
The value property represents a sum of all the individual values that were aggregated. Note. GetMetric does not support tracking the last value ......
Read more >
Application insights track metric is not working?
Everything works and data is sent to Azure, except TrackMetric. Metric is not available neither in metrics explorer, neither in diagnostic statistics. How...
Read more >
@microsoft/applicationinsights-shims | Yarn - Package Manager
While the Application Insights JS SDK will use the stubs defined in this ... x.x versions of the SDK or your runtime will...
Read more >
MetricTelemetry.setMin - Microsoft/ApplicationInsights-Java
setStandardDeviation(stdDev); for (Entry<String, String> entry ... if name is null or empty */ public void trackMetric(String name, double value, ...
Read more >
Aggregating pre-aggregated metrics in Azure Monitor
This client side aggregation means that the measured sample values are not actually sent, but only their count, sum, minimum, maximum and ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found