Skip to content
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 custom metrics, and an initial cache size metric #1254

Merged
merged 2 commits into from
Feb 6, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 1 addition & 44 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 0 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ actix-web = "4.3.1"
actix-web-extras = "0.1"
actix-web-httpauth = "0.8"
actix-web-opentelemetry = "0.20"
actix-web-prom = "0.9.0"
actix-web-static-files = "4.0.1"
anyhow = "1.0.72"
async-compression = "0.4.13"
Expand Down Expand Up @@ -101,7 +100,6 @@ peak_alloc = "0.2.0"
pem = "3"
percent-encoding = "2.3.1"
petgraph = { version = "0.7.1", features = ["serde-1"] }
prometheus = "0.13.3"
quick-xml = "0.37.0"
rand = "0.8.5"
reedline = "0.37.0"
Expand Down
4 changes: 1 addition & 3 deletions common/infrastructure/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ actix-web = { workspace = true, features = ["openssl"] }
actix-web-extras = { workspace = true }
actix-web-httpauth = { workspace = true }
actix-web-opentelemetry = { workspace = true, features = ["metrics"] }
actix-web-prom = { workspace = true }
anyhow = { workspace = true }
bytesize = { workspace = true }
clap = { workspace = true, features = ["derive", "env", "string"] }
Expand All @@ -23,9 +22,8 @@ mime = { workspace = true }
openssl = { workspace = true }
opentelemetry = { workspace = true }
opentelemetry-otlp = { workspace = true }
opentelemetry_sdk = { workspace = true, features = ["rt-tokio-current-thread"] }
opentelemetry_sdk = { workspace = true, features = ["rt-tokio-current-thread", "metrics"] }
parking_lot = { workspace = true }
prometheus = { workspace = true }
reqwest = { workspace = true }
serde = { workspace = true, features = ["derive", "rc"] }
serde_json = { workspace = true }
Expand Down
56 changes: 10 additions & 46 deletions common/infrastructure/src/app/http.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::{
app::{new_app, AppOptions},
endpoint::Endpoint,
otel::{Metrics as OtelMetrics, Tracing},
otel::{Metrics, Tracing},
};
use actix_cors::Cors;
use actix_tls::{accept::openssl::reexports::SslAcceptor, connect::openssl::reexports::SslMethod};
Expand All @@ -11,12 +11,10 @@ use actix_web::{
App, HttpResponse, HttpServer,
};
use actix_web_opentelemetry::{RequestMetrics, RequestTracing};
use actix_web_prom::{PrometheusMetrics, PrometheusMetricsBuilder};
use anyhow::{anyhow, Context};
use bytesize::ByteSize;
use clap::{value_parser, Arg, ArgMatches, Args, Command, Error, FromArgMatches};
use openssl::ssl::SslFiletype;
use prometheus::Registry;
use std::{
fmt::Debug,
marker::PhantomData,
Expand Down Expand Up @@ -279,7 +277,6 @@ pub struct HttpServerBuilder {
bind: Bind,
tls: Option<TlsConfiguration>,

metrics_factory: Option<Arc<dyn Fn() -> anyhow::Result<PrometheusMetrics> + Send + Sync>>,
cors_factory: Option<Arc<dyn Fn() -> Cors + Send + Sync>>,
authenticator: Option<Arc<Authenticator>>,
authorizer: Option<Authorizer>,
Expand All @@ -289,7 +286,7 @@ pub struct HttpServerBuilder {
json_limit: Option<usize>,
request_limit: Option<usize>,
tracing: Tracing,
metrics: OtelMetrics,
metrics: Metrics,

disable_log: bool,

Expand Down Expand Up @@ -321,7 +318,6 @@ impl HttpServerBuilder {
post_configurator: None,
bind: Bind::Address(DEFAULT_ADDR),
tls: None,
metrics_factory: None,
cors_factory: Some(Arc::new(Cors::permissive)),
authenticator: None,
authorizer: None,
Expand All @@ -330,7 +326,7 @@ impl HttpServerBuilder {
json_limit: None,
request_limit: None,
tracing: Tracing::default(),
metrics: OtelMetrics::default(),
metrics: Metrics::default(),
openapi_info: None,
disable_log: false,
}
Expand Down Expand Up @@ -372,7 +368,7 @@ impl HttpServerBuilder {
self
}

pub fn metrics_otel(mut self, metrics: OtelMetrics) -> Self {
pub fn metrics(mut self, metrics: Metrics) -> Self {
self.metrics = metrics;
self
}
Expand All @@ -398,31 +394,6 @@ impl HttpServerBuilder {
self
}

pub fn metrics(mut self, registry: impl Into<Registry>, namespace: impl AsRef<str>) -> Self {
let metrics = PrometheusMetricsBuilder::new(namespace.as_ref())
.registry(registry.into())
.build();

self.metrics_factory = Some(Arc::new(move || {
metrics.as_ref().cloned().map_err(|err| anyhow!("{err}"))
}));

self
}

pub fn metrics_factory<F>(mut self, metrics_factory: F) -> Self
where
F: Fn() -> Result<PrometheusMetrics, Box<dyn std::error::Error + Send + Sync>>
+ Send
+ Sync
+ 'static,
{
self.metrics_factory = Some(Arc::new(move || {
metrics_factory().map_err(|err| anyhow!("Failed to create prometheus registry: {err}"))
}));
self
}

pub fn listen(mut self, listener: TcpListener) -> Self {
self.bind = Bind::Listener(listener);
self
Expand Down Expand Up @@ -459,12 +430,6 @@ impl HttpServerBuilder {
}

pub async fn run(self) -> anyhow::Result<()> {
let metrics = self
.metrics_factory
.as_ref()
.map(|factory| factory())
.transpose()?;

if let Some(limit) = self.request_limit {
log::info!("JSON limit: {}", BinaryByteSize::from(limit));
}
Expand Down Expand Up @@ -495,28 +460,27 @@ impl HttpServerBuilder {
tracing_logger.is_some()
);

let otel_metrics = match self.metrics {
OtelMetrics::Disabled => None,
OtelMetrics::Enabled => Some(RequestMetrics::default()),
let metrics = match self.metrics {
Metrics::Disabled => None,
Metrics::Enabled => Some(RequestMetrics::default()),
};

log::debug!(
"Otel Metrics({}) - metrics: {}",
"OTELMetrics({}) - metrics: {}",
self.metrics,
otel_metrics.is_some()
metrics.is_some()
);

let mut app = new_app(AppOptions {
cors,
metrics: metrics.clone(),
authenticator: self.authenticator.clone(),
authorizer: self
.authorizer
.clone()
.unwrap_or_else(|| Authorizer::new(None)),
logger,
tracing_logger,
otel_metrics,
metrics,
})
.app_data(json)
.into_utoipa_app();
Expand Down
6 changes: 1 addition & 5 deletions common/infrastructure/src/app/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,18 @@ use actix_web::{
use actix_web_extras::middleware::Condition;
use actix_web_httpauth::{extractors::bearer::BearerAuth, middleware::HttpAuthentication};
use actix_web_opentelemetry::{RequestMetrics, RequestTracing};
use actix_web_prom::PrometheusMetrics;
use futures::{future::LocalBoxFuture, FutureExt};
use std::sync::Arc;
use trustify_auth::{authenticator::Authenticator, authorizer::Authorizer};

#[derive(Default)]
pub struct AppOptions {
pub cors: Option<Cors>,
pub metrics: Option<PrometheusMetrics>,
pub authenticator: Option<Arc<Authenticator>>,
pub authorizer: Authorizer,
pub logger: Option<Logger>,
pub tracing_logger: Option<RequestTracing>,
pub otel_metrics: Option<RequestMetrics>,
pub metrics: Option<RequestMetrics>,
}

/// create a new authenticator
Expand Down Expand Up @@ -79,8 +77,6 @@ pub fn new_app(
.wrap(Condition::from_option(options.cors))
// Next, record metrics for the request (should never fail)
.wrap(Condition::from_option(options.metrics))
// Next, record otel metrics for the request (should never fail)
.wrap(Condition::from_option(options.otel_metrics))
// Compress everything
.wrap(Compress::default())
// First log the request, so that we know what happens (can't fail)
Expand Down
20 changes: 10 additions & 10 deletions common/infrastructure/src/endpoint.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,13 @@
use std::fmt::Debug;
use clap::{
value_parser, Arg, ArgMatches, Args, Command, CommandFactory, Error, FromArgMatches, Parser,
};
use std::{
ffi::OsString,
fmt::Debug,
marker::PhantomData,
net::{AddrParseError, SocketAddr},
str::FromStr,
};
use url::Url;

pub trait Endpoint: Debug {
Expand All @@ -16,15 +25,6 @@ pub trait Endpoint: Debug {
}
}

use std::ffi::OsString;
use std::marker::PhantomData;
use std::net::{AddrParseError, SocketAddr};
use std::str::FromStr;

use clap::{
value_parser, Arg, ArgMatches, Args, Command, CommandFactory, Error, FromArgMatches, Parser,
};

#[derive(Clone, Debug)]
pub struct EndpointServerConfig<E: Endpoint> {
pub bind: String,
Expand Down
Loading