Skip to content

Commit 1994ace

Browse files
wip: work in progress
1 parent c6e1368 commit 1994ace

File tree

8 files changed

+764
-64
lines changed

8 files changed

+764
-64
lines changed

Cargo.lock

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

codegen/src/shuttle_main.rs

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,23 +12,27 @@ pub(crate) fn tokens(_attr: TokenStream, item: TokenStream) -> TokenStream {
1212
let mut user_main_fn = parse_macro_input!(item as ItemFn);
1313
let loader_runner = LoaderAndRunner::from_item_fn(&mut user_main_fn);
1414

15-
quote! {
15+
Into::into(quote! {
1616
fn main() {
1717
// manual expansion of #[tokio::main]
1818
::shuttle_runtime::tokio::runtime::Builder::new_multi_thread()
1919
.enable_all()
2020
.build()
2121
.unwrap()
2222
.block_on(async {
23-
::shuttle_runtime::__internals::start(__loader, __runner).await;
24-
})
23+
::shuttle_runtime::__internals::start(
24+
__loader,
25+
__runner,
26+
env!("CARGO_CRATE_NAME"),
27+
env!("CARGO_PKG_VERSION"),
28+
).await;
29+
})
2530
}
2631

2732
#loader_runner
2833

2934
#user_main_fn
30-
}
31-
.into()
35+
})
3236
}
3337

3438
struct LoaderAndRunner {
@@ -358,7 +362,7 @@ mod tests {
358362
assert_eq!(actual.fn_inputs, expected_inputs);
359363

360364
// Make sure attributes was removed from input
361-
if let syn::FnArg::Typed(param) = input.sig.inputs.first().unwrap() {
365+
if let FnArg::Typed(param) = input.sig.inputs.first().unwrap() {
362366
assert!(
363367
param.attrs.is_empty(),
364368
"some attributes were not removed: {:?}",

runtime/Cargo.toml

Lines changed: 45 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,48 @@ shuttle-service = { workspace = true }
1919
anyhow = { workspace = true }
2020
async-trait = { workspace = true }
2121
hyper = { workspace = true, features = ["http1", "server", "tcp"] }
22+
log = { version = "0.4", optional = true, default-features = false }
23+
opentelemetry = { version = "0.27", optional = true, default-features = false, features = ["logs", "metrics", "trace", "tracing"] }
24+
opentelemetry-otlp = { version = "0.27", optional = true, default-features = false, features = [
25+
"http-proto",
26+
"logs",
27+
"metrics",
28+
"reqwest-client",
29+
"reqwest-rustls",
30+
"trace",
31+
] }
32+
opentelemetry_sdk = { version = "0.27", optional = true, default-features = false, features = [
33+
"http",
34+
"logs",
35+
"metrics",
36+
"rt-tokio",
37+
"trace",
38+
"spec_unstable_logs_enabled",
39+
] }
40+
opentelemetry-semantic-conventions = { version = "0.27", optional = true, default-features = false, features = ["semconv_experimental"] }
2241
serde = { workspace = true }
2342
serde_json = { workspace = true }
2443
strfmt = { workspace = true }
2544
tokio = { workspace = true, features = ["full"] }
2645
tokio-util = { workspace = true }
2746
tokio-stream = { workspace = true }
2847
tonic = { workspace = true }
29-
tracing = { workspace = true }
30-
tracing-subscriber = { workspace = true, optional = true }
48+
tracing = { workspace = true, optional = true, features = ["attributes", "std"] }
49+
tracing-core = { version = "0.1", optional = true, default-features = false, features = ["std"] }
50+
tracing-log = { version = "0.2", optional = true, default-features = false, features = ["log-tracer", "std"] }
51+
tracing-opentelemetry = { version = "0.28.0", optional = true, default-features = false, features = ["metrics"] }
52+
tracing-subscriber = { workspace = true, optional = true, features = [
53+
"alloc",
54+
"env-filter",
55+
"fmt",
56+
"parking_lot",
57+
"registry",
58+
"smallvec",
59+
"std",
60+
"tracing",
61+
"tracing-log",
62+
"tracing-serde",
63+
] }
3164

3265
[dev-dependencies]
3366
portpicker = { workspace = true }
@@ -40,6 +73,14 @@ default = ["setup-tracing"]
4073
api-client-tracing = ["shuttle-api-client/tracing"]
4174

4275
setup-tracing = [
43-
"tracing-subscriber/default",
44-
"tracing-subscriber/env-filter",
76+
"dep:log",
77+
"dep:opentelemetry",
78+
"dep:opentelemetry-otlp",
79+
"dep:opentelemetry_sdk",
80+
"dep:opentelemetry-semantic-conventions",
81+
"dep:tracing",
82+
"dep:tracing-core",
83+
"dep:tracing-log",
84+
"dep:tracing-opentelemetry",
85+
"dep:tracing-subscriber",
4586
]

runtime/src/alpha.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ where
118118
}
119119
},
120120
Err(error) => {
121-
if error.is_panic() {
121+
return if error.is_panic() {
122122
let panic = error.into_panic();
123123
let msg = match panic.downcast_ref::<String>() {
124124
Some(msg) => msg.to_string(),
@@ -128,18 +128,18 @@ where
128128
},
129129
};
130130
println!("loading service panicked: {msg}");
131-
return Ok(Response::new(LoadResponse {
131+
Ok(Response::new(LoadResponse {
132132
success: false,
133133
message: msg,
134134
resources: vec![],
135-
}));
135+
}))
136136
} else {
137137
println!("loading service crashed: {error:#}");
138-
return Ok(Response::new(LoadResponse {
138+
Ok(Response::new(LoadResponse {
139139
success: false,
140140
message: error.to_string(),
141141
resources: vec![],
142-
}));
142+
}))
143143
}
144144
}
145145
};
@@ -214,7 +214,7 @@ where
214214

215215
println!("Starting on {service_address}");
216216

217-
let (kill_tx, kill_rx) = tokio::sync::oneshot::channel();
217+
let (kill_tx, kill_rx) = oneshot::channel();
218218
*self.kill_tx.lock().unwrap() = Some(kill_tx);
219219

220220
let handle = tokio::runtime::Handle::current();
@@ -296,7 +296,7 @@ where
296296
} else {
297297
println!("failed to stop deployment");
298298

299-
Ok(tonic::Response::new(StopResponse { success: false }))
299+
Ok(Response::new(StopResponse { success: false }))
300300
}
301301
}
302302

