From 701c1a0159544d7a8a7438c0eae3898b2e16f722 Mon Sep 17 00:00:00 2001 From: Patrick Koenig Date: Fri, 21 Jul 2023 22:32:03 -0700 Subject: [PATCH] Install logger provider --- opentelemetry-otlp/src/logs.rs | 33 +++++++++++++++++++++------------ 1 file changed, 21 insertions(+), 12 deletions(-) diff --git a/opentelemetry-otlp/src/logs.rs b/opentelemetry-otlp/src/logs.rs index 05b22487b7..a899dbf06a 100644 --- a/opentelemetry-otlp/src/logs.rs +++ b/opentelemetry-otlp/src/logs.rs @@ -54,7 +54,10 @@ use std::{ time::Duration, }; -use opentelemetry_api::logs::{LogError, LoggerProvider}; +use opentelemetry_api::{ + global, + logs::{LogError, LoggerProvider}, +}; use opentelemetry_sdk::{self, export::logs::LogData, logs::BatchMessage, runtime::RuntimeChannel}; impl OtlpPipeline { @@ -419,11 +422,12 @@ impl OtlpLogPipeline { self } - /// Returns a [`Logger`] with the name `opentelemetry-otlp` and the - /// current crate version, using the configured log exporter. + /// Install the configured log exporter. + /// + /// Returns a [`Logger`] with the name `opentelemetry-otlp` and the current crate version. /// /// [`Logger`]: opentelemetry_sdk::logs::Logger - pub fn simple(self) -> Result { + pub fn install_simple(self) -> Result { Ok(build_simple_with_exporter( self.exporter_builder .ok_or(crate::Error::NoExporterBuilder)? @@ -432,12 +436,13 @@ impl OtlpLogPipeline { )) } - /// Returns a [`Logger`] with the name `opentelemetry-otlp` and the - /// current crate version, using the configured log exporter and a - /// batch log processor. + /// Install the configured log exporter and a batch span processor using the + /// specified runtime. + /// + /// Returns a [`Logger`] with the name `opentelemetry-otlp` and the current crate version. /// /// [`Logger`]: opentelemetry_sdk::logs::Logger - pub fn batch>( + pub fn install_batch>( self, runtime: R, ) -> Result { @@ -461,12 +466,14 @@ fn build_simple_with_exporter( provider_builder = provider_builder.with_config(config); } let provider = provider_builder.build(); - provider.versioned_logger( + let logger = provider.versioned_logger( Cow::Borrowed("opentelemetry-otlp"), Some(Cow::Borrowed(env!("CARGO_PKG_VERSION"))), None, None, - ) + ); + let _ = global::set_logger_provider(provider); + logger } fn build_batch_with_exporter>( @@ -480,10 +487,12 @@ fn build_batch_with_exporter>( provider_builder = provider_builder.with_config(config); } let provider = provider_builder.build(); - provider.versioned_logger( + let logger = provider.versioned_logger( Cow::Borrowed("opentelemetry-otlp"), Some(Cow::Borrowed("CARGO_PKG_VERSION")), None, None, - ) + ); + let _ = global::set_logger_provider(provider); + logger }