-
Notifications
You must be signed in to change notification settings - Fork 0
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
feat: Add OpenTelemetry setup function #11
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -7,6 +7,72 @@ | |||||
|
||||||
Random rust utility functions and types | ||||||
|
||||||
## Telemetry | ||||||
|
||||||
For using this module the feature flag `telemetry` need to be added. | ||||||
This module contains a set of helpers to work with OpenTelemetry logs, traces and metrics. | ||||||
|
||||||
### Setup | ||||||
|
||||||
For setup all that's needed it to run the function `famedly_rust_utils::famedly_rust_utils::telemetry::init_otel`. The function returns a guard that takes care of properly shutting down the providers. | ||||||
|
||||||
If no configuration is present the exporting of logs traces and metrics is disable and the stdout logging is enable. | ||||||
|
||||||
The functions on the crate exporting opentelemetry traces should be annotated with `tracing::instrument` to generate a new span for that function. Documentation on this macro can be found on the [here](https://docs.rs/tracing/latest/tracing/attr.instrument.html) | ||||||
|
||||||
The opentelemetry information is exported using gRPC to and opentelemetry collector. By default the expected endpoint is `http://localhots:4317` | ||||||
The default level of logging and traces is `info` and the default filter directive is `opentelemetry=off,tonic=off,h2=off,reqwest=info,axum=info,hyper=info,hyper-tls=info,tokio=info,tower=info,josekit=info,openssl=info` | ||||||
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. This part is outdated, I suppose |
||||||
|
||||||
```rust | ||||||
#[tokio::main] | ||||||
async fn main() { | ||||||
let _guard = init_otel(config).unwrap(); | ||||||
|
||||||
} | ||||||
``` | ||||||
|
||||||
|
||||||
### Propagate the context | ||||||
|
||||||
A context can be propagated to allow linking the traces from two different services. This is done by injecting the context information on the request and retrieving it on the other service. | ||||||
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.
Suggested change
Still not sure if this is gramatically correct :/ |
||||||
|
||||||
#### reqwest | ||||||
|
||||||
For injecting the current context using the reqwest client we can warp a client on a [reqwest-middleware](https://crates.io/crates/reqwest-middleware) and use the `OtelMiddleware` middleware present on the crate. | ||||||
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.
Suggested change
|
||||||
|
||||||
```rust | ||||||
use famedly_rust_utils::telemetry::OtelMiddleware; | ||||||
|
||||||
let reqwest_client = reqwest::Client::builder().build().unwrap(); | ||||||
let client = reqwest_middleware::ClientBuilder::new(reqwest_client) | ||||||
// Insert the tracing middleware | ||||||
.with(OtelMiddleware::default()) | ||||||
.build(); | ||||||
client.get("http://localhost").send().await; | ||||||
``` | ||||||
|
||||||
### axum | ||||||
|
||||||
For retrieving a context using axum we can use the `OtelAxumLayer` from [axum_tracing_opentelemetry](https://crates.io/crates/axum-tracing-opentelemetry) | ||||||
|
||||||
> [!WARNING] | ||||||
> This only seems to be working using the feature flag `tracing_level_info`. See the [issue](https://github.com/davidB/tracing-opentelemetry-instrumentation-sdk/issues/148) | ||||||
|
||||||
This layer should run as soon as possible | ||||||
|
||||||
```rust | ||||||
use axum_tracing_opentelemetry::middleware::OtelAxumLayer; | ||||||
|
||||||
Router::new().layer(OtelAxumLayer::default()) | ||||||
|
||||||
``` | ||||||
|
||||||
### Metrics | ||||||
|
||||||
For adding metrics all that it's needed it to make a trace with specific prefix. The documentation on how it works is [here](https://docs.rs/tracing-opentelemetry/latest/tracing_opentelemetry/struct.MetricsLayer.html#usage) | ||||||
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.
Suggested change
|
||||||
|
||||||
For adding metrics to axum servers creates like [tower-otel-http-metrics](https://github.com/francoposa/tower-otel-http-metrics) | ||||||
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.
Suggested change
|
||||||
|
||||||
## Lints | ||||||
|
||||||
```sh | ||||||
|
Original file line number | Diff line number | Diff line change | ||
---|---|---|---|---|
@@ -0,0 +1,107 @@ | ||||
//! OpenTelemetry configuration | ||||
//! | ||||
//! Module containing the configuration struct for the OpenTelemetry | ||||
use std::str::FromStr as _; | ||||
|
||||
use serde::Deserialize; | ||||
use url::Url; | ||||
|
||||
use crate::LevelFilter; | ||||
|
||||
const DEFAULT_ENDPOINT: &str = "http://localhost:4317"; | ||||
|
||||
/// OpenTelemetry configuration | ||||
#[derive(Debug, Deserialize, Clone, Default)] | ||||
pub struct OtelConfig { | ||||
/// Enables logs on stdout | ||||
pub stdout: Option<StdoutLogsConfig>, | ||||
/// Configurations for exporting traces, metrics and logs | ||||
pub exporter: Option<ExporterConfig>, | ||||
} | ||||
|
||||
/// Configuration for exporting OpenTelemetry data | ||||
#[derive(Debug, Deserialize, Clone, Default)] | ||||
pub struct ExporterConfig { | ||||
/// gRPC endpoint for exporting using OTELP | ||||
pub endpoint: Option<Url>, | ||||
/// Application service name | ||||
pub service_name: String, | ||||
/// Application version | ||||
pub version: String, | ||||
|
||||
/// Logs exporting config | ||||
pub logs: Option<ProviderConfig>, | ||||
/// Traces exporting config | ||||
pub traces: Option<ProviderConfig>, | ||||
/// Metrics exporting config | ||||
pub metrics: Option<ProviderConfig>, | ||||
} | ||||
|
||||
/// Stdout logs configuration | ||||
#[derive(Debug, Deserialize, Clone)] | ||||
pub struct StdoutLogsConfig { | ||||
/// Enables the stdout logs | ||||
pub enabled: bool, | ||||
/// Level for the crate | ||||
#[serde(default = "default_level_filter")] | ||||
pub level: LevelFilter, | ||||
/// Level for the dependencies | ||||
#[serde(default = "default_level_filter")] | ||||
pub general_level: LevelFilter, | ||||
} | ||||
|
||||
/// Provider configuration for OpenTelemetry export | ||||
#[derive(Debug, Deserialize, Clone)] | ||||
pub struct ProviderConfig { | ||||
/// Enables provider | ||||
pub enabled: bool, | ||||
/// Level for the crate | ||||
#[serde(default = "default_level_filter")] | ||||
pub level: LevelFilter, | ||||
/// Level for the dependencies | ||||
#[serde(default = "default_level_filter")] | ||||
pub general_level: LevelFilter, | ||||
} | ||||
|
||||
impl ProviderConfig { | ||||
#[allow(clippy::expect_used)] | ||||
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.
Suggested change
|
||||
pub(crate) fn get_filter(&self, crate_name: &'static str) -> String { | ||||
format!("{},{}={}", self.general_level, crate_name, self.level) | ||||
} | ||||
} | ||||
|
||||
impl StdoutLogsConfig { | ||||
#[allow(clippy::expect_used)] | ||||
pub(crate) fn get_filter(&self, crate_name: &'static str) -> String { | ||||
format!("{},{}={}", self.general_level, crate_name, self.level) | ||||
} | ||||
} | ||||
|
||||
impl Default for StdoutLogsConfig { | ||||
fn default() -> Self { | ||||
Self { enabled: true, level: default_level_filter(), general_level: default_level_filter() } | ||||
} | ||||
} | ||||
|
||||
impl Default for ProviderConfig { | ||||
fn default() -> Self { | ||||
Self { | ||||
enabled: false, | ||||
level: default_level_filter(), | ||||
general_level: default_level_filter(), | ||||
} | ||||
} | ||||
} | ||||
|
||||
impl ExporterConfig { | ||||
#[allow(clippy::expect_used)] | ||||
pub(crate) fn get_endpoint(&self) -> Url { | ||||
self.endpoint | ||||
.clone() | ||||
.unwrap_or(Url::from_str(DEFAULT_ENDPOINT).expect("Error parsing default endpoint")) | ||||
} | ||||
} | ||||
|
||||
const fn default_level_filter() -> LevelFilter { | ||||
LevelFilter(tracing::level_filters::LevelFilter::INFO) | ||||
} |
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.