Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add log streaming in beta platform #1743

Merged
merged 4 commits into from
Apr 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 39 additions & 2 deletions cargo-shuttle/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,6 @@ impl Shuttle {
| Command::Stop
| Command::Clean
| Command::Status
| Command::Logs { .. }
) {
unimplemented!("This command is not yet implemented on the beta platform");
}
Expand Down Expand Up @@ -218,7 +217,13 @@ impl Shuttle {
Command::Run(run_args) => self.local_run(run_args).await,
Command::Deploy(deploy_args) => self.deploy(deploy_args).await,
Command::Status => self.status().await,
Command::Logs(logs_args) => self.logs(logs_args).await,
Command::Logs(logs_args) => {
if self.beta {
self.logs_beta(logs_args).await
} else {
self.logs(logs_args).await
}
}
Command::Deployment(DeploymentCommand::List { page, limit, raw }) => {
if self.beta {
unimplemented!();
Expand Down Expand Up @@ -838,6 +843,38 @@ impl Shuttle {
Ok(CommandOutcome::Ok)
}

async fn logs_beta(&self, args: LogsArgs) -> Result<CommandOutcome> {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

will there be a non-streaming option?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, and probably sooner rather than later since I've just discovered the cost of a Kinesis Stream 🥲

let client = self.client.as_ref().unwrap();
let mut stream = client
// TODO: use something else than a fake Uuid
.get_logs_ws(self.ctx.project_name(), &Uuid::new_v4())
iulianbarbu marked this conversation as resolved.
Show resolved Hide resolved
.await
.map_err(|err| {
suggestions::logs::get_logs_failure(err, "Connecting to the logs stream failed")
})?;

while let Some(Ok(msg)) = stream.next().await {
if let tokio_tungstenite::tungstenite::Message::Text(line) = msg {
match serde_json::from_str::<shuttle_common::LogItemBeta>(&line) {
Ok(log) => {
if args.raw {
println!("{}", log.line);
} else {
println!("[{}] ({}) {}", log.timestamp, log.source, log.line);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can use the Display impl?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think we should use Display there. It's only useful in the context of printing to the console.

}
}
Err(err) => {
// TODO better handle logs, by returning a different type than the log line
// if an error happened.
bail!(err);
}
}
}
}

Ok(CommandOutcome::Ok)
}

async fn logs(&self, args: LogsArgs) -> Result<CommandOutcome> {
let client = self.client.as_ref().unwrap();
let id = if let Some(id) = args.id {
Expand Down
2 changes: 2 additions & 0 deletions common/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ pub mod limits;
pub mod log;
#[cfg(feature = "service")]
pub use log::LogItem;
#[cfg(feature = "service")]
pub use log::LogItemBeta;
#[cfg(feature = "models")]
pub mod models;
pub mod resource;
Expand Down
39 changes: 39 additions & 0 deletions common/src/log.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,45 @@ pub struct LogItem {
pub line: String,
}

#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct LogItemBeta {
/// Time log was captured
pub timestamp: DateTime<Utc>,

/// Stdout/stderr
pub source: String,

/// The log line
pub line: String,
}

impl LogItemBeta {
pub fn new(timestamp: DateTime<Utc>, source: String, line: String) -> Self {
Self {
timestamp,
source,
line,
}
}
}

#[cfg(feature = "display")]
impl std::fmt::Display for LogItemBeta {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let datetime: chrono::DateTime<chrono::Local> = DateTime::from(self.timestamp);

write!(
f,
"{} [{}] {}",
datetime
.to_rfc3339_opts(chrono::SecondsFormat::Millis, false)
.dim(),
self.source,
self.line,
)
}
}

const LOGLINE_MAX_CHARS: usize = 2048;
const TRUNC_MSG: &str = "... (truncated)";

Expand Down