1
0
mirror of https://github.com/matrix-org/matrix-authentication-service.git synced 2025-08-06 06:02:40 +03:00

Flatten the telemetry config section

This commit is contained in:
Quentin Gliech
2024-03-21 17:46:33 +01:00
parent 809fe16d29
commit aa6178abe6
4 changed files with 128 additions and 176 deletions

View File

@@ -16,7 +16,10 @@ use std::time::Duration;
use anyhow::Context as _; use anyhow::Context as _;
use hyper::{header::CONTENT_TYPE, Body, Response}; use hyper::{header::CONTENT_TYPE, Body, Response};
use mas_config::{MetricsExporterConfig, Propagator, TelemetryConfig, TracingExporterConfig}; use mas_config::{
MetricsConfig, MetricsExporterKind, Propagator, TelemetryConfig, TracingConfig,
TracingExporterKind,
};
use opentelemetry::{ use opentelemetry::{
global, global,
propagation::{TextMapCompositePropagator, TextMapPropagator}, propagation::{TextMapCompositePropagator, TextMapPropagator},
@@ -52,9 +55,9 @@ pub fn setup(config: &TelemetryConfig) -> anyhow::Result<Option<Tracer>> {
mas_http::set_propagator(&propagator); mas_http::set_propagator(&propagator);
global::set_text_map_propagator(propagator); global::set_text_map_propagator(propagator);
let tracer = tracer(&config.tracing.exporter).context("Failed to configure traces exporter")?; let tracer = tracer(&config.tracing).context("Failed to configure traces exporter")?;
init_meter(&config.metrics.exporter).context("Failed to configure metrics exporter")?; init_meter(&config.metrics).context("Failed to configure metrics exporter")?;
Ok(tracer) Ok(tracer)
} }
@@ -114,13 +117,13 @@ fn otlp_tracer(endpoint: Option<&Url>) -> anyhow::Result<Tracer> {
Ok(tracer) Ok(tracer)
} }
fn tracer(config: &TracingExporterConfig) -> anyhow::Result<Option<Tracer>> { fn tracer(config: &TracingConfig) -> anyhow::Result<Option<Tracer>> {
let tracer_provider = match config { let tracer_provider = match config.exporter {
TracingExporterConfig::None => return Ok(None), TracingExporterKind::None => return Ok(None),
TracingExporterConfig::Stdout => stdout_tracer_provider(), TracingExporterKind::Stdout => stdout_tracer_provider(),
TracingExporterConfig::Otlp { endpoint } => { TracingExporterKind::Otlp => {
// The OTLP exporter already creates a tracer and installs it // The OTLP exporter already creates a tracer and installs it
return Ok(Some(otlp_tracer(endpoint.as_ref())?)); return Ok(Some(otlp_tracer(config.endpoint.as_ref())?));
} }
}; };
@@ -208,15 +211,15 @@ fn prometheus_metric_reader() -> anyhow::Result<PrometheusExporter> {
Ok(exporter) Ok(exporter)
} }
fn init_meter(config: &MetricsExporterConfig) -> anyhow::Result<()> { fn init_meter(config: &MetricsConfig) -> anyhow::Result<()> {
let meter_provider_builder = SdkMeterProvider::builder(); let meter_provider_builder = SdkMeterProvider::builder();
let meter_provider_builder = match config { let meter_provider_builder = match config.exporter {
MetricsExporterConfig::None => meter_provider_builder.with_reader(ManualReader::default()), MetricsExporterKind::None => meter_provider_builder.with_reader(ManualReader::default()),
MetricsExporterConfig::Stdout => meter_provider_builder.with_reader(stdout_metric_reader()), MetricsExporterKind::Stdout => meter_provider_builder.with_reader(stdout_metric_reader()),
MetricsExporterConfig::Otlp { endpoint } => { MetricsExporterKind::Otlp => {
meter_provider_builder.with_reader(otlp_metric_reader(endpoint.as_ref())?) meter_provider_builder.with_reader(otlp_metric_reader(config.endpoint.as_ref())?)
} }
MetricsExporterConfig::Prometheus => { MetricsExporterKind::Prometheus => {
meter_provider_builder.with_reader(prometheus_metric_reader()?) meter_provider_builder.with_reader(prometheus_metric_reader()?)
} }
}; };

View File

@@ -46,8 +46,8 @@ pub use self::{
policy::PolicyConfig, policy::PolicyConfig,
secrets::SecretsConfig, secrets::SecretsConfig,
telemetry::{ telemetry::{
MetricsConfig, MetricsExporterConfig, Propagator, TelemetryConfig, TracingConfig, MetricsConfig, MetricsExporterKind, Propagator, TelemetryConfig, TracingConfig,
TracingExporterConfig, TracingExporterKind,
}, },
templates::TemplatesConfig, templates::TemplatesConfig,
upstream_oauth2::{ upstream_oauth2::{

View File

@@ -35,42 +35,38 @@ pub enum Propagator {
Jaeger, Jaeger,
} }
fn otlp_endpoint_example() -> &'static str { #[allow(clippy::unnecessary_wraps)]
"https://localhost:4318" fn otlp_endpoint_default() -> Option<String> {
Some("https://localhost:4318".to_owned())
} }
/// Exporter to use when exporting traces /// Exporter to use when exporting traces
#[skip_serializing_none] #[skip_serializing_none]
#[derive(Clone, Debug, Serialize, Deserialize, JsonSchema)] #[derive(Clone, Copy, Debug, Serialize, Deserialize, JsonSchema, Default)]
#[serde(tag = "exporter", rename_all = "lowercase", deny_unknown_fields)] #[serde(rename_all = "lowercase")]
pub enum TracingExporterConfig { pub enum TracingExporterKind {
/// Don't export traces /// Don't export traces
#[default]
None, None,
/// Export traces to the standard output. Only useful for debugging /// Export traces to the standard output. Only useful for debugging
Stdout, Stdout,
/// Export traces to an OpenTelemetry protocol compatible endpoint /// Export traces to an OpenTelemetry protocol compatible endpoint
Otlp { Otlp,
/// OTLP compatible endpoint
#[schemars(url, example = "otlp_endpoint_example")]
#[serde(default)]
endpoint: Option<Url>,
},
}
impl Default for TracingExporterConfig {
fn default() -> Self {
Self::None
}
} }
/// Configuration related to exporting traces /// Configuration related to exporting traces
#[derive(Clone, Debug, Default, Serialize, Deserialize, JsonSchema)] #[derive(Clone, Debug, Default, Serialize, Deserialize, JsonSchema)]
pub struct TracingConfig { pub struct TracingConfig {
/// Exporter to use when exporting traces /// Exporter to use when exporting traces
#[serde(default, flatten)] #[serde(default)]
pub exporter: TracingExporterConfig, pub exporter: TracingExporterKind,
/// OTLP exporter: OTLP over HTTP compatible endpoint
#[serde(skip_serializing_if = "Option::is_none")]
#[schemars(url, default = "otlp_endpoint_default")]
pub endpoint: Option<Url>,
/// List of propagation formats to use for incoming and outgoing requests /// List of propagation formats to use for incoming and outgoing requests
pub propagators: Vec<Propagator>, pub propagators: Vec<Propagator>,
@@ -78,40 +74,35 @@ pub struct TracingConfig {
/// Exporter to use when exporting metrics /// Exporter to use when exporting metrics
#[skip_serializing_none] #[skip_serializing_none]
#[derive(Clone, Debug, Serialize, Deserialize, JsonSchema)] #[derive(Clone, Copy, Debug, Serialize, Deserialize, JsonSchema, Default)]
#[serde(tag = "exporter", rename_all = "lowercase")] #[serde(rename_all = "lowercase")]
pub enum MetricsExporterConfig { pub enum MetricsExporterKind {
/// Don't export metrics /// Don't export metrics
#[default]
None, None,
/// Export metrics to stdout. Only useful for debugging /// Export metrics to stdout. Only useful for debugging
Stdout, Stdout,
/// Export metrics to an OpenTelemetry protocol compatible endpoint /// Export metrics to an OpenTelemetry protocol compatible endpoint
Otlp { Otlp,
/// OTLP compatible endpoint
#[schemars(url, example = "otlp_endpoint_example")]
#[serde(default)]
endpoint: Option<Url>,
},
/// Export metrics via Prometheus. An HTTP listener with the `prometheus` /// Export metrics via Prometheus. An HTTP listener with the `prometheus`
/// resource must be setup to expose the Promethes metrics. /// resource must be setup to expose the Promethes metrics.
Prometheus, Prometheus,
} }
impl Default for MetricsExporterConfig {
fn default() -> Self {
Self::None
}
}
/// Configuration related to exporting metrics /// Configuration related to exporting metrics
#[derive(Clone, Debug, Default, Serialize, Deserialize, JsonSchema)] #[derive(Clone, Debug, Default, Serialize, Deserialize, JsonSchema)]
pub struct MetricsConfig { pub struct MetricsConfig {
/// Exporter to use when exporting metrics /// Exporter to use when exporting metrics
#[serde(default, flatten)] #[serde(default)]
pub exporter: MetricsExporterConfig, pub exporter: MetricsExporterKind,
/// OTLP exporter: OTLP over HTTP compatible endpoint
#[serde(skip_serializing_if = "Option::is_none")]
#[schemars(url, default = "otlp_endpoint_default")]
pub endpoint: Option<Url>,
} }
fn sentry_dsn_example() -> &'static str { fn sentry_dsn_example() -> &'static str {
@@ -123,7 +114,7 @@ fn sentry_dsn_example() -> &'static str {
pub struct SentryConfig { pub struct SentryConfig {
/// Sentry DSN /// Sentry DSN
#[schemars(url, example = "sentry_dsn_example")] #[schemars(url, example = "sentry_dsn_example")]
#[serde(default)] #[serde(skip_serializing_if = "Option::is_none")]
pub dsn: Option<String>, pub dsn: Option<String>,
} }

View File

@@ -110,9 +110,7 @@
"metrics": { "metrics": {
"exporter": "none" "exporter": "none"
}, },
"sentry": { "sentry": {}
"dsn": null
}
}, },
"allOf": [ "allOf": [
{ {
@@ -1108,9 +1106,7 @@
}, },
"sentry": { "sentry": {
"description": "Configuration related to the Sentry integration", "description": "Configuration related to the Sentry integration",
"default": { "default": {},
"dsn": null
},
"allOf": [ "allOf": [
{ {
"$ref": "#/definitions/SentryConfig" "$ref": "#/definitions/SentryConfig"
@@ -1122,68 +1118,25 @@
"TracingConfig": { "TracingConfig": {
"description": "Configuration related to exporting traces", "description": "Configuration related to exporting traces",
"type": "object", "type": "object",
"oneOf": [
{
"description": "Don't export traces",
"type": "object",
"required": [
"exporter"
],
"properties": {
"exporter": {
"type": "string",
"enum": [
"none"
]
}
},
"additionalProperties": false
},
{
"description": "Export traces to the standard output. Only useful for debugging",
"type": "object",
"required": [
"exporter"
],
"properties": {
"exporter": {
"type": "string",
"enum": [
"stdout"
]
}
},
"additionalProperties": false
},
{
"description": "Export traces to an OpenTelemetry protocol compatible endpoint",
"type": "object",
"required": [
"exporter"
],
"properties": {
"exporter": {
"type": "string",
"enum": [
"otlp"
]
},
"endpoint": {
"description": "OTLP compatible endpoint",
"examples": [
"https://localhost:4318"
],
"type": "string",
"format": "uri"
}
},
"additionalProperties": false
}
],
"required": [ "required": [
"propagators" "propagators"
], ],
"properties": { "properties": {
"exporter": {
"description": "Exporter to use when exporting traces",
"default": "none",
"allOf": [
{
"$ref": "#/definitions/TracingExporterKind"
}
]
},
"endpoint": {
"description": "OTLP exporter: OTLP over HTTP compatible endpoint",
"default": "https://localhost:4318",
"type": "string",
"format": "uri"
},
"propagators": { "propagators": {
"description": "List of propagation formats to use for incoming and outgoing requests", "description": "List of propagation formats to use for incoming and outgoing requests",
"type": "array", "type": "array",
@@ -1193,6 +1146,32 @@
} }
} }
}, },
"TracingExporterKind": {
"description": "Exporter to use when exporting traces",
"oneOf": [
{
"description": "Don't export traces",
"type": "string",
"enum": [
"none"
]
},
{
"description": "Export traces to the standard output. Only useful for debugging",
"type": "string",
"enum": [
"stdout"
]
},
{
"description": "Export traces to an OpenTelemetry protocol compatible endpoint",
"type": "string",
"enum": [
"otlp"
]
}
]
},
"Propagator": { "Propagator": {
"description": "Propagation format for incoming and outgoing requests", "description": "Propagation format for incoming and outgoing requests",
"oneOf": [ "oneOf": [
@@ -1222,75 +1201,55 @@
"MetricsConfig": { "MetricsConfig": {
"description": "Configuration related to exporting metrics", "description": "Configuration related to exporting metrics",
"type": "object", "type": "object",
"oneOf": [
{
"description": "Don't export metrics",
"type": "object",
"required": [
"exporter"
],
"properties": { "properties": {
"exporter": { "exporter": {
"type": "string", "description": "Exporter to use when exporting metrics",
"enum": [ "default": "none",
"none" "allOf": [
]
}
}
},
{ {
"description": "Export metrics to stdout. Only useful for debugging", "$ref": "#/definitions/MetricsExporterKind"
"type": "object",
"required": [
"exporter"
],
"properties": {
"exporter": {
"type": "string",
"enum": [
"stdout"
]
} }
}
},
{
"description": "Export metrics to an OpenTelemetry protocol compatible endpoint",
"type": "object",
"required": [
"exporter"
],
"properties": {
"exporter": {
"type": "string",
"enum": [
"otlp"
] ]
}, },
"endpoint": { "endpoint": {
"description": "OTLP compatible endpoint", "description": "OTLP exporter: OTLP over HTTP compatible endpoint",
"examples": [ "default": "https://localhost:4318",
"https://localhost:4318"
],
"type": "string", "type": "string",
"format": "uri" "format": "uri"
} }
} }
}, },
"MetricsExporterKind": {
"description": "Exporter to use when exporting metrics",
"oneOf": [
{
"description": "Don't export metrics",
"type": "string",
"enum": [
"none"
]
},
{
"description": "Export metrics to stdout. Only useful for debugging",
"type": "string",
"enum": [
"stdout"
]
},
{
"description": "Export metrics to an OpenTelemetry protocol compatible endpoint",
"type": "string",
"enum": [
"otlp"
]
},
{ {
"description": "Export metrics via Prometheus. An HTTP listener with the `prometheus` resource must be setup to expose the Promethes metrics.", "description": "Export metrics via Prometheus. An HTTP listener with the `prometheus` resource must be setup to expose the Promethes metrics.",
"type": "object",
"required": [
"exporter"
],
"properties": {
"exporter": {
"type": "string", "type": "string",
"enum": [ "enum": [
"prometheus" "prometheus"
] ]
} }
}
}
] ]
}, },
"SentryConfig": { "SentryConfig": {
@@ -1299,7 +1258,6 @@
"properties": { "properties": {
"dsn": { "dsn": {
"description": "Sentry DSN", "description": "Sentry DSN",
"default": null,
"examples": [ "examples": [
"https://public@host:port/1" "https://public@host:port/1"
], ],