Skip to content

Commit 9e563ac

Browse files
author
Clément Turmel
committed
feat(aggregator): add support of client_type in http server, and persist
it into metrics
1 parent e2e5e74 commit 9e563ac

File tree

14 files changed

+495
-197
lines changed

14 files changed

+495
-197
lines changed

mithril-aggregator/src/event_store/database/repository.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,7 @@ mod tests {
129129
value,
130130
Duration::from_secs(5),
131131
origin.to_string(),
132+
"CLIENT_TYPE_A".to_string(),
132133
metric_date.into(),
133134
);
134135

mithril-aggregator/src/http_server/routes/artifact_routes/cardano_database.rs

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ fn artifact_cardano_database_by_id(
2828
) -> impl Filter<Extract = (impl warp::Reply,), Error = warp::Rejection> + Clone {
2929
warp::path!("artifact" / "cardano-database" / String)
3030
.and(warp::get())
31-
.and(middlewares::with_origin_tag(dependency_manager))
31+
.and(middlewares::with_client_metadata(dependency_manager))
3232
.and(middlewares::with_logger(dependency_manager))
3333
.and(middlewares::with_http_message_service(dependency_manager))
3434
.and(middlewares::with_metrics_service(dependency_manager))
@@ -64,14 +64,16 @@ fn serve_cardano_database_dir(
6464
}
6565

6666
mod handlers {
67-
use crate::http_server::routes::reply;
68-
use crate::services::MessageService;
69-
use crate::MetricsService;
7067
use slog::{debug, warn, Logger};
7168
use std::convert::Infallible;
7269
use std::sync::Arc;
7370
use warp::http::StatusCode;
7471

72+
use crate::http_server::routes::middlewares::ClientMetadata;
73+
use crate::http_server::routes::reply;
74+
use crate::services::MessageService;
75+
use crate::MetricsService;
76+
7577
pub const LIST_MAX_ITEMS: usize = 20;
7678

7779
/// List artifacts
@@ -94,14 +96,17 @@ mod handlers {
9496
/// Get artifact by signed entity id
9597
pub async fn get_artifact_by_signed_entity_id(
9698
signed_entity_id: String,
97-
origin_tag: Option<String>,
99+
client_metadata: ClientMetadata,
98100
logger: Logger,
99101
http_message_service: Arc<dyn MessageService>,
100102
metrics_service: Arc<MetricsService>,
101103
) -> Result<impl warp::Reply, Infallible> {
102104
metrics_service
103105
.get_artifact_detail_cardano_database_total_served_since_startup()
104-
.increment(&[origin_tag.as_deref().unwrap_or_default()]);
106+
.increment(&[
107+
client_metadata.origin_tag.as_deref().unwrap_or_default(),
108+
client_metadata.client_type.as_deref().unwrap_or_default(),
109+
]);
105110

106111
match http_message_service
107112
.get_cardano_database_message(&signed_entity_id)
@@ -178,7 +183,7 @@ mod tests {
178183
CardanoDatabaseSnapshotMessage,
179184
};
180185
use mithril_common::test_utils::apispec::APISpec;
181-
use mithril_common::MITHRIL_ORIGIN_TAG_HEADER;
186+
use mithril_common::{MITHRIL_CLIENT_TYPE_HEADER, MITHRIL_ORIGIN_TAG_HEADER};
182187
use mithril_persistence::sqlite::HydrationError;
183188
use serde_json::Value::Null;
184189
use std::sync::Arc;
@@ -275,12 +280,13 @@ mod tests {
275280
let initial_counter_value = dependency_manager
276281
.metrics_service
277282
.get_artifact_detail_cardano_database_total_served_since_startup()
278-
.get(&["TEST"]);
283+
.get(&["TEST", "CLI"]);
279284

280285
request()
281286
.method(method)
282287
.path(path)
283288
.header(MITHRIL_ORIGIN_TAG_HEADER, "TEST")
289+
.header(MITHRIL_CLIENT_TYPE_HEADER, "CLI")
284290
.reply(&setup_router(RouterState::new_with_origin_tag_white_list(
285291
dependency_manager.clone(),
286292
&["TEST"],
@@ -292,7 +298,7 @@ mod tests {
292298
dependency_manager
293299
.metrics_service
294300
.get_artifact_detail_cardano_database_total_served_since_startup()
295-
.get(&["TEST"])
301+
.get(&["TEST", "CLI"]),
296302
);
297303
}
298304

mithril-aggregator/src/http_server/routes/artifact_routes/cardano_stake_distribution.rs

Lines changed: 25 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ fn artifact_cardano_stake_distribution_by_id(
2727
) -> impl Filter<Extract = (impl warp::Reply,), Error = warp::Rejection> + Clone {
2828
warp::path!("artifact" / "cardano-stake-distribution" / String)
2929
.and(warp::get())
30-
.and(middlewares::with_origin_tag(router_state))
30+
.and(middlewares::with_client_metadata(router_state))
3131
.and(middlewares::with_logger(router_state))
3232
.and(middlewares::with_http_message_service(router_state))
3333
.and(middlewares::with_metrics_service(router_state))
@@ -40,24 +40,26 @@ fn artifact_cardano_stake_distribution_by_epoch(
4040
) -> impl Filter<Extract = (impl warp::Reply,), Error = warp::Rejection> + Clone {
4141
warp::path!("artifact" / "cardano-stake-distribution" / "epoch" / String)
4242
.and(warp::get())
43-
.and(middlewares::with_origin_tag(router_state))
43+
.and(middlewares::with_client_metadata(router_state))
4444
.and(middlewares::with_logger(router_state))
4545
.and(middlewares::with_http_message_service(router_state))
4646
.and(middlewares::with_metrics_service(router_state))
4747
.and_then(handlers::get_artifact_by_epoch)
4848
}
4949

5050
pub mod handlers {
51-
use crate::http_server::routes::reply;
52-
use crate::services::MessageService;
53-
use crate::MetricsService;
54-
55-
use mithril_common::entities::Epoch;
5651
use slog::{warn, Logger};
5752
use std::convert::Infallible;
5853
use std::sync::Arc;
5954
use warp::http::StatusCode;
6055

56+
use mithril_common::entities::Epoch;
57+
58+
use crate::http_server::routes::middlewares::ClientMetadata;
59+
use crate::http_server::routes::reply;
60+
use crate::services::MessageService;
61+
use crate::MetricsService;
62+
6163
pub const LIST_MAX_ITEMS: usize = 20;
6264

6365
/// List CardanoStakeDistribution artifacts
@@ -80,14 +82,17 @@ pub mod handlers {
8082
/// Get Artifact by signed entity id
8183
pub async fn get_artifact_by_signed_entity_id(
8284
signed_entity_id: String,
83-
origin_tag: Option<String>,
85+
client_metadata: ClientMetadata,
8486
logger: Logger,
8587
http_message_service: Arc<dyn MessageService>,
8688
metrics_service: Arc<MetricsService>,
8789
) -> Result<impl warp::Reply, Infallible> {
8890
metrics_service
8991
.get_artifact_detail_cardano_stake_distribution_total_served_since_startup()
90-
.increment(&[origin_tag.as_deref().unwrap_or_default()]);
92+
.increment(&[
93+
client_metadata.origin_tag.as_deref().unwrap_or_default(),
94+
client_metadata.client_type.as_deref().unwrap_or_default(),
95+
]);
9196

9297
match http_message_service
9398
.get_cardano_stake_distribution_message(&signed_entity_id)
@@ -108,14 +113,17 @@ pub mod handlers {
108113
/// Get Artifact by epoch
109114
pub async fn get_artifact_by_epoch(
110115
epoch: String,
111-
origin_tag: Option<String>,
116+
client_metadata: ClientMetadata,
112117
logger: Logger,
113118
http_message_service: Arc<dyn MessageService>,
114119
metrics_service: Arc<MetricsService>,
115120
) -> Result<impl warp::Reply, Infallible> {
116121
metrics_service
117122
.get_artifact_detail_cardano_stake_distribution_total_served_since_startup()
118-
.increment(&[origin_tag.as_deref().unwrap_or_default()]);
123+
.increment(&[
124+
client_metadata.origin_tag.as_deref().unwrap_or_default(),
125+
client_metadata.client_type.as_deref().unwrap_or_default(),
126+
]);
119127

120128
let artifact_epoch = match epoch.parse::<u64>() {
121129
Ok(epoch) => Epoch(epoch),
@@ -161,7 +169,7 @@ pub mod tests {
161169
use mithril_common::{
162170
messages::{CardanoStakeDistributionListItemMessage, CardanoStakeDistributionMessage},
163171
test_utils::apispec::APISpec,
164-
MITHRIL_ORIGIN_TAG_HEADER,
172+
MITHRIL_CLIENT_TYPE_HEADER, MITHRIL_ORIGIN_TAG_HEADER,
165173
};
166174

167175
use crate::{initialize_dependencies, services::MockMessageService};
@@ -254,14 +262,15 @@ pub mod tests {
254262
let initial_counter_value = dependency_manager
255263
.metrics_service
256264
.get_artifact_detail_cardano_stake_distribution_total_served_since_startup()
257-
.get(&["TEST"]);
265+
.get(&["TEST", "CLI"]);
258266
{
259267
let path = "/artifact/cardano-stake-distribution/{hash}";
260268

261269
request()
262270
.method(method)
263271
.path(path)
264272
.header(MITHRIL_ORIGIN_TAG_HEADER, "TEST")
273+
.header(MITHRIL_CLIENT_TYPE_HEADER, "CLI")
265274
.reply(&setup_router(RouterState::new_with_origin_tag_white_list(
266275
dependency_manager.clone(),
267276
&["TEST"],
@@ -273,7 +282,7 @@ pub mod tests {
273282
dependency_manager
274283
.metrics_service
275284
.get_artifact_detail_cardano_stake_distribution_total_served_since_startup()
276-
.get(&["TEST"])
285+
.get(&["TEST", "CLI"])
277286
);
278287
}
279288

@@ -284,6 +293,7 @@ pub mod tests {
284293
.method(method)
285294
.path(&format!("{base_path}/123"))
286295
.header(MITHRIL_ORIGIN_TAG_HEADER, "TEST")
296+
.header(MITHRIL_CLIENT_TYPE_HEADER, "CLI")
287297
.reply(&setup_router(RouterState::new_with_origin_tag_white_list(
288298
dependency_manager.clone(),
289299
&["TEST"],
@@ -295,7 +305,7 @@ pub mod tests {
295305
dependency_manager
296306
.metrics_service
297307
.get_artifact_detail_cardano_stake_distribution_total_served_since_startup()
298-
.get(&["TEST"])
308+
.get(&["TEST", "CLI"])
299309
);
300310
}
301311
}

mithril-aggregator/src/http_server/routes/artifact_routes/cardano_transaction.rs

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -25,23 +25,24 @@ fn artifact_cardano_transaction_by_id(
2525
) -> impl Filter<Extract = (impl warp::Reply,), Error = warp::Rejection> + Clone {
2626
warp::path!("artifact" / "cardano-transaction" / String)
2727
.and(warp::get())
28-
.and(middlewares::with_origin_tag(router_state))
28+
.and(middlewares::with_client_metadata(router_state))
2929
.and(middlewares::with_logger(router_state))
3030
.and(middlewares::with_http_message_service(router_state))
3131
.and(middlewares::with_metrics_service(router_state))
3232
.and_then(handlers::get_artifact_by_signed_entity_id)
3333
}
3434

3535
pub mod handlers {
36-
use crate::http_server::routes::reply;
37-
use crate::services::MessageService;
38-
use crate::MetricsService;
39-
4036
use slog::{warn, Logger};
4137
use std::convert::Infallible;
4238
use std::sync::Arc;
4339
use warp::http::StatusCode;
4440

41+
use crate::http_server::routes::middlewares::ClientMetadata;
42+
use crate::http_server::routes::reply;
43+
use crate::services::MessageService;
44+
use crate::MetricsService;
45+
4546
pub const LIST_MAX_ITEMS: usize = 20;
4647

4748
/// List Cardano Transactions set artifacts
@@ -65,14 +66,17 @@ pub mod handlers {
6566
/// Get Artifact by signed entity id
6667
pub async fn get_artifact_by_signed_entity_id(
6768
signed_entity_id: String,
68-
origin_tag: Option<String>,
69+
client_metadata: ClientMetadata,
6970
logger: Logger,
7071
http_message_service: Arc<dyn MessageService>,
7172
metrics_service: Arc<MetricsService>,
7273
) -> Result<impl warp::Reply, Infallible> {
7374
metrics_service
7475
.get_artifact_detail_cardano_transaction_total_served_since_startup()
75-
.increment(&[origin_tag.as_deref().unwrap_or_default()]);
76+
.increment(&[
77+
client_metadata.origin_tag.as_deref().unwrap_or_default(),
78+
client_metadata.client_type.as_deref().unwrap_or_default(),
79+
]);
7680

7781
match http_message_service
7882
.get_cardano_transaction_message(&signed_entity_id)
@@ -100,11 +104,11 @@ pub mod tests {
100104
test::request,
101105
};
102106

103-
use mithril_common::test_utils::apispec::APISpec;
104107
use mithril_common::{
105108
messages::{CardanoTransactionSnapshotListItemMessage, CardanoTransactionSnapshotMessage},
106109
MITHRIL_ORIGIN_TAG_HEADER,
107110
};
111+
use mithril_common::{test_utils::apispec::APISpec, MITHRIL_CLIENT_TYPE_HEADER};
108112
use mithril_persistence::sqlite::HydrationError;
109113

110114
use crate::{initialize_dependencies, services::MockMessageService};
@@ -197,12 +201,13 @@ pub mod tests {
197201
let initial_counter_value = dependency_manager
198202
.metrics_service
199203
.get_artifact_detail_cardano_transaction_total_served_since_startup()
200-
.get(&["TEST"]);
204+
.get(&["TEST", "CLI"]);
201205

202206
request()
203207
.method(method)
204208
.path(path)
205209
.header(MITHRIL_ORIGIN_TAG_HEADER, "TEST")
210+
.header(MITHRIL_CLIENT_TYPE_HEADER, "CLI")
206211
.reply(&setup_router(RouterState::new_with_origin_tag_white_list(
207212
dependency_manager.clone(),
208213
&["TEST"],
@@ -214,7 +219,7 @@ pub mod tests {
214219
dependency_manager
215220
.metrics_service
216221
.get_artifact_detail_cardano_transaction_total_served_since_startup()
217-
.get(&["TEST"])
222+
.get(&["TEST", "CLI"])
218223
);
219224
}
220225

mithril-aggregator/src/http_server/routes/artifact_routes/mithril_stake_distribution.rs

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -26,23 +26,24 @@ fn artifact_mithril_stake_distribution_by_id(
2626
) -> impl Filter<Extract = (impl warp::Reply,), Error = warp::Rejection> + Clone {
2727
warp::path!("artifact" / "mithril-stake-distribution" / String)
2828
.and(warp::get())
29-
.and(middlewares::with_origin_tag(router_state))
29+
.and(middlewares::with_client_metadata(router_state))
3030
.and(middlewares::with_logger(router_state))
3131
.and(middlewares::with_http_message_service(router_state))
3232
.and(middlewares::with_metrics_service(router_state))
3333
.and_then(handlers::get_artifact_by_signed_entity_id)
3434
}
3535

3636
pub mod handlers {
37-
use crate::http_server::routes::reply;
38-
use crate::services::MessageService;
39-
use crate::MetricsService;
40-
4137
use slog::{warn, Logger};
4238
use std::convert::Infallible;
4339
use std::sync::Arc;
4440
use warp::http::StatusCode;
4541

42+
use crate::http_server::routes::middlewares::ClientMetadata;
43+
use crate::http_server::routes::reply;
44+
use crate::services::MessageService;
45+
use crate::MetricsService;
46+
4647
pub const LIST_MAX_ITEMS: usize = 20;
4748

4849
/// List MithrilStakeDistribution artifacts
@@ -65,14 +66,17 @@ pub mod handlers {
6566
/// Get Artifact by signed entity id
6667
pub async fn get_artifact_by_signed_entity_id(
6768
signed_entity_id: String,
68-
origin_tag: Option<String>,
69+
client_metadata: ClientMetadata,
6970
logger: Logger,
7071
http_message_service: Arc<dyn MessageService>,
7172
metrics_service: Arc<MetricsService>,
7273
) -> Result<impl warp::Reply, Infallible> {
7374
metrics_service
7475
.get_artifact_detail_mithril_stake_distribution_total_served_since_startup()
75-
.increment(&[origin_tag.as_deref().unwrap_or_default()]);
76+
.increment(&[
77+
client_metadata.origin_tag.as_deref().unwrap_or_default(),
78+
client_metadata.client_type.as_deref().unwrap_or_default(),
79+
]);
7680

7781
match http_message_service
7882
.get_mithril_stake_distribution_message(&signed_entity_id)
@@ -100,11 +104,11 @@ pub mod tests {
100104
test::request,
101105
};
102106

103-
use mithril_common::test_utils::apispec::APISpec;
104107
use mithril_common::{
105108
messages::{MithrilStakeDistributionListItemMessage, MithrilStakeDistributionMessage},
106109
MITHRIL_ORIGIN_TAG_HEADER,
107110
};
111+
use mithril_common::{test_utils::apispec::APISpec, MITHRIL_CLIENT_TYPE_HEADER};
108112
use mithril_persistence::sqlite::HydrationError;
109113

110114
use crate::{initialize_dependencies, services::MockMessageService};
@@ -197,12 +201,13 @@ pub mod tests {
197201
let initial_counter_value = dependency_manager
198202
.metrics_service
199203
.get_artifact_detail_mithril_stake_distribution_total_served_since_startup()
200-
.get(&["TEST"]);
204+
.get(&["TEST", "CLI"]);
201205

202206
request()
203207
.method(method)
204208
.path(path)
205209
.header(MITHRIL_ORIGIN_TAG_HEADER, "TEST")
210+
.header(MITHRIL_CLIENT_TYPE_HEADER, "CLI")
206211
.reply(&setup_router(RouterState::new_with_origin_tag_white_list(
207212
dependency_manager.clone(),
208213
&["TEST"],
@@ -214,7 +219,7 @@ pub mod tests {
214219
dependency_manager
215220
.metrics_service
216221
.get_artifact_detail_mithril_stake_distribution_total_served_since_startup()
217-
.get(&["TEST"])
222+
.get(&["TEST", "CLI"]),
218223
);
219224
}
220225

0 commit comments

Comments
 (0)