From fd89c8af51b684aef3b8dcb82a83f2e4af556ae7 Mon Sep 17 00:00:00 2001 From: Andy Lok Date: Wed, 9 Oct 2024 01:43:07 +0200 Subject: [PATCH] refactor: add OpentelemetryLogBuilder::labels (#61) Co-authored-by: tison --- src/append/opentelemetry.rs | 17 ++++++++++++++--- src/layout/kv.rs | 4 ++++ 2 files changed, 18 insertions(+), 3 deletions(-) diff --git a/src/append/opentelemetry.rs b/src/append/opentelemetry.rs index c0539a9..3dba414 100644 --- a/src/append/opentelemetry.rs +++ b/src/append/opentelemetry.rs @@ -70,7 +70,7 @@ impl OpentelemetryLogBuilder { /// Default to [`Grpc`]. /// /// [`Grpc`]: OpentelemetryWireProtocol::Grpc - pub fn with_protocol(mut self, protocol: OpentelemetryWireProtocol) -> Self { + pub fn protocol(mut self, protocol: OpentelemetryWireProtocol) -> Self { self.protocol = match protocol { OpentelemetryWireProtocol::Grpc => Protocol::Grpc, OpentelemetryWireProtocol::HttpBinary => Protocol::HttpBinary, @@ -79,8 +79,8 @@ impl OpentelemetryLogBuilder { self } - /// Add a label to the resource. - pub fn add_label( + /// Append a label to the resource. + pub fn label( mut self, key: impl Into>, value: impl Into>, @@ -89,6 +89,17 @@ impl OpentelemetryLogBuilder { self } + /// Append multiple labels to the resource. + pub fn labels(mut self, labels: impl IntoIterator) -> Self + where + K: Into>, + V: Into>, + { + self.labels + .extend(labels.into_iter().map(|(k, v)| (k.into(), v.into()))); + self + } + /// Build the [`OpentelemetryLog`] appender. pub fn build(self) -> Result { let OpentelemetryLogBuilder { diff --git a/src/layout/kv.rs b/src/layout/kv.rs index 8f9685b..cc32a5c 100644 --- a/src/layout/kv.rs +++ b/src/layout/kv.rs @@ -13,6 +13,8 @@ // limitations under the License. /// A helper struct to format log's key-value pairs. +/// +/// This is useful when you want to display log's key-value pairs in a log message. pub struct KvDisplay<'kvs> { kv: &'kvs dyn log::kv::Source, } @@ -47,6 +49,8 @@ impl<'a, 'kvs> log::kv::Visitor<'kvs> for KvWriter<'a, 'kvs> { } /// A helper to collect log's key-value pairs. +/// +/// This is useful when you want to collect log's key-value pairs for further processing. pub fn collect_kvs(kv: &dyn log::kv::Source) -> Vec<(String, String)> { let mut collector = KvCollector { kv: Vec::new() }; kv.visit(&mut collector).ok();