@@ -324,7 +324,7 @@ where
324324
Ok(Response::new(ReceiverStream::new(rx)))
325325
}
326326

327-
async fn version(&self, _requset: Request<Ping>) -> Result<Response<VersionInfo>, Status> {
327+
async fn version(&self, _request: Request<Ping>) -> Result<Response<VersionInfo>, Status> {
328328
Ok(Response::new(VersionInfo {
329329
version: crate::VERSION_STRING.to_owned(),
330330
}))

runtime/src/lib.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ mod plugins;
1212
mod rt;
1313
mod start;
1414

15+
#[cfg(feature = "setup-tracing")]
16+
mod trace;
17+
1518
// Public API
1619
pub use plugins::{Metadata, Secrets};
1720
pub use shuttle_codegen::main;
@@ -51,7 +54,7 @@ pub mod __internals {
5154
O: Future<Output = Result<Vec<Vec<u8>>, Error>> + Send,
5255
{
5356
async fn load(self, factory: ResourceFactory) -> Result<Vec<Vec<u8>>, Error> {
54-
(self)(factory).await
57+
self(factory).await
5558
}
5659
}
5760

@@ -72,7 +75,7 @@ pub mod __internals {
7275
type Service = S;
7376

7477
async fn run(self, resources: Vec<Vec<u8>>) -> Result<Self::Service, Error> {
75-
(self)(resources).await
78+
self(resources).await
7679
}
7780
}
7881
}

runtime/src/rt.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ pub async fn start(loader: impl Loader + Send + 'static, runner: impl Runner + S
9393
// light hyper server
9494
let make_service = make_service_fn(|_conn| async {
9595
Ok::<_, Infallible>(service_fn(|_req| async move {
96-
trace!("Receivied health check");
96+
trace!("Received health check");
9797
// TODO: A hook into the `Service` trait can be added here
9898
trace!("Responding to health check");
9999
Result::<Response<Body>, hyper::Error>::Ok(Response::new(Body::empty()))
@@ -130,7 +130,7 @@ pub async fn start(loader: impl Loader + Send + 'static, runner: impl Runner + S
130130
// Sort secrets by key
131131
let secrets = BTreeMap::from_iter(secrets.into_iter().map(|(k, v)| (k, Secret::new(v))));
132132

133-
// TODO: rework resourcefactory
133+
// TODO: rework `ResourceFactory`
134134
let factory = ResourceFactory::new(project_name, secrets.clone(), env);
135135
let mut resources = match loader.load(factory).await {
136136
Ok(r) => r,

runtime/src/start.rs

Lines changed: 16 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,12 @@ impl Args {
4848
}
4949
}
5050

51-
pub async fn start(loader: impl Loader + Send + 'static, runner: impl Runner + Send + 'static) {
51+
pub async fn start(
52+
loader: impl Loader + Send + 'static,
53+
runner: impl Runner + Send + 'static,
54+
#[cfg_attr(not(feature = "setup-tracing"), allow(unused_variables))] project_name: &'static str,
55+
project_version: &'static str,
56+
) {
5257
// `--version` overrides any other arguments. Used by cargo-shuttle to check compatibility on local runs.
5358
if std::env::args().any(|arg| arg == "--version") {
5459
println!("{}", crate::VERSION_STRING);
@@ -70,32 +75,17 @@ pub async fn start(loader: impl Loader + Send + 'static, runner: impl Runner + S
7075

7176
// this is handled after arg parsing to not interfere with --version above
7277
#[cfg(feature = "setup-tracing")]
73-
{
74-
use tracing_subscriber::{fmt, prelude::*, registry, EnvFilter};
75-
registry()
76-
.with(fmt::layer().without_time())
77-
.with(
78-
// let user override RUST_LOG in local run if they want to
79-
EnvFilter::try_from_default_env()
80-
// otherwise use our default
81-
.or_else(|_| {
82-
EnvFilter::try_new(if args.beta {
83-
"info"
84-
} else {
85-
"info,shuttle=trace"
86-
})
87-
})
88-
.unwrap(),
89-
)
90-
.init();
78+
let _guard = crate::trace::init_tracing_subscriber(project_name, project_version);
9179

92-
if args.beta {
93-
tracing::warn!(
94-
"Default tracing subscriber initialized (https://docs.shuttle.dev/docs/logs)"
95-
);
96-
} else {
97-
tracing::warn!("Default tracing subscriber initialized (https://docs.shuttle.rs/configuration/logs)");
98-
}
80+
#[cfg(feature = "setup-tracing")]
81+
if args.beta {
82+
eprintln!(
83+
"INFO - Default tracing subscriber initialized (https://docs.shuttle.dev/docs/logs)"
84+
);
85+
} else {
86+
eprintln!(
87+
"INFO - Default tracing subscriber initialized (https://docs.shuttle.rs/configuration/logs)"
88+
);
9989
}
10090

10191
if args.beta {

0 commit comments

Comments
 (0)