

# Metering usage

Metering records how much each customer uses your application. You define metrics for the activity you charge for, your application reports usage events, and AWS App Monetization aggregates them so that your usage-based plans bill accurately. Your application doesn't need to track or aggregate usage itself.

**Topics**
+ [Defining metrics](#metering-metrics)
+ [Adding metering hooks](#metering-hooks)
+ [Quota enforcement](#entitlements)

## Defining metrics
<a name="metering-metrics"></a>

A metric measures one kind of billable usage, such as API calls. Your application reports usage events, and the metric aggregates them within each billing period. You reference a metric in your plans to include usage or charge for it.

An application can have up to 4 metrics. This is the Metrics per app quota. The limit is per app, not per plan. For more information, see [Quotas and Regions for AWS App Monetization](quotas-and-regions.md).

You can define and manage metrics in the console, with the AWS App Monetization skill, or through the REST API or the AWS SDKs.

A metric has the following properties:
+ **Event type** – The type of usage event that the metric aggregates. Your application sends this value as the `eventType` of each usage event. More than one metric can aggregate the same event type, for example with different filters.
+ **Display name** – The name shown to customers on the pricing page and on invoices, 1 to 128 characters.
+ **Description** – An optional description of what the metric measures, up to 512 characters.
+ **Unit label** – The unit of measurement shown on invoices, such as `requests` or `tokens`. Use lowercase snake\_case, 1 to 64 characters.
+ **Aggregation** – How AWS App Monetization combines usage events within a billing period:
  + **Count** adds 1 for each matching event.
  + **Sum** adds the value of the metric's property across the matching events.
  + **Max** takes the largest value of the metric's property across the matching events.
+ **Property** – Required for Sum and Max. The name of the event property that AWS App Monetization aggregates, or `quantity` to aggregate the event's `quantity` value. Property names use lowercase snake\_case, 1 to 64 characters, and start with a letter.
+ **Filters** – Optional conditions on event properties that select which events the metric counts. A metric can have up to 10 filters, and an event must match all of them. Each filter compares a property with one or more values by using equals, not equals, in, or not in.
+ **Group by** – Optional event properties, up to 5, that AWS App Monetization groups the aggregated results by.

A metric's totals follow the customer's subscription billing period, and reset when the period ends. On an annual plan, they reset once a year.

You can change a metric until you publish it. After you publish a metric, its properties are locked. To change a published metric, create a new metric.

## Adding metering hooks
<a name="metering-hooks"></a>

A metering hook is code that reports a usage event to AWS App Monetization. When you set up pricing with Guided Pricing Strategy, the AWS App Monetization skill adds metering hooks to your application for you.

**Note**  
The skill adds code to your application. You are responsible for reviewing those changes and evaluating them for accuracy and appropriateness for your use case.

You can also add metering hooks yourself. At each point where a billable action occurs, your application does the following:

1. Before the action, it checks the customer's entitlement with the `GetEntitlementDecision` API operation, and proceeds only if the decision is `ALLOW`. For more information, see [Quota enforcement](#entitlements).

1. It performs the action.

1. After the action completes successfully, it reports the usage with the `PutEvent` API operation, so that you don't meter failed requests.

A `PutEvent` request includes the following:
+ `userId` – An identifier that you choose for the customer. Use an opaque, stable value, such as a UUID. Don't use an email address, a name, or other personal data.
+ `eventType` – The event type of the metrics that aggregate the event.
+ `quantity` – The amount of usage that the event represents, as a non-negative decimal string, such as `"1"` or `"12.5"`.
+ `timestamp` – The time the event occurred.
+ `properties` – Optional key-value pairs that metric filters, group-by fields, and Sum and Max metrics read.
+ `scope` – `live` or `sandbox`. AWS App Monetization keeps usage separately for each environment.
+ `clientToken` – An optional idempotency token. If you retry a request with the same token, AWS App Monetization ignores the repeat, so the usage isn't counted twice.

AWS App Monetization validates each event against your app's metrics:
+ An event whose event type has no metric is rejected.
+ For each Sum or Max metric on the event type, the event must carry the metric's property as a plain non-negative decimal, with up to 15 digits before the decimal point and 6 after, and no sign, exponent, spaces, or commas. For example, `"1200"` or `"12.5"`. If any Sum or Max metric on the event type can't read its property, the whole event is rejected, even if another metric could count it.

AWS App Monetization counts events by the time they occurred. An event that arrives late is still counted in the billing period that its timestamp falls in. To see what AWS App Monetization counted for a customer, call the `GetMetricUsage` API operation.

**Important**  
A newly published metric takes about a minute to reach metering. Wait about a minute after you publish before you send usage events for a new metric. AWS App Monetization accepts events that arrive sooner, or that have a timestamp earlier than the publish, but doesn't count or bill them.

## Quota enforcement
<a name="entitlements"></a>

AWS App Monetization enforces each customer's plan limits when your application checks their entitlement. Before a billable action, call the `GetEntitlementDecision` API operation with the same `eventType`, `quantity`, and `properties` that you will send on the usage event. AWS App Monetization applies each metric's filters and aggregation to the pending event, compares the result with the customer's plan, and returns `ALLOW` or `DENY` with a reason. Your application proceeds only on `ALLOW`.

The decision is `DENY` when the customer's usage has already reached a gated limit or usage cap, or when the pending event would push it past that limit. For information about setting included quantities, gates, overage pricing, and usage caps, see [Adding usage pricing to a metric](defining-pricing.md#pricing-usage).

The following table lists the reasons that a decision can return.


| Reason | Decision | Meaning | 
| --- | --- | --- | 
| ALLOWED | ALLOW | The action is permitted. | 
| WITHIN\_QUOTA | ALLOW | Usage is within the applicable quota. | 
| NO\_QUOTA\_RESTRICTION | ALLOW | No quota applies to the action. | 
| OVERAGE\_ACCRUING | ALLOW | Usage is past the included quantity and the plan charges overage, so each further unit is billed. | 
| COVERED\_BY\_PURCHASED\_UNITS | ALLOW | The customer has no subscription, and the action is covered by units bought through a one-time price. | 
| PAYMENT\_PAST\_DUE | ALLOW | The customer's last payment failed, and they're within the grace period. Prompt them to update their payment method. | 
| SERVICE\_DEGRADED | ALLOW | AWS App Monetization couldn't fully evaluate the request and allowed the action. If the action is costly, apply your own stricter policy. | 
| QUOTA\_EXCEEDED | DENY | The applicable quota or usage cap is exhausted. | 
| CAPABILITY\_NOT\_INCLUDED | DENY | The action isn't included in the customer's plan. | 
| OUT\_OF\_UNITS | DENY | The customer has no subscription, and the units they bought through one-time prices for this metric are used up. Offer them more units. | 
| NO\_SUBSCRIPTION | DENY | The customer has no subscription. | 
| SUBSCRIPTION\_INACTIVE | DENY | The customer's subscription isn't active. | 
| SUBSCRIPTION\_SUSPENDED | DENY | The subscription is suspended for non-payment after the grace period. It's restored when the customer pays the outstanding invoice or updates their payment method. | 

The first entitlement check for a customer can create their subscription to your app's free plan. Use the same `scope` as your usage events, so that checks and usage apply to the same environment.

We recommend that you verify enforcement in Sandbox before you publish. For more information, see [Quota enforcement does not behave as expected](troubleshooting.md#troubleshooting-enforcement).