Skip to content

Commit 604787e

Browse files
authored
Let people set custom labels for metrics (#92)
1 parent 39609c8 commit 604787e

File tree

1 file changed

+20
-3
lines changed
  • aptos-indexer-processors-sdk/sdk-server-framework/src

1 file changed

+20
-3
lines changed

aptos-indexer-processors-sdk/sdk-server-framework/src/lib.rs

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,10 @@ where
4848
C: RunnableConfig,
4949
{
5050
let health_port = config.health_check_port;
51+
let additional_labels = config.metrics_config.additional_labels.clone();
5152
// Start liveness and readiness probes.
5253
let task_handler = handle.spawn(async move {
53-
register_probes_and_metrics_handler(health_port).await;
54+
register_probes_and_metrics_handler(health_port, additional_labels).await;
5455
anyhow::Ok(())
5556
});
5657
let main_task_handler = handle.spawn(async move { config.run().await });
@@ -69,10 +70,19 @@ pub struct GenericConfig<T> {
6970
// Shared configuration among all services.
7071
pub health_check_port: u16,
7172

73+
#[serde(default)]
74+
pub metrics_config: MetricsConfig,
75+
7276
// Specific configuration for each service.
7377
pub server_config: T,
7478
}
7579

80+
#[derive(Clone, Deserialize, Debug, Default, Serialize)]
81+
pub struct MetricsConfig {
82+
/// Additional labels to use for metrics.
83+
pub additional_labels: Vec<(String, String)>,
84+
}
85+
7686
#[async_trait::async_trait]
7787
impl<T> RunnableConfig for GenericConfig<T>
7888
where
@@ -158,8 +168,15 @@ pub fn setup_logging() {
158168
}
159169

160170
/// Register readiness and liveness probes and set up metrics endpoint.
161-
pub async fn register_probes_and_metrics_handler(port: u16) {
162-
let mut registry = <Registry>::default();
171+
pub async fn register_probes_and_metrics_handler(
172+
port: u16,
173+
additional_labels: Vec<(String, String)>,
174+
) {
175+
let mut registry = Registry::with_labels(
176+
additional_labels
177+
.into_iter()
178+
.map(|(k, v)| (k.into(), v.into())),
179+
);
163180
init_step_metrics_registry(&mut registry);
164181
init_channel_metrics_registry(&mut registry);
165182
AutometricsSettings::builder()

0 commit comments

Comments
 (0)