flareapp-core 0.3.0

The core of the Flare client: config, the client, and the worker thread that sends data.
Documentation
//! The integration that owns the spans of the app, when the spans do not come from
//! the `tracing` integration of Flare.
//!
//! The OpenTelemetry bridge sets a [`SpanSource`]. The `tracing` integration then
//! makes no spans of its own, because two span pipelines send each trace twice. Log
//! lines take their trace from the span source, and the client gives each queued
//! report to the span source, which adds the report to the current span.

use std::sync::OnceLock;

use crate::client::QueuedReport;
use crate::log_line::TraceContext;

/// The functions of the integration that owns the spans. See
/// [`set_span_source`].
///
/// The Flare integrations use this type. This type is not part of the public API.
#[derive(Debug, Clone, Copy)]
pub struct SpanSource {
    /// Returns the trace and the span of the code that runs now, or `None` when
    /// the code runs outside a span.
    pub current: fn() -> Option<TraceContext>,
    /// Adds a queued report to the span of the code that runs now, as an
    /// `exception` span event. The client calls this function on the thread that
    /// made the report.
    pub record_report: fn(QueuedReport),
}

/// The span source, when an integration set one.
static SPAN_SOURCE: OnceLock<SpanSource> = OnceLock::new();

/// Sets the integration that owns the spans of the app. Only the first call has an
/// effect.
///
/// The Flare integrations use this function. This function is not part of the
/// public API.
pub fn set_span_source(source: SpanSource) {
    let _ = SPAN_SOURCE.set(source);
}

/// Returns the span source, or `None` when no integration set one. Without a
/// span source, the `tracing` integration of Flare owns the spans.
///
/// The Flare integrations use this function. This function is not part of the
/// public API.
pub fn span_source() -> Option<&'static SpanSource> {
    SPAN_SOURCE.get()
}