web search done right
Export number of search requests as a prometheus metric
Mikkel Denker53d72eb2f28a
diff --git a/configs/frontend.toml b/configs/frontend.tomlindex 676fd6b2..54654a16 100644--- a/configs/frontend.toml+++ b/configs/frontend.toml@@ -1,6 +1,7 @@queries_csv_path = "data/queries_us.csv"host = "0.0.0.0:3000"-search_servers = [["0.0.0.0:3001"]]+prometheus_host = "0.0.0.0:3001"+search_servers = [["0.0.0.0:3002"]]crossencoder_model_path = "data/cross_encoder"# qa_model_path = "data/qa_model"bangs_path = "data/bangs.json"diff --git a/configs/search_server.toml b/configs/search_server.tomlindex d2692b76..68d86a95 100644--- a/configs/search_server.toml+++ b/configs/search_server.toml@@ -1,4 +1,4 @@-host = "0.0.0.0:3001"+host = "0.0.0.0:3002"index_path = "data/index"entity_index_path = "data/entity"centrality_store_path = "data/centrality"\ No newline at end of filediff --git a/core/src/api/metrics.rs b/core/src/api/metrics.rsnew file mode 100644index 00000000..5a36de69--- /dev/null+++ b/core/src/api/metrics.rs@@ -0,0 +1,29 @@+// Cuely is an open source web search engine.+// Copyright (C) 2022 Cuely ApS+//+// This program is free software: you can redistribute it and/or modify+// it under the terms of the GNU Affero General Public License as+// published by the Free Software Foundation, either version 3 of the+// License, or (at your option) any later version.+//+// This program is distributed in the hope that it will be useful,+// but WITHOUT ANY WARRANTY; without even the implied warranty of+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the+// GNU Affero General Public License for more details.+//+// You should have received a copy of the GNU Affero General Public License+// along with this program. If not, see <https://www.gnu.org/licenses/>.++use std::sync::Arc;++use axum::{response::IntoResponse, Extension};+use axum_macros::debug_handler;++#[allow(clippy::unused_async)]+#[allow(clippy::match_wild_err_arm)]+#[debug_handler]+pub async fn route(+ Extension(registry): Extension<Arc<crate::metrics::PrometheusRegistry>>,+) -> impl IntoResponse {+ format!("{}", registry)+}diff --git a/core/src/api/mod.rs b/core/src/api/mod.rsindex b3f84ca6..a3fd4a3c 100644--- a/core/src/api/mod.rs+++ b/core/src/api/mod.rs@@ -24,6 +24,7 @@ use crate::{ranking::models::cross_encoder::CrossEncoderModel,searcher::{DistributedSearcher, Shard},summarizer::Summarizer,+ FrontendConfig,};use anyhow::Result;use std::sync::Arc;@@ -39,6 +40,7 @@ use axum::{mod about;mod autosuggest;mod index;+mod metrics;mod opensearch;mod optics;mod privacy;@@ -51,6 +53,7 @@ pub struct HtmlTemplate<T>(T);pub struct State {pub searcher: DistributedSearcher,pub autosuggest: Autosuggest,+ pub search_counter: crate::metrics::Counter,pub summarizer: Arc<Summarizer>,}@@ -80,35 +83,31 @@ pub async fn favicon() -> impl IntoResponse {.unwrap()}-pub fn router(- queries_csv_path: &str,- crossencoder_model_path: &str,- qa_model_path: &Option<String>,- bangs_path: &str,- summarizer_path: &str,- shards: Vec<Vec<String>>,-) -> Result<Router> {- let shards: Vec<_> = shards+pub fn router(config: &FrontendConfig, search_counter: crate::metrics::Counter) -> Result<Router> {+ let shards: Vec<_> = config+ .search_servers+ .clone().into_iter().enumerate().map(|(id, replicas)| Shard::new(id as u64, replicas)).collect();- let autosuggest = Autosuggest::load_csv(queries_csv_path)?;- let crossencoder = CrossEncoderModel::open(crossencoder_model_path)?;+ let autosuggest = Autosuggest::load_csv(&config.queries_csv_path)?;+ let crossencoder = CrossEncoderModel::open(&config.crossencoder_model_path)?;- let qa_model = match qa_model_path {+ let qa_model = match &config.qa_model_path {Some(path) => Some(QaModel::open(path)?),None => None,};- let bangs = Bangs::from_path(bangs_path);+ let bangs = Bangs::from_path(&config.bangs_path);let searcher = DistributedSearcher::new(shards, crossencoder, qa_model, bangs);let state = Arc::new(State {searcher,autosuggest,- summarizer: Arc::new(Summarizer::open(summarizer_path)?),+ search_counter,+ summarizer: Arc::new(Summarizer::open(&config.summarizer_path)?),});Ok(Router::new()@@ -136,3 +135,9 @@ pub fn router(.merge(Router::new().route("/summarize", get(summarize::route))).layer(Extension(state)))}++pub fn metrics_router(registry: crate::metrics::PrometheusRegistry) -> Result<Router> {+ Ok(Router::new()+ .route("/metrics", get(metrics::route))+ .layer(Extension(Arc::new(registry))))+}diff --git a/core/src/api/search.rs b/core/src/api/search.rsindex 26fead6e..c1a9af45 100644--- a/core/src/api/search.rs+++ b/core/src/api/search.rs@@ -97,6 +97,8 @@ pub async fn route(Extension(state): Extension<Arc<State>>,extract::OriginalUri(uri): extract::OriginalUri,) -> Result<impl IntoResponse, StatusCode> {+ state.search_counter.inc();+let query = params.get("q").cloned().unwrap_or_default();let skip_pages: usize = params@@ -243,6 +245,8 @@ pub async fn api(extract::Json(mut query): extract::Json<SearchQuery>,Extension(state): Extension<Arc<State>>,) -> impl IntoResponse {+ state.search_counter.inc();+query.num_results = query.num_results.min(50 * NUM_RESULTS_PER_PAGE);match state.searcher.search(&query).await {diff --git a/core/src/entrypoint/frontend.rs b/core/src/entrypoint/frontend.rsindex f59a2b79..29395cce 100644--- a/core/src/entrypoint/frontend.rs+++ b/core/src/entrypoint/frontend.rs@@ -16,22 +16,35 @@use anyhow::Result;-use crate::{api::router, FrontendConfig};+use crate::{+ api::{metrics_router, router},+ FrontendConfig,+};pub async fn run(config: FrontendConfig) -> Result<()> {- let app = router(- &config.queries_csv_path,- &config.crossencoder_model_path,- &config.qa_model_path,- &config.bangs_path,- &config.summarizer_path,- config.search_servers,- )?;+ let search_counter = crate::metrics::Counter::default();+ let mut registry = crate::metrics::PrometheusRegistry::default();++ let group = registry+ .new_group(+ "total_search_requests".to_string(),+ Some("Total number of incoming search requests.".to_string()),+ )+ .unwrap();+ group.register(search_counter.clone(), Vec::new());++ let app = router(&config, search_counter)?;+ let metrics_app = metrics_router(registry)?;+let addr = config.host.parse()?;- tracing::info!("listening on {}", addr);- axum::Server::bind(&addr)- .serve(app.into_make_service())- .await?;+ tracing::info!("frontend server listening on {}", addr);+ let server = axum::Server::bind(&addr).serve(app.into_make_service());++ let addr = config.prometheus_host.parse()?;+ tracing::info!("prometheus exporter listening on {}", addr);+ let metrics_server = axum::Server::bind(&addr).serve(metrics_app.into_make_service());++ tokio::try_join!(server, metrics_server)?;Ok(())}diff --git a/core/src/lib.rs b/core/src/lib.rsindex 3868797c..7b00c810 100644--- a/core/src/lib.rs+++ b/core/src/lib.rs@@ -49,6 +49,7 @@ pub mod index;mod intmap;mod kahan_sum;mod kv;+mod metrics;pub mod prehashed;mod qa_model;mod query;@@ -161,6 +162,7 @@ pub struct HttpConfig {pub struct FrontendConfig {pub queries_csv_path: String,pub host: String,+ pub prometheus_host: String,pub crossencoder_model_path: String,pub qa_model_path: Option<String>,pub bangs_path: String,diff --git a/core/src/metrics.rs b/core/src/metrics.rsnew file mode 100644index 00000000..11b8b5a8--- /dev/null+++ b/core/src/metrics.rs@@ -0,0 +1,251 @@+// Cuely is an open source web search engine.+// Copyright (C) 2022 Cuely ApS+//+// This program is free software: you can redistribute it and/or modify+// it under the terms of the GNU Affero General Public License as+// published by the Free Software Foundation, either version 3 of the+// License, or (at your option) any later version.+//+// This program is distributed in the hope that it will be useful,+// but WITHOUT ANY WARRANTY; without even the implied warranty of+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the+// GNU Affero General Public License for more details.+//+// You should have received a copy of the GNU Affero General Public License+// along with this program. If not, see <https://www.gnu.org/licenses/>.++use std::{+ collections::HashMap,+ fmt::Display,+ sync::{+ atomic::{AtomicU64, Ordering},+ Arc,+ },+ time::{SystemTime, UNIX_EPOCH},+};++use thiserror::Error;++#[derive(Debug, Error)]+pub enum Error {+ #[error("Group already exists")]+ GroupExists,+}++#[derive(Default, Clone)]+pub struct Counter(Arc<AtomicU64>);++impl Counter {+ pub fn inc(&self) {+ self.0.fetch_add(1, Ordering::SeqCst);+ }+}++pub enum PrometheusMetric {+ Counter(Counter),+}++impl PrometheusMetric {+ fn prom_type(&self) -> &'static str {+ match self {+ PrometheusMetric::Counter(_) => "counter",+ }+ }++ fn prom_val(&self) -> String {+ match self {+ PrometheusMetric::Counter(counter) => format!("{}", counter.0.load(Ordering::SeqCst)),+ }+ }+}++impl From<Counter> for PrometheusMetric {+ fn from(counter: Counter) -> Self {+ Self::Counter(counter)+ }+}++type Name = String;++#[derive(Default)]+pub struct PrometheusRegistry {+ groups: HashMap<Name, PrometheusGroup>,+}++impl PrometheusRegistry {+ pub fn new_group(+ &mut self,+ name: Name,+ help: Option<String>,+ ) -> Result<&mut PrometheusGroup, Error> {+ if self.groups.contains_key(&name) {+ return Err(Error::GroupExists);+ }++ self.groups.insert(+ name.clone(),+ PrometheusGroup {+ metrics: Vec::new(),+ help,+ forced_timestamp: None,+ name: name.clone(),+ },+ );++ Ok(self.groups.get_mut(&name).unwrap())+ }+}++impl Display for PrometheusRegistry {+ fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {+ for (i, (_, r)) in self.groups.iter().enumerate() {+ f.write_str(&format!("{r}"))?;+ f.write_str("\n")?;++ if i < self.groups.len() - 1 {+ f.write_str("\n")?;+ }+ }++ Ok(())+ }+}++pub struct Label {+ pub key: String,+ pub val: String,+}++struct LabelledMetric {+ metric: PrometheusMetric,+ labels: Vec<Label>,+}+impl Display for LabelledMetric {+ fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {+ if !self.labels.is_empty() {+ f.write_str("{")?;++ let num_labels = self.labels.len();+ for (i, label) in self.labels.iter().enumerate() {+ f.write_str(&format!("{}=\"{}\"", label.key, label.val))?;++ if i < num_labels - 1 {+ f.write_str(",")?;+ }+ }++ f.write_str("}")?;+ }++ f.write_str(" ")?;+ f.write_str(&self.metric.prom_val())?;++ Ok(())+ }+}++pub struct PrometheusGroup {+ metrics: Vec<LabelledMetric>,+ help: Option<String>,+ forced_timestamp: Option<u128>,+ name: Name,+}+impl PrometheusGroup {+ pub fn register<M: Into<PrometheusMetric>>(&mut self, metric: M, labels: Vec<Label>) {+ self.metrics.push(LabelledMetric {+ metric: metric.into(),+ labels,+ });+ }+}++impl Display for PrometheusGroup {+ fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {+ if let Some(help) = self.help.as_ref() {+ f.write_str("# HELP ")?;+ f.write_str(&self.name)?;+ f.write_str(" ")?;+ f.write_str(help)?;+ f.write_str("\n")?;+ }++ let timestamp = match self.forced_timestamp {+ Some(timestamp) => timestamp,+ None => SystemTime::now()+ .duration_since(UNIX_EPOCH)+ .unwrap()+ .as_millis(),+ };++ if let Some(first) = self.metrics.first() {+ f.write_str("# TYPE ")?;+ f.write_str(&self.name)?;+ f.write_str(" ")?;+ f.write_str(first.metric.prom_type())?;+ }++ for m in &self.metrics {+ f.write_str("\n")?;+ f.write_str(&self.name)?;+ f.write_str(&format!("{m}"))?;+ f.write_str(" ")?;+ f.write_str(&format!("{timestamp}"))?;+ }++ Ok(())+ }+}++#[cfg(test)]+mod tests {+ use std::time::{SystemTime, UNIX_EPOCH};++ use super::*;++ #[test]+ fn counter() {+ let counter = Counter::default();+ let mut registry = PrometheusRegistry::default();++ let group = registry+ .new_group(+ "test_counter".to_string(),+ Some("Test counter help.".to_string()),+ )+ .unwrap();+ group.register(+ counter.clone(),+ vec![Label {+ key: "test_label".to_string(),+ val: "123".to_string(),+ }],+ );++ let t = SystemTime::now()+ .duration_since(UNIX_EPOCH)+ .unwrap()+ .as_millis();++ for group in registry.groups.values_mut() {+ group.forced_timestamp = Some(t);+ }++ let expected = format!(+ r##"# HELP test_counter Test counter help.+# TYPE test_counter counter+test_counter{{test_label="123"}} 0 {t}+"##+ );+ assert_eq!(format!("{registry}"), expected);++ counter.inc();++ let expected = format!(+ r##"# HELP test_counter Test counter help.+# TYPE test_counter counter+test_counter{{test_label="123"}} 1 {t}+"##+ );+ assert_eq!(format!("{registry}"), expected);+ }+}