Skip to content

Commit

Permalink
Merge pull request #162 from emit-rs/docs/zipkin-prom-examples
Browse files Browse the repository at this point in the history
Include the examples for zipkin and prometheus
  • Loading branch information
KodrAus authored Oct 24, 2024
2 parents 2beffbc + 9bc84b7 commit a6bea8d
Show file tree
Hide file tree
Showing 8 changed files with 144 additions and 3 deletions.
4 changes: 3 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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.
2 changes: 1 addition & 1 deletion book/src/producing-events/metrics.md
Original file line number Diff line number Diff line change
Expand Up @@ -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._
2 changes: 1 addition & 1 deletion book/src/producing-events/tracing.md
Original file line number Diff line number Diff line change
Expand Up @@ -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._
16 changes: 16 additions & 0 deletions examples/metric_prometheus/Cargo.toml
Original file line number Diff line number Diff line change
@@ -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"
34 changes: 34 additions & 0 deletions examples/metric_prometheus/src/main.rs
Original file line number Diff line number Diff line change
@@ -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));
}
13 changes: 13 additions & 0 deletions examples/trace_zipkin/Cargo.toml
Original file line number Diff line number Diff line change
@@ -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"
72 changes: 72 additions & 0 deletions examples/trace_zipkin/src/main.rs
Original file line number Diff line number Diff line change
@@ -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<String, Error> {
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<i32, Error> {
Ok(n.parse()?)
}

#[emit::span("format {n}")]
fn format(n: i32) -> String {
n.to_string()
}

type Error = Box<dyn std::error::Error + 'static>;

fn err_as_ref(err: &Error) -> &(dyn std::error::Error + 'static) {
&**err
}

0 comments on commit a6bea8d

Please sign in to comment.