Skip to content

Commit 7973639

Browse files
committed
example: tracing example using is_container_run() gate
A new examples/tracing crate showing the cookbook recipe end-to-end: a tracing_subscriber installed in main() only when is_container_run() returns true. Two containers (greet, shout) emit tracing::info! that flow through the subscriber when dispatched in-pod. Demonstrates both paths verbatim: - cargo run -p cargo-athena-example-tracing -> emits YAML, no tracing setup runs - CARGO_ATHENA_TEMPLATE=...-greet cargo run -p ... -- '"athena"' -> runs greet in-process; INFO greeting{name=athena} appears Adds tracing 0.1 + tracing-subscriber 0.3 as direct deps on the example crate only (not the workspace).
1 parent 09397bc commit 7973639

4 files changed

Lines changed: 160 additions & 0 deletions

File tree

Cargo.lock

Lines changed: 87 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

examples/tracing/Cargo.toml

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
[package]
2+
name = "cargo-athena-example-tracing"
3+
edition.workspace = true
4+
version = "0.0.0"
5+
license.workspace = true
6+
publish = false
7+
8+
# Demonstrates `cargo_athena::is_container_run()`: gating in-pod-only
9+
# setup (here, a tracing subscriber) so it fires only when Argo
10+
# dispatches a container body in a pod -- not on every local
11+
# `cargo athena emit` / `ls` / `describe` / `submit` invocation.
12+
13+
[dependencies]
14+
cargo-athena = { workspace = true }
15+
tracing = "0.1"
16+
tracing-subscriber = { version = "0.3", features = ["env-filter"] }

examples/tracing/src/lib.rs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
//! Two containers that emit `tracing::info!` records. The subscriber
2+
//! is installed once in `main.rs`, gated on `is_container_run()` so
3+
//! it only fires in-pod.
4+
5+
use cargo_athena::{container, workflow};
6+
7+
#[workflow]
8+
pub fn pipeline() {
9+
let s = greet("athena".to_string());
10+
shout(s);
11+
}
12+
13+
#[container]
14+
pub fn greet(name: String) -> String {
15+
// These flow through the subscriber installed in `main.rs` only when
16+
// running in-pod. Locally via `cargo athena emit` they never fire,
17+
// because the body never executes.
18+
tracing::info!(name = %name, "greeting");
19+
format!("hello, {name}")
20+
}
21+
22+
#[container]
23+
pub fn shout(msg: String) {
24+
tracing::info!(msg = %msg, "shouting");
25+
println!("{}", msg.to_uppercase());
26+
}

examples/tracing/src/main.rs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
//! Demonstrates `is_container_run()`: gate one-time setup (a tracing
2+
//! subscriber) so it fires only when Argo is dispatching a container
3+
//! body in-pod -- not on every `cargo athena emit` / `ls` / `describe`
4+
//! / `submit` invocation, which would otherwise also spawn this
5+
//! binary to introspect templates.
6+
//!
7+
//! Try it both ways:
8+
//!
9+
//! cargo run -p cargo-athena-example-tracing
10+
//! -> emits the WorkflowTemplate YAML; NO tracing setup runs.
11+
//!
12+
//! CARGO_ATHENA_TEMPLATE=cargo-athena-example-tracing-greet \
13+
//! cargo run -p cargo-athena-example-tracing -- '"athena"'
14+
//! -> runs `greet` in-process; tracing IS initialized, you see
15+
//! the `INFO greeting{name=athena}` line.
16+
17+
fn main() {
18+
// Only initialize the subscriber in-pod. The returned guard (here
19+
// just `()`) drops at the end of main(); if your setup needs
20+
// flush-on-exit, return a real Drop type instead.
21+
let _gate = cargo_athena::is_container_run().then(|| {
22+
tracing_subscriber::fmt()
23+
.with_env_filter(
24+
tracing_subscriber::EnvFilter::try_from_default_env()
25+
.unwrap_or_else(|_| tracing_subscriber::EnvFilter::new("info")),
26+
)
27+
.init();
28+
});
29+
30+
cargo_athena::entrypoint!(cargo_athena_example_tracing::pipeline);
31+
}

0 commit comments

Comments
 (0)