From 9bc84b78476da23b42d0a707eb3cc9d092d9a148 Mon Sep 17 00:00:00 2001 From: Ashley Mannix Date: Fri, 25 Oct 2024 09:03:35 +1000 Subject: [PATCH] include the examples for zipkin and prometheus --- Cargo.toml | 4 +- README.md | 4 ++ book/src/producing-events/metrics.md | 2 +- book/src/producing-events/tracing.md | 2 +- examples/metric_prometheus/Cargo.toml | 16 ++++++ examples/metric_prometheus/src/main.rs | 34 ++++++++++++ examples/trace_zipkin/Cargo.toml | 13 +++++ examples/trace_zipkin/src/main.rs | 72 ++++++++++++++++++++++++++ 8 files changed, 144 insertions(+), 3 deletions(-) create mode 100644 examples/metric_prometheus/Cargo.toml create mode 100644 examples/metric_prometheus/src/main.rs create mode 100644 examples/trace_zipkin/Cargo.toml create mode 100644 examples/trace_zipkin/src/main.rs diff --git a/Cargo.toml b/Cargo.toml index 542db2a85..b01fb70f3 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -15,7 +15,9 @@ members = [ "examples/common_patterns", "examples/opentelemetry/direct_otlp", "examples/opentelemetry/via_sdk", - "test/ui", + "examples/trace_zipkin", + "examples/metric_prometheus", + "test/ui", "examples/trace_zipkin", "examples/metric_prometheus", ] [package] diff --git a/README.md b/README.md index 8b2935c1c..9b2501d67 100644 --- a/README.md +++ b/README.md @@ -59,6 +59,8 @@ fn greet(user: &str) { ![An example trace produced by `emit` in Zipkin](https://github.com/emit-rs/emit/blob/main/asset/trace-zipkin.png?raw=true) +The above screenshot was generated by [this example application](https://github.com/emit-rs/emit/tree/main/examples/trace_zipkin). + See [the guide](https://emit-rs.io/producing-events/tracing.html) for details. ## Metrics @@ -67,4 +69,6 @@ See [the guide](https://emit-rs.io/producing-events/tracing.html) for details. ![An example metric produced by `emit` in Prometheus](https://github.com/emit-rs/emit/blob/main/asset/metric-prometheus.png?raw=true) +The above screenshot was generated by [this example application](https://github.com/emit-rs/emit/tree/main/examples/metric_prometheus). + See [the guide](https://emit-rs.io/producing-events/metrics.html) for details. diff --git a/book/src/producing-events/metrics.md b/book/src/producing-events/metrics.md index 454439674..3b77de7d2 100644 --- a/book/src/producing-events/metrics.md +++ b/book/src/producing-events/metrics.md @@ -40,4 +40,4 @@ Event { ![an example metric in Prometheus](../asset/metric-prometheus.png) -_An example metric produced by `emit` in Prometheus_ +_A metric produced by [this example application](https://github.com/emit-rs/emit/tree/main/examples/metric_prometheus) in Prometheus._ diff --git a/book/src/producing-events/tracing.md b/book/src/producing-events/tracing.md index 2a125d35c..4e8821f6b 100644 --- a/book/src/producing-events/tracing.md +++ b/book/src/producing-events/tracing.md @@ -63,4 +63,4 @@ The level of a span may also depend on its execution. See [Fallible functions](. ![an example trace in Zipkin](../asset/trace-zipkin.png) -_An example trace produced by `emit` in Zipkin_ +_A trace produced by [this example application](https://github.com/emit-rs/emit/tree/main/examples/trace_zipkin) in Zipkin._ diff --git a/examples/metric_prometheus/Cargo.toml b/examples/metric_prometheus/Cargo.toml new file mode 100644 index 000000000..69f7d1475 --- /dev/null +++ b/examples/metric_prometheus/Cargo.toml @@ -0,0 +1,16 @@ +[package] +name = "emit_example_metric_prometheus" +version = "0.0.0" +edition = "2021" +publish = false + +[dependencies.emit] +version = "0.11.0-alpha.21" +path = "../../" + +[dependencies.emit_otlp] +version = "0.11.0-alpha.21" +path = "../../emitter/otlp" + +[dependencies.rand] +version = "0.8" diff --git a/examples/metric_prometheus/src/main.rs b/examples/metric_prometheus/src/main.rs new file mode 100644 index 000000000..aad70fe45 --- /dev/null +++ b/examples/metric_prometheus/src/main.rs @@ -0,0 +1,34 @@ +/*! +An example of emitting metrics to Prometheus. + +Prometheus has direct support for OTLP: https://prometheus.io/docs/guides/opentelemetry/ +*/ + +use rand::Rng; + +fn main() { + let rt = emit::setup() + .emit_to( + emit_otlp::new() + .resource(emit::props! { + #[emit::key("service.name")] + service_name: "emit-sample", + }) + .metrics(emit_otlp::metrics_http_proto( + "http://localhost:9090/api/v1/otlp/v1/metrics", + )) + .spawn(), + ) + .init(); + + let mut bytes_written = 0usize; + for _ in 0..60 { + bytes_written += rand::thread_rng().gen_range(0..750); + + emit::emit!(evt: emit::Metric::new(emit::pkg!(), "bytes_written", "count", emit::clock().now(), bytes_written, emit::Empty)); + + std::thread::sleep(std::time::Duration::from_secs(1)); + } + + rt.blocking_flush(std::time::Duration::from_secs(5)); +} diff --git a/examples/trace_zipkin/Cargo.toml b/examples/trace_zipkin/Cargo.toml new file mode 100644 index 000000000..5f5f83183 --- /dev/null +++ b/examples/trace_zipkin/Cargo.toml @@ -0,0 +1,13 @@ +[package] +name = "emit_example_trace_zipkin" +version = "0.0.0" +edition = "2021" +publish = false + +[dependencies.emit] +version = "0.11.0-alpha.21" +path = "../../" + +[dependencies.emit_otlp] +version = "0.11.0-alpha.21" +path = "../../emitter/otlp" diff --git a/examples/trace_zipkin/src/main.rs b/examples/trace_zipkin/src/main.rs new file mode 100644 index 000000000..b815641ee --- /dev/null +++ b/examples/trace_zipkin/src/main.rs @@ -0,0 +1,72 @@ +/*! +An example of emitting traces to Zipkin. + +You can emit traces as OTLP to the OpenTelemetry Collector, and forward from there to Zipkin. +Here's an example Collector configuration that does this: + +```yaml +receivers: + otlp: + protocols: + http: + endpoint: 0.0.0.0:4319 + +exporters: + zipkin: + endpoint: "http://localhost:9411/api/v2/spans" + format: proto + default_service_name: emit-sample + +service: + pipelines: + traces: + receivers: [otlp] + exporters: [zipkin] +``` +*/ + +fn main() { + let rt = emit::setup() + .emit_to( + emit_otlp::new() + .resource(emit::props! { + #[emit::key("service.name")] + service_name: "emit-sample", + }) + .traces(emit_otlp::traces_http_proto( + "http://localhost:4319/v1/traces", + )) + .spawn(), + ) + .init(); + + let _ = add("1", "3"); + + rt.blocking_flush(std::time::Duration::from_secs(5)); +} + +#[emit::span(err: err_as_ref, "add {a} and {b}")] +fn add(a: &str, b: &str) -> Result { + let a = parse(a)?; + let b = parse(b)?; + + let r = a + b; + + Ok(format(r)) +} + +#[emit::span(err: err_as_ref, "parse {n}")] +fn parse(n: &str) -> Result { + Ok(n.parse()?) +} + +#[emit::span("format {n}")] +fn format(n: i32) -> String { + n.to_string() +} + +type Error = Box; + +fn err_as_ref(err: &Error) -> &(dyn std::error::Error + 'static) { + &**err +}