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
Prev Previous commit
Next Next commit
rename examples to make it more understanding
  • Loading branch information
mellowagain committed Sep 7, 2023
commit ca685a612ecc704196007f94b3ff11cca64dccbf
4 changes: 2 additions & 2 deletions examples/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ cargo run --package example-{name of example}
- [axum](./axum) - Use autometrics to instrument HTTP handlers using the `axum` framework
- [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
- [opentelemetry-push](./opentelemetry-push/) - Push metrics to an OpenTelemetry Collector via the OTLP gRPC protocol
- [opentelemetry-push-http](./opentelemetry-push-http/) - Push metrics to an OpenTelemetry Collector via the OTLP HTTP protocol using the Autometrics provided interface
- [opentelemetry-push](./opentelemetry-push/) - Push metrics to an OpenTelemetry Collector via the OTLP HTTP or gRPC protocol using the Autometrics provided interface
- [opentelemetry-push-custom](./opentelemetry-push-custom/) - Push metrics to an OpenTelemetry Collector via the OTLP gRPC protocol using custom options

## Full Example

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

[dependencies]
autometrics = { path = "../../autometrics", features = ["opentelemetry-0_20"] }
autometrics-example-util = { path = "../util" }
# Note that the version of the opentelemetry crate MUST match
# the version used by autometrics
opentelemetry = { version = "0.20", features = ["metrics", "rt-tokio"] }
opentelemetry-otlp = { version = "0.13", features = ["tonic", "metrics"] }
opentelemetry-semantic-conventions = { version = "0.12.0" }
tokio = { version = "1", features = ["full"] }
Original file line number Diff line number Diff line change
@@ -1,22 +1,21 @@
# Autometrics + OTLP push controller
# Autometrics + OTLP push controller (custom)

This example demonstrates how you can push autometrics via OTLP HTTP protocol
to the [OpenTelemetry Collector](https://opentelemetry.io/docs/collector/) or another OTel-compatible solution. It
uses the Autometrics provided wrapper to achieve this.
This example demonstrates how you can push autometrics via OTLP gRPC protocol to the [OpenTelemetry Collector](https://opentelemetry.io/docs/collector/) or another OTel-compatible solution
without using the Autometrics provided interface.

## Running the example

### Start a basic OTEL-Collector

You can use the [`otel-collector-config.yml`](./otel-collector-config.yml) file to start an otel-collector container that listens on 0.0.0.0:4317 for incoming otlp-gRPC traffic as well as on 0.0.0.0:4318 for incoming otlp-http traffic, and exports the metrics it receives to stdout.
You can use the [`otel-collector-config.yml`](./otel-collector-config.yml) file to start an otel-collector container that listens on 0.0.0.0:4317 for incoming otlp-gRPC traffic, and exports the metrics it receives to stdout.

Run the following command in a second terminal to start a container in interactive mode:

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

You should see the collector initialization output, that should end with something like:
Expand All @@ -32,7 +31,7 @@ You should see the collector initialization output, that should end with somethi
Then come back on your primary shell and run this example:

```shell
cargo run -p example-opentelemetry-push-http
cargo run -p example-opentelemetry-push-custom
```

### Check the output
Expand Down Expand Up @@ -101,3 +100,7 @@ Then delete the container with
```bash
docker rm otel-col
```

## 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.
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
receivers:
otlp:
protocols:
http:
endpoint: 0.0.0.0:4318
grpc:
endpoint: 0.0.0.0:4317

Expand Down
49 changes: 49 additions & 0 deletions examples/opentelemetry-push-custom/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
use autometrics::autometrics;
use autometrics_example_util::sleep_random_duration;
use opentelemetry::metrics::MetricsError;
use opentelemetry::sdk::metrics::MeterProvider;
use opentelemetry::{runtime, Context};
use opentelemetry_otlp::{ExportConfig, WithExportConfig};
use std::error::Error;
use std::time::Duration;
use tokio::time::sleep;

fn init_metrics() -> Result<MeterProvider, MetricsError> {
let export_config = ExportConfig {
endpoint: "http://localhost:4317".to_string(),
..ExportConfig::default()
};
let push_interval = Duration::from_secs(1);
opentelemetry_otlp::new_pipeline()
.metrics(runtime::Tokio)
.with_exporter(
opentelemetry_otlp::new_exporter()
.tonic()
.with_export_config(export_config),
)
.with_period(push_interval)
.build()
}

#[autometrics]
async fn do_stuff() {
println!("Doing stuff...");
sleep_random_duration().await;
}

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

for _ in 0..100 {
do_stuff().await;
}

println!("Waiting so that we could see metrics going down...");
sleep(Duration::from_secs(10)).await;
meter_provider.force_flush(&cx)?;

meter_provider.shutdown()?;
Ok(())
}
10 changes: 0 additions & 10 deletions examples/opentelemetry-push-http/Cargo.toml

This file was deleted.

27 changes: 0 additions & 27 deletions examples/opentelemetry-push-http/src/main.rs

This file was deleted.

7 changes: 1 addition & 6 deletions examples/opentelemetry-push/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,6 @@ publish = false
edition = "2021"

[dependencies]
autometrics = { path = "../../autometrics", features = ["opentelemetry-0_20"] }
autometrics = { path = "../../autometrics", features = ["opentelemetry-0_20", "otel-push-exporter-http", "otel-push-exporter-tokio"] }
autometrics-example-util = { path = "../util" }
# Note that the version of the opentelemetry crate MUST match
# the version used by autometrics
opentelemetry = { version = "0.20", features = ["metrics", "rt-tokio"] }
opentelemetry-otlp = { version = "0.13", features = ["tonic", "metrics"] }
opentelemetry-semantic-conventions = { version = "0.12.0" }
tokio = { version = "1", features = ["full"] }
12 changes: 5 additions & 7 deletions examples/opentelemetry-push/README.md
Original file line number Diff line number Diff line change
@@ -1,20 +1,22 @@
# Autometrics + OTLP push controller

This example demonstrates how you can push autometrics via OTLP gRPC protocol to the [OpenTelemetry Collector](https://opentelemetry.io/docs/collector/) or another OTel-compatible solution.
This example demonstrates how you can push autometrics via OTLP HTTP and GRPC protocol
to the [OpenTelemetry Collector](https://opentelemetry.io/docs/collector/) or another OTel-compatible solution. It
uses the Autometrics provided wrapper to achieve this.

## Running the example

### Start a basic OTEL-Collector

You can use the [`otel-collector-config.yml`](./otel-collector-config.yml) file to start an otel-collector container that listens on 0.0.0.0:4317 for incoming otlp-gRPC traffic, and exports the metrics it receives to stdout.
You can use the [`otel-collector-config.yml`](./otel-collector-config.yml) file to start an otel-collector container that listens on 0.0.0.0:4317 for incoming otlp-gRPC traffic as well as on 0.0.0.0:4318 for incoming otlp-http traffic, and exports the metrics it receives to stdout.

Run the following command in a second terminal to start a container in interactive mode:

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

You should see the collector initialization output, that should end with something like:
Expand Down Expand Up @@ -99,7 +101,3 @@ Then delete the container with
```bash
docker rm otel-col
```

## 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.
2 changes: 2 additions & 0 deletions examples/opentelemetry-push/otel-collector-config.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
receivers:
otlp:
protocols:
http:
endpoint: 0.0.0.0:4318
grpc:
endpoint: 0.0.0.0:4317

Expand Down
30 changes: 4 additions & 26 deletions examples/opentelemetry-push/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,30 +1,9 @@
use autometrics::autometrics;
use autometrics::{autometrics, otel_push_exporter};
use autometrics_example_util::sleep_random_duration;
use opentelemetry::metrics::MetricsError;
use opentelemetry::sdk::metrics::MeterProvider;
use opentelemetry::{runtime, Context};
use opentelemetry_otlp::{ExportConfig, WithExportConfig};
use std::error::Error;
use std::time::Duration;
use tokio::time::sleep;

fn init_metrics() -> Result<MeterProvider, MetricsError> {
let export_config = ExportConfig {
endpoint: "http://localhost:4317".to_string(),
..ExportConfig::default()
};
let push_interval = Duration::from_secs(1);
opentelemetry_otlp::new_pipeline()
.metrics(runtime::Tokio)
.with_exporter(
opentelemetry_otlp::new_exporter()
.tonic()
.with_export_config(export_config),
)
.with_period(push_interval)
.build()
}

#[autometrics]
async fn do_stuff() {
println!("Doing stuff...");
Expand All @@ -33,17 +12,16 @@ async fn do_stuff() {

#[tokio::main]
async fn main() -> Result<(), Box<dyn Error + Send + Sync + 'static>> {
let meter_provider = init_metrics()?;
let cx = Context::current();
let _meter_provider = otel_push_exporter::init_http("http://0.0.0.0:4318")?;
// or: otel_push_exporter::init_grpc("http://0.0.0.0:4317");

for _ in 0..100 {
do_stuff().await;
}

println!("Waiting so that we could see metrics going down...");
sleep(Duration::from_secs(10)).await;
meter_provider.force_flush(&cx)?;

meter_provider.shutdown()?;
// no need to call `.shutdown` as the returned `OtelMeterProvider` has a `Drop` implementation
Ok(())
}