Dynamic Sampling
Dynamic sampling is a cost-savings feature that lets you control which API calls are logged to Moesif based on customer or API behavior. This can be done within the Moesif UI and doesn’t require any code changes or restarts. Moesif will push new rules to your server integrations automatically.
Because Moesif never receives skipped API calls, they do not count against your subscription’s event quota. In addition, Moesif automatically extrapolates and normalizes metrics, so your reporting is accurate even if different customers and behaviors have different sample rates.
Example use cases
Below are some common examples of how you can leverage dynamic sampling:
- Sample customers who signed up in the last 90 days at 100% for maximum support visibility; everyone else is sampled at 50%
- Down-sample your largest volume of customers to 25%, so they don’t consume your quota.
- Suppress collection of health probes and other low-value traffic.
- Opt-out specific customers from data collection based on their contract terms and privacy requirements.
- Ensure all 4xx or 5xx errors are always logged (100% sample rate)
You can create sampling rules based on customer demographics, user behaviors, and specific regex matches on API fields like URI or status codes.
Dynamic Sampling is available only on enterprise plans. Companies with high-volume APIs (millions or billions of API calls per day) benefit most from the cost savings from dynamic sampling. {: .text-center .notice–info}
How sampling works
With sampling, Moesif only sees a portion of your API traffic based on behaviors which can be any of:
- User or company behavior (such as whether they signed up in the last 90 days or experienced an error during the previous 7 days).
- User or company demographics (such as free vs. paid customers).
- API attributes such as status code or URI.
Because Moesif doesn’t see all traffic, you can reduce your Moesif subscription cost as sampled API calls do not count against your quota. So, for example, if your API usually sees 1 billion API calls per month, but you set the global sample rate to 25%, you only need a plan for 250 million events per month. However, because Moesif has full knowledge of your sampling rules, the platform is intelligent enough to extrapolate usage metrics even if different customers have different sample rates.
Dynamic Sampling works only on API Calls. Actions triggered from a UI are not sampled. {: .text-center .notice–info}
How to set sample rates
Moesif supports four levels of sampling (prioritized in this order):
- Sample API calls for a single user or company
- Sample API calls based on regex rules on API fields like specific URIs
- Sample API calls based on user behaviors and demographics (via saved cohorts)
- Sample API calls globally
Sample rates are prioritized in the above order. If both the user and company have a defined sample rate, the user’s rate always takes priority over the company’s rate. Meaning a sample rate set for a specific customer takes precedence over the sample rate associated with a behavioral cohort that a customer belongs to. Unsurprisingly, the global sample rate has the lowest priority and is, by default, set to 100% for all customers.
To access dynamic sampling, click your Avatar at the bottom left of the navigation pane and select Dynamic Sampling.
User behavior-driven sampling
Behaviorally driven sampling is the most powerful and common way to leverage dynamic sampling. With behaviorally driven sampling, you apply a sample rate to members of a behavioral cohort, such as “Companies who made over 1M API Calls last 4 weeks” or “Newly integrated users who made purchase transactions.” You can create both a company rule and a user rule.
For high reliability, set your global sampling rate to the “expected” sampling you want for most customers, and do not set global sampling to a very low number, like 1%. Then, use behavioral cohorts to opt-in groups of users/companies to higher or lower sample rates. Behavioral cohorts are dynamically refreshed every few minutes. However, there is a small window where a new user or company has not yet enrolled into a cohort and has their rates propagated to your server integration. This causes the user/company to fall back to global and regex-based sampling rules.
To set a sample rate for a cohort of users or companies:
1. Save a behavioral cohort
If you have not created a behavioral cohort (which drives sampling rules and governance rules, among other things), you will need to create one.
- Click the orange new button at the top left, and select User Cohort or Company Cohort.
- Add the customer demographics or behavioral filters you want for the cohort; once done, click the _Create Cohort_button at the top left and give it a name.
In this example, we are selecting users who were created in the last 4 months and performed at least 1 API call to either /widgets/buying
or /purchases
in the last year.
2. Set the sample rate for the cohort
Once you have created a cohort, go back to Dynamic Sampling from the bottom left menu. Then, click the orange + Rule button and select New User Rule or click New Company Rule based on what you created in step 1.
From here, you will see a list of saved behavioral cohorts. Select one and then set the sample rate.
In this case, 100% means to log all API calls, whereas 0% suppresses all data collection.
Static rate for a single user or company
Finally, any user or company can have a defined sample rate from 0% to 100%. By default, all users sample 100% of API traffic (i.e., all traffic). Reducing any customer to 0% effectively suppresses all data collection for that user (A mechanism to enable GDPR/CCPA compliance). A use case of leveraging user-level sampling is to set your 10 largest customers to a less than 100% sample rate, which will collect 100% of API traffic for all others.
To set the sample rate for an individual user or company, click on Users or Companies in the top header, then look up the user or company of interest. Once you open their profile view, click the orange button Edit Sample Rate.
Regex rules
Regex rules allow you to dynamically adjust sample rates based on specific fields within the API call. For example, you may want to log any response status code > 400 at 100%. You can also use regex rules to suppress data collection on uninteresting traffic such as GET /health/probe
. API Calls do not need to be attributed to a user or company for regex rules to work, which makes them perfect for health probes and test traffic.
To get started, click your Avatar at the bottom left and select the Dynamic Sampling menu.
Click the orange + Rule button and select New Regex Rule. This will open a panel where you can choose the fields and the regular expression to match.
Select the field to match on, such as the Request Route or the Response Status. Then, add your regular expression. If you need help, view regex101 to build one.
Then select the sample rate that should be applied for this regular expression. In the above example, we want to suppress data collection for non-interesting traffic by not logging health probes.
Global sampling
The global sample rate is used by any user (or company) that doesn’t have its own sample rate nor belongs to any behavioral cohort. To modify the global sample rate, click on Settings button.
This also brings up two other options to suppress traffic: IP addresses, IP address ranges, and known bots and crawlers.
Auditing effective sample rates
There are two ways to audit the effective sample rate for a customer. First, the effective rate includes any statically defined sample rate for a user/company, that could be used for GDPR compliance, and any sample rates that are dynamically set by behavioral cohort rules. It does not include non-customer-centric rates like regex rules which are applied on a per-request level. To view the effective sample rate for a customer:
- Go to any user or company profile to view the Sample Rate.
- Click your Avatar at the bottom left, select Dynamic Sampling, and click the orange Settings button at the top. A modal will appear with the following at the bottom of the window:
The time when your cohort rules were last updated is also displayed. This means the most recent time when either you manually saved the cohort criteria or because a user/company was added or removed from a cohort based on their behavior. If the cohort hasn’t been updated, this could imply no customers have been added or removed recently.
SDK/Plugin support for Dynamic Sampling
Contact us if your SDK is not supported and we can prioritize on our roadmap.
SDK | User Rule | Company Rule | Regex Rule |
AWS Lambda Node | Y | Y | N |
AWS Lambda Python | N | N | N |
AWS Lambda GO | N | N | N |
AWS Lambda Ruby | N | N | N |
Azure APIM | Y | Y | N |
Cloudflare | Y | Y | N |
C# .NET | Y | Y | Y |
Envoy | Y | Y | Y |
Go | Y | Y | N |
Java Servlet | Y | Y | N |
Kong API Gateway | Y | Y | Y |
K8s NGINX Ingress | Y | Y | N |
NGINX | Y | Y | Y |
Node.js | Y | Y | N |
PHP Laravel | N | N | N |
PHP Slim | Y | Y | N |
PHP Symfony | N | N | N |
Python Django | Y | Y | Y |
Python Tornado | Y | Y | N |
Python WSGI | Y | Y | Y |
Python ASGI | Y | Y | N |
Python Requests | Y | Y | N |
Ruby Rack | Y | Y | Y |
Tyk API Gateway | Y | Y | N |
3Scale | Y | Y | N |
Play Filter | Y | Y | N |