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

[DRAFT] Add Custom Thread-Based Runtime for OpenTelemetry batch Processing #2390

Draft
wants to merge 5 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 3 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 Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,4 @@ tokio-stream = "0.1"
tracing = { version = "0.1", default-features = false }
tracing-core = { version = "0.1", default-features = false }
tracing-subscriber = { version = "0.3", default-features = false }
url = { version = "2.5.2", default-features = false } #https://github.com/servo/rust-url/issues/992
url = {version = "=2.5.2", default-features = false }
15 changes: 15 additions & 0 deletions examples/logs-batch/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
[package]
name = "logs-batch"
version = "0.1.0"
edition = "2021"
license = "Apache-2.0"
publish = false

[dependencies]
opentelemetry = { path = "../../opentelemetry", features = ["logs"] }
opentelemetry_sdk = { path = "../../opentelemetry-sdk", features = ["logs"] }
opentelemetry-stdout = { path = "../../opentelemetry-stdout", features = ["logs"]}
opentelemetry-appender-log = { path = "../../opentelemetry-appender-log", default-features = false}
opentelemetry-semantic-conventions = { path = "../../opentelemetry-semantic-conventions" }
log = { workspace = true }
serde_json = { workspace = true }
17 changes: 17 additions & 0 deletions examples/logs-batch/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# OpenTelemetry Log Appender for log - Example using Batch Log Processor and thread runtime

This example shows how to use the opentelemetry-appender-log crate, which is a
[logging
appender](https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/glossary.md#log-appender--bridge)
that bridges logs from the [log crate](https://docs.rs/log/latest/log/) to
OpenTelemetry. The example setups a LoggerProvider with stdout exporter, so logs
are emitted to stdout.

## Usage

Run the following, and Logs emitted using [log](https://docs.rs/log/latest/log/)
will be written out to stdout.

```shell
cargo run
```
38 changes: 38 additions & 0 deletions examples/logs-batch/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
use log::{error, Level};
use opentelemetry::KeyValue;
use opentelemetry_appender_log::OpenTelemetryLogBridge;
use opentelemetry_sdk::logs::LoggerProvider;
use opentelemetry_sdk::thread_runtime::CustomThreadRuntime;
use opentelemetry_sdk::Resource;
use opentelemetry_semantic_conventions::resource::SERVICE_NAME;

fn main() {
// Setup LoggerProvider with a stdout exporter
let exporter = opentelemetry_stdout::LogExporter::default();
let runtime = CustomThreadRuntime::new(2, 5); // 1 worker thread
let logger_provider = LoggerProvider::builder()
.with_resource(Resource::new([KeyValue::new(
SERVICE_NAME,
"logs-basic-example",
)]))
.with_batch_exporter(exporter, runtime)
.build();

// Setup Log Appender for the log crate.
let otel_log_appender = OpenTelemetryLogBridge::new(&logger_provider);
log::set_boxed_logger(Box::new(otel_log_appender)).unwrap();
log::set_max_level(Level::Error.to_level_filter());

// Emit logs using macros from the log crate.
// These logs gets piped through OpenTelemetry bridge and gets exported to stdout.
// 10 error events
for i in 0..10000 {
error!(target: "my-target", "hello from {}. My price is {} at itr {}", "apple", 2.99, i);
//sleep 1 sec every 100 secs
if i % 10 == 0 {
std::thread::sleep(std::time::Duration::from_secs(1));
}
}
println!("Flushing logs explicitly before exiting..");
logger_provider.force_flush();
}
7 changes: 7 additions & 0 deletions opentelemetry-sdk/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ tokio = { workspace = true, features = ["rt", "time"], optional = true }
tokio-stream = { workspace = true, optional = true }
http = { workspace = true, optional = true }
tracing = {workspace = true, optional = true}
async-compat = { version = "0.2"}
Copy link
Member Author

@lalitb lalitb Dec 6, 2024

Choose a reason for hiding this comment

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

This package is for marshalling async events from futures runtime to tokio /async-std. Its dependency can be eliminated by integrating the necessary code from this package, as it appears to be relatively small.


[package.metadata.docs.rs]
all-features = true
Expand All @@ -36,6 +37,12 @@ rustdoc-args = ["--cfg", "docsrs"]
[dev-dependencies]
criterion = { workspace = true, features = ["html_reports"] }
temp-env = { workspace = true }
http = { workspace = true }
http-body-util = { workspace = true }
hyper = { workspace = true }
hyper-util = { workspace = true, features = ["client-legacy", "http1", "http2"]}
hyper-tls = "0.6"
reqwest = {workspace = true, features = ["blocking"]}

[target.'cfg(not(target_os = "windows"))'.dev-dependencies]
pprof = { version = "0.13", features = ["flamegraph", "criterion"] }
Expand Down
1 change: 1 addition & 0 deletions opentelemetry-sdk/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ pub mod runtime;
#[cfg(any(feature = "testing", test))]
#[cfg_attr(docsrs, doc(cfg(any(feature = "testing", test))))]
pub mod testing;
pub mod thread_runtime;

#[allow(deprecated)]
#[cfg(feature = "trace")]
Expand Down
Loading
Loading