bevy_fleet 0.1.0

bevy swarm diagnostic, event, metric, and telemetry client
Documentation
use bevy::prelude::*;
use serde::{Deserialize, Serialize};

/// Configuration options specific to the OTLP exporter.
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct OtlpConfig {
    /// Whether to prefer HTTP/JSON when communicating with the collector.
    ///
    /// The exporter still sends Protobuf-encoded payloads; this flag only
    /// changes the advertised `Content-Type`.
    pub use_http_json: bool,

    /// Additional resource attributes that will be attached to every export.
    pub resource_attributes: std::collections::HashMap<String, String>,
}

impl Default for OtlpConfig {
    fn default() -> Self {
        Self {
            use_http_json: true,
            resource_attributes: std::collections::HashMap::new(),
        }
    }
}

/// Configuration for the Fleet plugin.
#[derive(Clone, Debug, Resource, Serialize, Deserialize)]
pub struct FleetConfig {
    /// Unique identifier for this application instance.
    pub app_id: String,

    /// Application name (used in OTLP as `service.name`).
    pub app_name: String,

    /// Application version (defaults to the git version when the `git-version` feature is enabled).
    pub app_version: String,

    /// OTLP collector endpoint.
    pub aggregation_url: String,

    /// Whether the plugin is enabled.
    pub enabled: bool,

    /// Interval in seconds between telemetry publishing.
    pub publish_interval_secs: u64,

    /// Maximum number of queued telemetry payloads.
    pub max_queue_size: usize,

    /// OTLP exporter tuning.
    pub otlp_config: OtlpConfig,
}

impl Default for FleetConfig {
    fn default() -> Self {
        Self {
            app_id: "bevy-app".to_string(),
            app_name: "bevy-fleet".to_string(),
            app_version: get_default_app_version(),
            aggregation_url: "http://localhost:4318/v1/metrics".to_string(),
            enabled: true,
            publish_interval_secs: 60,
            max_queue_size: 100,
            otlp_config: OtlpConfig::default(),
        }
    }
}

/// Gets the default app version (from git-version if available)
fn get_default_app_version() -> String {
    #[cfg(feature = "git-version")]
    {
        git_version::git_version!(args = ["--always", "--dirty=-modified"], fallback = "unknown")
            .to_string()
    }

    #[cfg(not(feature = "git-version"))]
    {
        "unknown".to_string()
    }
}

/// The Fleet plugin that can be added to a Bevy app
#[derive(Clone, Default)]
pub struct FleetPlugin {
    pub config: FleetConfig,
}