Skip to content

Commit 11c9314

Browse files
committed
Use global tracer provider
Signed-off-by: Caleb Schoepp <caleb.schoepp@fermyon.com>
1 parent 5fddcff commit 11c9314

File tree

3 files changed

+20
-20
lines changed

3 files changed

+20
-20
lines changed

crates/factor-observe/src/host.rs

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ use opentelemetry::trace::Tracer;
55
use opentelemetry::Context;
66
use spin_core::async_trait;
77
use spin_core::wasmtime::component::Resource;
8-
use spin_telemetry::traces::WASI_OBSERVE_TRACER;
98
use spin_world::wasi::clocks0_2_0::wall_clock::Datetime;
109
use spin_world::wasi::observe::traces::{self, KeyValue, Span as WitSpan};
1110
use tracing_opentelemetry::OpenTelemetrySpanExt;
@@ -36,12 +35,7 @@ impl traces::HostSpan for InstanceState {
3635
};
3736

3837
// Create the underlying opentelemetry span
39-
let otel_span = WASI_OBSERVE_TRACER
40-
.lock()
41-
.unwrap()
42-
.clone()
43-
.unwrap()
44-
.start_with_context(name, &parent_context);
38+
let otel_span = self.tracer.start_with_context(name, &parent_context);
4539

4640
let span_id = otel_span.span_context().span_id().to_string();
4741

crates/factor-observe/src/lib.rs

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,11 @@ mod host;
33
use std::sync::{Arc, RwLock};
44

55
use indexmap::IndexMap;
6-
use opentelemetry::{global::ObjectSafeSpan, trace::TraceContextExt, Context};
6+
use opentelemetry::{
7+
global::{self, BoxedTracer, ObjectSafeSpan},
8+
trace::TraceContextExt,
9+
Context,
10+
};
711
use spin_factors::{Factor, SelfInstanceBuilder};
812
use tracing_opentelemetry::OpenTelemetrySpanExt;
913

@@ -32,14 +36,18 @@ impl Factor for ObserveFactor {
3236

3337
fn prepare<T: spin_factors::RuntimeFactors>(
3438
&self,
35-
_ctx: spin_factors::PrepareContext<Self>,
39+
ctx: spin_factors::PrepareContext<Self>,
3640
_builders: &mut spin_factors::InstanceBuilders<T>,
3741
) -> anyhow::Result<Self::InstanceBuilder> {
42+
// TODO(Lann): Is it fine that we're using BoxedTracer and BoxedSpan? Are there perf tradeoffs?
43+
// TODO(Lann): Would it make sense to put the tracer on the app state and just hold a reference to it in the instance state?
44+
let tracer = global::tracer(ctx.app_component().app.id().to_string());
3845
Ok(InstanceState {
3946
state: Arc::new(RwLock::new(State {
4047
guest_spans: table::Table::new(1024),
4148
active_spans: Default::default(),
4249
})),
50+
tracer,
4351
})
4452
}
4553
}
@@ -52,6 +60,7 @@ impl ObserveFactor {
5260

5361
pub struct InstanceState {
5462
pub(crate) state: Arc<RwLock<State>>,
63+
pub(crate) tracer: BoxedTracer,
5564
}
5665

5766
impl SelfInstanceBuilder for InstanceState {}
@@ -65,7 +74,10 @@ impl InstanceState {
6574
}
6675
}
6776

68-
/// Internal state of the observe factor.
77+
/// Internal state of the ObserveFactor instance state.
78+
///
79+
/// This data lives here rather than directly on InstanceState so that we can have multiple things
80+
/// take Arc references to it.
6981
pub(crate) struct State {
7082
/// A resource table that holds the guest spans.
7183
pub guest_spans: table::Table<GuestSpan>,
@@ -78,10 +90,10 @@ pub(crate) struct State {
7890
pub active_spans: IndexMap<String, u32>,
7991
}
8092

81-
/// The WIT resource Span. Effectively wraps an [opentelemetry_sdk::trace::Span].
93+
/// The WIT resource Span. Effectively wraps an [opentelemetry::global::BoxedSpan].
8294
pub struct GuestSpan {
83-
/// The [opentelemetry_sdk::trace::Span] we use to do the actual tracing work.
84-
pub inner: opentelemetry_sdk::trace::Span,
95+
/// The [opentelemetry::global::BoxedSpan] we use to do the actual tracing work.
96+
pub inner: opentelemetry::global::BoxedSpan,
8597
}
8698

8799
/// TODO comment

crates/telemetry/src/traces.rs

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
1-
use std::{sync::Mutex, time::Duration};
1+
use std::time::Duration;
22

33
use anyhow::bail;
44
use opentelemetry_otlp::SpanExporterBuilder;
55
use opentelemetry_sdk::{
66
resource::{EnvResourceDetector, TelemetryResourceDetector},
7-
trace::Tracer,
87
Resource,
98
};
109
use tracing::Subscriber;
@@ -13,9 +12,6 @@ use tracing_subscriber::{registry::LookupSpan, EnvFilter, Layer};
1312
use crate::detector::SpinResourceDetector;
1413
use crate::env::OtlpProtocol;
1514

16-
// TODO(Caleb): I think there is probably a better way to do this than just a global tracer. Maybe I should make telemetry a factor?
17-
pub static WASI_OBSERVE_TRACER: Mutex<Option<Tracer>> = Mutex::new(None);
18-
1915
/// Constructs a layer for the tracing subscriber that sends spans to an OTEL collector.
2016
///
2117
/// It pulls OTEL configuration from the environment based on the variables defined
@@ -53,8 +49,6 @@ pub(crate) fn otel_tracing_layer<S: Subscriber + for<'span> LookupSpan<'span>>(
5349
.with_trace_config(opentelemetry_sdk::trace::config().with_resource(resource))
5450
.install_batch(opentelemetry_sdk::runtime::Tokio)?;
5551

56-
*WASI_OBSERVE_TRACER.lock().unwrap() = Some(tracer.clone());
57-
5852
let env_filter = match EnvFilter::try_from_env("SPIN_OTEL_TRACING_LEVEL") {
5953
Ok(filter) => filter,
6054
// If it isn't set or it fails to parse default to info

0 commit comments

Comments
 (0)