Skip to content

Commit 32715a3

Browse files
committed
Use tracing plumbing to manage warnings
Signed-off-by: itowlson <[email protected]>
1 parent 8448830 commit 32715a3

File tree

3 files changed

+47
-2
lines changed

3 files changed

+47
-2
lines changed

crates/loader/src/local.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -493,10 +493,10 @@ impl LocalLoader {
493493
}
494494

495495
if glob_or_path == "*" {
496-
terminal::warn!("A component is including the entire application directory as asset files. This is unlikely to be what you want.\nIf this is what you want, use the pattern \"./*\" to avoid this warning.\nLearn more: https://spinframework.dev/writing-apps#including-files-with-components\n");
496+
tracing::warn!(alert_in_dev = true, "A component is including the entire application directory as asset files. This is unlikely to be what you want.\nIf this is what you want, use the pattern \"./*\" to avoid this warning.\nLearn more: https://spinframework.dev/writing-apps#including-files-with-components\n");
497497
}
498498
if glob_or_path == "**/*" {
499-
terminal::warn!("A component is including the entire application directory tree as asset files. This is unlikely to be what you want.\nIf this is what you want, use the pattern \"./**/*\" to avoid this warning.\nLearn more: https://spinframework.dev/writing-apps#including-files-with-components\n");
499+
tracing::warn!(alert_in_dev = true, "A component is including the entire application directory tree as asset files. This is unlikely to be what you want.\nIf this is what you want, use the pattern \"./**/*\" to avoid this warning.\nLearn more: https://spinframework.dev/writing-apps#including-files-with-components\n");
500500
}
501501

502502
let path = self.app_root.join(glob_or_path);

crates/telemetry/src/alert_in_dev.rs

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
//! Provides a way for warnings to be force-raised in the
2+
//! `spin up` environment even if RUST_LOG is not set to warn.
3+
//! This is useful for things that are not errors but where we
4+
//! want application developers to know they have a problem.
5+
6+
use tracing::{Event, Subscriber};
7+
use tracing_subscriber::{filter::filter_fn, registry::LookupSpan, Layer};
8+
9+
const ALERT_IN_DEV_TAG: &str = "alert_in_dev";
10+
11+
/// A layer which prints a terminal warning (using [terminal::warn!]) if
12+
/// a trace event contains the tag "alert_in_dev" (with any value).
13+
pub(crate) fn alert_in_dev_layer<S: Subscriber + for<'span> LookupSpan<'span> + 'static>(
14+
) -> impl Layer<S> {
15+
CommandLineAlertingLayer.with_filter(filter_fn(|meta| {
16+
meta.fields().field(ALERT_IN_DEV_TAG).is_some()
17+
}))
18+
}
19+
20+
pub struct CommandLineAlertingLayer;
21+
22+
impl<S: Subscriber> Layer<S> for CommandLineAlertingLayer {
23+
fn on_event(&self, event: &Event<'_>, _ctx: tracing_subscriber::layer::Context<'_, S>) {
24+
warn(event);
25+
}
26+
}
27+
28+
fn warn(event: &Event<'_>) {
29+
let mut visitor = PrintMessageAsWarning;
30+
event.record(&mut visitor);
31+
}
32+
33+
struct PrintMessageAsWarning;
34+
35+
impl tracing::field::Visit for PrintMessageAsWarning {
36+
fn record_debug(&mut self, field: &tracing::field::Field, value: &dyn std::fmt::Debug) {
37+
if field.name() == "message" {
38+
terminal::warn!("{value:?}");
39+
}
40+
}
41+
}

crates/telemetry/src/lib.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ use env::otel_tracing_enabled;
77
use opentelemetry_sdk::propagation::TraceContextPropagator;
88
use tracing_subscriber::{fmt, prelude::*, registry, EnvFilter, Layer};
99

10+
mod alert_in_dev;
1011
pub mod detector;
1112
mod env;
1213
pub mod logs;
@@ -83,11 +84,14 @@ pub fn init(spin_version: String) -> anyhow::Result<()> {
8384
None
8485
};
8586

87+
let alert_in_dev_layer = alert_in_dev::alert_in_dev_layer();
88+
8689
// Build a registry subscriber with the layers we want to use.
8790
registry()
8891
.with(otel_tracing_layer)
8992
.with(otel_metrics_layer)
9093
.with(fmt_layer)
94+
.with(alert_in_dev_layer)
9195
.init();
9296

9397
// Used to propagate trace information in the standard W3C TraceContext format. Even if the otel

0 commit comments

Comments
 (0)