Usage Component
Overview
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.
Purpose
The primary purposes of the Usage component are:
Track workspace usage across the platform
Calculate credit consumption based on workspace class and usage time
Manage billing cycles and subscription periods
Integrate with payment providers (primarily Stripe)
Enforce usage limits and spending controls
Provide usage reporting and analytics
Reset usage counters at billing cycle boundaries
Manage cost centers and team billing
Support different pricing tiers and workspace classes
Architecture
The Usage component is built as a Go service with several key components:
Usage Service: Tracks and calculates workspace usage
Billing Service: Manages billing subscriptions and payments
Scheduler: Runs periodic jobs for usage calculation and billing
Stripe Integration: Connects to Stripe for payment processing
Pricer: Calculates credit costs based on workspace class
Cost Center Manager: Manages spending limits and cost centers
The component uses a scheduler to periodically run jobs that calculate usage, update ledgers, and reset usage counters at billing cycle boundaries.
Key Files and Structure
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 integration
Scheduler Jobs
The Usage component runs several scheduled jobs:
Ledger Trigger Job: Periodically calculates usage and updates billing ledgers
Reset Usage Job: Resets usage counters at the end of billing cycles
Billing Sync Job: Synchronizes billing information with payment providers
These jobs are scheduled based on configuration and use distributed locks (via Redis) to ensure they only run on one instance at a time.
Credit Calculation
The component calculates credit usage based on:
Workspace Class: Different workspace classes have different credit rates
Usage Time: Time spent in workspaces
Billing Period: Usage is tracked within billing periods
Team Membership: Usage may be attributed to teams or individuals
The pricer component handles the calculation of credits based on workspace class and usage time.
Stripe Integration
The Usage component integrates with Stripe for payment processing:
Subscription Management: Creating and updating subscriptions
Invoice Generation: Generating invoices for usage
Payment Processing: Processing payments for usage
Webhook Handling: Processing Stripe webhook events
Customer Management: Creating and updating customer records
Configuration
The Usage component is configured through a JSON configuration file:
Dependencies
Internal Dependencies
components/common-go
: Common Go utilitiescomponents/gitpod-db
: Database accesscomponents/public-api
: Public API definitionscomponents/usage-api
: Usage API definitions
External Dependencies
Stripe API for payment processing
Redis for distributed locking and caching
GORM for database access
gRPC for API communication
Integration Points
The Usage component integrates with:
Database: For storing usage and billing information
Redis: For distributed locking and job scheduling
Stripe: For payment processing
Server: For user and team information
Workspace Manager: For workspace usage information
Security Considerations
The component implements several security measures:
Encryption: Sensitive payment information is encrypted
Access Control: Usage information is only accessible to authorized users
Audit Logging: Changes to billing and usage are logged
Webhook Verification: Stripe webhooks are verified for authenticity
Rate Limiting: API endpoints are rate-limited to prevent abuse
Metrics
The component exposes various metrics:
Usage calculation durations
Billing operation counts
Stripe API call latencies
Job execution times
Error counts
Common Usage Patterns
The Usage component is typically used to:
Track workspace usage for billing purposes
Calculate credit consumption for users and teams
Process payments through Stripe
Enforce usage limits and spending controls
Generate usage reports and analytics
Reset usage counters at billing cycle boundaries
Related Components
Server: Provides user and team information
Database: Stores usage and billing information
Workspace Manager: Provides workspace usage information
Public API Server: Exposes usage and billing APIs