Search Sreeni's Insights

Filter by content type
cloud October 24, 2019 7 min read

How to control the cost of Application Insights? Sampling, retention and MI logs - Part 3

How to control the Application Insights cost with sampling, data retention, MI logs and custom events, with a flowchart to choose the sampling type.

Share:

How to control the cost of Application Insights? Sampling, retention and MI logs - Part 3

Every APM tool has to answer one question sooner or later, how much data do we really need? Application Insights license works based on data ingestion, so the cost is directly proportional to how much data we send, and by default it ingests everything (100%). If we don’t manage it, the bill grows with the traffic and nobody knows which data is worth the money. In the previous article, we discussed how to enrich the telemetry and how to exclude the noise. In this article, we are going to see the sampling options, how to choose between them, the data retention rules and how to collect Management Information (MI) logs with custom events.

TL;DR Summary

  • Application Insights ingests 100% of the data by default, keep it only for the initial period and then set the sampling rate. It is mandatory.
  • Products which need MI logs can use 100% sampling, but the data must have a clear justification.
  • High traffic applications which don’t need MI logs should apply sampling between 30% to 70%.
  • Low traffic applications need no sampling when the daily data is not going beyond one GB.
  • Use adaptive sampling for pure API products, and fixed-rate sampling for web applications, SPAs and product specific APIs.
  • Don’t change the default 90 days retention without a clear justification. Two years is the maximum.
  • Use custom events for MI logs and the log4net appender for trace logs.

Why do we need to control the data?

Let us discuss the key concerns,

Cost

More data means more cost. A busy application sending every request, dependency and page view will ingest a lot of data, and most of it is repeating the same information.

Correlation

It is tempting to apply a different sampling rate to each component. But when the client side keeps 30% and the server side keeps 70%, the trace of a request is broken in many places, and we can’t correlate them any more.

Ownership

Somebody has to be responsible for the data. In our guidelines, product teams are responsible for managing the data as well as the cost incurred. It makes the teams think before they log.

What is sampling?

Sampling is a technique to keep only a percentage of telemetry items and drop the rest, in a way that the statistics are still reasonably accurate. Simply put, we don’t store every record, we store enough of them to see the pattern. All APM products apply it, and Microsoft strongly recommends applying it product wise for storage and cost effectiveness.

Application Insights provides three different options for data sampling,

  1. Adaptive sampling - The SDK adjusts the rate automatically based on the volume of the traffic.
  2. Fixed-rate sampling - We set the percentage, and the same rate is applied all the time.
  3. Ingestion sampling - It is applied at the service end, after the data reaches Application Insights. It doesn’t reduce the traffic from the application, it only reduces the stored data.

For more information, refer the sampling documentation.

What data is affected by sampling?

Not everything is subject to sampling. Find below the data which Application Insights collects from an application,

#DataCollection ModeIs sampling applicable?Remarks
1RequestsAutoYes (default 100%)URLs, user agent, etc.
2DependenciesAutoYes (default 100%)External dependencies like SQL server, APIs, etc.
3Page viewsAutoYes (default 100%)Browser side monitoring details
4Browser performance timingsAutoYes (default 100%)Web server time, network connectivity, front end time, etc.
5ExceptionsAutoNoServer and browser side exceptions
6Performance countersAutoYes (default 100%)Server memory and CPU metrics
7TracesManualYes (default 100%)Application logs and states, associated with the APM logs
8Custom eventsManualYes (default 100%)Can be used for MI logs, but it is applicable for sampling

It is important to notice that exceptions are not sampled. So, even with a low sampling rate, we will not lose the failures.

How to choose the sampling type?

Based on the type of the product, we derived the following rules. The following diagram explains the decision flow. The flow highlighted in purple is the usual case for a high traffic web application which doesn’t use MI logs,

How to choose the sampling type
How to choose the sampling type

Must Do:

  • Use 100% sampling when your product needs MI logs, with clear justification for the data.
  • Apply sampling between 30% to 70% for high traffic applications which don’t need MI logs.
  • Don’t apply sampling to low traffic applications when the daily data usage is not going beyond one GB.
  • Use adaptive sampling for pure API products.
  • Use fixed-rate sampling for server end web applications and SPAs.
  • Use fixed-rate sampling for product specific APIs, because all API calls must correlate with either the server requests or the browser requests. Correlation might not be possible when adaptive sampling is used for them.

Info: The quick summary of our best practices says 30% for high traffic applications, and the detailed recommendation says between 30% to 70%. Start with the value which suits your traffic, and tune it over the period based on the usefulness of the logs and the ingestion.

Keep in mind that changing the sampling rate is not a one-time activity. Review the data and the cost regularly.

How long do we keep the data?

By default, Application Insights provides 90 days of retention. Don’t change it, and any increase needs a clear justification. For MI logs, we can set a longer retention, but it needs approval from the service delivery managers. The maximum retention in Application Insights is two years.

What are MI logs and custom events?

MI logs are the business usage data of an application, like which button was clicked or which page was opened, and the same approach is used for A/B testing related data. Application Insights collects them through the custom event API of its SDK, at both server and client end.

  • MI logs must be collected manually using the Application Insights SDK.
  • Application Insights uses Log Analytics as the backend to store and retrieve the data.
  • Collected data is stored in the customEvents table of Log Analytics.
  • We can use Log Analytics to retrieve and project the data, and integrate with BI tools like Power BI.
How MI logs flow from the application to reports
How MI logs flow from the application to reports

How to add MI logs with the SDK?

Find some examples below for reference. Refer the custom events API for more information.

Client (JavaScript):

appInsights.trackEvent("ActiveListClick", {"applicationId": "00000-0000", "page": "HomePage"});

Server (C#):

var data = new Dictionary<string, string> { { "applicationId", "00000-00000" }, { "page", "HomePage" } };
telemetry.TrackEvent("ActiveListClick", data);

Here TrackEvent has two parameters, the first one is the event name and the second one is the custom event data. With the help of custom event data, we can send any data which is associated with the event.

How to retrieve the MI logs?

To get the data back, we use the customDimensions property of the customEvents table with a Kusto query. Refer the following queries,

customEvents
| where name == "ActiveListClick"
customEvents
| where name == "ActiveListClick" and customDimensions.applicationId == "00000-0000"

Must Do:

  • Use 100% sampling when your product needs to collect MI logs.
  • Have a clear justification for whatever data you store as part of MI logs.

What about trace and audit logs?

Trace/application logs

Application Insights provides the trace log feature to add application logs, which helps developers to keep all the logs in one place. Trace log collection is subject to the sampling rate. It is mandatory to use the log4net appender for Application Insights to add trace or application logs. Refer the trace logs documentation for the steps.

Audit logs

We can use custom events or trace logs for audit logs, but only when the sampling rate is not reduced from the default (100%). Otherwise, we may lose the audit records, so refer the MI logs section above for the right approach.

Wrap up

In this article, we have looked at why we need to control the data, the three sampling options, how to choose the right one for a product, the retention rules and how to collect and read the MI logs. Thanks for reading this article. In the next article, I will walk you through the tools, Kusto queries and the dashboards which help us to make use of all this data.

Related Posts