Skip to content

Commit

Permalink
refactor: default registry and match TS labeling
Browse files Browse the repository at this point in the history
  • Loading branch information
hopeyen committed Nov 4, 2023
1 parent bd2cd68 commit e0e37e8
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 125 deletions.
5 changes: 0 additions & 5 deletions common/src/indexer_errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -261,8 +261,6 @@ impl IndexerErrorCode {
}
}

// pub type IndexerErrorCause = Box<dyn std::error::Error + Send + Sync>;

#[derive(Debug)]
pub struct IndexerErrorCause(Box<dyn Error + Send + Sync>);

Expand Down Expand Up @@ -336,9 +334,6 @@ impl IndexerError {
}
}

// pub fn indexer_error(code: IndexerErrorCode, cause: Option<IndexerErrorCause>) -> IndexerError {
// IndexerError::new(code, cause)
// }
pub fn indexer_error(code: IndexerErrorCode) -> IndexerError {
IndexerError::new(code.clone(), Some(code.explanation().into()))
}
Expand Down
39 changes: 10 additions & 29 deletions common/src/metrics/mod.rs
Original file line number Diff line number Diff line change
@@ -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<IntCounterVec> = 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<Registry> = Lazy::new(Registry::new);

#[allow(dead_code)]
pub fn register_metrics(registry: &Registry, metrics: Vec<Box<dyn Collector>>) {
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(&REGISTRY, 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");
}
3 changes: 0 additions & 3 deletions service/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
Expand Down
115 changes: 27 additions & 88 deletions service/src/metrics/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,123 +5,62 @@ 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<IntCounterVec> = 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<IntCounterVec> = 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<IntCounterVec> = 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<IntCounterVec> = 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<IntCounterVec> = 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<HistogramVec> = 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(
&REGISTRY,
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),
Err(err) => (StatusCode::INTERNAL_SERVER_ERROR, format!("{err:?}")),
}
}

#[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();
Expand All @@ -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");
}

0 comments on commit e0e37e8

Please sign in to comment.