From c2a4b558ac6b661740da8ec46b27bcb0c897376b Mon Sep 17 00:00:00 2001 From: tison Date: Thu, 3 Dec 2026 08:49:57 +0800 Subject: [PATCH 1/2] fix(append-opentelemetry): emit module_path instead of code.function.name Rust compile-time logging callsites only expose `module_path!()`, not the enclosing function or method name. Recording a module path under `code.function.name` conflates distinct functions in the same module when backends aggregate by `code.function.name` (tracked upstream in open-telemetry/opentelemetry-rust#3896 and open-telemetry/semantic-conventions#4440). Emit `Record::module_path` under `module_path` while retaining `code.file.path`, `code.line.number`, and `code.column.number` for file, line, and column metadata. Signed-off-by: tison --- CHANGELOG.md | 2 +- appenders/opentelemetry/README.md | 18 +++++++++--------- appenders/opentelemetry/src/lib.rs | 14 ++++++++------ .../tests/append_test/opentelemetry.rs | 7 ++----- 4 files changed, 20 insertions(+), 21 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 33b87087..668e257b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -55,7 +55,7 @@ All significant changes to this project will be documented in this file. * Remove the redundant `"timestamp"` event property (and `jiff` dependency) from `FastraceEvent` in `logforth-append-fastrace` and require an active sampled `SpanContext::current_local_parent()` before recording events (skipping parentless `LocalCollector::start()` scopes); read event timestamps from `fastrace::collector::EventRecord::timestamp_unix_ns` instead. * Broaden `Journald::with_syslog_identifier` in `logforth-append-journald` from `String` to `impl Into`; pass string literals directly without `.into()` where type inference would otherwise be ambiguous. * Upgrade `opentelemetry` and `opentelemetry_sdk` from `0.32.0` to `0.33.1` (re-exporting both crates), remove the mandatory `opentelemetry-otlp` dependency, and replace `OpentelemetryLogBuilder` (including `OpentelemetryLogBuilder::{label, labels}`) with `OpentelemetryLog::{new, with_make_body}` over `(InstrumentationScope, SdkLoggerProvider)` in `logforth-append-opentelemetry` so multiple channels can share a provider, custom `Resource` attributes, and custom `LogProcessor` pipelines without premature shutdown on `Drop`. -* `OpentelemetryLog` in `logforth-append-opentelemetry` now emits source location attributes under the OpenTelemetry semantic convention keys `code.function.name` (`Record::module_path`), `code.file.path`, `code.line.number`, and `code.column.number` instead of `module_path`, `file`, `line`, and `column`; update queries and processors matching the previous attribute names. +* `OpentelemetryLog` in `logforth-append-opentelemetry` now emits file, line, and column source location attributes under the OpenTelemetry semantic convention keys `code.file.path`, `code.line.number`, and `code.column.number` instead of `file`, `line`, and `column` (retaining `module_path` for `Record::module_path`); update queries and processors matching the previous attribute names. * Replace `fasyslog` with `logforth-syslog` in `logforth-append-syslog` and remove the `rustls` and `native-tls` Cargo features from `logforth-append-syslog` and `logforth`: `Syslog` and `SyslogBuilder` are renamed to `SocketSyslog` and `SocketSyslogBuilder` (re-exported as `logforth::append::SocketSyslog`), and `Facility`, `Severity`, `ParseError`, `SyslogContext`, `Framing`, and `jiff` are exported at the crate root. * Mark `SyslogFormat` as `#[non_exhaustive]` in `logforth-append-syslog` and rename its variants `{RFC3164, RFC5424}` to `{Rfc3164, Rfc5424}`. * Replace `{tcp_well_known, udp_well_known, broadcast_well_known, rustls*, native_tls*, new}` on `SocketSyslogBuilder` in `logforth-append-syslog` with fallible constructors returning `Result` (preserving `io::Error` via `.source()`), auto-binding `udp(remote)` (use `udp_bind` or `udp_socket` for explicit sockets), `tcp_tls(addr, handshake)` over caller-supplied TLS closures (defaulting to `Rfc5424` with RFC 5425 `Framing::OctetCounting`), and unbuffered `Framing::LineFeed` (`\n`) with lazy reconnection instead of buffered `\r\n` on stream transports. diff --git a/appenders/opentelemetry/README.md b/appenders/opentelemetry/README.md index c1c6da0a..f558ded6 100644 --- a/appenders/opentelemetry/README.md +++ b/appenders/opentelemetry/README.md @@ -53,14 +53,14 @@ fn main() -> Result<(), Box> { Each `Record` is projected onto an `opentelemetry_sdk::logs::SdkLogRecord`: -| `SdkLogRecord` field | Source in Logforth | -| :------------------------- | :-------------------------------------------------------------------------------------------------------------------------------------------------------------------- | -| `timestamp` | `Record::time()` (event creation time, preserved across `Async` queues and explicit `RecordBuilder::time`). | -| `observed_timestamp` | `SystemTime::now()` captured when `OpentelemetryLog::append` processes the record. | -| `severity_number` / `text` | 1-to-1 mapping across all 24 `Level` variants (`Trace`..=`Fatal4` -> `Severity::Trace`..=`Severity::Fatal4`) and `Level::name()`. | -| `body` | `AnyValue::String` from `Record::payload()` (omitted when empty), or custom `AnyValue` via `MakeBody` (such as `MakeBodyLayout`). | -| `trace_context` | Extracted from `Diagnostic` `("trace_id", "span_id")` (32- and 16-digit non-zero hex) and optional boolean `"sampled"`. | -| `attributes` | Present `code.function.name` (`module_path`), `code.file.path`, `code.line.number`, `code.column.number`, remaining `Diagnostic` entries, and `Record::key_values()`. | +| `SdkLogRecord` field | Source in Logforth | +| :------------------------- | :----------------------------------------------------------------------------------------------------------------------------------------------- | +| `timestamp` | `Record::time()` (event creation time, preserved across `Async` queues and explicit `RecordBuilder::time`). | +| `observed_timestamp` | `SystemTime::now()` captured when `OpentelemetryLog::append` processes the record. | +| `severity_number` / `text` | 1-to-1 mapping across all 24 `Level` variants (`Trace`..=`Fatal4` -> `Severity::Trace`..=`Severity::Fatal4`) and `Level::name()`. | +| `body` | `AnyValue::String` from `Record::payload()` (omitted when empty), or custom `AnyValue` via `MakeBody` (such as `MakeBodyLayout`). | +| `trace_context` | Extracted from `Diagnostic` `("trace_id", "span_id")` (32- and 16-digit non-zero hex) and optional boolean `"sampled"`. | +| `attributes` | Present `module_path`, `code.file.path`, `code.line.number`, `code.column.number`, remaining `Diagnostic` entries, and `Record::key_values()`. | | `OpentelemetryLog` method | Effect | | :-------------------------- | :------------------------------------------------------------------------------------------------------- | @@ -68,7 +68,7 @@ Each `Record` is projected onto an `opentelemetry_sdk::logs::SdkLogRecord`: | `with_make_body(make_body)` | Overrides how `SdkLogRecord` bodies (`AnyValue`) are constructed via a custom `MakeBody` implementation. | | `allow_duplicate_keys()` | Forwards attributes in source location, diagnostic, then record order without deduplicating keys. | -By default, `OpentelemetryLog` enforces unique top-level attribute keys per the [OpenTelemetry attribute collection specification](https://opentelemetry.io/docs/specs/otel/common/#attribute-collections): explicit `Record::key_values()` override colliding `Diagnostic` entries, `Diagnostic` entries override `code.*` source location attributes, and the last entry wins within each source while preserving relative order. Because many receivers handle duplicate attribute keys unpredictably, callers enabling `allow_duplicate_keys()` are responsible for ensuring exported keys are unique unless targeting a custom processor designed for repeated keys. +By default, `OpentelemetryLog` enforces unique top-level attribute keys per the [OpenTelemetry attribute collection specification](https://opentelemetry.io/docs/specs/otel/common/#attribute-collections): explicit `Record::key_values()` override colliding `Diagnostic` entries, `Diagnostic` entries override source location attributes (`module_path` and `code.*`), and the last entry wins within each source while preserving relative order. Because many receivers handle duplicate attribute keys unpredictably, callers enabling `allow_duplicate_keys()` are responsible for ensuring exported keys are unique unless targeting a custom processor designed for repeated keys. `opentelemetry` and `opentelemetry_sdk` are re-exported at `logforth_append_opentelemetry::{opentelemetry, opentelemetry_sdk}` so callers do not need direct Cargo dependencies solely to name SDK traits or types. diff --git a/appenders/opentelemetry/src/lib.rs b/appenders/opentelemetry/src/lib.rs index 6352376a..d8653304 100644 --- a/appenders/opentelemetry/src/lib.rs +++ b/appenders/opentelemetry/src/lib.rs @@ -25,7 +25,7 @@ //! - `trace_context` comes from [`Diagnostic`] entries when both `trace_id` and `span_id` are //! nonzero hexadecimal IDs of 32 and 16 digits respectively. An optional boolean `sampled` sets //! the trace flags. Unconsumed entries remain available as attributes. -//! - `attributes` begin with the available source location fields (`code.function.name` from +//! - `attributes` begin with the available source location fields (`module_path` from //! [`Record::module_path`], `code.file.path`, `code.line.number`, and `code.column.number`), //! followed by diagnostic entries and then [`Record::key_values`]. By default only the last //! occurrence of each key survives, in its original position. @@ -78,7 +78,9 @@ use opentelemetry_sdk::logs::SdkLogRecord; use opentelemetry_sdk::logs::SdkLogger; use opentelemetry_sdk::logs::SdkLoggerProvider; -const ATTR_CODE_FUNCTION_NAME: &str = "code.function.name"; +// Rust compile-time callsites expose only `module_path!()`, not the enclosing function name. +// See https://github.com/open-telemetry/semantic-conventions/issues/4440. +const ATTR_MODULE_PATH: &str = "module_path"; const ATTR_CODE_FILE_PATH: &str = "code.file.path"; const ATTR_CODE_LINE_NUMBER: &str = "code.line.number"; const ATTR_CODE_COLUMN_NUMBER: &str = "code.column.number"; @@ -302,13 +304,13 @@ fn add_source_location( mut keep: impl FnMut(&str) -> bool, ) { if let Some(module_path) = record.module_path_static() { - if keep(ATTR_CODE_FUNCTION_NAME) { - log_record.add_attribute(ATTR_CODE_FUNCTION_NAME, module_path); + if keep(ATTR_MODULE_PATH) { + log_record.add_attribute(ATTR_MODULE_PATH, module_path); } } else if let Some(module_path) = record.module_path() - && keep(ATTR_CODE_FUNCTION_NAME) + && keep(ATTR_MODULE_PATH) { - log_record.add_attribute(ATTR_CODE_FUNCTION_NAME, module_path.to_owned()); + log_record.add_attribute(ATTR_MODULE_PATH, module_path.to_owned()); } if let Some(file) = record.file_static() { diff --git a/tests-integration/tests/append_test/opentelemetry.rs b/tests-integration/tests/append_test/opentelemetry.rs index a9723079..9e27ece2 100644 --- a/tests-integration/tests/append_test/opentelemetry.rs +++ b/tests-integration/tests/append_test/opentelemetry.rs @@ -135,10 +135,7 @@ fn opentelemetry_appender_preserves_event_timestamp_maps_all_levels_and_shuts_do assert_eq!( attrs_vec(&first.record), vec![ - ( - "code.function.name".to_owned(), - AnyValue::from("app::billing") - ), + ("module_path".to_owned(), AnyValue::from("app::billing")), ( "code.file.path".to_owned(), AnyValue::from("src/billing.rs") @@ -216,7 +213,7 @@ fn opentelemetry_appender_encodes_typed_attributes_and_diagnostics() { .filter(|(k, _)| { !matches!( k.as_str(), - "code.function.name" | "code.file.path" | "code.line.number" | "code.column.number" + "module_path" | "code.file.path" | "code.line.number" | "code.column.number" ) }) .collect(); From a419bb3eb727f8df7a2df70d7d0c286d067f8aaf Mon Sep 17 00:00:00 2001 From: tison Date: Thu, 3 Dec 2026 09:04:06 +0800 Subject: [PATCH 2/2] fix(append-opentelemetry): emit code.module.name for Record::module_path Emit `Record::module_path` under `code.module.name` alongside `code.file.path`, `code.line.number`, and `code.column.number`, matching the three-segment `code..` Semantic Conventions naming model proposed in open-telemetry/semantic-conventions#4440. Signed-off-by: tison --- CHANGELOG.md | 2 +- appenders/opentelemetry/README.md | 18 +++++++++--------- appenders/opentelemetry/src/lib.rs | 16 ++++++++-------- .../tests/append_test/opentelemetry.rs | 7 +++++-- 4 files changed, 23 insertions(+), 20 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 377e2b8d..cd0f165e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -55,7 +55,7 @@ All significant changes to this project will be documented in this file. * Remove the redundant `"timestamp"` event property (and `jiff` dependency) from `FastraceEvent` in `logforth-append-fastrace` and require an active sampled `SpanContext::current_local_parent()` before recording events (skipping parentless `LocalCollector::start()` scopes); read event timestamps from `fastrace::collector::EventRecord::timestamp_unix_ns` instead. * Broaden `Journald::with_syslog_identifier` in `logforth-append-journald` from `String` to `impl Into`; pass string literals directly without `.into()` where type inference would otherwise be ambiguous. * Upgrade `opentelemetry` and `opentelemetry_sdk` from `0.32.0` to `0.33.1` (re-exporting both crates), remove the mandatory `opentelemetry-otlp` dependency, and replace `OpentelemetryLogBuilder` (including `OpentelemetryLogBuilder::{label, labels}`) with `OpentelemetryLog::{new, with_make_body}` over `(InstrumentationScope, SdkLoggerProvider)` in `logforth-append-opentelemetry` so multiple channels can share a provider, custom `Resource` attributes, and custom `LogProcessor` pipelines without premature shutdown on `Drop`. -* `OpentelemetryLog` in `logforth-append-opentelemetry` now emits file, line, and column source location attributes under the OpenTelemetry semantic convention keys `code.file.path`, `code.line.number`, and `code.column.number` instead of `file`, `line`, and `column` (retaining `module_path` for `Record::module_path`); update queries and processors matching the previous attribute names. +* `OpentelemetryLog` in `logforth-append-opentelemetry` now emits source location attributes under `code.module.name` (`Record::module_path`), `code.file.path`, `code.line.number`, and `code.column.number` instead of `module_path`, `file`, `line`, and `column`; update queries and processors matching the previous attribute names. * Replace `fasyslog` with `logforth-syslog` in `logforth-append-syslog` and remove the `rustls` and `native-tls` Cargo features from `logforth-append-syslog` and `logforth`: `Syslog` and `SyslogBuilder` are renamed to `SocketSyslog` and `SocketSyslogBuilder` (re-exported as `logforth::append::SocketSyslog`), and `Facility`, `Severity`, `ParseError`, `SyslogContext`, `Framing`, and `jiff` are exported at the crate root. * Mark `SyslogFormat` as `#[non_exhaustive]` in `logforth-append-syslog` and rename its variants `{RFC3164, RFC5424}` to `{Rfc3164, Rfc5424}`. * Replace `{tcp_well_known, udp_well_known, broadcast_well_known, rustls*, native_tls*, new}` on `SocketSyslogBuilder` in `logforth-append-syslog` with fallible constructors returning `Result` (preserving `io::Error` via `.source()`), auto-binding `udp(remote)` (use `udp_bind` or `udp_socket` for explicit sockets), `tcp_tls(addr, handshake)` over caller-supplied TLS closures (defaulting to `Rfc5424` with RFC 5425 `Framing::OctetCounting`), and unbuffered `Framing::LineFeed` (`\n`) with lazy reconnection instead of buffered `\r\n` on stream transports. diff --git a/appenders/opentelemetry/README.md b/appenders/opentelemetry/README.md index f558ded6..7eb01669 100644 --- a/appenders/opentelemetry/README.md +++ b/appenders/opentelemetry/README.md @@ -53,14 +53,14 @@ fn main() -> Result<(), Box> { Each `Record` is projected onto an `opentelemetry_sdk::logs::SdkLogRecord`: -| `SdkLogRecord` field | Source in Logforth | -| :------------------------- | :----------------------------------------------------------------------------------------------------------------------------------------------- | -| `timestamp` | `Record::time()` (event creation time, preserved across `Async` queues and explicit `RecordBuilder::time`). | -| `observed_timestamp` | `SystemTime::now()` captured when `OpentelemetryLog::append` processes the record. | -| `severity_number` / `text` | 1-to-1 mapping across all 24 `Level` variants (`Trace`..=`Fatal4` -> `Severity::Trace`..=`Severity::Fatal4`) and `Level::name()`. | -| `body` | `AnyValue::String` from `Record::payload()` (omitted when empty), or custom `AnyValue` via `MakeBody` (such as `MakeBodyLayout`). | -| `trace_context` | Extracted from `Diagnostic` `("trace_id", "span_id")` (32- and 16-digit non-zero hex) and optional boolean `"sampled"`. | -| `attributes` | Present `module_path`, `code.file.path`, `code.line.number`, `code.column.number`, remaining `Diagnostic` entries, and `Record::key_values()`. | +| `SdkLogRecord` field | Source in Logforth | +| :------------------------- | :------------------------------------------------------------------------------------------------------------------------------------------------------------------ | +| `timestamp` | `Record::time()` (event creation time, preserved across `Async` queues and explicit `RecordBuilder::time`). | +| `observed_timestamp` | `SystemTime::now()` captured when `OpentelemetryLog::append` processes the record. | +| `severity_number` / `text` | 1-to-1 mapping across all 24 `Level` variants (`Trace`..=`Fatal4` -> `Severity::Trace`..=`Severity::Fatal4`) and `Level::name()`. | +| `body` | `AnyValue::String` from `Record::payload()` (omitted when empty), or custom `AnyValue` via `MakeBody` (such as `MakeBodyLayout`). | +| `trace_context` | Extracted from `Diagnostic` `("trace_id", "span_id")` (32- and 16-digit non-zero hex) and optional boolean `"sampled"`. | +| `attributes` | Present `code.module.name` (`module_path`), `code.file.path`, `code.line.number`, `code.column.number`, remaining `Diagnostic` entries, and `Record::key_values()`. | | `OpentelemetryLog` method | Effect | | :-------------------------- | :------------------------------------------------------------------------------------------------------- | @@ -68,7 +68,7 @@ Each `Record` is projected onto an `opentelemetry_sdk::logs::SdkLogRecord`: | `with_make_body(make_body)` | Overrides how `SdkLogRecord` bodies (`AnyValue`) are constructed via a custom `MakeBody` implementation. | | `allow_duplicate_keys()` | Forwards attributes in source location, diagnostic, then record order without deduplicating keys. | -By default, `OpentelemetryLog` enforces unique top-level attribute keys per the [OpenTelemetry attribute collection specification](https://opentelemetry.io/docs/specs/otel/common/#attribute-collections): explicit `Record::key_values()` override colliding `Diagnostic` entries, `Diagnostic` entries override source location attributes (`module_path` and `code.*`), and the last entry wins within each source while preserving relative order. Because many receivers handle duplicate attribute keys unpredictably, callers enabling `allow_duplicate_keys()` are responsible for ensuring exported keys are unique unless targeting a custom processor designed for repeated keys. +By default, `OpentelemetryLog` enforces unique top-level attribute keys per the [OpenTelemetry attribute collection specification](https://opentelemetry.io/docs/specs/otel/common/#attribute-collections): explicit `Record::key_values()` override colliding `Diagnostic` entries, `Diagnostic` entries override `code.*` source location attributes, and the last entry wins within each source while preserving relative order. Because many receivers handle duplicate attribute keys unpredictably, callers enabling `allow_duplicate_keys()` are responsible for ensuring exported keys are unique unless targeting a custom processor designed for repeated keys. `opentelemetry` and `opentelemetry_sdk` are re-exported at `logforth_append_opentelemetry::{opentelemetry, opentelemetry_sdk}` so callers do not need direct Cargo dependencies solely to name SDK traits or types. diff --git a/appenders/opentelemetry/src/lib.rs b/appenders/opentelemetry/src/lib.rs index d6ba1b51..90931a3f 100644 --- a/appenders/opentelemetry/src/lib.rs +++ b/appenders/opentelemetry/src/lib.rs @@ -25,7 +25,7 @@ //! - `trace_context` comes from [`Diagnostic`] entries when both `trace_id` and `span_id` are //! nonzero hexadecimal IDs of 32 and 16 digits respectively. An optional boolean `sampled` sets //! the trace flags. Unconsumed entries remain available as attributes. -//! - `attributes` begin with the available source location fields (`module_path` from +//! - `attributes` begin with the available source location fields (`code.module.name` from //! [`Record::module_path`], `code.file.path`, `code.line.number`, and `code.column.number`), //! followed by diagnostic entries and then [`Record::key_values`]. By default only the last //! occurrence of each key survives, in its original position. @@ -78,9 +78,9 @@ use opentelemetry_sdk::logs::SdkLogRecord; use opentelemetry_sdk::logs::SdkLogger; use opentelemetry_sdk::logs::SdkLoggerProvider; -// Rust compile-time callsites expose only `module_path!()`, not the enclosing function name. -// See https://github.com/open-telemetry/semantic-conventions/issues/4440. -const ATTR_MODULE_PATH: &str = "module_path"; +// Rust compile-time callsites expose only `module_path!()` without a function name. +// `code.module.name` is proposed in https://github.com/open-telemetry/semantic-conventions/issues/4440. +const ATTR_CODE_MODULE_NAME: &str = "code.module.name"; const ATTR_CODE_FILE_PATH: &str = "code.file.path"; const ATTR_CODE_LINE_NUMBER: &str = "code.line.number"; const ATTR_CODE_COLUMN_NUMBER: &str = "code.column.number"; @@ -304,13 +304,13 @@ fn add_source_location( mut keep: impl FnMut(&str) -> bool, ) { if let Some(module_path) = record.module_path_static() { - if keep(ATTR_MODULE_PATH) { - log_record.add_attribute(ATTR_MODULE_PATH, module_path); + if keep(ATTR_CODE_MODULE_NAME) { + log_record.add_attribute(ATTR_CODE_MODULE_NAME, module_path); } } else if let Some(module_path) = record.module_path() - && keep(ATTR_MODULE_PATH) + && keep(ATTR_CODE_MODULE_NAME) { - log_record.add_attribute(ATTR_MODULE_PATH, module_path.to_owned()); + log_record.add_attribute(ATTR_CODE_MODULE_NAME, module_path.to_owned()); } if let Some(file) = record.file_static() { diff --git a/tests-integration/tests/append_test/opentelemetry.rs b/tests-integration/tests/append_test/opentelemetry.rs index 9e27ece2..77e1bcc7 100644 --- a/tests-integration/tests/append_test/opentelemetry.rs +++ b/tests-integration/tests/append_test/opentelemetry.rs @@ -135,7 +135,10 @@ fn opentelemetry_appender_preserves_event_timestamp_maps_all_levels_and_shuts_do assert_eq!( attrs_vec(&first.record), vec![ - ("module_path".to_owned(), AnyValue::from("app::billing")), + ( + "code.module.name".to_owned(), + AnyValue::from("app::billing") + ), ( "code.file.path".to_owned(), AnyValue::from("src/billing.rs") @@ -213,7 +216,7 @@ fn opentelemetry_appender_encodes_typed_attributes_and_diagnostics() { .filter(|(k, _)| { !matches!( k.as_str(), - "module_path" | "code.file.path" | "code.line.number" | "code.column.number" + "code.module.name" | "code.file.path" | "code.line.number" | "code.column.number" ) }) .collect();