Skip to main content

encore/
telemetry.rs

1//! Application-owned telemetry APIs.
2//!
3//! Use [`tracing`] to compose Encore's tracing layers, or [`log`] to install
4//! Encore's direct logger for the standard log facade. These are separate
5//! logging paths; choose one. The tracing bridge forwards `log` records to the
6//! tracing subscriber; it does not forward tracing events to `log`.
7//!
8//! Metrics remain SDK-owned; pass [`crate::metrics::meter_provider`] directly
9//! to instrumentation libraries. The SDK does not enable `tracing/log`.
10//!
11//! Entered tracing spans activate their OpenTelemetry context for Encore
12//! resources and other OTel instrumentation, restoring the prior context on
13//! exit. Use `tracing::Instrument` for futures; never hold an enter guard over
14//! an await. Explicit tracing parents and `OpenTelemetrySpanExt::set_parent`
15//! are supported. Native OTel contexts nested inside a tracing span take
16//! precedence until detached.
17
18use std::borrow::Cow;
19
20use encore_sdk_core::telemetry::opentelemetry::trace::TracerProvider as _;
21use encore_sdk_core::telemetry::opentelemetry::InstrumentationScope;
22use encore_sdk_core::telemetry::{Pipeline, SpanContext, SpanContextExt};
23
24pub mod log;
25pub mod tracing;
26
27pub use encore_sdk_core::telemetry::Tracer;
28
29/// Encore's tracer provider, an OpenTelemetry
30/// [`TracerProvider`](crate::opentelemetry::trace::TracerProvider) recording
31/// into the runtime's telemetry pipeline. Cheap to clone.
32#[derive(Debug, Clone)]
33pub struct TracerProvider(Pipeline);
34
35impl crate::opentelemetry::trace::TracerProvider for TracerProvider {
36    type Tracer = Tracer;
37
38    fn tracer_with_scope(&self, scope: InstrumentationScope) -> Tracer {
39        self.0.tracer_with_scope(scope)
40    }
41}
42
43/// Encore's tracer provider.
44pub fn tracer_provider() -> TracerProvider {
45    TracerProvider(crate::runtime::core().telemetry().clone())
46}
47
48/// A [`Tracer`] recording into Encore's pipeline with instrumentation scope
49/// `name`.
50pub fn tracer(name: impl Into<Cow<'static, str>>) -> Tracer {
51    tracer_provider().tracer_with_scope(InstrumentationScope::builder(name).build())
52}
53
54/// Parses a W3C `traceparent` header value and an optional `tracestate` into
55/// a remote span context. `None` for malformed input.
56pub fn parse_traceparent(traceparent: &str, tracestate: Option<&str>) -> Option<SpanContext> {
57    SpanContext::parse_traceparent(traceparent, tracestate)
58}