Skip to content
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

Add stdout exporter example for Logs. #1072

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion opentelemetry-stdout/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ categories = [
"development-tools::profiling",
"asynchronous",
]
keywords = ["opentelemetry", "tracing", "metrics"]
keywords = ["opentelemetry", "tracing", "metrics", "logs"]
license = "Apache-2.0"
edition = "2021"
rust-version = "1.60"
Expand Down
58 changes: 58 additions & 0 deletions opentelemetry-stdout/examples/logs_basics.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
//! run with `$ cargo run --example basic --all-features

#[cfg(all(feature = "logs", feature = "trace"))]
use opentelemetry_api::{
logs::LoggerProvider as _,
logs::LogRecordBuilder,
logs::Logger,
logs::Severity,
trace::{Span, Tracer, TracerProvider as _, mark_span_as_active},
Context, KeyValue,
};
#[cfg(all(feature = "logs", feature = "trace"))]
use opentelemetry_sdk::{
logs::LoggerProvider,
runtime,
trace::TracerProvider,
};

#[cfg(all(feature = "logs", feature = "trace"))]
fn init_trace() -> TracerProvider {
let exporter = opentelemetry_stdout::SpanExporter::default();
TracerProvider::builder()
.with_simple_exporter(exporter)
.build()
}

#[cfg(all(feature = "logs", feature = "trace"))]
fn init_logger() -> LoggerProvider {
let exporter = opentelemetry_stdout::LogExporter::default();
LoggerProvider::builder()
.with_simple_exporter(exporter)
.build()
}

#[tokio::main]
#[cfg(all(feature = "logs", feature = "trace"))]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
use opentelemetry_api::logs::LogRecordBuilder;

let logger_provider = init_logger();
let tracer_provider = init_trace();
let cx = Context::new();

let tracer = tracer_provider.tracer("stdout-test");
let logger = logger_provider.versioned_logger("stdout-test", None, None, None, true);
let mut span = tracer.start("test_span");
span.set_attribute(KeyValue::new("test_key", "test_value"));
let span_active = mark_span_as_active(span);
let log_record = LogRecordBuilder::new()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should not be showing this, as this is never intended for end-users to use like this. This could be part of a tutorial for "how to write own appenders", or shown with existing log api like in this PR: #1071

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes agree, but I think it is still helpful to have a vendor neutral example (with no external dependencies) using only core API and SDK - which would be useful to both - external developers who want to (somehow) use only the core api and sdk in their application, and the developers creating their own appenders/subscriber. We can add a disclaimer as comment for LogRecordBuilder that it is not recommended for front facing API if that helps.

Said that, I don't have strong opinion, and can close this PR if we all agree this not to be good showcase example :)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Options:

  1. If the intended audience is authors of new appenders, cover it in the docs, no need of example.
  2. Keep this, but explicitly warn at the top saying this is not for end users to call directly. They should use the existing Log APIs. (eg: slog,log,tracing...)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd go for option 1 as that seems least confusing for lower context people.

.with_body("test log".into())
.with_severity_number(Severity::Info)
.build();
logger.emit(log_record);
drop(span_active);
Ok(())
}
#[cfg(not(all(feature = "logs", feature = "trace")))]
fn main() {}