This is a guest blog post by Greg Cobb, Ben Watts, Michael Smykowski and Dieu Cao from Pivotal.
As a Cloud Foundry operator, you may wish to monetize your application runtime and services usage. While Cloud Foundry does not include a built-in billing system, it does provide features to allow operators to construct their own billing tools that meet their individual needs and requirements.
Cloud Foundry exposes usage events for both applications and services. These usage events detail when Cloud Foundry users start, stop, and upgrade apps and services. These events contain additional information about the resource usage of the apps and services. Usage events are guaranteed to only appear for operations that have successfully completed and to be in chronological order. Operators can query the Cloud Foundry API to get a list of these events and then construct billing periods and invoices according to their own billing policies.
When billing on Cloud Foundry, there are a few important concepts that operators must understand in order to ensure accurate billing.
First, usage events expire periodically. The expiration period of application usage events configurable via the Cloud Foundry manifest ‘cc.app_usage_events.cutoff_age_in_days’ property, and defaults to 31 days. Service usage events expiration will be included in a future release of Cloud Foundry. Operators should maintain an external data warehouse to persist events across expiration periods.
Second, when first starting billing for either apps or services, operators must trigger a “billing epoch” to create start events for existing apps or services. It is important that operators only do this once, when they first start billing, or usage events may be lost during the epoch creation. This is because when the epoch is created, all usage events are deleted, and new events are created for all running apps and services.
Third, Cloud Foundry provides an ‘after_guid’ query parameter that will return all of the usage events that have occurred after an indicated usage event. Operators may be tempted the use the guid of the last known event when making this query; however, the list of usage events returned from the API is not guaranteed to be complete. Events may still be processing at the time of the query, so events that occurred before the final event may still appear. If the final event is used, these events will be lost, which will result in missed usage events.
To combat this, it is recommended that operators select their ‘after_guid’ from an event far enough back in time to ensure that all events have been processed. The exact buffer period depends on the particular Cloud Foundry installation, but 1 minute is typically sufficient to prevent data loss.
For more information about usage events and billing see the official docs: