Skip to content

Commit

Permalink
Merge pull request containers#1800 from wasup-yash/journal
Browse files Browse the repository at this point in the history
Journald Logger
  • Loading branch information
openshift-merge-bot[bot] authored Oct 21, 2024
2 parents 9ef8c7f + 8d55a6f commit fd287ed
Show file tree
Hide file tree
Showing 9 changed files with 380 additions and 220 deletions.
2 changes: 2 additions & 0 deletions conmon-rs/common/proto/conmon.capnp
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ interface Conmon {
containerRuntimeInterface @0;
# The JSON logger, requires `path` to be set.
json @1;
# The journald logger.
journald @2;
}
}

Expand Down
File renamed without changes.
83 changes: 83 additions & 0 deletions conmon-rs/server/src/container_log/journald.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
use crate::{container_io::Pipe, journal::Journal};
use anyhow::{Context, Result};
use std::io::Write;
use tokio::io::{AsyncBufRead, AsyncBufReadExt};
use tracing::debug;

#[derive(Debug)]
pub struct JournaldLogger;

impl JournaldLogger {
pub fn new(_: Option<usize>) -> Result<Self> {
Ok(Self)
}

pub async fn init(&mut self) -> Result<()> {
debug!("Initializing journald logger");
Ok(())
}

pub async fn write<T>(&mut self, _: Pipe, mut bytes: T) -> Result<()>
where
T: AsyncBufRead + Unpin,
{
let mut line_buf = String::new();
while bytes.read_line(&mut line_buf).await? > 0 {
Journal
.write_all(line_buf.as_bytes())
.context("write to journal")?;
line_buf.clear();
}

Ok(())
}

pub async fn reopen(&mut self) -> Result<()> {
debug!("Reopen journald log");
Ok(())
}
}

#[cfg(test)]
mod tests {
use super::*;
use std::io::Cursor;

#[tokio::test]
async fn test_journald_logger_new() {
JournaldLogger::new(Some(1000)).unwrap();
}

#[tokio::test]
async fn test_journald_logger_init() {
let mut logger = JournaldLogger::new(Some(1000)).unwrap();
assert!(logger.init().await.is_ok());
}

#[tokio::test]
async fn test_journald_logger_write() {
let mut logger = JournaldLogger::new(Some(1000)).unwrap();
logger.init().await.unwrap();

let cursor = Cursor::new(b"Test log message\n".to_vec());
assert!(logger.write(Pipe::StdOut, cursor).await.is_ok());

// Verifying the actual log message in Journald might require additional setup or permissions.
}

#[tokio::test]
async fn test_journald_logger_reopen() {
let mut logger = JournaldLogger::new(Some(1000)).unwrap();
logger.init().await.unwrap();

let cursor = Cursor::new(b"Test log message before reopen\n".to_vec());
assert!(logger.write(Pipe::StdOut, cursor).await.is_ok());

assert!(logger.reopen().await.is_ok());

let cursor = Cursor::new(b"Test log message after reopen\n".to_vec());
assert!(logger.write(Pipe::StdOut, cursor).await.is_ok());

// As with the write test, verifying the actual log messages in Journald might require additional setup or permissions.
}
}
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
use crate::{container_io::Pipe, cri_logger::CriLogger, json_logger::JsonLogger};
mod cri;
mod journald;
mod json;

use crate::{
container_io::Pipe, container_log::cri::CriLogger, container_log::journald::JournaldLogger,
container_log::json::JsonLogger,
};
use anyhow::Result;
use capnp::struct_list::Reader;
use conmon_common::conmon_capnp::conmon::log_driver::{Owned, Type};
Expand All @@ -16,6 +23,7 @@ pub struct ContainerLog {
#[derive(Debug)]
enum LogDriver {
ContainerRuntimeInterface(CriLogger),
Journald(JournaldLogger),
Json(JsonLogger),
}

Expand All @@ -25,6 +33,7 @@ impl ContainerLog {
Arc::new(RwLock::new(Self::default()))
}

/// Create a new SharedContainerLog from an owned reader.
pub fn from(reader: Reader<Owned>) -> Result<SharedContainerLog> {
let drivers = reader
.iter()
Expand All @@ -48,6 +57,13 @@ impl ContainerLog {
None
},
)?)),
Type::Journald => Ok(LogDriver::Journald(JournaldLogger::new(
if x.get_max_size() > 0 {
Some(x.get_max_size() as usize)
} else {
None
},
)?)),
}
})
.collect::<Result<Vec<_>>>()?;
Expand All @@ -64,6 +80,7 @@ impl ContainerLog {
cri_logger.init().boxed()
}
LogDriver::Json(ref mut json_logger) => json_logger.init().boxed(),
LogDriver::Journald(ref mut journald_logger) => journald_logger.init().boxed(),
})
.collect::<Vec<_>>(),
)
Expand All @@ -83,6 +100,9 @@ impl ContainerLog {
cri_logger.reopen().boxed()
}
LogDriver::Json(ref mut json_logger) => json_logger.reopen().boxed(),
LogDriver::Journald(ref mut journald_logger) => {
journald_logger.reopen().boxed()
}
})
.collect::<Vec<_>>(),
)
Expand Down Expand Up @@ -110,6 +130,9 @@ impl ContainerLog {
LogDriver::ContainerRuntimeInterface(cri_logger) => {
cri_logger.write(pipe, bytes).await
}
LogDriver::Journald(journald_logger) => {
journald_logger.write(pipe, bytes).await
}
LogDriver::Json(json_logger) => json_logger.write(pipe, bytes).await,
}
}
Expand Down
2 changes: 0 additions & 2 deletions conmon-rs/server/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,9 @@ mod child_reaper;
mod config;
mod container_io;
mod container_log;
mod cri_logger;
mod fd_socket;
mod init;
mod journal;
mod json_logger;
mod listener;
mod oom_watcher;
mod pause;
Expand Down
Loading

0 comments on commit fd287ed

Please sign in to comment.