-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: make NodeState generic over DB with DatabaseMetadata
- Loading branch information
Showing
4 changed files
with
157 additions
and
51 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,62 @@ | ||
use std::sync::Arc; | ||
use std::{collections::HashMap, sync::Arc}; | ||
|
||
use metrics::{counter, gauge, histogram, Label}; | ||
|
||
use crate::database::Database; | ||
|
||
/// Extends [Database], adding a function that can be used as a hook for metric reporting. | ||
pub trait DatabaseMetrics: Database { | ||
/// Reports metrics for the database. | ||
fn report_metrics(&self); | ||
fn report_metrics(&self) { | ||
for (name, value, labels) in self.gauge_metrics() { | ||
gauge!(name, value, labels); | ||
} | ||
|
||
for (name, value, labels) in self.counter_metrics() { | ||
counter!(name, value, labels); | ||
} | ||
|
||
for (name, value, labels) in self.histogram_metrics() { | ||
histogram!(name, value, labels); | ||
} | ||
} | ||
|
||
/// Returns a list of [Gauge](metrics::Gauge) metrics for the database. | ||
fn gauge_metrics(&self) -> Vec<(&'static str, f64, Vec<Label>)> { | ||
vec![] | ||
} | ||
|
||
/// Returns a list of [Counter](metrics::Counter) metrics for the database. | ||
fn counter_metrics(&self) -> Vec<(&'static str, u64, Vec<Label>)> { | ||
vec![] | ||
} | ||
|
||
/// Returns a list of [Histogram](metrics::Histogram) metrics for the database. | ||
fn histogram_metrics(&self) -> Vec<(&'static str, f64, Vec<Label>)> { | ||
vec![] | ||
} | ||
} | ||
|
||
impl<DB: DatabaseMetrics> DatabaseMetrics for Arc<DB> { | ||
fn report_metrics(&self) { | ||
<DB as DatabaseMetrics>::report_metrics(self) | ||
} | ||
} | ||
|
||
/// Extends [Database] to include a [Metadata] type, which can be used by methods which need to | ||
/// dynamically retrieve information about the database. | ||
pub trait DatabaseMetadata: Database { | ||
/// The type used to store metadata about the database. | ||
type Metadata; | ||
|
||
/// Returns a [HashMap] of [&'static str]-addressable metadata about the database. | ||
fn metadata(&self) -> HashMap<&'static str, Self::Metadata>; | ||
} | ||
|
||
impl<DB: DatabaseMetadata> DatabaseMetadata for Arc<DB> { | ||
type Metadata = DB::Metadata; | ||
|
||
fn metadata(&self) -> HashMap<&'static str, Self::Metadata> { | ||
<DB as DatabaseMetadata>::metadata(self) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters