memory-bank/components/usage.md
The Usage component in Gitpod is responsible for tracking, calculating, and managing workspace usage and billing. It provides services for monitoring workspace usage, calculating credit consumption, managing billing subscriptions, and integrating with payment providers like Stripe. This component is central to Gitpod's usage-based billing model and ensures accurate tracking of resource consumption.
The primary purposes of the Usage component are:
The Usage component is built as a Go service with several key components:
The component uses a scheduler to periodically run jobs that calculate usage, update ledgers, and reset usage counters at billing cycle boundaries.
main.go: Entry point for the applicationcmd/root.go: Command-line interface setupcmd/run.go: Main server run commandpkg/server/server.go: Core server implementationpkg/apiv1/usage.go: Usage service implementationpkg/apiv1/billing.go: Billing service implementationpkg/apiv1/pricer.go: Credit pricing calculationspkg/scheduler/: Scheduling of periodic jobspkg/stripe/: Stripe payment integrationThe Usage component runs several scheduled jobs:
These jobs are scheduled based on configuration and use distributed locks (via Redis) to ensure they only run on one instance at a time.
The component calculates credit usage based on:
The pricer component handles the calculation of credits based on workspace class and usage time.
The Usage component integrates with Stripe for payment processing:
The Usage component is configured through a JSON configuration file:
{
"controllerSchedule": "1m",
"resetUsageSchedule": "24h",
"creditsPerMinuteByWorkspaceClass": {
"default": 0.5,
"large": 1.0
},
"stripeCredentialsFile": "/etc/gitpod/stripe/credentials.json",
"server": {
"port": 3000,
"address": "0.0.0.0"
},
"defaultSpendingLimit": {
"forTeams": 500,
"forUsers": 100
},
"stripePrices": {
"individualUsagePriceId": "price_1234",
"teamUsagePriceId": "price_5678"
},
"redis": {
"address": "redis:6379"
},
"serverAddress": "server:3000",
"gitpodHost": "gitpod.io"
}
components/common-go: Common Go utilitiescomponents/gitpod-db: Database accesscomponents/public-api: Public API definitionscomponents/usage-api: Usage API definitionsThe Usage component integrates with:
The component implements several security measures:
The component exposes various metrics:
The Usage component is typically used to: