encore-dev 2.0.0-beta.9

Rust SDK for Encore applications
Documentation
//! Application-owned telemetry APIs.
//!
//! Use [`tracing`] to compose Encore's tracing layers, or [`log`] to install
//! Encore's direct logger for the standard log facade. These are separate
//! logging paths; choose one. The tracing bridge forwards `log` records to the
//! tracing subscriber; it does not forward tracing events to `log`.
//!
//! Metrics remain SDK-owned; pass [`crate::metrics::meter_provider`] directly
//! to instrumentation libraries. The SDK does not enable `tracing/log`.
//!
//! Entered tracing spans activate their OpenTelemetry context for Encore
//! resources and other OTel instrumentation, restoring the prior context on
//! exit. Use `tracing::Instrument` for futures; never hold an enter guard over
//! an await. Explicit tracing parents and `OpenTelemetrySpanExt::set_parent`
//! are supported. Native OTel contexts nested inside a tracing span take
//! precedence until detached.

use std::borrow::Cow;

use encore_sdk_core::telemetry::opentelemetry::trace::TracerProvider as _;
use encore_sdk_core::telemetry::opentelemetry::InstrumentationScope;
use encore_sdk_core::telemetry::{Pipeline, SpanContext, SpanContextExt};

pub mod log;
pub mod tracing;

pub use encore_sdk_core::telemetry::Tracer;

/// Encore's tracer provider, an OpenTelemetry
/// [`TracerProvider`](crate::opentelemetry::trace::TracerProvider) recording
/// into the runtime's telemetry pipeline. Cheap to clone.
#[derive(Debug, Clone)]
pub struct TracerProvider(Pipeline);

impl crate::opentelemetry::trace::TracerProvider for TracerProvider {
    type Tracer = Tracer;

    fn tracer_with_scope(&self, scope: InstrumentationScope) -> Tracer {
        self.0.tracer_with_scope(scope)
    }
}

/// Encore's tracer provider.
pub fn tracer_provider() -> TracerProvider {
    TracerProvider(crate::runtime::core().telemetry().clone())
}

/// A [`Tracer`] recording into Encore's pipeline with instrumentation scope
/// `name`.
pub fn tracer(name: impl Into<Cow<'static, str>>) -> Tracer {
    tracer_provider().tracer_with_scope(InstrumentationScope::builder(name).build())
}

/// Parses a W3C `traceparent` header value and an optional `tracestate` into
/// a remote span context. `None` for malformed input.
pub fn parse_traceparent(traceparent: &str, tracestate: Option<&str>) -> Option<SpanContext> {
    SpanContext::parse_traceparent(traceparent, tracestate)
}