Skip to content

Commit

Permalink
Treat log's key-values as attributes in log bridge (#1628)
Browse files Browse the repository at this point in the history
Co-authored-by: Zhongyang Wu <[email protected]>
Co-authored-by: Cijo Thomas <[email protected]>
  • Loading branch information
3 people authored May 2, 2024
1 parent 832fad4 commit 9eb548a
Show file tree
Hide file tree
Showing 5 changed files with 1,025 additions and 31 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ futures-util = { version = "0.3", default-features = false }
hyper = { version = "0.14", default-features = false }
http = { version = "0.2", default-features = false }
isahc = { version = "1.4", default-features = false }
log = "0.4"
log = "0.4.21"
once_cell = "1.13"
ordered-float = "4.0"
pin-project-lite = "0.2"
Expand Down
2 changes: 2 additions & 0 deletions opentelemetry-appender-log/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

## vNext

- Add log key-values as attributes [#1628](https://github.com/open-telemetry/opentelemetry-rust/pull/1628)

## v0.3.0

## v0.2.0
Expand Down
7 changes: 6 additions & 1 deletion opentelemetry-appender-log/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,17 @@ edition = "2021"

[dependencies]
opentelemetry = { version = "0.22", path = "../opentelemetry", features = ["logs"]}
log = { workspace = true, features = ["kv_unstable", "std"]}
log = { workspace = true, features = ["kv", "std"]}
serde = { workspace = true, optional = true, features = ["std"] }

[features]
logs_level_enabled = ["opentelemetry/logs_level_enabled"]
with-serde = ["log/kv_serde", "serde"]
default = ["logs_level_enabled"]

[dev-dependencies]
opentelemetry_sdk = { path = "../opentelemetry-sdk", features = [ "testing", "logs_level_enabled" ] }
opentelemetry-stdout = { path = "../opentelemetry-stdout", features = ["logs"]}
log = { workspace = true, features = ["kv_serde"] }
tokio = { workspace = true }
serde = { workspace = true, features = ["std", "derive"] }
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//! run with `$ cargo run --example logs-basic-in-memory
//! run with `$ cargo run --example logs-basic`
/// This example shows how to use in_memory_exporter for logs. This uses opentelemetry-appender-log crate, which is a
/// [logging appender](https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/glossary.md#log-appender--bridge) that bridges logs from the [log crate](https://docs.rs/log/latest/log/) to OpenTelemetry.
Expand All @@ -8,15 +8,15 @@ use log::{error, info, warn, Level};
use opentelemetry_appender_log::OpenTelemetryLogBridge;
use opentelemetry_sdk::logs::{BatchLogProcessor, LoggerProvider};
use opentelemetry_sdk::runtime;
use opentelemetry_sdk::testing::logs::InMemoryLogsExporter;
use opentelemetry_stdout::LogExporter;

#[tokio::main]
async fn main() {
//Create an InMemoryLogsExporter
let exporter: InMemoryLogsExporter = InMemoryLogsExporter::default();
//Create an exporter that writes to stdout
let exporter = LogExporter::default();
//Create a LoggerProvider and register the exporter
let logger_provider = LoggerProvider::builder()
.with_log_processor(BatchLogProcessor::builder(exporter.clone(), runtime::Tokio).build())
.with_log_processor(BatchLogProcessor::builder(exporter, runtime::Tokio).build())
.build();

// Setup Log Appender for the log crate.
Expand All @@ -25,14 +25,12 @@ async fn main() {
log::set_max_level(Level::Info.to_level_filter());

// Emit logs using macros from the log crate.
error!("hello from {}. My price is {}", "apple", 2.99);
let fruit = "apple";
let price = 2.99;

error!(fruit, price; "hello from {fruit}. My price is {price}");
warn!("warn!");
info!("test log!");

logger_provider.force_flush();

let emitted_logs = exporter.get_emitted_logs().unwrap();
for log in emitted_logs {
println!("{:?}", log);
}
}
Loading

0 comments on commit 9eb548a

Please sign in to comment.