Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
1 change: 1 addition & 0 deletions examples/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ cargo run --package example-{name of example}
- [axum](./axum) - Use autometrics to instrument HTTP handlers
- [custom-metrics](./custom-metrics/) - Define your own custom metrics alongside the ones generated by autometrics (using any of the metrics collection crates)
- [exemplars-tracing](./exemplars-tracing/) - Use fields from `tracing::Span`s as Prometheus exemplars
- [otlp-push-controller](./otlp-push-controller/) - Push autometrics via OTLP gRPC protocol.

## Full Example

Expand Down
12 changes: 12 additions & 0 deletions examples/otlp-push-controller/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
[package]
name = "example-otlp-push-controller"
version = "0.0.0"
publish = false
edition = "2021"

[dependencies]
autometrics = { path = "../../autometrics", features = ["opentelemetry"] }
opentelemetry = { version = "0.19", features = ["metrics", "rt-tokio"] }
opentelemetry-otlp = { version = "0.12", features = ["tonic", "metrics"] }
opentelemetry-semantic-conventions = { version = "0.11" }
tokio = { version = "1", features = ["full"] }
30 changes: 30 additions & 0 deletions examples/otlp-push-controller/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# Autometrics + OTLP push controller

This example demonstrates how you can push autometrics via OTLP gRPC protocol to an OTEL-Collector or another compatible solution.

## ⚠️ Warning

At this step, it's absolutely required that the version of the opentelemetry crates used in the project matches the version used as a dependency in `autometrics-rs` crate (See [Issue 91](https://github.com/autometrics-dev/autometrics-rs/issues/91)).

## Running the example

### Start a basic OTEL-Collector

You can use the `otel-collector-config.yaml` file to start an otel-collector container that listen on 0.0.0.0:4317 for incoming otlp-gRPC traffic, and export received metrics to standard output.

```bash
docker run -d --name otel-col \
-p 4317:4317 -p 13133:13133 \
-v $PWD/otel-collector-config.yaml:/etc/otelcol/config.yaml \
otel/opentelemetry-collector:latest
```

### Execute example code

```shell
cargo run -p example-otlp-push-controller
```

## OpenTelemetry Metrics Push Controller

The metric push controller is implemented as from this [example](https://github.com/open-telemetry/opentelemetry-rust/blob/f20c9b40547ee20b6ec99414bb21abdd3a54d99b/examples/basic-otlp/src/main.rs#L35-L52) from `opentelemetry-rust` crate.
19 changes: 19 additions & 0 deletions examples/otlp-push-controller/otel-collector-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
receivers:
otlp:
protocols:
grpc:
endpoint: 0.0.0.0:4317

exporters:
logging:
loglevel: debug

processors:
batch:

service:
pipelines:
metrics:
receivers: [otlp]
processors: []
exporters: [logging]
51 changes: 51 additions & 0 deletions examples/otlp-push-controller/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
use autometrics::autometrics;
use opentelemetry::{runtime, Context};
use opentelemetry::sdk::export::metrics::aggregation::cumulative_temporality_selector;
use opentelemetry::sdk::metrics::controllers::BasicController;
use opentelemetry::sdk::metrics::selectors;
use opentelemetry::metrics;
use opentelemetry_otlp::{ExportConfig, WithExportConfig};
use tokio::time::sleep;
use std::error::Error;
use std::time::Duration;

fn init_metrics() -> metrics::Result<BasicController> {
let export_config = ExportConfig {
endpoint: "http://localhost:4317".to_string(),
..ExportConfig::default()
};
opentelemetry_otlp::new_pipeline()
.metrics(
selectors::simple::inexpensive(),
cumulative_temporality_selector(),
runtime::Tokio,
)
.with_exporter(
opentelemetry_otlp::new_exporter()
.tonic()
.with_export_config(export_config),
)
.build()
}

#[tokio::main]
async fn main() -> Result<(), Box<dyn Error + Send + Sync + 'static>> {
let meter_provider = init_metrics()?;
let cx = Context::current();

for numb in 0..10 {
do_stuff(numb).await;
}

println!("Waiting so that we could see metrics being pushed via OTLP every 10 seconds...");
sleep(Duration::from_secs(60)).await;
meter_provider.stop(&cx)?;

Ok(())
}

#[autometrics]
async fn do_stuff(numb: u64) {
println!("Doing stuff for {} seconds...", numb);
sleep(Duration::from_secs(numb)).await;
}