Skip to content

Commit

Permalink
Merge branch 'master' into master
Browse files Browse the repository at this point in the history
  • Loading branch information
hawkw authored Jan 25, 2024
2 parents 4107d47 + b461897 commit fef5c00
Show file tree
Hide file tree
Showing 24 changed files with 250 additions and 57 deletions.
18 changes: 9 additions & 9 deletions .github/workflows/CI.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ jobs:
name: cargo check
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4
- uses: dtolnay/rust-toolchain@stable
- name: Check
run: cargo check --all --tests --benches
Expand All @@ -46,7 +46,7 @@ jobs:
needs: check
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4
- uses: dtolnay/rust-toolchain@stable
with:
components: rustfmt
Expand All @@ -58,7 +58,7 @@ jobs:
runs-on: ubuntu-latest
needs: check
steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4
- uses: dtolnay/rust-toolchain@stable
with:
components: clippy
Expand Down Expand Up @@ -88,7 +88,7 @@ jobs:
- tracing
- tracing-subscriber
steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4
- uses: dtolnay/rust-toolchain@stable
- name: install cargo-hack
uses: taiki-e/install-action@cargo-hack
Expand Down Expand Up @@ -146,7 +146,7 @@ jobs:
- 1.63.0
- stable
steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4
- name: install Rust nightly
uses: dtolnay/rust-toolchain@nightly
- name: "install Rust ${{ matrix.toolchain }}"
Expand Down Expand Up @@ -210,7 +210,7 @@ jobs:
fail-fast: false
runs-on: ${{ matrix.os }}
steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4
- name: "install Rust ${{ matrix.rust }}"
uses: dtolnay/rust-toolchain@master
with:
Expand Down Expand Up @@ -252,7 +252,7 @@ jobs:
- tracing-tower
fail-fast: false
steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4
- uses: dtolnay/rust-toolchain@stable
with:
target: wasm32-unknown-unknown
Expand All @@ -268,7 +268,7 @@ jobs:
subcrate:
- tracing
steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4
- uses: dtolnay/rust-toolchain@stable
with:
target: wasm32-unknown-unknown
Expand All @@ -283,7 +283,7 @@ jobs:
needs: check
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4
- uses: dtolnay/rust-toolchain@stable
- name: "Test log support"
run: cargo test
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/audit.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ jobs:
security_audit:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4
- uses: actions-rs/audit-check@v1
with:
token: ${{ secrets.GITHUB_TOKEN }}
2 changes: 1 addition & 1 deletion .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ jobs:
if: github.repository_owner == 'tokio-rs'
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4
- uses: taiki-e/create-gh-release-action@v1
with:
prefix: tracing(-[a-z]+)?
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -438,7 +438,7 @@ please let us know!)
[Tracy]: https://github.com/wolfpld/tracy
[`tracing-elastic-apm`]: https://crates.io/crates/tracing-elastic-apm
[Elastic APM]: https://www.elastic.co/apm
[`tracing-etw`]: https://github.com/microsoft/tracing-etw
[`tracing-etw`]: https://github.com/microsoft/rust_win_etw/tree/main/win_etw_tracing
[ETW]: https://docs.microsoft.com/en-us/windows/win32/etw/about-event-tracing
[`sentry-tracing`]: https://crates.io/crates/sentry-tracing
[Sentry]: https://sentry.io/welcome/
Expand Down
2 changes: 1 addition & 1 deletion examples/examples/fmt/yak_shave.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use tracing::{debug, error, info, span, trace, warn, Level};

