From e0e37e8eec466171d478ee2641b27a54796ab8de Mon Sep 17 00:00:00 2001 From: hopeyen Date: Thu, 2 Nov 2023 22:00:44 +0200 Subject: [PATCH] refactor: default registry and match TS labeling --- common/src/indexer_errors.rs | 5 -- common/src/metrics/mod.rs | 39 +++--------- service/src/main.rs | 3 - service/src/metrics/mod.rs | 115 ++++++++--------------------------- 4 files changed, 37 insertions(+), 125 deletions(-) diff --git a/common/src/indexer_errors.rs b/common/src/indexer_errors.rs index 4ab49b79..76e373a5 100644 --- a/common/src/indexer_errors.rs +++ b/common/src/indexer_errors.rs @@ -261,8 +261,6 @@ impl IndexerErrorCode { } } -// pub type IndexerErrorCause = Box; - #[derive(Debug)] pub struct IndexerErrorCause(Box); @@ -336,9 +334,6 @@ impl IndexerError { } } -// pub fn indexer_error(code: IndexerErrorCode, cause: Option) -> IndexerError { -// IndexerError::new(code, cause) -// } pub fn indexer_error(code: IndexerErrorCode) -> IndexerError { IndexerError::new(code.clone(), Some(code.explanation().into())) } diff --git a/common/src/metrics/mod.rs b/common/src/metrics/mod.rs index aaf55c41..68d35c29 100644 --- a/common/src/metrics/mod.rs +++ b/common/src/metrics/mod.rs @@ -1,33 +1,14 @@ -// Copyright 2023-, GraphOps and Semiotic Labs. +// Copyright 2023-, GraphOps, and Semiotic Labs. // SPDX-License-Identifier: Apache-2.0 -use once_cell::sync::Lazy; -use prometheus::{core::Collector, register, IntCounterVec, Opts, Registry}; +use lazy_static::lazy_static; +use prometheus::{register_int_counter_vec, IntCounterVec}; -#[allow(dead_code)] -pub static INDEXER_ERROR: Lazy = Lazy::new(|| { - let m = IntCounterVec::new( - Opts::new("indexer_error", "Indexer errors observed over time") - .namespace("indexer") - .subsystem("service"), - &["code"], - ) - .expect("Failed to create indexer_error"); - register(Box::new(m.clone())).expect("Failed to register indexer_error counter"); - m -}); - -#[allow(dead_code)] -pub static REGISTRY: Lazy = Lazy::new(Registry::new); - -#[allow(dead_code)] -pub fn register_metrics(registry: &Registry, metrics: Vec>) { - for metric in metrics { - registry.register(metric).expect("Cannot register metrics"); - } -} - -/// Register indexer error metrics in Prometheus registry -pub fn register_indexer_error_metrics() { - register_metrics(®ISTRY, vec![Box::new(INDEXER_ERROR.clone())]); +lazy_static! { + /// Register indexer error metrics in Prometheus registry + pub static ref INDEXER_ERROR: IntCounterVec = register_int_counter_vec!( + "indexer_error", + "Indexer errors observed over time", + &["code"] + ).expect("Create indexer_error metrics"); } diff --git a/service/src/main.rs b/service/src/main.rs index b388ac2d..31fbcd6b 100644 --- a/service/src/main.rs +++ b/service/src/main.rs @@ -161,9 +161,6 @@ async fn main() -> Result<(), std::io::Error> { let query_processor = QueryProcessor::new(graph_node.clone(), attestation_signers.clone(), tap_manager); - indexer_common::metrics::register_indexer_error_metrics(); - metrics::register_query_metrics(); - // Start indexer service basic metrics tokio::spawn(handle_serve_metrics( String::from("0.0.0.0"), diff --git a/service/src/metrics/mod.rs b/service/src/metrics/mod.rs index 01947383..2b5c3b77 100644 --- a/service/src/metrics/mod.rs +++ b/service/src/metrics/mod.rs @@ -5,115 +5,54 @@ use autometrics::{encode_global_metrics, global_metrics_exporter}; use axum::http::StatusCode; use axum::routing::get; use axum::Router; -use indexer_common::metrics::{register_metrics, REGISTRY}; -use once_cell::sync::Lazy; - -use prometheus::{linear_buckets, HistogramOpts, HistogramVec, IntCounterVec, Opts}; +use lazy_static::lazy_static; +use prometheus::{ + register_histogram_vec, register_int_counter_vec, HistogramVec, IntCounterVec, +}; use std::{net::SocketAddr, str::FromStr}; use tracing::info; -pub static QUERIES: Lazy = Lazy::new(|| { - let m = IntCounterVec::new( - Opts::new("queries", "Incoming queries") - .namespace("indexer") - .subsystem("service"), +// Record Queries related metrics +lazy_static! { + pub static ref QUERIES: IntCounterVec = register_int_counter_vec!( + "indexer_service_queries_total", + "Incoming queries", &["deployment"], ) .expect("Failed to create queries counters"); - prometheus::register(Box::new(m.clone())).expect("Failed to register queries counter"); - m -}); - -pub static SUCCESSFUL_QUERIES: Lazy = Lazy::new(|| { - let m = IntCounterVec::new( - Opts::new("successfulQueries", "Successfully executed queries") - .namespace("indexer") - .subsystem("service"), + pub static ref SUCCESSFUL_QUERIES: IntCounterVec = register_int_counter_vec!( + "indexer_service_queries_ok", + "Successfully executed queries", &["deployment"], ) .expect("Failed to create successfulQueries counters"); - prometheus::register(Box::new(m.clone())) - .expect("Failed to register successfulQueries counter"); - m -}); - -pub static FAILED_QUERIES: Lazy = Lazy::new(|| { - let m = IntCounterVec::new( - Opts::new("failedQueries", "Queries that failed to execute") - .namespace("indexer") - .subsystem("service"), + pub static ref FAILED_QUERIES: IntCounterVec = register_int_counter_vec!( + "indexer_service_queries_failed", + "Queries that failed to execute", &["deployment"], ) .expect("Failed to create failedQueries counters"); - prometheus::register(Box::new(m.clone())).expect("Failed to register failedQueries counter"); - m -}); - -pub static QUERIES_WITH_INVALID_RECEIPT_HEADER: Lazy = Lazy::new(|| { - let m = IntCounterVec::new( - Opts::new( - "queriesWithInvalidReceiptHeader", - "Queries that failed executing because they came with an invalid receipt header", - ) - .namespace("indexer") - .subsystem("service"), + pub static ref QUERIES_WITH_INVALID_RECEIPT_HEADER: IntCounterVec = register_int_counter_vec!( + "indexer_service_queries_with_invalid_receipt_header", + "Queries that failed executing because they came with an invalid receipt header", &["deployment"], ) .expect("Failed to create queriesWithInvalidReceiptHeader counters"); - prometheus::register(Box::new(m.clone())) - .expect("Failed to register queriesWithInvalidReceiptHeader counter"); - m -}); - -pub static QUERIES_WITHOUT_RECEIPT: Lazy = Lazy::new(|| { - let m = IntCounterVec::new( - Opts::new( - "queriesWithoutReceipt", - "Queries that failed executing because they came without a receipt", - ) - .namespace("indexer") - .subsystem("service"), + pub static ref QUERIES_WITHOUT_RECEIPT: IntCounterVec = register_int_counter_vec!( + "indexer_service_queries_without_receipt", + "Queries that failed executing because they came without a receipt", &["deployment"], ) .expect("Failed to create queriesWithoutReceipt counters"); - prometheus::register(Box::new(m.clone())) - .expect("Failed to register queriesWithoutReceipt counter"); - m -}); - -pub static QUERY_DURATION: Lazy = Lazy::new(|| { - let m = HistogramVec::new( - HistogramOpts::new( - "query_duration", - "Duration of processing a query from start to end", - ) - .namespace("indexer") - .subsystem("service") - .buckets(linear_buckets(0.0, 1.0, 20).unwrap()), + pub static ref QUERY_DURATION: HistogramVec = register_histogram_vec!( + "indexer_service_query_duration", + "Duration of processing a query from start to end", &["deployment"], ) - .expect("Failed to create query_duration histograms"); - prometheus::register(Box::new(m.clone())).expect("Failed to register query_duration counter"); - m -}); - -/// Start the basic metrics for indexer services -pub fn register_query_metrics() { - register_metrics( - ®ISTRY, - vec![ - Box::new(QUERIES.clone()), - Box::new(SUCCESSFUL_QUERIES.clone()), - Box::new(FAILED_QUERIES.clone()), - Box::new(QUERIES_WITH_INVALID_RECEIPT_HEADER.clone()), - Box::new(QUERIES_WITHOUT_RECEIPT.clone()), - Box::new(QUERY_DURATION.clone()), - ], - ); + .unwrap(); } /// This handler serializes the metrics into a string for Prometheus to scrape -#[allow(dead_code)] pub async fn get_metrics() -> (StatusCode, String) { match encode_global_metrics() { Ok(metrics) => (StatusCode::OK, metrics), @@ -121,7 +60,7 @@ pub async fn get_metrics() -> (StatusCode, String) { } } -#[allow(dead_code)] +/// Metrics server router pub async fn handle_serve_metrics(host: String, port: u16) { // Set up the exporter to collect metrics let _exporter = global_metrics_exporter(); @@ -138,5 +77,5 @@ pub async fn handle_serve_metrics(host: String, port: u16) { server .serve(app.into_make_service()) .await - .expect("Error starting example API server"); + .expect("Error starting Prometheus metrics port"); }