//! Datadog tracing initialization.

use crate::config::LogFormat;
use crate::tracing::layers::datadog::datadog_layer_with_log_filter;
use opentelemetry_datadog::DatadogPropagator;
use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt};

use super::TracingShutdownHandle;

pub(crate) const DEFAULT_DATADOG_AGENT_ENDPOINT: &str = "http://localhost:8126";

/// Initialize Datadog tracing with the given configuration.
///
/// This sets up both logging (with Datadog trace correlation) and span export to the Datadog Agent.
pub(crate) fn init(
    endpoint: Option<&str>,
    service_name: &str,
    log_format: LogFormat,
    log_level: &str,
) -> TracingShutdownHandle {
    opentelemetry::global::set_text_map_propagator(DatadogPropagator::new());

    let endpoint = endpoint.unwrap_or(DEFAULT_DATADOG_AGENT_ENDPOINT);

    let (dd_layer, provider) = datadog_layer_with_log_filter(
        service_name,
        endpoint,
        log_format,
        log_level,
    );
    tracing_subscriber::registry().with(dd_layer).init();

    TracingShutdownHandle::new(provider)
}
