Skip to content
This repository was archived by the owner on Sep 10, 2024. It is now read-only.

Commit aa6178a

Browse files
committed
Flatten the telemetry config section
1 parent 809fe16 commit aa6178a

File tree

4 files changed

+128
-176
lines changed

4 files changed

+128
-176
lines changed

crates/cli/src/telemetry.rs

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,10 @@ use std::time::Duration;
1616

1717
use anyhow::Context as _;
1818
use hyper::{header::CONTENT_TYPE, Body, Response};
19-
use mas_config::{MetricsExporterConfig, Propagator, TelemetryConfig, TracingExporterConfig};
19+
use mas_config::{
20+
MetricsConfig, MetricsExporterKind, Propagator, TelemetryConfig, TracingConfig,
21+
TracingExporterKind,
22+
};
2023
use opentelemetry::{
2124
global,
2225
propagation::{TextMapCompositePropagator, TextMapPropagator},
@@ -52,9 +55,9 @@ pub fn setup(config: &TelemetryConfig) -> anyhow::Result<Option<Tracer>> {
5255
mas_http::set_propagator(&propagator);
5356
global::set_text_map_propagator(propagator);
5457

55-
let tracer = tracer(&config.tracing.exporter).context("Failed to configure traces exporter")?;
58+
let tracer = tracer(&config.tracing).context("Failed to configure traces exporter")?;
5659

57-
init_meter(&config.metrics.exporter).context("Failed to configure metrics exporter")?;
60+
init_meter(&config.metrics).context("Failed to configure metrics exporter")?;
5861

5962
Ok(tracer)
6063
}
@@ -114,13 +117,13 @@ fn otlp_tracer(endpoint: Option<&Url>) -> anyhow::Result<Tracer> {
114117
Ok(tracer)
115118
}
116119

117-
fn tracer(config: &TracingExporterConfig) -> anyhow::Result<Option<Tracer>> {
118-
let tracer_provider = match config {
119-
TracingExporterConfig::None => return Ok(None),
120-
TracingExporterConfig::Stdout => stdout_tracer_provider(),
121-
TracingExporterConfig::Otlp { endpoint } => {
120+
fn tracer(config: &TracingConfig) -> anyhow::Result<Option<Tracer>> {
121+
let tracer_provider = match config.exporter {
122+
TracingExporterKind::None => return Ok(None),
123+
TracingExporterKind::Stdout => stdout_tracer_provider(),
124+
TracingExporterKind::Otlp => {
122125
// The OTLP exporter already creates a tracer and installs it
123-
return Ok(Some(otlp_tracer(endpoint.as_ref())?));
126+
return Ok(Some(otlp_tracer(config.endpoint.as_ref())?));
124127
}
125128
};
126129

@@ -208,15 +211,15 @@ fn prometheus_metric_reader() -> anyhow::Result<PrometheusExporter> {
208211
Ok(exporter)
209212
}
210213

211-
fn init_meter(config: &MetricsExporterConfig) -> anyhow::Result<()> {
214+
fn init_meter(config: &MetricsConfig) -> anyhow::Result<()> {
212215
let meter_provider_builder = SdkMeterProvider::builder();
213-
let meter_provider_builder = match config {
214-
MetricsExporterConfig::None => meter_provider_builder.with_reader(ManualReader::default()),
215-
MetricsExporterConfig::Stdout => meter_provider_builder.with_reader(stdout_metric_reader()),
216-
MetricsExporterConfig::Otlp { endpoint } => {
217-
meter_provider_builder.with_reader(otlp_metric_reader(endpoint.as_ref())?)
216+
let meter_provider_builder = match config.exporter {
217+
MetricsExporterKind::None => meter_provider_builder.with_reader(ManualReader::default()),
218+
MetricsExporterKind::Stdout => meter_provider_builder.with_reader(stdout_metric_reader()),
219+
MetricsExporterKind::Otlp => {
220+
meter_provider_builder.with_reader(otlp_metric_reader(config.endpoint.as_ref())?)
218221
}
219-
MetricsExporterConfig::Prometheus => {
222+
MetricsExporterKind::Prometheus => {
220223
meter_provider_builder.with_reader(prometheus_metric_reader()?)
221224
}
222225
};

crates/config/src/sections/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,8 @@ pub use self::{
4646
policy::PolicyConfig,
4747
secrets::SecretsConfig,
4848
telemetry::{
49-
MetricsConfig, MetricsExporterConfig, Propagator, TelemetryConfig, TracingConfig,
50-
TracingExporterConfig,
49+
MetricsConfig, MetricsExporterKind, Propagator, TelemetryConfig, TracingConfig,
50+
TracingExporterKind,
5151
},
5252
templates::TemplatesConfig,
5353
upstream_oauth2::{

crates/config/src/sections/telemetry.rs

Lines changed: 28 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -35,83 +35,74 @@ pub enum Propagator {
3535
Jaeger,
3636
}
3737

38-
fn otlp_endpoint_example() -> &'static str {
39-
"https://localhost:4318"
38+
#[allow(clippy::unnecessary_wraps)]
39+
fn otlp_endpoint_default() -> Option<String> {
40+
Some("https://localhost:4318".to_owned())
4041
}
4142

4243
/// Exporter to use when exporting traces
4344
#[skip_serializing_none]
44-
#[derive(Clone, Debug, Serialize, Deserialize, JsonSchema)]
45-
#[serde(tag = "exporter", rename_all = "lowercase", deny_unknown_fields)]
46-
pub enum TracingExporterConfig {
45+
#[derive(Clone, Copy, Debug, Serialize, Deserialize, JsonSchema, Default)]
46+
#[serde(rename_all = "lowercase")]
47+
pub enum TracingExporterKind {
4748
/// Don't export traces
49+
#[default]
4850
None,
4951

5052
/// Export traces to the standard output. Only useful for debugging
5153
Stdout,
5254

5355
/// Export traces to an OpenTelemetry protocol compatible endpoint
54-
Otlp {
55-
/// OTLP compatible endpoint
56-
#[schemars(url, example = "otlp_endpoint_example")]
57-
#[serde(default)]
58-
endpoint: Option<Url>,
59-
},
60-
}
61-
62-
impl Default for TracingExporterConfig {
63-
fn default() -> Self {
64-
Self::None
65-
}
56+
Otlp,
6657
}
6758

6859
/// Configuration related to exporting traces
6960
#[derive(Clone, Debug, Default, Serialize, Deserialize, JsonSchema)]
7061
pub struct TracingConfig {
7162
/// Exporter to use when exporting traces
72-
#[serde(default, flatten)]
73-
pub exporter: TracingExporterConfig,
63+
#[serde(default)]
64+
pub exporter: TracingExporterKind,
65+
66+
/// OTLP exporter: OTLP over HTTP compatible endpoint
67+
#[serde(skip_serializing_if = "Option::is_none")]
68+
#[schemars(url, default = "otlp_endpoint_default")]
69+
pub endpoint: Option<Url>,
7470

7571
/// List of propagation formats to use for incoming and outgoing requests
7672
pub propagators: Vec<Propagator>,
7773
}
7874

7975
/// Exporter to use when exporting metrics
8076
#[skip_serializing_none]
81-
#[derive(Clone, Debug, Serialize, Deserialize, JsonSchema)]
82-
#[serde(tag = "exporter", rename_all = "lowercase")]
83-
pub enum MetricsExporterConfig {
77+
#[derive(Clone, Copy, Debug, Serialize, Deserialize, JsonSchema, Default)]
78+
#[serde(rename_all = "lowercase")]
79+
pub enum MetricsExporterKind {
8480
/// Don't export metrics
81+
#[default]
8582
None,
8683

8784
/// Export metrics to stdout. Only useful for debugging
8885
Stdout,
8986

9087
/// Export metrics to an OpenTelemetry protocol compatible endpoint
91-
Otlp {
92-
/// OTLP compatible endpoint
93-
#[schemars(url, example = "otlp_endpoint_example")]
94-
#[serde(default)]
95-
endpoint: Option<Url>,
96-
},
88+
Otlp,
9789

9890
/// Export metrics via Prometheus. An HTTP listener with the `prometheus`
9991
/// resource must be setup to expose the Promethes metrics.
10092
Prometheus,
10193
}
10294

103-
impl Default for MetricsExporterConfig {
104-
fn default() -> Self {
105-
Self::None
106-
}
107-
}
108-
10995
/// Configuration related to exporting metrics
11096
#[derive(Clone, Debug, Default, Serialize, Deserialize, JsonSchema)]
11197
pub struct MetricsConfig {
11298
/// Exporter to use when exporting metrics
113-
#[serde(default, flatten)]
114-
pub exporter: MetricsExporterConfig,
99+
#[serde(default)]
100+
pub exporter: MetricsExporterKind,
101+
102+
/// OTLP exporter: OTLP over HTTP compatible endpoint
103+
#[serde(skip_serializing_if = "Option::is_none")]
104+
#[schemars(url, default = "otlp_endpoint_default")]
105+
pub endpoint: Option<Url>,
115106
}
116107

117108
fn sentry_dsn_example() -> &'static str {
@@ -123,7 +114,7 @@ fn sentry_dsn_example() -> &'static str {
123114
pub struct SentryConfig {
124115
/// Sentry DSN
125116
#[schemars(url, example = "sentry_dsn_example")]
126-
#[serde(default)]
117+
#[serde(skip_serializing_if = "Option::is_none")]
127118
pub dsn: Option<String>,
128119
}
129120

0 commit comments

Comments
 (0)