-
Notifications
You must be signed in to change notification settings - Fork 2.1k
feat(new sink): new journald sink
#23593
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
Open
wiktorsikora
wants to merge
15
commits into
vectordotdev:master
Choose a base branch
from
wiktorsikora:add-journald-sink
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
bfbb79d
Add support for journald sink
wiktorsikora 53b7dc9
Add changelog entry
wiktorsikora 4b09114
Fixed clippy lints, remove unnecessary code
wiktorsikora 3bf819a
Use tokio::net::UnixDatagram instead of std
wiktorsikora af9e4c3
Fixed key sanitization for > 64 bytes fields
wiktorsikora 27f24df
Merge branch 'master' into add-journald-sink
wiktorsikora 8068f45
Merge branch 'vectordotdev:master' into add-journald-sink
wiktorsikora 2066684
Add generated doc
wiktorsikora bb67fab
Add website doc
wiktorsikora d3c7fa3
Merge branch 'master' into add-journald-sink
wiktorsikora 39cf8c3
Spelling fix
wiktorsikora 8976b8f
Add 'memfd' to spelling allow list
wiktorsikora 4d57648
Merge branch 'master' into add-journald-sink
wiktorsikora 715b439
Merge remote-tracking branch 'origin/master' into add-journald-sink
pront 1ebae95
cargo fmt
pront File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -308,6 +308,7 @@ Medion | |
| MEF | ||
| Meizu | ||
| meme | ||
| memfd | ||
| Mertz | ||
| messagebird | ||
| metakey | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| Added journald sink. | ||
|
|
||
| authors: wiktorsikora |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,48 @@ | ||
| # Journald Sink Example | ||
| # ------------------------------------------------------------------------------ | ||
| # A simple example showing how to send logs to systemd journal. | ||
| # This demonstrates forwarding logs from a dummy source, parsing syslog messages, | ||
| # and mapping syslog severity levels to journal priority levels. | ||
|
|
||
| sources: | ||
| dummy_logs: | ||
| type: "demo_logs" | ||
| format: "syslog" | ||
| interval: 1 | ||
|
|
||
| transforms: | ||
| parse_syslog: | ||
| type: "remap" | ||
| inputs: ["dummy_logs"] | ||
| source: | | ||
| . = parse_syslog!(string!(.message)) | ||
|
|
||
| map_severity_to_priority: | ||
| # Map the syslog severity to journal priority levels | ||
| type: "remap" | ||
| inputs: ["parse_syslog"] | ||
| source: | | ||
| .priority = if .severity == "emerg" { | ||
| 0 | ||
| } else if .severity == "alert" { | ||
| 1 | ||
| } else if .severity == "crit" { | ||
| 2 | ||
| } else if .severity == "err" { | ||
| 3 | ||
| } else if .severity == "warning" { | ||
| 4 | ||
| } else if .severity == "notice" { | ||
| 5 | ||
| } else if .severity == "info" { | ||
| 6 | ||
| } else if .severity == "debug" { | ||
| 7 | ||
| } else { | ||
| 6 | ||
| } | ||
|
|
||
| sinks: | ||
| local_journal: | ||
| inputs: ["map_severity_to_priority"] | ||
| type: "journald" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,87 @@ | ||
| use futures::{FutureExt, future}; | ||
| use vector_lib::configurable::configurable_component; | ||
|
|
||
| use crate::{ | ||
| config::{AcknowledgementsConfig, GenerateConfig, Input, SinkConfig, SinkContext}, | ||
| sinks::{Healthcheck, VectorSink, journald::sink::JournaldSink}, | ||
| }; | ||
|
|
||
| /// Configuration for the `journald` sink. | ||
| #[configurable_component(sink( | ||
| "journald", | ||
| "Send observability events to the systemd journal for local logging." | ||
| ))] | ||
| #[derive(Clone, Debug)] | ||
| #[serde(deny_unknown_fields)] | ||
| pub struct JournaldSinkConfig { | ||
| /// Path to the journald socket. | ||
| /// If not specified, the default systemd journal socket will be used. | ||
| #[configurable(metadata(docs::examples = "\"/run/systemd/journal/socket\".to_string()"))] | ||
| #[serde(default = "default_journald_path")] | ||
| pub journald_path: String, | ||
|
|
||
| #[configurable(derived)] | ||
| #[serde( | ||
| default, | ||
| deserialize_with = "crate::serde::bool_or_struct", | ||
| skip_serializing_if = "crate::serde::is_default" | ||
| )] | ||
| pub acknowledgements: AcknowledgementsConfig, | ||
| } | ||
|
|
||
| fn default_journald_path() -> String { | ||
| "/run/systemd/journal/socket".to_string() | ||
| } | ||
|
|
||
| impl Default for JournaldSinkConfig { | ||
| fn default() -> Self { | ||
| Self { | ||
| journald_path: default_journald_path(), | ||
| acknowledgements: AcknowledgementsConfig::default(), | ||
| } | ||
| } | ||
| } | ||
|
|
||
| impl GenerateConfig for JournaldSinkConfig { | ||
| fn generate_config() -> toml::Value { | ||
| toml::Value::try_from(Self::default()).unwrap() | ||
| } | ||
| } | ||
|
|
||
| #[async_trait::async_trait] | ||
| #[typetag::serde(name = "journald")] | ||
| impl SinkConfig for JournaldSinkConfig { | ||
| async fn build(&self, _cx: SinkContext) -> crate::Result<(VectorSink, Healthcheck)> { | ||
| let sink = JournaldSink::new(self.clone())?; | ||
| let healthcheck = future::ok(()).boxed(); | ||
|
|
||
| Ok((VectorSink::from_event_streamsink(sink), healthcheck)) | ||
| } | ||
|
|
||
| fn input(&self) -> Input { | ||
| Input::log() | ||
| } | ||
|
|
||
| fn acknowledgements(&self) -> &AcknowledgementsConfig { | ||
| &self.acknowledgements | ||
| } | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| mod tests { | ||
| use super::*; | ||
|
|
||
| #[test] | ||
| fn generate_config() { | ||
| crate::test_util::test_generate_config::<JournaldSinkConfig>(); | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_config_default() { | ||
| let config = JournaldSinkConfig::default(); | ||
| assert_eq!( | ||
| config.journald_path, | ||
| "/run/systemd/journal/socket".to_string() | ||
| ); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,128 @@ | ||
| use nix::fcntl::{FcntlArg, SealFlag}; | ||
| use nix::sys::socket::{ControlMessage, MsgFlags}; | ||
| use std::io; | ||
| use std::os::fd::AsRawFd; | ||
| use std::path::Path; | ||
|
|
||
| /// A writer for journald that sends log messages over a Unix domain socket. | ||
| /// For protocol details, see [this link](https://systemd.io/JOURNAL_NATIVE_PROTOCOL/) | ||
| pub struct JournaldWriter { | ||
| socket: tokio::net::UnixDatagram, | ||
| buf: Vec<u8>, | ||
| } | ||
|
|
||
| impl JournaldWriter { | ||
| pub fn new(journald_path: impl AsRef<Path>) -> io::Result<Self> { | ||
| let socket = tokio::net::UnixDatagram::unbound()?; | ||
| socket.connect(journald_path)?; | ||
| let writer = Self { | ||
| socket, | ||
| buf: vec![], | ||
| }; | ||
| Ok(writer) | ||
| } | ||
|
|
||
| /// Add a string field to the buffer. | ||
| pub fn add_str(&mut self, key: &str, value: &str) { | ||
| self.write_with_length(key, |w| { | ||
| w.buf.extend_from_slice(value.as_bytes()); | ||
| }); | ||
| } | ||
|
|
||
| /// Add a field with arbitrary bytes to the buffer. | ||
| pub fn add_bytes(&mut self, key: &str, value: &[u8]) { | ||
| self.write_with_length(key, |w| { | ||
| w.buf.extend_from_slice(value); | ||
| }); | ||
| } | ||
|
|
||
| /// Write the buffered data to journald. | ||
| /// Returns the number of bytes sent. | ||
| pub async fn write(&mut self) -> io::Result<usize> { | ||
| if self.buf.is_empty() { | ||
| return Ok(0); | ||
| } | ||
| let bytes_sent = self.send_payload(&self.buf).await?; | ||
| // Clear the buffer after sending | ||
| // We could also keep the buffer for reuse, but by doing this we ensure that | ||
| // we don't allocate too much memory for long time in case of rare large payloads. | ||
| self.buf = vec![]; | ||
| Ok(bytes_sent) | ||
| } | ||
|
|
||
| /// Send the payload to journald. | ||
| /// If the payload is too large, it will attempt to send it via a memfd. | ||
| /// Returns the number of bytes sent. | ||
| async fn send_payload(&self, payload: &[u8]) -> io::Result<usize> { | ||
| self.socket.send(payload).await.or_else(|error| { | ||
| if Some(nix::libc::EMSGSIZE) == error.raw_os_error() { | ||
| self.send_with_memfd(payload) | ||
| } else { | ||
| Err(error) | ||
| } | ||
| }) | ||
| } | ||
|
|
||
| /// Send the payload using a memfd if the payload is too large for a direct send. | ||
| /// This method uses a blocking call to write the payload to a memfd. | ||
| fn send_with_memfd(&self, payload: &[u8]) -> io::Result<usize> { | ||
| // If the payload is too large, we should try to send it via a memfd | ||
| // This method is described in the journald protocol: https://systemd.io/JOURNAL_NATIVE_PROTOCOL/ | ||
| let memfd = nix::sys::memfd::memfd_create( | ||
| c"journald_payload", | ||
| nix::sys::memfd::MemFdCreateFlag::MFD_ALLOW_SEALING, | ||
| )?; | ||
|
|
||
| // Write the payload to the memfd | ||
| let written = nix::unistd::write(memfd, payload)?; | ||
| if written != payload.len() { | ||
| return Err(io::Error::other("Failed to write all data to memfd")); | ||
| } | ||
|
|
||
| // Seal the memfd as required by journald protocol | ||
| nix::fcntl::fcntl(memfd, FcntlArg::F_ADD_SEALS(SealFlag::all()))?; | ||
|
|
||
| // Send the memfd file descriptor to journald | ||
| let scm = &[memfd]; | ||
| let cmsgs = [ControlMessage::ScmRights(scm)]; | ||
| nix::sys::socket::sendmsg::<()>( | ||
| self.socket.as_raw_fd(), | ||
| &[], | ||
| &cmsgs, | ||
| MsgFlags::empty(), | ||
| None, | ||
| )?; | ||
|
|
||
| Ok(written) | ||
| } | ||
|
|
||
| /// Append a sanitized and length-encoded field into the buffer. | ||
| fn write_with_length(&mut self, key: &str, write_cb: impl FnOnce(&mut Self)) { | ||
| self.sanitize_key(key); | ||
| self.buf.push(b'\n'); | ||
| self.buf.extend_from_slice(&[0; 8]); // Length tag, to be populated after writing the value | ||
| let start = self.buf.len(); | ||
| write_cb(self); | ||
| let end = self.buf.len(); | ||
| self.buf[start - 8..start].copy_from_slice(&((end - start) as u64).to_le_bytes()); | ||
| self.buf.push(b'\n'); | ||
| } | ||
|
|
||
| /// Sanitize a key and convert it to uppercase. | ||
| fn sanitize_key(&mut self, key: &str) { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. One small edge case I noticed: this may still produce field names that journald rejects. The native receiver validates both plain and length-encoded fields with A few examples that look worth handling:
|
||
| self.buf.extend( | ||
| key.bytes() | ||
| .map(|c| match c { | ||
| // As per journald protocol, '=' and '\n' are illegal in keys so we replace them with '_'. | ||
| b'=' | b'\n' => b'_', | ||
| // '.' is ignored in keys, so we replace it with '_'. | ||
| b'.' => b'_', | ||
| _ => c, | ||
| }) | ||
| .filter(|&c| c == b'_' || char::from(c).is_ascii_alphanumeric()) | ||
| .map(|c| char::from(c).to_ascii_uppercase() as u8) | ||
| // Journald keys are limited to 64 bytes | ||
| .take(64), | ||
| ); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| mod config; | ||
| mod sink; | ||
|
|
||
| mod journald_writer; | ||
|
|
||
| pub use config::JournaldSinkConfig; |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If
send_payloadreturns an error here,self.bufis not cleared, so the next event may append to the previous failed payload.