From da81051446645d993966b955e007834a00717f64 Mon Sep 17 00:00:00 2001 From: andylokandy Date: Thu, 19 Sep 2024 16:32:58 +0800 Subject: [PATCH 1/2] chore: add OpentelemetryLogBuilder::with_labels --- src/append/opentelemetry.rs | 27 ++++++++++++++++++++++++--- src/layout/kv.rs | 4 ++++ 2 files changed, 28 insertions(+), 3 deletions(-) diff --git a/src/append/opentelemetry.rs b/src/append/opentelemetry.rs index 7a84d4a..147d215 100644 --- a/src/append/opentelemetry.rs +++ b/src/append/opentelemetry.rs @@ -80,16 +80,37 @@ impl OpentelemetryLogBuilder { } /// Add a label to the resource. - pub fn add_label(mut self, key: impl Into>, value: impl Into>) -> Self { + pub fn with_label( + mut self, + key: impl Into>, + value: impl Into>, + ) -> Self { self.labels.push((key.into(), value.into())); self } + /// Add multiple labels to the resource. + pub fn with_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 { name, endpoint, protocol, labels } = self; + let OpentelemetryLogBuilder { + name, + endpoint, + protocol, + labels, + } = self; - let collector_timeout = Duration::from_secs(opentelemetry_otlp::OTEL_EXPORTER_OTLP_TIMEOUT_DEFAULT); + let collector_timeout = + Duration::from_secs(opentelemetry_otlp::OTEL_EXPORTER_OTLP_TIMEOUT_DEFAULT); let exporter = match protocol { Protocol::Grpc => opentelemetry_otlp::new_exporter() .tonic() 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(); From 8d5c3d7f27ee16ab13f0c90474b0a0a6bf257911 Mon Sep 17 00:00:00 2001 From: tison Date: Tue, 8 Oct 2024 17:33:28 -0600 Subject: [PATCH 2/2] apply builder pattern Signed-off-by: tison --- src/append/opentelemetry.rs | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/append/opentelemetry.rs b/src/append/opentelemetry.rs index 429a3bb..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 with_label( + /// Append a label to the resource. + pub fn label( mut self, key: impl Into>, value: impl Into>, @@ -89,8 +89,8 @@ impl OpentelemetryLogBuilder { self } - /// Add multiple labels to the resource. - pub fn with_labels(mut self, labels: impl IntoIterator) -> Self + /// Append multiple labels to the resource. + pub fn labels(mut self, labels: impl IntoIterator) -> Self where K: Into>, V: Into>,