// the `#[tracing::instrument]` attribute creates and enters a span
// every time the instrumented function is called. The span is named after
// the function or method. Paramaters passed to the function are recorded as fields.
// the function or method. Parameters passed to the function are recorded as fields.
#[tracing::instrument]
pub fn shave(yak: usize) -> Result<(), Box<dyn Error + Send + Sync + 'static>> {
// this creates an event at the TRACE log level with two fields:
Expand Down
2 changes: 1 addition & 1 deletion tracing-core/src/metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -378,7 +378,7 @@ impl Kind {
pub const SPAN: Kind = Kind(Self::SPAN_BIT);

/// `enabled!` callsite. [`Collect`][`crate::collect::Collect`]s can assume
/// this `Kind` means they will never recieve a
/// this `Kind` means they will never receive a
/// full event with this [`Metadata`].
pub const HINT: Kind = Kind(Self::HINT_BIT);

Expand Down
2 changes: 1 addition & 1 deletion tracing-error/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
- **TracedError**: `TracedError`, an error type wrapper that annotates an error
with the current span.
- **SpanTrace**:`SpanTrace::status` method and `SpanTraceStatus` type for
determing whether a `SpanTrace` was successfully captured (#614)
determining whether a `SpanTrace` was successfully captured (#614)

### Changed

Expand Down
2 changes: 1 addition & 1 deletion tracing-error/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ where
// erased `ErrorImpl` back to its original type, which is needed in order to forward our
// error/display/debug impls to the internal error type from the type erased error type.
//
// The repr(C) is necessary to ensure that the struct is layed out in the order we
// The repr(C) is necessary to ensure that the struct is laid out in the order we
// specified it, so that we can safely access the vtable and spantrace fields through a type
// erased pointer to the original object.
let vtable = &ErrorVTable {
Expand Down
177 changes: 163 additions & 14 deletions tracing-journald/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,14 +62,16 @@ mod socket;
/// names by translating `.`s into `_`s, stripping leading `_`s and non-ascii-alphanumeric
/// characters other than `_`, and upcasing.
///
/// Levels are mapped losslessly to journald `PRIORITY` values as follows:
/// By default, levels are mapped losslessly to journald `PRIORITY` values as follows:
///
/// - `ERROR` => Error (3)
/// - `WARN` => Warning (4)
/// - `INFO` => Notice (5)
/// - `DEBUG` => Informational (6)
/// - `TRACE` => Debug (7)
///
/// These mappings can be changed with [`Subscriber::with_priority_mappings`].
///
/// The standard journald `CODE_LINE` and `CODE_FILE` fields are automatically emitted. A `TARGET`
/// field is emitted containing the event's target.
///
Expand All @@ -86,6 +88,7 @@ pub struct Subscriber {
field_prefix: Option<String>,
syslog_identifier: String,
additional_fields: Vec<u8>,
priority_mappings: PriorityMappings,
}

#[cfg(unix)]
Expand All @@ -111,6 +114,7 @@ impl Subscriber {
// If we fail to get the name of the current executable fall back to an empty string.
.unwrap_or_default(),
additional_fields: Vec::new(),
priority_mappings: PriorityMappings::new(),
};
// Check that we can talk to journald, by sending empty payload which journald discards.
// However if the socket didn't exist or if none listened we'd get an error here.
Expand All @@ -131,6 +135,41 @@ impl Subscriber {
self
}

/// Sets how [`tracing_core::Level`]s are mapped to [journald priorities](Priority).
///
/// # Examples
///
/// ```rust
/// use tracing_journald::{Priority, PriorityMappings};
/// use tracing_subscriber::prelude::*;
/// use tracing::error;
///
/// let registry = tracing_subscriber::registry();
/// match tracing_journald::subscriber() {
/// Ok(subscriber) => {
/// registry.with(
/// subscriber
/// // We can tweak the mappings between the trace level and
/// // the journal priorities.
/// .with_priority_mappings(PriorityMappings {
/// info: Priority::Informational,
/// ..PriorityMappings::new()
/// }),
/// );
/// }
/// // journald is typically available on Linux systems, but nowhere else. Portable software
/// // should handle its absence gracefully.
/// Err(e) => {
/// registry.init();
/// error!("couldn't connect to journald: {}", e);
/// }
/// }
/// ```
pub fn with_priority_mappings(mut self, mappings: PriorityMappings) -> Self {
self.priority_mappings = mappings;
self
}

/// Sets the syslog identifier for this logger.
///
/// The syslog identifier comes from the classic syslog interface (`openlog()`
Expand Down Expand Up @@ -234,6 +273,20 @@ impl Subscriber {
memfd::seal_fully(mem.as_raw_fd())?;
socket::send_one_fd_to(&self.socket, mem.as_raw_fd(), JOURNALD_PATH)
}

fn put_priority(&self, buf: &mut Vec<u8>, meta: &Metadata) {
put_field_wellformed(
buf,
"PRIORITY",
&[match *meta.level() {
Level::ERROR => self.priority_mappings.error as u8,
Level::WARN => self.priority_mappings.warn as u8,
Level::INFO => self.priority_mappings.info as u8,
Level::DEBUG => self.priority_mappings.debug as u8,
Level::TRACE => self.priority_mappings.trace as u8,
}],
);
}
}

/// Construct a journald subscriber
Expand Down Expand Up @@ -288,7 +341,7 @@ where
}

// Record event fields
put_priority(&mut buf, event.metadata());
self.put_priority(&mut buf, event.metadata());
put_metadata(&mut buf, event.metadata(), None);
put_field_length_encoded(&mut buf, "SYSLOG_IDENTIFIER", |buf| {
write!(buf, "{}", self.syslog_identifier).unwrap()
Expand Down Expand Up @@ -376,18 +429,114 @@ impl Visit for EventVisitor<'_> {
}
}

fn put_priority(buf: &mut Vec<u8>, meta: &Metadata) {
put_field_wellformed(
buf,
"PRIORITY",
match *meta.level() {
Level::ERROR => b"3",
Level::WARN => b"4",
Level::INFO => b"5",
Level::DEBUG => b"6",
Level::TRACE => b"7",
},
);
/// A priority (called "severity code" by syslog) is used to mark the
/// importance of a message.
///
/// Descriptions and examples are taken from the [Arch Linux wiki].
/// Priorities are also documented in the
/// [section 6.2.1 of the Syslog protocol RFC][syslog].
///
/// [Arch Linux wiki]: https://wiki.archlinux.org/title/Systemd/Journal#Priority_level
/// [syslog]: https://www.rfc-editor.org/rfc/rfc5424#section-6.2.1
#[derive(Debug, Clone, Copy, Hash, PartialEq, Eq)]
#[repr(u8)]
pub enum Priority {
/// System is unusable.
///
/// Examples:
///
/// - severe Kernel BUG
/// - systemd dumped core
///
/// This level should not be used by applications.
Emergency = b'0',
/// Should be corrected immediately.
///
/// Examples:
///
/// - Vital subsystem goes out of work, data loss:
/// - `kernel: BUG: unable to handle kernel paging request at ffffc90403238ffc`
Alert = b'1',
/// Critical conditions
///
/// Examples:
///
/// - Crashe, coredumps
/// - `systemd-coredump[25319]: Process 25310 (plugin-container) of user 1000 dumped core`
Critical = b'2',
/// Error conditions
///
/// Examples:
///
/// - Not severe error reported
/// - `kernel: usb 1-3: 3:1: cannot get freq at ep 0x84, systemd[1]: Failed unmounting /var`
/// - `libvirtd[1720]: internal error: Failed to initialize a valid firewall backend`
Error = b'3',
/// May indicate that an error will occur if action is not taken.
///
/// Examples:
///
/// - a non-root file system has only 1GB free
/// - `org.freedesktop. Notifications[1860]: (process:5999): Gtk-WARNING **: Locale not supported by C library. Using the fallback 'C' locale`
Warning = b'4',
/// Events that are unusual, but not error conditions.
///
/// Examples:
///
/// - `systemd[1]: var.mount: Directory /var to mount over is not empty, mounting anyway`
/// - `gcr-prompter[4997]: Gtk: GtkDialog mapped without a transient parent. This is discouraged`
Notice = b'5',
/// Normal operational messages that require no action.
///
/// Example: `lvm[585]: 7 logical volume(s) in volume group "archvg" now active`
Informational = b'6',
/// Information useful to developers for debugging the
/// application.
///
/// Example: `kdeinit5[1900]: powerdevil: Scheduling inhibition from ":1.14" "firefox" with cookie 13 and reason "screen"`
Debug = b'7',
}

/// Mappings from tracing [`Level`]s to journald [priorities].
///
/// [priorities]: Priority
#[derive(Debug, Clone)]
pub struct PriorityMappings {
/// Priority mapped to the `ERROR` level
pub error: Priority,
/// Priority mapped to the `WARN` level
pub warn: Priority,
/// Priority mapped to the `INFO` level
pub info: Priority,
/// Priority mapped to the `DEBUG` level
pub debug: Priority,
/// Priority mapped to the `TRACE` level
pub trace: Priority,
}

impl PriorityMappings {
/// Returns the default priority mappings:
///
/// - [`tracing::Level::ERROR`]: [`Priority::Error`] (3)
/// - [`tracing::Level::WARN`]: [`Priority::Warning`] (4)
/// - [`tracing::Level::INFO`]: [`Priority::Notice`] (5)
/// - [`tracing::Level::DEBUG`]: [`Priority::Informational`] (6)
/// - [`tracing::Level::TRACE`]: [`Priority::Debug`] (7)
pub fn new() -> PriorityMappings {
Self {
error: Priority::Error,
warn: Priority::Warning,
info: Priority::Notice,
debug: Priority::Informational,
trace: Priority::Debug,
}
}
}

impl Default for PriorityMappings {
fn default() -> Self {
Self::new()
}
}

fn put_metadata(buf: &mut Vec<u8>, meta: &Metadata, prefix: Option<&str>) {
Expand Down
Loading

0 comments on commit fef5c00

Please sign in to comment.