This chapter covers Application Insights sampling and cost control mechanisms, which are critical for managing telemetry data volume and costs in Azure. Understanding these features is essential for the AZ-204 exam, as questions on monitoring and cost management appear in approximately 15-20% of the exam. You will learn how to configure sampling, set daily caps, and choose the right sampling type for your application. By the end, you'll be able to optimize telemetry collection without losing critical insights.
Jump to a section
Imagine a large water treatment plant that supplies water to a city. The plant has a main pipeline that carries all the water. Installing a flow meter on every single household tap would be expensive and generate massive data. Instead, the plant uses a smart sampling system: it places a meter on a subset of taps, say 1 in 10, and records their flow. To control costs, they also set a maximum number of readings per day. This is like Application Insights sampling. The plant knows that if the water usage pattern is similar across households, the sampled data accurately represents the whole city. However, if a factory suddenly uses a huge amount of water (a critical event), the smart meter can detect the anomaly and temporarily increase sampling for that tap. This is adaptive sampling. The plant also has a budget: they can't afford to process more than 10,000 readings per day. So they set an ingestion cap. If the cap is reached, they stop recording data until the next day, but they might miss important events. To avoid this, they can set a 'daily cap' that triggers an alert when 80% of the budget is used, allowing them to increase the cap if needed. This is exactly how Application Insights sampling and daily caps work: sampling reduces data volume while preserving statistical accuracy, and caps prevent runaway costs.
What is Sampling and Why Does It Exist?
Application Insights collects telemetry data (requests, dependencies, exceptions, traces, etc.) from your application. In high-traffic scenarios, the volume of data can become enormous, leading to high costs and storage overhead. Sampling is a technique that reduces the amount of telemetry data sent to Application Insights while preserving the ability to detect statistically significant events and trends. The goal is to keep a representative subset of all telemetry, so that queries and metrics remain accurate.
How Sampling Works Internally
Sampling operates by selecting a subset of telemetry items based on a sampling rate. For example, a sampling rate of 10% means that only 10 out of every 100 telemetry items are sent. The key challenge is to ensure that related telemetry items (e.g., a request and its associated dependencies and exceptions) are either all kept or all dropped. This is called 'correlated sampling.' Application Insights uses a hashing algorithm on the operation ID (or a combination of IDs) to deterministically decide whether to keep a telemetry item. This ensures that all items belonging to the same operation are sampled together.
Types of Sampling
There are three types of sampling in Application Insights:
Adaptive Sampling: This is the default and recommended sampling type for ASP.NET and ASP.NET Core applications. It automatically adjusts the sampling rate based on the volume of telemetry. It aims to keep the telemetry volume within a target rate (default: 5 events per second per host). Adaptive sampling is implemented as a telemetry processor that runs in the application process. It monitors the rate of telemetry generation and dynamically changes the sampling rate. When the volume is low, it may sample 100%; when high, it reduces to keep below the target. This is ideal for most applications because it prevents both over- and under-sampling.
Fixed-rate Sampling: You specify a fixed sampling percentage (e.g., 10%). This is useful when you want predictable cost and know your traffic patterns. However, it can over-sample during low traffic and under-sample during spikes. Fixed-rate sampling is configured via the SamplingPercentage setting in the Application Insights configuration.
Ingestion Sampling: This happens at the Application Insights service endpoint. It is a fixed-rate sampling applied to all telemetry from all applications in a workspace. It is configured in the Application Insights resource under 'Usage and estimated costs' > 'Data sampling'. Ingestion sampling is applied after telemetry is sent, so it does not reduce bandwidth or client-side processing. It is useful when you want to reduce costs without modifying application code.
Configuration Details
#### Adaptive Sampling Defaults
For ASP.NET and ASP.NET Core, adaptive sampling is enabled by default. The default settings are:
Target telemetry rate: 5 events per second per host
Evaluation interval: 15 seconds
Sampling percentage update interval: 10 seconds
Minimum sampling percentage: 0.1%
Maximum sampling percentage: 100%
Telemetry types included: All (requests, dependencies, exceptions, traces, events, metrics, page views)
Excluded types: None by default
You can modify these settings in the appsettings.json or via code.
#### Fixed-rate Sampling Configuration
In code, you can set fixed-rate sampling like this:
using Microsoft.ApplicationInsights.Extensibility;
using Microsoft.ApplicationInsights.WindowsServer.TelemetryChannel;
var config = TelemetryConfiguration.Active;
var samplingProcessor = new SamplingTelemetryProcessor(config);
samplingProcessor.SamplingPercentage = 10.0; // 10%
config.TelemetryProcessorChainBuilder.Use(samplingProcessor);
config.TelemetryProcessorChainBuilder.Build();In appsettings.json:
{
"ApplicationInsights": {
"SamplingPercentage": 10.0
}
}#### Ingestion Sampling Configuration
In the Azure portal, navigate to your Application Insights resource, click on 'Usage and estimated costs', then 'Data sampling'. You can set a percentage from 0 to 100. Note that ingestion sampling is applied after data is sent, so it does not affect the amount of data transmitted over the network.
Daily Cap and Cost Control
Application Insights has a daily cap feature that limits the amount of data ingested per day. This is essential for cost control. The daily cap is set in GB per day. When the cap is reached, data ingestion stops for the rest of the day. You can also set a 'notification threshold' at a percentage (e.g., 80%) that sends an alert when usage approaches the cap.
Key points about daily cap:
The cap is applied to all telemetry types, including live metrics.
When the cap is reached, telemetry is dropped silently. You can see the 'Daily cap reached' event in the Application Insights resource.
You can increase the cap or disable it (set to unlimited).
There is also a 'monthly cap' for continuous export scenarios, but the daily cap is more common.
How to set daily cap:
In Azure portal, go to your Application Insights resource, click 'Usage and estimated costs', then 'Daily cap'. Enter the daily cap in GB. You can also set the notification threshold.
Interaction with Sampling and Caps
Sampling and daily caps work together. Sampling reduces the volume before it reaches the cap, so you are less likely to hit the cap. However, if sampling is too aggressive, you may lose important data. Conversely, if you set a low daily cap, you might hit it even with sampling, causing data loss. Best practice is to use adaptive sampling to keep volume within a target rate, and set a daily cap as a safety net to prevent cost overruns.
Important Exam Details
Adaptive sampling is enabled by default for ASP.NET/ASP.NET Core SDKs.
The default target rate is 5 events/second/host.
Adaptive sampling ensures correlated sampling (all items of an operation are kept/dropped together).
Ingestion sampling is applied server-side and does not reduce network bandwidth.
Fixed-rate sampling is configured via SamplingPercentage.
Daily cap stops ingestion when reached; data is lost.
You can set a notification threshold (e.g., 80%) to get alerts.
Sampling is not applied to metrics that are aggregated server-side (e.g., performance counters). It only applies to individual telemetry items.
Commands and Verification
To check current sampling settings for adaptive sampling, you can use Application Insights Analytics queries:
// Check sampling rate over time
let start = ago(1h);
requests
| where timestamp > start
| summarize count() by bin(timestamp, 5m)
| extend sampledCount = count_ * (1 / samplingRate)To see if daily cap has been reached, query:
// Check daily cap events
union traces, exceptions, requests
| where timestamp > ago(1d)
| where message contains "Daily cap"Summary of Mechanisms
Adaptive Sampling: Dynamic, automatic, based on telemetry rate.
Fixed-rate Sampling: Static, predictable.
Ingestion Sampling: Server-side, no client benefit.
Daily Cap: Hard limit on data volume per day.
Understanding these mechanisms is crucial for the AZ-204 exam, where you may be asked to recommend a sampling strategy based on requirements like cost, accuracy, and complexity.
Enable Adaptive Sampling
When you add Application Insights to your ASP.NET or ASP.NET Core application via the SDK, adaptive sampling is enabled by default. The SDK installs a telemetry processor that monitors the rate of telemetry generation. Every 15 seconds (evaluation interval), it calculates the current rate. If the rate exceeds the target (default 5 events/sec/host), it reduces the sampling percentage. If the rate is below, it increases the percentage. The adjustment is applied every 10 seconds (update interval). This ensures the telemetry volume stays near the target without manual intervention.
Configure Sampling Settings
You can customize adaptive sampling by editing the `appsettings.json` file or in code. For example, to change the target rate to 10 events per second, set `"Sampling": { "TargetTelemetryRate": 10.0 }`. You can also exclude certain telemetry types from sampling, such as `"ExcludedTypes": "Event"`. This is useful if you need 100% accuracy for custom events. For fixed-rate sampling, set `"SamplingPercentage": 25.0` to keep 25% of all telemetry. Remember that fixed-rate does not adapt to traffic spikes.
Set Daily Cap and Alert
In the Azure portal, navigate to your Application Insights resource. Under 'Usage and estimated costs', click 'Daily cap'. Enter a value in GB (e.g., 1 GB). Set the notification threshold to 80% (0.8). This will send an email alert when 80% of the daily cap is consumed. When the cap is reached, telemetry ingestion stops. You can also disable the cap by setting it to 'Unlimited' (0). Note that the cap is applied to all telemetry, including live metrics. If you hit the cap, you will lose data for the rest of the day.
Monitor Sampling and Cap Status
Use Application Insights Analytics to monitor sampling effectiveness. Query `requests | where timestamp > ago(1d) | summarize count() by bin(timestamp, 1h)` to see the number of requests after sampling. Compare with the actual request count from your application logs to calculate the sampling rate. To check if the daily cap was reached, query `union traces, exceptions, requests | where timestamp > ago(1d) | where message contains "Daily cap"`. You can also set up alerts on the 'Daily cap reached' metric.
Test and Validate
Simulate high traffic to test adaptive sampling. You can use tools like Apache JMeter or a simple load test script. Monitor the Application Insights 'Live Metrics' stream to see the current telemetry rate. Observe that the sampling percentage adjusts dynamically. Also, verify that correlated telemetry (e.g., a request and its dependency calls) are either all present or all missing. You can check by querying `requests | where operation_Id == "some-id"` and then checking the associated dependencies. If sampling is working correctly, you should see either all or none of the items for a given operation.
Enterprise Scenario 1: E-Commerce Platform with Variable Traffic
A large e-commerce platform experiences massive traffic spikes during Black Friday sales. Without sampling, telemetry volume could increase 100x, leading to exorbitant costs. The engineering team implements adaptive sampling with a target rate of 5 events/sec/host. During normal traffic, sampling percentage is near 100%. During spikes, it drops to 1-2%, keeping costs predictable. However, they also set a daily cap of 50 GB as a safety net. During the spike, the cap is reached after 6 hours, causing data loss for the remainder of the day. They learn to increase the cap temporarily during known events. They also set a notification threshold at 80% to get early warnings.
Enterprise Scenario 2: Financial Services with Compliance Requirements
A financial services application must retain 100% of audit-related telemetry (e.g., user authentication events). They use fixed-rate sampling of 25% for all telemetry but exclude 'Event' telemetry type from sampling. They configure the SDK to exclude 'Event' telemetry: excludedTypes: "Event". This ensures that all custom events (which include audit logs) are sent. They also set a daily cap of 10 GB. Because the audit events are low volume, they rarely hit the cap. However, they monitor the cap usage daily and have an alert at 80%. This approach balances cost and compliance.
Enterprise Scenario 3: IoT Backend with High Volume
An IoT backend processes telemetry from millions of devices. Each device sends a heartbeat every minute. The total volume is enormous. They use ingestion sampling set to 10% at the Application Insights resource level. This is simpler than modifying each device's SDK. The downside is that network bandwidth is still consumed (since ingestion sampling is server-side). To reduce bandwidth, they also implement client-side fixed-rate sampling at 50% on the devices. This reduces network traffic by half. The combined effect: only 5% of original telemetry reaches the storage. They also set a daily cap of 100 GB. This multi-layer approach is common in high-volume scenarios.
What AZ-204 Tests
This topic falls under Objective 4.1: 'Monitor and analyze application performance with Application Insights.' The exam expects you to:
Understand the three types of sampling: adaptive, fixed-rate, and ingestion.
Know the default adaptive sampling target rate (5 events/sec/host).
Know that adaptive sampling is enabled by default for ASP.NET/ASP.NET Core.
Understand that correlated sampling ensures all items of an operation are kept/dropped.
Know how to configure sampling via code or config files.
Understand daily cap and its implications (data loss when reached).
Know that ingestion sampling does not reduce network bandwidth.
Common Wrong Answers and Why
'Ingestion sampling reduces bandwidth' – This is false. Ingestion sampling happens after the data is received by the service, so the full volume is still transmitted over the network. Candidates confuse it with client-side sampling.
'Fixed-rate sampling is the default' – The default is adaptive sampling. Fixed-rate must be explicitly configured.
'Daily cap stops sampling' – No, the daily cap stops ingestion entirely. Sampling is independent and continues to run, but data is dropped at the ingestion endpoint.
'Sampling is applied to all telemetry types equally' – You can exclude specific types. The exam may test that you can exclude 'Event' telemetry from sampling.
Specific Numbers and Terms
Default adaptive sampling target: 5 events per second per host.
Evaluation interval: 15 seconds.
Update interval: 10 seconds.
Minimum sampling percentage: 0.1%.
Maximum sampling percentage: 100%.
Daily cap notification threshold: 80% (default).
Edge Cases and Exceptions
If you use both adaptive and fixed-rate sampling, adaptive takes precedence.
Sampling is not applied to metrics that are aggregated server-side (e.g., performance counters). Only individual telemetry items (requests, dependencies, exceptions, traces, events) are sampled.
If you set a daily cap of 0, it means unlimited (no cap).
When the daily cap is reached, you can still see live metrics for a short time, but eventually they stop.
How to Eliminate Wrong Answers
If a question asks about reducing network bandwidth, look for 'adaptive' or 'fixed-rate' sampling (client-side). Ingestion sampling is not the answer.
If the question mentions 'automatic adjustment based on volume,' it's adaptive.
If the question mentions 'predictable cost regardless of traffic,' it's fixed-rate.
If the question mentions 'no code change required,' it's ingestion sampling.
If the question mentions 'data loss when cap reached,' it's daily cap, not sampling.
Adaptive sampling is the default for ASP.NET/ASP.NET Core SDKs with a target of 5 events/sec/host.
Ingestion sampling is server-side and does not reduce network bandwidth.
Fixed-rate sampling requires explicit configuration via SamplingPercentage.
Daily cap stops all ingestion when reached, causing data loss; set a notification threshold at 80%.
Sampling ensures correlated telemetry (all items of an operation kept or dropped together).
You can exclude specific telemetry types (e.g., Events) from sampling.
The evaluation interval for adaptive sampling is 15 seconds; update interval is 10 seconds.
Minimum sampling percentage is 0.1%; maximum is 100%.
These come up on the exam all the time. Here's how to tell them apart.
Adaptive Sampling
Automatically adjusts rate based on telemetry volume
Target default: 5 events/sec/host
Ensures correlated sampling
Ideal for variable traffic
Enabled by default for ASP.NET/ASP.NET Core
Fixed-rate Sampling
Static percentage configured by user
Predictable cost and volume
Also ensures correlated sampling
May over-sample in low traffic, under-sample in spikes
Must be explicitly enabled
Mistake
Adaptive sampling always keeps 100% of telemetry during low traffic.
Correct
Adaptive sampling can keep up to 100%, but it may still sample at a lower percentage if the target rate is exceeded even during low traffic (e.g., if the target is 5 events/sec and you generate 10 events/sec, it will sample at 50%). It only goes to 100% when the rate is below the target.
Mistake
Ingestion sampling reduces the amount of data sent over the network.
Correct
Ingestion sampling is applied server-side after the data is already transmitted. It reduces storage costs but does not reduce network bandwidth. Client-side sampling (adaptive or fixed-rate) does reduce network usage.
Mistake
Daily cap stops sampling, so no data is lost – it just pauses.
Correct
When the daily cap is reached, data ingestion stops completely. Data sent after that point is dropped. There is no queuing or resumption later. You lose that data permanently.
Mistake
You can set different sampling rates for different telemetry types using adaptive sampling.
Correct
Adaptive sampling applies the same sampling percentage to all included telemetry types. To have different rates, you must use fixed-rate sampling and exclude certain types, or implement custom telemetry processors.
Mistake
Sampling percentages are exact – if set to 10%, exactly 10% of telemetry is sent.
Correct
Sampling is probabilistic. Over a large number of events, the percentage will be close to the target, but short-term fluctuations occur. The hashing algorithm ensures correlation, not exact percentages per second.
Reveal each answer, then mark whether you got it right. Score 60%+ to unlock the next chapter.
The default is adaptive sampling with a target of 5 events per second per host. It is enabled automatically when you add the Application Insights SDK. The actual sampling percentage varies dynamically based on traffic volume.
To disable sampling, you can set the sampling percentage to 100% (fixed-rate) or configure adaptive sampling with a target rate high enough that it never samples. However, it's not recommended to disable sampling entirely in high-traffic applications due to cost. In code, you can remove the SamplingTelemetryProcessor from the telemetry processor chain.
Yes, but Application Insights uses statistical methods to correct metrics. For example, the 'average request duration' metric is adjusted based on the sampling rate to reflect the true population. However, if you query raw data, you must account for sampling yourself.
Yes, they are complementary. Adaptive sampling reduces the volume so you are less likely to hit the daily cap. The daily cap is a safety net to prevent cost overruns. You should set the cap high enough to avoid data loss under normal conditions.
You can query the Application Insights logs for 'Daily cap reached' events: `union traces, exceptions, requests | where message contains "Daily cap"`. You can also set up an alert on the 'Daily cap reached' metric in the Azure portal.
Live metrics may continue to stream for a short time but will eventually stop once the cap is exceeded. Data is dropped at the ingestion endpoint. You will see a message indicating that the daily cap has been reached.
Yes, you can exclude telemetry types such as 'Event', 'Exception', 'Trace', etc., by configuring the ExcludedTypes property. This ensures that those types are always sent at full fidelity.
You've just covered Application Insights Sampling and Cost Control — now see how well it sticks with free AZ-204 practice questions. Full explanations included, no account needed.
Done with this chapter?