pub fn init(spin_version: String) -> Result<ShutdownGuard>
Expand description
Initializes telemetry for Spin using the tracing library.
Under the hood this involves initializing a tracing::Subscriber with multiple Layers. One Layer emits tracing events to stderr, another sends spans to an OTel collector, and another sends metrics to an OTel collector.
Configuration for the OTel layers is pulled from the environment.
Examples of emitting traces from Spin:
#[instrument(name = "span_name", err(level = Level::INFO), fields(otel.name = "dynamically set name"))]
fn func_you_want_to_trace() -> anyhow::Result<String> {
Ok("Hello, world!".to_string())
}
Some notes on tracing:
- If you don’t want the span to be collected by default emit it at a trace or debug level.
- Make sure you
.in_current_span()
any spawned tasks so the span context is propagated. - Use the otel.name attribute to dynamically set the span name.
- Use the err argument to have instrument automatically handle errors.
Examples of emitting metrics from Spin:
spin_telemetry::metrics::monotonic_counter!(spin.metric_name = 1, metric_attribute = "value");