DEV Community

Cover image for What is Dynamic Sampling and How it Works
Xing Wang for moesif

Posted on

What is Dynamic Sampling and How it Works

Sometimes you may want to limit the amount of analytics data coming into Moesif. This could be because you want to exclude specific traffic, such as internal or health check traffic, or you may want to reduce unnecessary data to control cost. Dynamic Sampling, available to customers on our Enterprise plan, was built to do just this. Dynamic Sampling lets you control which API calls are logged to Moesif based on customer or API behavior. Moesif intelligently extrapolates metrics for accurate reporting even with multiple sample rates. That means that no matter what rules or sample rates you have set up, you can be sure you are still seeing an accurate representation of your data.

Dynamic Sampling is controlled by setting up sampling rules in the Moesif Dashboard. You can create rules based on criteria such as request path, response status codes, user or company behaviors, and more. You can also control the applied sample rate for each rule. The sample rate is the percentage of events that will make their way into Moesif. For example, if you set a rule to sample 50% of events, Moesif will log half of the matching events intelligently (and not just log every other event to meet the 50% sampling rate).

Why use Dynamic Sampling

Our Dynamic Sampling feature serves two use cases for customers:

  • Gives users the capability to trim down excessive or uninteresting calls populating within Moesif while retaining the insight you gain from the platform.
  • Allows users to reduce their Enterprise subscription costs and save money. Moesif considers the sampling rules you have created and intelligently extrapolates usage metrics.

Sampled API calls do not count against your quota; for example, if your API usually sees 1 million API calls per month but the global sample rate is set to 25%, only the 250 thousand events per month coming into Moesif will count towards that quota.

When not to use Dynamic Sampling

It would be best if you avoided Dynamic Sampling on specific events moving into Moesif. These use cases include when you are leveraging with other Moesif features like Metered Billing or Governance Rule where you’ll want every call to be assessed in Moesif.

Dynamically sampling the number of API calls coming into Moesif would impact a monetized API because it would fail to track the exact usage, likely missing some events depending on the sampling rate applied. This will affect your billing meters and the invoices being sent to customers. However, a way around this is to apply Dynamic Sampling to endpoints that are not monetized and let events counted within your Billing Meter sample at 100%.

Another reason not to use Dynamic Sampling is when your logged API calls are less than the number of calls your subscription plan supports. For example, suppose you are making fewer API calls than your subscription plan includes. In that case, dynamic sampling is unnecessary unless you are simply trying to exclude data you don’t want in Moesif to keep data cleaner. Examples of this may be not including traffic from health check probes or internal testing.

Four types of sample rates

Dynamic Sampling rules are categorized into four kinds of sample sets and are prioritized in the following manner:

  • Sample rates for a single user or company
  • Sample rates based on regex rules
  • Sample rates based on user behaviors and demographics (Saved Cohorts)
  • Sample rates applied globally This means that if a sample rate is set for a specific customer, this will take priority over a sample rate associated with a behavioral cohort that a customer belongs to. Global sample rates have the lowest priority and are, by default, set to 100% for all customers. If an API call matches multiple rules, it gets processed by the first kind of sample rate in the prioritization hierarchy.

How to create Dynamic Sampling rules in Moesif

Now that you’ve learned what Dynamic Sampling is and what it can do. Let’s briefly look at how to set up some Dynamic Sampling rules. First, navigate to the Dynamic Sampling screen within Moesif.

A view of moesif's "product" report, showing various charts demonstrating API usage behavior based on real-time data
Here is where we can access all of the sampling rates that have been created.

Setting sampling rules settings for "My App," including a rule to sample users who made over 1m API calls in the last 4 weeks, and users from companies making over 1m API calls over the last 4 weeks
Selecting the + Rule button will allow us to create new user or company, regex, or cohort sampling rules.

Setting new sampling rules by clicking +rule in the top right corner of the settings window

Specific users or companies

After selecting either New User Rule or New Company Rule, select the user lookup or company lookup button, respectively.

In this example we will be showcasing the user lookup however the process is the same for companies.

A new user sampling rule form, showing a drop down menu to select a cohort, a slider to choose sample rate %, a priority slider, and a link to sampling documentation. In the bottom right is a "save" button.

Proceed to select an ID from the user list to enter a their Profile View. You can use the Users Where filter to quickly get at the user you are looking for.

Image description

On the Profile View under Sample Rate, select the pencil icon to change the specific user’s sample rate.

The profile view dashboard in Moesif, highlighting the users where section

Disable the inherit toggle to allow adjustments of the user’s sample rate. Set the desired sample rate by typing in the rate manually, or by using the slider, and select save.

The profile view dashboard in Moesif, with a specific user profile displayed

Regex rules

Returning to the Dynamic Sampling rules page, select New Regex Rule in the + New dropdown, enter your desired criteria and sample rate and save it. In this example; we have set the sampling rate to zero to exclude all GET requests to any of our health endpoints. These calls will not appear in the UI or count against our event quota.

Image description

Behaviors or demographics via Saved Cohorts

Behavior and demographic sampling is accomplished by utilizing Saved Cohorts; if you are unfamiliar with Saved Cohorts check out our blog post, written guide, or video guide to learn more.

Setting sampling rates for cohorts is quite easy. Select the + New button on the Dynamic Sampling rules page then either New User Rule or New Company Rule. A modal will appear letting you select a perviously saved cohort. Continue to set your desired sample rate as well as the priority. Priorities are ordered within user or company cohorts. User sample rules always takes precedence over company sample rules.

Image description


You can apply global rules by clicking the Settings button on the Dynamic Sampling rules page. The resulting modal that appears allows you to set the global sample rate, suppress known bot traffic, as well as stop traffic from given IP addresses either expressly or through regex rules. The bottom of the modal showcases all users or companies that have their sample rates set within their respective user profiles.

Image description

Try it out

For many reasons, Dynamic Sampling can be an essential part of your Moesif configuration. We also reviewed some limitations and scenarios where Dynamic Sampling would not be the best choice, such as when applying a Billing Meter or Governance Rule to an endpoint. Lastly, we went through how to set up different types of rules within Moesif itself.

If you are already on a Moesif Enterprise plan, Dynamic Sampling is available to you and can be applied. Feel free to check out our in-depth written tutorial or video guide. If you are not on an Enterprise plan, you can contact our Sales team to unlock this powerful capability. If you’re new to Moesif, sign up today to get started on unlocking the power of API analytics for your organization.

Top comments (0)