API Reference
Complete API reference for telemetry-kit
Overview
This page provides a complete reference for the telemetry-kit API.
TelemetryKit
The main entry point for telemetry operations.
Builder Pattern
Builder Methods
service_name(name: impl Into<String>) -> Result<Self>
Set the service name (required). Must contain only lowercase alphanumeric characters, dashes, and underscores.
service_version(version: impl Into<String>) -> Self
Set the service version (optional, defaults to CARGO_PKG_VERSION).
db_path(path: impl Into<PathBuf>) -> Self
Set custom database path for event storage.
Default: ~/.telemetry-kit/<service-name>.db
Sync Configuration
with_sync_credentials(...) -> Result<Self>
Configure sync with telemetry-kit.dev using credentials.
sync(config: SyncConfig) -> Self
Configure sync with a custom SyncConfig.
Auto-Sync Configuration
auto_sync(enabled: bool) -> Self
Enable or disable automatic background syncing (default: enabled when sync is configured).
sync_interval(seconds: u64) -> Self
Set the auto-sync interval in seconds (default: 60).
sync_on_shutdown(enabled: bool) -> Self
Configure whether to sync on shutdown (default: true).
Event Tracking
track_command<F>(...) -> Result<()>
Track a command execution.
Parameters:
command: impl Into<String>- Command namebuilder_fn: F- Closure to configure the event
track_feature<F>(...) -> Result<()>
Track a feature usage.
Parameters:
feature: impl Into<String>- Feature namebuilder_fn: F- Closure to configure the event
track_custom(...) -> Result<()>
Track a custom event.
Parameters:
event_type: impl Into<String>- Event typedata: serde_json::Value- Event data
Statistics and Sync
stats() -> Result<EventStats>
Get statistics about buffered events.
sync() -> Result<()>
Manually trigger a sync (requires sync configuration).
cleanup() -> Result<usize>
Clean up old synced events (older than 7 days).
shutdown() -> Result<()>
Gracefully shutdown auto-sync task with optional final sync.
Event Builders
CommandEventBuilder
Builder for command execution events.
Methods:
flag(flag: impl Into<String>) -> Self- Add a flagarg(arg: impl Into<String>) -> Self- Add an argumentsuccess(success: bool) -> Self- Set success statusduration_ms(ms: u64) -> Self- Set duration in millisecondsexit_code(code: i32) -> Self- Set exit code
FeatureEventBuilder
Builder for feature usage events.
Methods:
method(method: impl Into<String>) -> Self- Set method/variantsuccess(success: bool) -> Self- Set success statusmetadata(key: impl Into<String>, value: impl Into<String>) -> Self- Add metadata
Types
EventStats
Statistics about buffered events.
SyncConfig
Configuration for sync protocol.
Fields:
org_id- Organization ID (required)app_id- Application ID (required)token- Authentication token (required)secret- HMAC secret key (required)endpoint- Custom endpoint URL (optional)batch_size- Events per batch (optional, default: 100)
AutoSyncConfig
Configuration for auto-sync behavior.
Error Handling
TelemetryError
Main error type for telemetry operations.
Variants:
InvalidConfig(String)- Invalid configurationStorage(String)- Storage/database errorSync(String)- Synchronization errorNetwork(String)- Network errorSerialization(String)- Serialization error
Feature Flags
sync
Enables synchronization features (enabled by default).
cli
Enables CLI tool compilation.
Examples
Complete Example
See Also
- Getting Started - Initial setup
- Auto-Sync - Background synchronization
- CLI Tool - Command-line interface
- Examples - Working code examples