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 http;
25pub mod log;
26pub mod tracing;
27
28pub use encore_sdk_core::telemetry::Tracer;
29
30/// Encore's tracer provider, an OpenTelemetry
31/// [`TracerProvider`](crate::opentelemetry::trace::TracerProvider) recording
32/// into the runtime's telemetry pipeline. Cheap to clone.
33#[derive(Debug, Clone)]
34pub struct TracerProvider(Pipeline);
35
36impl crate::opentelemetry::trace::TracerProvider for TracerProvider {
37 type Tracer = Tracer;
38
39 fn tracer_with_scope(&self, scope: InstrumentationScope) -> Tracer {
40 self.0.tracer_with_scope(scope)
41 }
42}
43
44/// Encore's tracer provider.
45pub fn tracer_provider() -> TracerProvider {
46 TracerProvider(crate::runtime::core().telemetry().clone())
47}
48
49/// A [`Tracer`] recording into Encore's pipeline with instrumentation scope
50/// `name`.
51pub fn tracer(name: impl Into<Cow<'static, str>>) -> Tracer {
52 tracer_provider().tracer_with_scope(InstrumentationScope::builder(name).build())
53}
54
55/// Parses a W3C `traceparent` header value and an optional `tracestate` into
56/// a remote span context. `None` for malformed input.
57pub fn parse_traceparent(traceparent: &str, tracestate: Option<&str>) -> Option<SpanContext> {
58 SpanContext::parse_traceparent(traceparent, tracestate)
59}