1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
use axum::{extract::Extension, http::header::HeaderName, Router};
use clap::{Args, ValueHint};
use miette::Result;
use opentelemetry::{
global,
sdk::{export::trace::stdout, trace, trace::Tracer},
};
use opentelemetry_aws::trace::XrayPropagator;
use std::{net::SocketAddr, path::PathBuf};
use tokio::time::Duration;
use tokio_graceful_shutdown::{SubsystemHandle, Toplevel};
use tower_http::{
catch_panic::CatchPanicLayer,
request_id::{MakeRequestUuid, PropagateRequestIdLayer, SetRequestIdLayer},
trace::TraceLayer,
};
use tracing::{info, Subscriber};
use tracing_opentelemetry::OpenTelemetryLayer;
use tracing_subscriber::registry::LookupSpan;
mod requests;
mod runtime_router;
mod scheduler;
use scheduler::*;
mod trigger_router;
mod watch_installer;
const RUNTIME_EMULATOR_PATH: &str = "/.rt";
#[derive(Args, Clone, Debug)]
#[clap(name = "watch", visible_alias = "start", trailing_var_arg = true)]
pub struct Watch {
#[clap(long)]
no_reload: bool,
#[clap(short = 'p', long, default_value = "9000")]
invoke_port: u16,
#[clap(long)]
print_traces: bool,
#[clap(flatten)]
cargo_options: CargoOptions,
#[clap(value_hint = ValueHint::CommandWithArguments)]
watch_args: Vec<String>,
}
#[derive(Args, Clone, Debug)]
struct CargoOptions {
#[clap(long, value_name = "PATH", parse(from_os_str), value_hint = ValueHint::FilePath)]
#[clap(default_value = "Cargo.toml")]
manifest_path: PathBuf,
#[clap(long)]
features: Option<String>,
#[clap(long)]
release: bool,
}
impl Watch {
#[tracing::instrument(skip(self), target = "cargo_lambda")]
pub async fn run(&self) -> Result<()> {
tracing::trace!(options = ?self, "watching project");
if !self.no_reload && which::which("cargo-watch").is_err() {
watch_installer::install().await?;
}
let port = self.invoke_port;
let no_reload = self.no_reload;
let watch_args = self.watch_args.clone();
let cargo_options = self.cargo_options.clone();
Toplevel::new()
.start("Lambda server", move |s| {
start_server(s, port, watch_args, cargo_options, no_reload)
})
.catch_signals()
.handle_shutdown_requests(Duration::from_millis(1000))
.await
.map_err(|e| miette::miette!("{}", e))
}
pub fn xray_layer<S>(&self) -> OpenTelemetryLayer<S, Tracer>
where
S: Subscriber + for<'span> LookupSpan<'span>,
{
global::set_text_map_propagator(XrayPropagator::default());
let builder = stdout::new_pipeline().with_trace_config(
trace::config()
.with_sampler(trace::Sampler::AlwaysOn)
.with_id_generator(trace::XrayIdGenerator::default()),
);
let tracer = if self.print_traces {
builder.install_simple()
} else {
builder.with_writer(std::io::sink()).install_simple()
};
tracing_opentelemetry::layer().with_tracer(tracer)
}
}
async fn start_server(
subsys: SubsystemHandle,
invoke_port: u16,
watch_args: Vec<String>,
cargo_options: CargoOptions,
no_reload: bool,
) -> Result<(), axum::Error> {
let addr = SocketAddr::from(([127, 0, 0, 1], invoke_port));
let runtime_addr = format!("http://{addr}{RUNTIME_EMULATOR_PATH}");
let req_cache = RequestCache::new(runtime_addr);
let req_tx = init_scheduler(
&subsys,
req_cache.clone(),
watch_args,
cargo_options,
no_reload,
)
.await;
let resp_cache = ResponseCache::new();
let x_request_id = HeaderName::from_static("lambda-runtime-aws-request-id");
let app = Router::new()
.merge(trigger_router::routes())
.nest(RUNTIME_EMULATOR_PATH, runtime_router::routes())
.layer(SetRequestIdLayer::new(
x_request_id.clone(),
MakeRequestUuid,
))
.layer(PropagateRequestIdLayer::new(x_request_id))
.layer(Extension(req_tx.clone()))
.layer(Extension(req_cache))
.layer(Extension(resp_cache))
.layer(TraceLayer::new_for_http())
.layer(CatchPanicLayer::new());
info!("invoke server listening on {}", addr);
axum::Server::bind(&addr)
.serve(app.into_make_service())
.with_graceful_shutdown(subsys.on_shutdown_requested())
.await
.map_err(axum::Error::new)
}