From fab4871f0231f7074a7d2b004ac91b06899334b5 Mon Sep 17 00:00:00 2001 From: Simon Hornby Date: Mon, 22 Jan 2024 15:38:18 +0200 Subject: [PATCH] feat: add endpoints for metrics that support using the proxy/all (#399) * feat: add endpoints for metrics that support using the proxy/all and frontend/all endpoints for clients --- server/src/frontend_api.rs | 98 ++++++++++++++++++++++++++++++++++++-- server/src/openapi.rs | 3 ++ 2 files changed, 98 insertions(+), 3 deletions(-) diff --git a/server/src/frontend_api.rs b/server/src/frontend_api.rs index b55c7237..3ae67cb0 100644 --- a/server/src/frontend_api.rs +++ b/server/src/frontend_api.rs @@ -115,6 +115,58 @@ async fn post_proxy_all_features( ) } +#[utoipa::path( + context_path = "/api/frontend", + responses( + (status = 202, description = "Accepted client metrics"), + (status = 403, description = "Was not allowed to post metrics"), + ), + request_body = ClientMetrics, + security( + ("Authorization" = []) + ) + )] +#[post("/all/client/metrics")] +async fn post_all_proxy_metrics( + edge_token: EdgeToken, + metrics: Json, + metrics_cache: Data, +) -> EdgeResult { + crate::metrics::client_metrics::register_client_metrics( + edge_token, + metrics.into_inner(), + metrics_cache, + ); + + Ok(HttpResponse::Accepted().finish()) +} + +#[utoipa::path( + context_path = "/api/frontend", + responses( + (status = 202, description = "Accepted client metrics"), + (status = 403, description = "Was not allowed to post metrics"), + ), + request_body = ClientMetrics, + security( + ("Authorization" = []) + ) + )] +#[post("/all/client/metrics")] +async fn post_all_frontend_metrics( + edge_token: EdgeToken, + metrics: Json, + metrics_cache: Data, +) -> EdgeResult { + crate::metrics::client_metrics::register_client_metrics( + edge_token, + metrics.into_inner(), + metrics_cache, + ); + + Ok(HttpResponse::Accepted().finish()) +} + #[utoipa::path( context_path = "/api/frontend", responses( @@ -578,7 +630,8 @@ fn configure_frontend_endpoints(cfg: &mut web::ServiceConfig, disable_all_endpoi .service(post_frontend_enabled_features) .service(post_frontend_register) .service(post_frontend_evaluate_single_feature) - .service(get_frontend_evaluate_single_feature), + .service(get_frontend_evaluate_single_feature) + .service(post_all_frontend_metrics), ); } else { cfg.service( @@ -625,7 +678,8 @@ fn configure_proxy_endpoints(cfg: &mut web::ServiceConfig, disable_all_endpoint: .service(get_enabled_proxy) .service(post_proxy_metrics) .service(post_proxy_enabled_features) - .service(post_proxy_register), + .service(post_proxy_register) + .service(post_all_proxy_metrics), ); } else { cfg.service( @@ -732,8 +786,12 @@ mod tests { use crate::types::{EdgeToken, TokenType, TokenValidationStatus}; async fn make_test_request() -> Request { + make_test_request_to("/api/proxy/client/metrics").await + } + + async fn make_test_request_to(path: &str) -> Request { test::TestRequest::post() - .uri("/api/proxy/client/metrics") + .uri(path) .insert_header(ContentType::json()) .insert_header(( "Authorization", @@ -981,6 +1039,40 @@ mod tests { assert_eq!(found_metric.no, expected.no); } + #[actix_web::test] + async fn metrics_all_does_the_same_thing_as_base_metrics() { + let metrics_cache = Arc::new(MetricsCache::default()); + + let app = test::init_service( + App::new() + .app_data(Data::from(metrics_cache.clone())) + .service(web::scope("/api/proxy").service(super::post_proxy_metrics)) + .service(web::scope("/api/frontend").service(super::post_all_frontend_metrics)), + ) + .await; + + let req = make_test_request_to("/api/proxy/client/metrics").await; + test::call_and_read_body(&app, req).await; + + let req = make_test_request_to("/api/frontend/all/client/metrics").await; + test::call_and_read_body(&app, req).await; + + let found_metric = metrics_cache + .metrics + .get(&MetricsKey { + app_name: "some-app".into(), + feature_name: "some-feature".into(), + environment: "development".into(), + timestamp: DateTime::parse_from_rfc3339("1867-11-07T12:00:00Z") + .unwrap() + .with_timezone(&Utc), + }) + .unwrap(); + + assert_eq!(found_metric.yes, 2); + assert_eq!(found_metric.no, 0); + } + #[tokio::test] async fn when_running_in_offline_mode_with_proxy_key_should_not_filter_features() { let client_features = client_features_with_constraint_requiring_user_id_of_seven(); diff --git a/server/src/openapi.rs b/server/src/openapi.rs index 2a067e5c..68ab80ca 100644 --- a/server/src/openapi.rs +++ b/server/src/openapi.rs @@ -12,6 +12,9 @@ use utoipa::{ crate::frontend_api::post_frontend_enabled_features, crate::frontend_api::get_proxy_all_features, crate::frontend_api::get_frontend_all_features, + crate::frontend_api::post_all_proxy_metrics, + crate::frontend_api::post_all_frontend_metrics, + crate::frontend_api::post_proxy_all_features, crate::frontend_api::post_frontend_all_features, crate::frontend_api::post_proxy_register,