Skip to content

Commit

Permalink
Improved log interface performance and streaming
Browse files Browse the repository at this point in the history
This commit enhances the log interface by reducing lag and improving the
rendering speed. It introduces a feature to load the latest logs first and logs are
now streamed in real-time from backend, ensuring immediate access to new logs.
Implements a new pagination system to limit displayed log lines with scrolling
direction from bottom to top.
  • Loading branch information
samuchila committed Jan 15, 2024
1 parent eeccb2d commit 8aae987
Show file tree
Hide file tree
Showing 11 changed files with 522 additions and 156 deletions.
122 changes: 42 additions & 80 deletions api/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions api/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ futures = { version = "0.3", features = ["compat"] }
handlebars = "4.5"
http-api-problem = "0.57"
jira_query = "1.3"
k8s-openapi = { version = "0.18", default-features = false, features = ["v1_24"] }
kube = { version = "0.84", default-features = false, features = ["client", "derive", "rustls-tls"] }
k8s-openapi = { version = "0.20", default-features = false, features = ["v1_24"] }
kube = { version = "0.87.2", default-features = false, features = ["client", "derive", "rustls-tls"] }
lazy_static = "1.4"
log = "0.4"
multimap = "0.9"
Expand Down
56 changes: 56 additions & 0 deletions api/src/apps/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,13 @@ pub use crate::apps::AppsServiceError as AppsError;
use crate::config::{Config, ConfigError};
use crate::deployment::deployment_unit::DeploymentUnitBuilder;
use crate::infrastructure::Infrastructure;
use crate::infrastructure::LogEvents;
use crate::models::service::{ContainerType, Service, ServiceStatus};
use crate::models::{AppName, AppStatusChangeId, LogChunk, ServiceConfig};
use crate::registry::Registry;
use crate::registry::RegistryError;
use chrono::{DateTime, FixedOffset};
use futures::stream::BoxStream;
use handlebars::RenderError;
pub use host_meta_cache::new as host_meta_crawling;
pub use host_meta_cache::HostMetaCache;
Expand Down Expand Up @@ -343,6 +345,16 @@ impl AppsService {
}
}

pub async fn stream_logs<'a>(
&'a self,
app_name: &'a AppName,
service_name: &'a str,
limit: usize,
) -> BoxStream<'a, Result<LogEvents, failure::Error>> {
self.infrastructure
.stream_logs(app_name, service_name, limit)
}

pub async fn get_logs(
&self,
app_name: &AppName,
Expand Down Expand Up @@ -442,6 +454,7 @@ mod tests {
use crate::models::{EnvironmentVariable, ServiceBuilder};
use crate::sc;
use chrono::Utc;
use futures::StreamExt;
use secstr::SecUtf8;
use std::hash::Hash;
use std::io::Write;
Expand Down Expand Up @@ -703,6 +716,49 @@ Log msg 3 of service-a of app master
Ok(())
}

#[tokio::test]
async fn should_stream_logs_from_infrastructure() -> Result<(), AppsServiceError> {
let config = Config::default();
let infrastructure = Box::new(Dummy::new());
let apps = AppsService::new(config, infrastructure)?;

let app_name = AppName::from_str("master").unwrap();

apps.create_or_update(
&app_name,
&AppStatusChangeId::new(),
None,
&vec![sc!("service-a"), sc!("service-b")],
)
.await?;

let service_for_logs = String::from("service-a");
let mut count = 0;
let mut log_chunk = apps.stream_logs(&app_name, &service_for_logs, 5).await;
while let Some(result) = log_chunk.next().await {
match result {
Ok(events) => match events {
LogEvents::Message(message) => assert_eq!(
message,
format!("Log msg 1 of service-a of app master\nLog msg 2 of service-a of app master")
),
LogEvents::Line(line) => {
assert_eq!(
line,
format!("Streaming log msg {count} of service-a of app master")
);
count += 1;
}
},
Err(_e) => {
break;
}
}
}

Ok(())
}

#[tokio::test]
async fn should_deploy_companions() -> Result<(), AppsServiceError> {
let config = config_from_str!(
Expand Down
Loading

0 comments on commit 8aae987

Please sign in to comment.