Expand description
Bridge log
into OpenTelemetry.
This library implements a log appender for the log
crate using the Logs Bridge API.
§Getting Started
The bridge requires configuration on both the log
and OpenTelemetry sides.
For OpenTelemetry, configure a LoggerProvider
with the desired exporter:
let exporter = opentelemetry_stdout::LogExporter::default();
let logger_provider = SdkLoggerProvider::builder()
.with_log_processor(BatchLogProcessor::builder(exporter).build())
.build();
For log
, set the global logger to an OpenTelemetryLogBridge
instance using the LoggerProvider
:
let otel_log_appender = OpenTelemetryLogBridge::new(&logger_provider);
log::set_boxed_logger(Box::new(otel_log_appender)).unwrap();
§Mapping Log Records
This section outlines how log records produced by log
are mapped into OpenTelemetry log records.
Each subsection deals with a different property on opentelemetry::logs::LogRecord
.
§Body
The body is the stringified message (log::Record::args
).
§Severity
The severity number and text are mapped from the log::Level
(log::Record::level
):
log::Level | Severity Text | Severity Number |
---|---|---|
Error | Error | 17 |
Warn | Warn | 13 |
Info | Info | 9 |
Debug | Debug | 5 |
Trace | Trace | 1 |
§Attributes
Any key-values (log::Record::key_values
) are converted into attributes:
Type | Result | Notes |
---|---|---|
i8 -i128 | AnyValue::Int | If the value is too big then it will be stringified using std::fmt::Display |
u8 -u128 | AnyValue::Int | If the value is too big then it will be stringified using std::fmt::Display |
f32 -f64 | AnyValue::Double | |
bool | AnyValue::Boolean | |
str | AnyValue::String | |
Bytes | AnyValue::Bytes | Requires the with-serde feature, otherwise it will be stringified using std::fmt::Debug |
() | - | Unit values are discared |
Some | Any | Some variants use their inner value |
None | - | None variants are discared |
Unit struct | AnyValue::String | Uses the name of the struct |
Unit variant | AnyValue::String | Uses the name of the variant |
Newtype struct | Any | Uses the inner value of the newtype |
Newtype variant | AnyValue::Map | An internally-tagged map. Requires the with-serde feature, otherwise it will be stringified using std::fmt::Debug |
Sequence | AnyValue::ListAny | Requires the with-serde feature, otherwise it will be stringified using std::fmt::Debug |
Tuple | AnyValue::ListAny | Requires the with-serde feature, otherwise it will be stringified using std::fmt::Debug |
Tuple struct | AnyValue::ListAny | Requires the with-serde feature, otherwise it will be stringified using std::fmt::Debug |
Tuple variant | AnyValue::Map | An internally-tagged map. Requires the with-serde feature, otherwise it will be stringified using std::fmt::Debug |
Map | AnyValue::Map | Requires the with-serde feature, otherwise it will be stringified using std::fmt::Debug |
Struct | AnyValue::Map | Requires the with-serde feature, otherwise it will be stringified using std::fmt::Debug |
Struct variant | AnyValue::Map | An internally-tagged map. Requires the with-serde feature, otherwise it will be stringified using std::fmt::Debug |
§Feature Flags
This library provides the following Cargo features:
spec_unstable_logs_enabled
: Allow users to control the log level.with-serde
: Support complex values as attributes without stringifying them.
§Supported Rust Versions
OpenTelemetry is built against the latest stable release. The minimum supported version is 1.70. The current OpenTelemetry version is not guaranteed to build on Rust versions earlier than the minimum supported version.
The current stable Rust compiler and the three most recent minor versions before it will always be supported. For example, if the current stable compiler version is 1.49, the minimum supported version will not be increased past 1.46, three minor versions prior. Increasing the minimum supported compiler version is not considered a semver breaking change as long as doing so complies with this policy.