-
Notifications
You must be signed in to change notification settings - Fork 70
feat: Add opentelemetry initial declarative configuration #487
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
Open
andborja
wants to merge
10
commits into
open-telemetry:main
Choose a base branch
from
andborja:andborja/DeclarativeConfig
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
85505ee
feat: Add opentelemetry declarative configuration.
andborja 13bb9a9
Merge branch 'main' into andborja/DeclarativeConfig
andborja 51765dc
Improve test coverage
andborja 1d84ea6
Refactor configurators to providers
andborja 7803577
Make the model extensible for external crates
andborja f5eaf56
Merge branch 'main' into andborja/DeclarativeConfig
andborja aeb9d2b
Merge branch 'main' into andborja/DeclarativeConfig
andborja 8506458
Fix lint (ubuntu-22.04-arm)
andborja faca570
Use registration function instead of trait
andborja 0e39a74
Add missing new line at the end of Cargo.toml file
andborja 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
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,9 @@ | ||
| # Changelog | ||
|
|
||
| ## vNext | ||
|
|
||
| ## v0.1.0 | ||
|
|
||
| ### Added | ||
|
|
||
| - Initial declarative configuration for stdout (console) |
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,5 @@ | ||
| # Code owners file. | ||
| # This file controls who is tagged for review for any given pull request. | ||
|
|
||
| # For anything not explicitly taken by someone else: | ||
| * @open-telemetry/rust-approvers |
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,17 @@ | ||
| [package] | ||
| name = "opentelemetry-config-stdout" | ||
| version = "0.1.0" | ||
| description = "Declarative configuration for OpenTelemetry SDK using console (stdout) configuration" | ||
| license = "Apache-2.0" | ||
| edition = "2021" | ||
| rust-version = "1.75.0" | ||
|
|
||
| [dependencies] | ||
| opentelemetry-config = { path = "../opentelemetry-config" } | ||
| opentelemetry_sdk = { version = "0.31.0" } | ||
| opentelemetry-stdout = { version = "0.31.0" } | ||
| serde = { version = "1.0", features = ["derive"] } | ||
| serde_yaml = { version = "0.9.34" } | ||
|
|
||
| [lints] | ||
| workspace = true |
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,128 @@ | ||
| # OpenTelemetry Declarative Configuration for stdout (console) | ||
|
|
||
| ![OpenTelemetry — An observability framework for cloud-native software.][splash] | ||
|
|
||
| [splash]: https://raw.githubusercontent.com/open-telemetry/opentelemetry-rust/main/assets/logo-text.png | ||
|
|
||
| Declarative configuration for applications instrumented with [`OpenTelemetry`]. | ||
|
|
||
| [`OpenTelemetry`]: https://crates.io/crates/opentelemetry | ||
|
|
||
| ## Overview | ||
|
|
||
| This crate provides a declarative configuration extension for OpenTelemetry that enables stdout (console) metric exports. It integrates with the `opentelemetry-config` crate to allow YAML-based configuration of the console exporter. | ||
|
|
||
| ### Features | ||
|
|
||
| - Console/stdout metrics exporter configuration via YAML | ||
| - Support for both Delta and Cumulative temporality | ||
| - Integration with OpenTelemetry declarative configuration | ||
| - Simple registration API for declarative configuration | ||
|
|
||
| ## Installation | ||
|
|
||
| Add this to your `Cargo.toml`: | ||
|
|
||
| ```toml | ||
| [dependencies] | ||
| opentelemetry-config-stdout = "0.1.0" | ||
| ``` | ||
|
|
||
| ## Quick Start | ||
|
|
||
| ### 1. Create a YAML Configuration File | ||
|
|
||
| Create a file named `otel-config.yaml`: | ||
|
|
||
| ```yaml | ||
| metrics: | ||
| readers: | ||
| - periodic: | ||
| exporter: | ||
| console: | ||
| temporality: delta | ||
|
|
||
| resource: | ||
| service.name: "my-service" | ||
| service.version: "1.0.0" | ||
| ``` | ||
|
|
||
| ### 2. Load and Apply Configuration | ||
|
|
||
| ```rust | ||
| use opentelemetry_config::{ConfigurationProvidersRegistry, providers::TelemetryProvider}; | ||
|
|
||
| fn main() -> Result<(), Box<dyn std::error::Error>> { | ||
| // Create configuration registry and register console exporter | ||
| let mut registry = ConfigurationProvidersRegistry::new(); | ||
| let metrics_registry = registry.metrics_mut(); | ||
| metrics_registry.register_periodic_exporter_factory( | ||
| "console".to_string(), | ||
| opentelemetry_config_stdout::register_console_exporter | ||
| ); | ||
|
|
||
| // Load configuration from YAML file | ||
| let telemetry_provider = TelemetryProvider::new(); | ||
| let providers = telemetry_provider | ||
| .configure_from_yaml_file(®istry, "otel-config.yaml")?; | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can this be a static method, so we don't need to create instance of TelemetryProvider? |
||
|
|
||
| // Use the configured providers | ||
| if let Some(meter_provider) = providers.meter_provider() { | ||
| // Your application code here | ||
|
|
||
| // Shutdown the created meter provider when done. | ||
| meter_provider.shutdown()?; | ||
| } | ||
|
|
||
| Ok(()) | ||
| } | ||
| ``` | ||
|
|
||
| ## Examples | ||
|
|
||
| ### Console Exporter Example | ||
|
|
||
| See the [examples/console](examples/console) directory for a complete working example that demonstrates: | ||
|
|
||
| - Setting up a console exporter factory | ||
| - Loading configuration from a YAML file | ||
| - Configuring a meter provider | ||
| - Proper shutdown handling | ||
|
|
||
| To run the example: | ||
|
|
||
| ```bash | ||
| cd examples/console | ||
| cargo run -- --file ../metrics_console.yaml | ||
| ``` | ||
|
|
||
| ## Configuration Schema | ||
|
|
||
| ### Metrics Configuration | ||
|
|
||
| ```yaml | ||
| metrics: | ||
| readers: | ||
| - periodic: | ||
| exporter: | ||
| console: | ||
| temporality: delta # or cumulative | ||
| ``` | ||
|
|
||
| #### Configuration Options | ||
|
|
||
| - **`temporality`** (optional): Controls how metrics are aggregated | ||
| - `delta`: Reports the change since the last export (useful for rate-based metrics like requests per second) | ||
| - `cumulative`: Reports the total accumulated value since the start (default, useful for gauges and cumulative counters) | ||
|
|
||
| ## Contributing | ||
|
|
||
| Contributions are welcome! Please feel free to submit issues or pull requests. | ||
|
|
||
| ## License | ||
|
|
||
| This project is licensed under the Apache-2.0 license. | ||
|
|
||
| ## Release Notes | ||
|
|
||
| You can find the release notes (changelog) [here](CHANGELOG.md). | ||
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,13 @@ | ||
| [package] | ||
| name = "opentelemetry-config-console-example" | ||
| version = "0.1.0" | ||
| description = "Declarative configuration for OpenTelemetry SDK example using console configuration" | ||
| license = "Apache-2.0" | ||
| edition = "2021" | ||
| rust-version = "1.75.0" | ||
|
|
||
| [workspace] | ||
|
|
||
| [dependencies] | ||
| opentelemetry-config-stdout = { path = "../../" } | ||
| opentelemetry-config = { path = " ../../../../../opentelemetry-config" } |
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,59 @@ | ||
| //! # Example OpenTelemetry Config Console | ||
| //! | ||
| //! This example demonstrates how to configure OpenTelemetry Metrics | ||
| //! using the OpenTelemetry Config crate with a Console Exporter. | ||
|
|
||
| use opentelemetry_config::{providers::TelemetryProvider, ConfigurationProvidersRegistry}; | ||
|
|
||
| use std::env; | ||
|
|
||
| pub fn main() -> Result<(), Box<dyn std::error::Error>> { | ||
| let args: Vec<String> = env::args().collect(); | ||
|
|
||
| if args.len() == 1 || (args.len() > 1 && args[1] == "--help") { | ||
| println!("Usage: cargo run -- --file ../metrics_console.yaml"); | ||
| println!("This example demonstrates how to configure OpenTelemetry Metrics using the OpenTelemetry Config crate with a Console Exporter."); | ||
| return Ok(()); | ||
| } | ||
| if args.len() < 3 || args[1] != "--file" { | ||
| println!("Error: Configuration file path not provided."); | ||
| println!("Usage: cargo run -- --file ../metrics_console.yaml"); | ||
| return Ok(()); | ||
| } | ||
| let config_file = &args[2]; | ||
|
|
||
| // Setup configuration registry with console exporter provider. | ||
| let mut configuration_providers_registry = ConfigurationProvidersRegistry::new(); | ||
| let metrics_registry = configuration_providers_registry.metrics_mut(); | ||
| metrics_registry.register_periodic_exporter_factory( | ||
| "console".to_string(), | ||
| opentelemetry_config_stdout::register_console_exporter, | ||
| ); | ||
|
|
||
| let telemetry_provider = TelemetryProvider::new(); | ||
| let providers = telemetry_provider | ||
| .configure_from_yaml_file(&configuration_providers_registry, config_file)?; | ||
|
|
||
| if let Some(meter_provider) = providers.meter_provider() { | ||
| println!("Meter provider is configured. Shutting it down..."); | ||
| meter_provider.shutdown()?; | ||
| } else { | ||
| println!("No Meter Provider configured."); | ||
| } | ||
|
|
||
| if let Some(logs_provider) = providers.logs_provider() { | ||
| println!("Logs provider is configured. Shutting it down..."); | ||
| logs_provider.shutdown()?; | ||
| } else { | ||
| println!("No Logs Provider configured."); | ||
| } | ||
|
|
||
| if let Some(traces_provider) = providers.traces_provider() { | ||
| println!("Traces provider is configured. Shutting it down..."); | ||
| traces_provider.shutdown()?; | ||
| } else { | ||
| println!("No Traces Provider configured."); | ||
| } | ||
|
|
||
| Ok(()) | ||
| } |
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,9 @@ | ||
| metrics: | ||
| readers: | ||
| - periodic: | ||
| exporter: | ||
| console: | ||
| temporality: delta | ||
| resource: | ||
| service.name: "test-service" | ||
| service.version: "1.0.0" |
Oops, something went wrong.
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is
metrics_mutavoidable by directly providingregister_metric_config?