From 133b23f937810f9f223e93ad1a0039a7f610529c Mon Sep 17 00:00:00 2001 From: Andy Lok Date: Wed, 18 Sep 2024 19:43:26 +0800 Subject: [PATCH] feat: add labels to opentelemetry append (#59) --- src/append/opentelemetry.rs | 25 +++++++++++++++++-------- 1 file changed, 17 insertions(+), 8 deletions(-) diff --git a/src/append/opentelemetry.rs b/src/append/opentelemetry.rs index ce292b5..2c9b692 100644 --- a/src/append/opentelemetry.rs +++ b/src/append/opentelemetry.rs @@ -12,6 +12,7 @@ // See the License for the specific language governing permissions and // limitations under the License. +use std::borrow::Cow; use std::sync::Arc; use std::time::Duration; use std::time::SystemTime; @@ -46,7 +47,6 @@ pub enum OpentelemetryWireProtocol { #[derive(Debug)] pub struct OpentelemetryLog { name: String, - category: String, library: Arc, provider: LoggerProvider, } @@ -54,12 +54,19 @@ pub struct OpentelemetryLog { impl OpentelemetryLog { pub fn new( name: impl Into, - category: impl Into, otlp_endpoint: impl Into, protocol: OpentelemetryWireProtocol, + ) -> Result { + Self::new_with_labels(name, otlp_endpoint, protocol, []) + } + + pub fn new_with_labels( + name: impl Into, + otlp_endpoint: impl Into, + protocol: OpentelemetryWireProtocol, + labels: impl IntoIterator, Cow<'static, str>)>, ) -> Result { let name = name.into(); - let category = category.into(); let otlp_endpoint = otlp_endpoint.into(); let exporter = match protocol { @@ -91,13 +98,18 @@ impl OpentelemetryLog { let provider = LoggerProvider::builder() .with_batch_exporter(exporter, opentelemetry_sdk::runtime::Tokio) + .with_resource(opentelemetry_sdk::Resource::new(labels.into_iter().map( + |(key, value)| opentelemetry::KeyValue { + key: key.into(), + value: value.into(), + }, + ))) .build(); let library = Arc::new(InstrumentationLibrary::builder(name.clone()).build()); Ok(Self { name, - category, library, provider, }) @@ -158,10 +170,7 @@ impl Append for OpentelemetryLog { .into_iter() .filter_map(|r| r.err()) { - eprintln!( - "failed to flush logger ({}@{}): {}", - self.name, self.category, err - ); + eprintln!("failed to flush logger {}: {}", self.name, err); } } }