Overview
The@proton/metrics package provides a comprehensive metrics and telemetry system for Proton applications. It handles automatic batching, retry logic, and privacy-respecting analytics to help monitor application health and usage.
Installation
Key Features
- Automatic metric batching
- Retry logic with exponential backoff
- Privacy-respecting telemetry
- Type-safe metric schemas
- Request timeout handling
- Automatic error tracking
- Low-priority network requests
Quick Start
Basic Usage
Initialization
The metrics singleton is automatically configured with sensible defaults:Configuration
Custom Metrics Instance
Create a custom metrics instance with specific configuration:MetricsApi Configuration
Tracking Metrics
Performance Metrics
User Actions
Error Tracking
Observing API Errors
Automatic API error observation:Metric Schemas
The package includes generated TypeScript types from JSON schemas:Batching and Sending
Automatic Batching
Metrics are automatically batched and sent based on configuration:Manual Flush
Force sending of queued metrics:Request Handling
Retry Logic
Automatic retry with exponential backoff:- Retries on network failures
- Respects
Retry-Afterheaders - Maximum 3 attempts by default
- 5-second default retry delay
Request Timeout
Requests timeout after 10 seconds by default:Rate Limiting
Handles HTTP 429 (Too Many Requests) automatically:Constants
Privacy Considerations
The metrics library is designed with privacy in mind:
- No personally identifiable information (PII) is tracked
- User IDs are hashed
- IP addresses are not logged
- All metrics are aggregated
What is NOT tracked:
- Email content
- Email addresses
- Personal data
- Precise location data
- Identifying information
What IS tracked:
- Performance metrics
- Feature usage (aggregated)
- Error rates
- Application health
- Anonymous usage patterns
Development
Generating Metrics
Generate metric types from JSON schemas:Metrics Registry
Metrics are defined in a centralized schema registry:Testing
Mocking in Tests
Production Usage
Disable Metrics
Environment-Based Configuration
Network Priority
Metrics requests use low network priority to avoid impacting user experience:Error Handling
TypeScript Support
Full TypeScript support with generated types:Dependencies
Runtime Dependencies
Runtime Dependencies
@proton/shared- Shared utilities and constants
Build Dependencies
Build Dependencies
json-schema-to-typescript- Type generationtypescript- TypeScript compilerprettier- Code formatting
Best Practices
Metrics Best Practices
- Don’t over-track: Only track what you need
- Batch efficiently: Use default batching settings
- Handle failures gracefully: Metrics should never break your app
- Respect privacy: Never track PII
- Use type-safe metrics: Leverage TypeScript types
- Test metrics tracking: Mock metrics in tests
Performance Impact
The metrics library is designed for minimal performance impact:- Metrics are queued in memory (minimal overhead)
- Network requests are batched
- Requests use low priority
- Non-blocking operations
- Automatic retry without UI impact
Related Packages
@proton/shared
Shared utilities and constants
@proton/drive
Drive SDK with metrics integration
@proton/components
Components with built-in metrics