Crate opentelemetry_appender_log

Source
Expand description

Bridge log into OpenTelemetry.

This library implements a log appender for the log crate using the Logs Bridge API.

Supported Rust Versions

§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::LevelSeverity TextSeverity Number
ErrorError17
WarnWarn13
InfoInfo9
DebugDebug5
TraceTrace1

§Attributes

Any key-values (log::Record::key_values) are converted into attributes:

TypeResultNotes
i8-i128AnyValue::IntIf the value is too big then it will be stringified using std::fmt::Display
u8-u128AnyValue::IntIf the value is too big then it will be stringified using std::fmt::Display
f32-f64AnyValue::Double
boolAnyValue::Boolean
strAnyValue::String
BytesAnyValue::BytesRequires the with-serde feature, otherwise it will be stringified using std::fmt::Debug
()-Unit values are discared
SomeAnySome variants use their inner value
None-None variants are discared
Unit structAnyValue::StringUses the name of the struct
Unit variantAnyValue::StringUses the name of the variant
Newtype structAnyUses the inner value of the newtype
Newtype variantAnyValue::MapAn internally-tagged map. Requires the with-serde feature, otherwise it will be stringified using std::fmt::Debug
SequenceAnyValue::ListAnyRequires the with-serde feature, otherwise it will be stringified using std::fmt::Debug
TupleAnyValue::ListAnyRequires the with-serde feature, otherwise it will be stringified using std::fmt::Debug
Tuple structAnyValue::ListAnyRequires the with-serde feature, otherwise it will be stringified using std::fmt::Debug
Tuple variantAnyValue::MapAn internally-tagged map. Requires the with-serde feature, otherwise it will be stringified using std::fmt::Debug
MapAnyValue::MapRequires the with-serde feature, otherwise it will be stringified using std::fmt::Debug
StructAnyValue::MapRequires the with-serde feature, otherwise it will be stringified using std::fmt::Debug
Struct variantAnyValue::MapAn 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.

Structs§

OpenTelemetryLogBridge