Unverified Commit cbe0b177 authored by Keiven C's avatar Keiven C Committed by GitHub
Browse files

refactor: redesign the metrics API from Trait to composition to make the code...


refactor: redesign the metrics API from Trait to composition to make the code cleaner and easier to understand (#3687)
Signed-off-by: default avatarKeiven Chang <keivenchang@users.noreply.github.com>
parent eb8d07cb
......@@ -17,7 +17,7 @@
//!
//! Note: `NATS_AUTH_USERNAME` and `NATS_AUTH_PASSWORD` must be used together.
use crate::traits::events::EventPublisher;
use crate::{Result, metrics::MetricsRegistry};
use crate::{Result, metrics::MetricsHierarchy};
use async_nats::connection::State;
use async_nats::{Subscriber, client, jetstream};
......@@ -910,32 +910,33 @@ pub struct DRTNatsClientPrometheusMetrics {
impl DRTNatsClientPrometheusMetrics {
/// Create a new instance of NATS client metrics using a DistributedRuntime's Prometheus constructors
pub fn new(drt: &crate::DistributedRuntime, nats_client: client::Client) -> Result<Self> {
let in_bytes = drt.create_intgauge(
let metrics = drt.metrics();
let in_bytes = metrics.create_intgauge(
nats_metrics::IN_TOTAL_BYTES,
"Total number of bytes received by NATS client",
&[],
)?;
let out_bytes = drt.create_intgauge(
let out_bytes = metrics.create_intgauge(
nats_metrics::OUT_OVERHEAD_BYTES,
"Total number of bytes sent by NATS client",
&[],
)?;
let in_messages = drt.create_intgauge(
let in_messages = metrics.create_intgauge(
nats_metrics::IN_MESSAGES,
"Total number of messages received by NATS client",
&[],
)?;
let out_messages = drt.create_intgauge(
let out_messages = metrics.create_intgauge(
nats_metrics::OUT_MESSAGES,
"Total number of messages sent by NATS client",
&[],
)?;
let connects = drt.create_intgauge(
let connects = metrics.create_intgauge(
nats_metrics::CURRENT_CONNECTIONS,
"Current number of active connections for NATS client",
&[],
)?;
let connection_state = drt.create_intgauge(
let connection_state = metrics.create_intgauge(
nats_metrics::CONNECTION_STATE,
"Current connection state of NATS client (0=disconnected, 1=connected, 2=reconnecting)",
&[],
......
......@@ -357,14 +357,14 @@
//!
//! ```rust
//! use dynamo_runtime::utils::tasks::tracker::{TaskTracker, SemaphoreScheduler, LogOnlyPolicy};
//! use dynamo_runtime::metrics::MetricsRegistry;
//! use dynamo_runtime::DistributedRuntime;
//!
//! # async fn example(registry: &dyn MetricsRegistry) -> anyhow::Result<()> {
//! # async fn example(drt: &DistributedRuntime) -> anyhow::Result<()> {
//! // Root tracker with Prometheus metrics
//! let tracker = TaskTracker::new_with_prometheus(
//! SemaphoreScheduler::with_permits(10),
//! LogOnlyPolicy::new(),
//! registry,
//! drt,
//! "my_component"
//! )?;
//!
......@@ -383,7 +383,7 @@ use std::pin::Pin;
use std::sync::Arc;
use std::sync::atomic::{AtomicU64, Ordering};
use crate::metrics::MetricsRegistry;
use crate::metrics::MetricsHierarchy;
use crate::metrics::prometheus_names::task_tracker;
use anyhow::Result;
use async_trait::async_trait;
......@@ -1548,47 +1548,45 @@ impl PrometheusTaskMetrics {
/// ```rust
/// # use std::sync::Arc;
/// # use dynamo_runtime::utils::tasks::tracker::PrometheusTaskMetrics;
/// # use dynamo_runtime::metrics::MetricsRegistry;
/// # fn example(registry: Arc<dyn MetricsRegistry>) -> anyhow::Result<()> {
/// let metrics = PrometheusTaskMetrics::new(registry.as_ref(), "main_tracker")?;
/// # use dynamo_runtime::DistributedRuntime;
/// # fn example(drt: &DistributedRuntime) -> anyhow::Result<()> {
/// let metrics = PrometheusTaskMetrics::new(drt, "main_tracker")?;
/// # Ok(())
/// # }
/// ```
pub fn new<R: MetricsRegistry + ?Sized>(
registry: &R,
component_name: &str,
) -> anyhow::Result<Self> {
let issued_counter = registry.create_intcounter(
pub fn new<R: MetricsHierarchy>(registry: &R, component_name: &str) -> anyhow::Result<Self> {
let metrics = registry.metrics();
let issued_counter = metrics.create_intcounter(
&format!("{}_{}", component_name, task_tracker::TASKS_ISSUED_TOTAL),
"Total number of tasks issued/submitted",
&[],
)?;
let started_counter = registry.create_intcounter(
let started_counter = metrics.create_intcounter(
&format!("{}_{}", component_name, task_tracker::TASKS_STARTED_TOTAL),
"Total number of tasks started",
&[],
)?;
let success_counter = registry.create_intcounter(
let success_counter = metrics.create_intcounter(
&format!("{}_{}", component_name, task_tracker::TASKS_SUCCESS_TOTAL),
"Total number of successfully completed tasks",
&[],
)?;
let cancelled_counter = registry.create_intcounter(
let cancelled_counter = metrics.create_intcounter(
&format!("{}_{}", component_name, task_tracker::TASKS_CANCELLED_TOTAL),
"Total number of cancelled tasks",
&[],
)?;
let failed_counter = registry.create_intcounter(
let failed_counter = metrics.create_intcounter(
&format!("{}_{}", component_name, task_tracker::TASKS_FAILED_TOTAL),
"Total number of failed tasks",
&[],
)?;
let rejected_counter = registry.create_intcounter(
let rejected_counter = metrics.create_intcounter(
&format!("{}_{}", component_name, task_tracker::TASKS_REJECTED_TOTAL),
"Total number of rejected tasks",
&[],
......@@ -2051,20 +2049,20 @@ impl TaskTracker {
/// # use std::sync::Arc;
/// # use tokio::sync::Semaphore;
/// # use dynamo_runtime::utils::tasks::tracker::{TaskTracker, SemaphoreScheduler, LogOnlyPolicy};
/// # use dynamo_runtime::metrics::MetricsRegistry;
/// # fn example(registry: Arc<dyn MetricsRegistry>) -> anyhow::Result<()> {
/// # use dynamo_runtime::DistributedRuntime;
/// # fn example(drt: &DistributedRuntime) -> anyhow::Result<()> {
/// let scheduler = SemaphoreScheduler::with_permits(10);
/// let error_policy = LogOnlyPolicy::new();
/// let tracker = TaskTracker::new_with_prometheus(
/// scheduler,
/// error_policy,
/// registry.as_ref(),
/// drt,
/// "main_tracker"
/// )?;
/// # Ok(())
/// # }
/// ```
pub fn new_with_prometheus<R: MetricsRegistry + ?Sized>(
pub fn new_with_prometheus<R: MetricsHierarchy>(
scheduler: Arc<dyn TaskScheduler>,
error_policy: Arc<dyn OnErrorPolicy>,
registry: &R,
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment