-
-
Notifications
You must be signed in to change notification settings - Fork 59
feat: add OTLP log exporter #333
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
davidB
merged 5 commits into
davidB:main
from
cozyGalvinism:feature/implement-log-exporter
Apr 26, 2026
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
ce57015
feat: implement log exporter
cozyGalvinism 0321590
feat(examples): add logging example with OTLP export
cozyGalvinism c9eef50
feat(examples): extend logging example with more levels
cozyGalvinism d35503c
docs: add logs section to init-tracing-opentelemetry README
cozyGalvinism 01641b0
fix(logs): honor `.with_logs()`
cozyGalvinism File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| [package] | ||
| name = "examples-logging" | ||
| publish = false | ||
| edition.workspace = true | ||
| version.workspace = true | ||
| repository.workspace = true | ||
| license.workspace = true | ||
|
|
||
| [dependencies] | ||
| init-tracing-opentelemetry = { path = "../../init-tracing-opentelemetry", features = [ | ||
| "otlp", | ||
| "tracing_subscriber_ext", | ||
| "logs" | ||
| ] } | ||
| memory-stats = "1" | ||
| opentelemetry = { workspace = true } | ||
| serde_json = "1" | ||
| tokio = { version = "1", features = ["full"] } | ||
| tracing = { workspace = true } | ||
| tracing-opentelemetry-instrumentation-sdk = { path = "../../tracing-opentelemetry-instrumentation-sdk" } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| #[tracing::instrument] | ||
| async fn log() { | ||
| tracing::error!("This is ground control to Major Tom"); | ||
| tracing::warn!("Houston, we have a problem"); | ||
| tracing::info!("We have contact"); | ||
| tracing::debug!("Roger, copy that"); | ||
| tokio::time::sleep(tokio::time::Duration::from_secs(3)).await; | ||
| } | ||
|
|
||
| #[tracing::instrument] | ||
| async fn calc(a: i32, b: i32) { | ||
| let result = a + b; | ||
| tracing::info!(result, "calculated result"); | ||
| } | ||
|
|
||
| #[tokio::main] | ||
| async fn main() -> Result<(), Box<dyn std::error::Error>> { | ||
| // setting up tracing | ||
| let _guard = init_tracing_opentelemetry::TracingConfig::production().init_subscriber()?; | ||
|
|
||
| log().await; | ||
| calc(1, 2).await; | ||
|
|
||
| Ok(()) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,69 @@ | ||
| use super::infer_protocol; | ||
| use opentelemetry_otlp::{ExporterBuildError, LogExporter}; | ||
| use opentelemetry_sdk::{Resource, logs::LoggerProviderBuilder, logs::SdkLoggerProvider}; | ||
| #[cfg(feature = "tls")] | ||
| use {opentelemetry_otlp::WithTonicConfig, tonic::transport::ClientTlsConfig}; | ||
|
|
||
| #[must_use] | ||
| pub fn identity(v: LoggerProviderBuilder) -> LoggerProviderBuilder { | ||
| v | ||
| } | ||
|
|
||
| pub fn init_loggerprovider<F>( | ||
| resource: Resource, | ||
| transform: F, | ||
| ) -> Result<SdkLoggerProvider, ExporterBuildError> | ||
| where | ||
| F: FnOnce(LoggerProviderBuilder) -> LoggerProviderBuilder, | ||
| { | ||
| let (maybe_protocol, maybe_endpoint) = read_protocol_and_endpoint_from_env(); | ||
| let protocol = infer_protocol(maybe_protocol.as_deref(), maybe_endpoint.as_deref()); | ||
|
|
||
| let exporter: Option<LogExporter> = match protocol.as_deref() { | ||
| Some("http/protobuf") => Some(LogExporter::builder().with_http().build()?), | ||
| #[cfg(feature = "tls")] | ||
| Some("grpc/tls") => Some( | ||
| LogExporter::builder() | ||
| .with_tonic() | ||
| .with_tls_config(ClientTlsConfig::new().with_enabled_roots()) | ||
| .build()?, | ||
| ), | ||
| Some("grpc") => Some(LogExporter::builder().with_tonic().build()?), | ||
| Some(x) => { | ||
| tracing::warn!( | ||
| "unknown '{x}' env var set or infered for OTEL_EXPORTER_OTLP_LOGS_PROTOCOL or OTEL_EXPORTER_OTLP_PROTOCOL; no log exporter will be created" | ||
| ); | ||
| None | ||
| } | ||
| None => { | ||
| tracing::warn!( | ||
| "no env var set or infered for OTEL_EXPORTER_OTLP_LOGS_PROTOCOL or OTEL_EXPORTER_OTLP_PROTOCOL; no log exporter will be created" | ||
| ); | ||
| None | ||
| } | ||
| }; | ||
| let mut logger_provider = SdkLoggerProvider::builder().with_resource(resource); | ||
| if let Some(exporter) = exporter { | ||
| logger_provider = logger_provider.with_batch_exporter(exporter); | ||
| } | ||
|
|
||
| logger_provider = transform(logger_provider); | ||
| Ok(logger_provider.build()) | ||
| } | ||
|
|
||
| fn read_protocol_and_endpoint_from_env() -> (Option<String>, Option<String>) { | ||
| let maybe_protocol = std::env::var("OTEL_EXPORTER_OTLP_LOGS_PROTOCOL") | ||
| .or_else(|_| std::env::var("OTEL_EXPORTER_OTLP_PROTOCOL")) | ||
| .ok(); | ||
| let maybe_endpoint = std::env::var("OTEL_EXPORTER_OTLP_LOGS_ENDPOINT") | ||
| .or_else(|_| { | ||
| std::env::var("OTEL_EXPORTER_OTLP_ENDPOINT").map(|endpoint| match &maybe_protocol { | ||
| Some(protocol) if protocol.contains("http") => { | ||
| format!("{endpoint}/v1/logs") | ||
| } | ||
| _ => endpoint, | ||
| }) | ||
| }) | ||
| .ok(); | ||
| (maybe_protocol, maybe_endpoint) | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The
maybe_endpointretrieved from environment variables is never used to configure theLogExporter. This means the exporter will always use default endpoints (e.g.,http://localhost:4318/v1/logsfor HTTP orhttp://localhost:4317for gRPC), ignoring theOTEL_EXPORTER_OTLP_LOGS_ENDPOINTorOTEL_EXPORTER_OTLP_ENDPOINTsettings.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Similar logic is used for tracing code, I pretty much copied the implementation from traces. Can fix that though.