-
Notifications
You must be signed in to change notification settings - Fork 34
OTLP push controller example #113
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
Merged
emschwartz
merged 6 commits into
autometrics-dev:main
from
egtwp:example-opentelemetry-push
Jun 19, 2023
Merged
Changes from 1 commit
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
387ad7c
[WIP] OTLP push controller example initial commit
egtwp ae21291
Put opentelemetry version warning in cargo.toml
egtwp dbfc2f1
Implemented code review suggestions
egtwp dcfad4b
Reduced metrics push interval + used `autometrics_example_util::sleep…
egtwp 2877043
Apply typos fix from code review
egtwp 9b61189
Merge branch 'main' into example-opentelemetry-push
egtwp File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next
Next commit
[WIP] OTLP push controller example initial commit
- Loading branch information
commit 387ad7c1c26aa08a6836b73fa1a5dab8316dce18
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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"] } | ||
egtwp marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| opentelemetry-otlp = { version = "0.12", features = ["tonic", "metrics"] } | ||
| opentelemetry-semantic-conventions = { version = "0.11" } | ||
| tokio = { version = "1", features = ["full"] } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 | ||
egtwp marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| 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. | ||
egtwp marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| ```bash | ||
| docker run -d --name otel-col \ | ||
egtwp marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| -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. | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| receivers: | ||
egtwp marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| otlp: | ||
| protocols: | ||
| grpc: | ||
| endpoint: 0.0.0.0:4317 | ||
|
|
||
| exporters: | ||
| logging: | ||
| loglevel: debug | ||
|
|
||
| processors: | ||
| batch: | ||
|
|
||
| service: | ||
| pipelines: | ||
| metrics: | ||
| receivers: [otlp] | ||
| processors: [] | ||
| exporters: [logging] | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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) { | ||
egtwp marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| println!("Doing stuff for {} seconds...", numb); | ||
| sleep(Duration::from_secs(numb)).await; | ||
egtwp marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
egtwp marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.