Skip to main content

micromegas_main

Attribute Macro micromegas_main 

Source
#[micromegas_main]
Expand description

micromegas_main: Creates a tokio runtime with proper micromegas tracing callbacks and telemetry setup

This is a drop-in replacement for #[tokio::main] that automatically configures:

  • Tokio runtime with proper micromegas tracing thread lifecycle callbacks
  • Telemetry guard with sensible defaults (ctrl-c handling, info console level, overridable via MICROMEGAS_LOCAL_SINK_MAX_LEVEL)
  • Automatic authentication configuration from environment variables

§Authentication

The macro automatically configures telemetry authentication based on environment variables:

  • API Key: Set MICROMEGAS_INGESTION_API_KEY=your-key
  • OIDC Client Credentials: Set MICROMEGAS_OIDC_TOKEN_ENDPOINT, MICROMEGAS_OIDC_CLIENT_ID, MICROMEGAS_OIDC_CLIENT_SECRET
  • No auth: If no env vars are set, telemetry is sent unauthenticated (requires --disable-auth on ingestion server)

§Failure to initialize

The generated main builds the telemetry guard before the function body runs and panics if that fails – e.g. a malformed MICROMEGAS_PROCESS_PROPERTIES – printing anyhow’s full context chain and exiting non-zero rather than continuing with telemetry silently off. A missing telemetry URL or unset auth env is not a failure: the sink is simply omitted.

§Parameters

  • ctrlc_handling: bool (default: true) — enable Ctrl-C graceful shutdown
  • install_log_capture: bool (default: false) — capture log crate output
  • interop_max_level: string (e.g., "info") — interop max level override
  • local_sink_enabled: bool (default: true) — enable local stdout sink
  • local_sink_max_level: string (default: "info") — max level for local sink; overridable at runtime via MICROMEGAS_LOCAL_SINK_MAX_LEVEL when the attribute is absent
  • max_level_override: string (e.g., "warn") — global max level override
  • system_metrics: bool (default: true) — collect system metrics
  • telemetry_url: string — override the telemetry ingestion URL
  • api_key: string — embed a literal API key (takes precedence over env-var auth)

§Examples

ⓘ
use micromegas::tracing::prelude::*;

#[micromegas_main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    info!("Server starting - telemetry already configured!");
    Ok(())
}

#[micromegas_main(interop_max_level = "info")]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    info!("Server starting with info interop level!");
    Ok(())
}

#[micromegas_main(max_level_override = "warn", interop_max_level = "info")]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    info!("Server starting with both level overrides!");
    Ok(())
}

#[micromegas_main(telemetry_url = "http://localhost:9000", api_key = "my-secret-key")]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    info!("Server with explicit URL and embedded API key!");
    Ok(())
}