Skip to content

Commit

Permalink
Add missing documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
sfauvel committed Oct 16, 2024
1 parent 1c87643 commit f3791e2
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 8 deletions.
2 changes: 1 addition & 1 deletion internal/mithril-metric/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,4 @@ check:
${CARGO} fmt --check

doc:
${CARGO} doc --no-deps --open --features full
${CARGO} doc --no-deps --open
10 changes: 6 additions & 4 deletions internal/mithril-metric/src/helper.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
//! Helper to create a metric service.

/// Create a MetricService.
///
/// To build the service you need to provide the structure name and a list of metrics.
Expand Down Expand Up @@ -28,17 +30,17 @@ macro_rules! build_metrics_service {
paste::item! {
/// Metrics service which is responsible for recording and exposing metrics.
pub struct $service {
registry: Registry,
registry: prometheus::Registry,
$(
$metric_attribute: $metric_type,
)*
}

impl $service {
/// Create a new MetricsService instance.
pub fn new(logger: Logger) -> StdResult<Self> {
pub fn new(logger: slog::Logger) -> mithril_common::StdResult<Self> {

let registry = Registry::new();
let registry = prometheus::Registry::new();

$(
let $metric_attribute = $metric_type::new(
Expand All @@ -65,7 +67,7 @@ macro_rules! build_metrics_service {
}

impl MetricsServiceExporter for $service {
fn export_metrics(&self) -> StdResult<String> {
fn export_metrics(&self) -> mithril_common::StdResult<String> {
Ok(prometheus::TextEncoder::new().encode_to_string(&self.registry.gather())?)
}
}
Expand Down
2 changes: 2 additions & 0 deletions internal/mithril-metric/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#![warn(missing_docs)]

//! metrics module.
//! This module contains the tools to create a metrics service and a metrics server.

Expand Down
8 changes: 8 additions & 0 deletions internal/mithril-metric/src/metric.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
//! This module contains wrapper to prometheus metrics for use in a metrics service.

use prometheus::{core::Collector, Counter, Gauge, Opts};
use slog::{debug, Logger};

Expand Down Expand Up @@ -26,6 +28,7 @@ pub struct MetricCounter {
}

impl MetricCounter {
/// Create a new metric counter.
pub fn new(logger: Logger, name: &str, help: &str) -> StdResult<Self> {
let counter = MetricCounter::create_metric_counter(name, help)?;
Ok(Self {
Expand All @@ -35,11 +38,13 @@ impl MetricCounter {
})
}

/// Increment the counter.
pub fn increment(&self) {
debug!(self.logger, "Incrementing '{}' counter", self.name);
self.counter.inc();
}

/// Get the counter value.
pub fn get(&self) -> CounterValue {
self.counter.get().round() as CounterValue
}
Expand Down Expand Up @@ -70,6 +75,7 @@ pub struct MetricGauge {
}

impl MetricGauge {
/// Create a new metric gauge.
pub fn new(logger: Logger, name: &str, help: &str) -> StdResult<Self> {
let gauge = MetricGauge::create_metric_gauge(name, help)?;
Ok(Self {
Expand All @@ -79,6 +85,7 @@ impl MetricGauge {
})
}

/// Record a value in the gauge.
pub fn record<T: Into<f64> + Copy>(&self, value: T) {
debug!(
self.logger,
Expand All @@ -89,6 +96,7 @@ impl MetricGauge {
self.gauge.set(value.into());
}

/// Get the gauge value.
pub fn get(&self) -> f64 {
self.gauge.get().round()
}
Expand Down
2 changes: 2 additions & 0 deletions internal/mithril-metric/src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ use tokio::sync::oneshot::Receiver;
use mithril_common::logging::LoggerExtensions;
use mithril_common::StdResult;

/// Metrics service exporter gives the possibility of exporting metrics.
pub trait MetricsServiceExporter {
/// Export metrics.
fn export_metrics(&self) -> StdResult<String>;
}

Expand Down
5 changes: 2 additions & 3 deletions mithril-signer/src/metrics/service.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
use mithril_metric::{build_metrics_service, MetricsServiceExporter};
use prometheus::Registry;
use slog::Logger;

use mithril_common::{entities::Epoch, StdResult};
use mithril_common::entities::Epoch;

use mithril_metric::metric::{CounterValue, MetricCollector, MetricCounter, MetricGauge};

Expand Down Expand Up @@ -147,6 +145,7 @@ impl MetricsService {

#[cfg(test)]
mod tests {
use mithril_common::StdResult;
use prometheus_parse::Value;
use std::collections::BTreeMap;

Expand Down

0 comments on commit f3791e2

Please sign in to comment.