Skip to content

Commit

Permalink
fix: metrics (#281)
Browse files Browse the repository at this point in the history
  • Loading branch information
lauti7 authored Oct 31, 2023
1 parent e33aa4b commit 539170b
Show file tree
Hide file tree
Showing 9 changed files with 278 additions and 68 deletions.
270 changes: 233 additions & 37 deletions Cargo.lock

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ config = "0.13"
serde = "1.0.136"
log = "0.4.16"
env_logger = "0.9.0"
actix-web-prom = "0.6.0"
async-trait = "0.1.57"
tracing = "0.1"
tracing-log = "0.1"
Expand Down Expand Up @@ -44,6 +43,7 @@ warp = "0.3"
tokio-tungstenite = "0.18.0"
urlencoding = "2.1.2"
prometheus = { version = "0.13.3", features = ["process"] }
dcl-http-prom-metrics = "0.1.0"

[build-dependencies]
dcl-rpc = "2.3.5"
Expand Down
19 changes: 12 additions & 7 deletions src/api/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ use actix_web::body::MessageBody;
use actix_web::dev::{Server, ServiceFactory};
use actix_web::middleware;
use actix_web::{web::Data, App, HttpServer};
use dcl_http_prom_metrics::HttpMetricsCollector;
use tracing_actix_web::TracingLogger;

use crate::components::app::AppComponents;
use crate::components::configuration::Config;
use crate::components::metrics::initialize_metrics;
use crate::components::tracing::init_telemetry;

use super::middlewares::check_auth::CheckAuthToken;
Expand All @@ -27,7 +27,10 @@ pub fn run_service(data: Data<AppComponents>) -> Result<Server, std::io::Error>
init_telemetry();
let server_port = data.config.server.port;

let server = HttpServer::new(move || get_app_router(&data))
let http_metrics_collector =
Data::new(dcl_http_prom_metrics::HttpMetricsCollectorBuilder::default().build());

let server = HttpServer::new(move || get_app_router(&data, &http_metrics_collector))
.bind(("0.0.0.0", server_port))?
.run();

Expand All @@ -47,6 +50,7 @@ const ROUTES_NEED_AUTH_TOKEN: [&str; 3] = [

pub fn get_app_router(
data: &Data<AppComponents>,
http_metrics_collector: &Data<HttpMetricsCollector>,
) -> App<
impl ServiceFactory<
actix_web::dev::ServiceRequest,
Expand All @@ -63,13 +67,14 @@ pub fn get_app_router(

App::new()
.app_data(data.clone())
.wrap(TracingLogger::default())
// .wrap(initialize_metrics(data.config.env.clone()))
// .wrap(CheckMetricsToken::new(
// data.config.wkc_metrics_bearer_token.clone(),
// ))
.app_data(http_metrics_collector.clone())
.wrap(CheckAuthToken::new(protected_routes))
.wrap(dcl_http_prom_metrics::metrics())
.wrap(CheckMetricsToken::new(
data.config.wkc_metrics_bearer_token.clone(),
))
.wrap(middleware::NormalizePath::trim())
.wrap(TracingLogger::default())
.service(live)
.service(health)
.service(version)
Expand Down
13 changes: 0 additions & 13 deletions src/components/metrics.rs

This file was deleted.

1 change: 0 additions & 1 deletion src/components/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ pub mod app;
pub mod configuration;
pub mod database;
pub mod health;
pub mod metrics;
pub mod notifications;
pub mod redis;
pub mod synapse;
Expand Down
6 changes: 5 additions & 1 deletion tests/common/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use std::collections::HashMap;

use actix_web::{body::MessageBody, dev::ServiceFactory, web::Data, App};
use dcl_http_prom_metrics::HttpMetricsCollectorBuilder;
use social_service::{
api::app::get_app_router,
components::{
Expand Down Expand Up @@ -37,7 +38,10 @@ pub async fn get_app(
> {
let app_components = components.unwrap_or(AppComponents::new(Some(config)).await);
let app_data = Data::new(app_components);
get_app_router(&app_data)

let http_metrics_collector = Data::new(HttpMetricsCollectorBuilder::default().build());

get_app_router(&app_data, &http_metrics_collector)
}

/// We need this to avoid conccurency issues in Tests
Expand Down
9 changes: 7 additions & 2 deletions tests/route_synapse.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
mod common;
pub use common::*;
use dcl_http_prom_metrics::HttpMetricsCollectorBuilder;

use std::collections::HashMap;
use wiremock::{
Expand Down Expand Up @@ -51,7 +52,9 @@ async fn should_be_200_and_has_user_in_cache() {
let app_components = AppComponents::new(Some(config)).await;
let app_data = Data::new(app_components);

let router = get_app_router(&app_data);
let http_metrics_collector = Data::new(HttpMetricsCollectorBuilder::default().build());

let router = get_app_router(&app_data, &http_metrics_collector);

let app = test::init_service(router).await;

Expand Down Expand Up @@ -121,7 +124,9 @@ async fn should_be_500_and_not_user_in_cache() {
let app_components = AppComponents::new(Some(config)).await;
let app_data = Data::new(app_components);

let router = get_app_router(&app_data);
let http_metrics_collector = Data::new(HttpMetricsCollectorBuilder::default().build());

let router = get_app_router(&app_data, &http_metrics_collector);

let app = test::init_service(router).await;

Expand Down
21 changes: 16 additions & 5 deletions tests/routes/v1/friendships/get.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use std::collections::HashMap;

use actix_http::StatusCode;
use actix_web::{test, web::Data};
use dcl_http_prom_metrics::HttpMetricsCollectorBuilder;
use social_service::{
api::{app::get_app_router, routes::v1::friendships::types::FriendshipsResponse},
components::{app::AppComponents, database::DatabaseComponentImplementation},
Expand All @@ -28,7 +29,9 @@ async fn test_get_friendships_me_when_active() {
let app_components = AppComponents::new(Some(config)).await;
let app_data = Data::new(app_components);

let router = get_app_router(&app_data);
let http_metrics_collector = Data::new(HttpMetricsCollectorBuilder::default().build());

let router = get_app_router(&app_data, &http_metrics_collector);

let app = test::init_service(router).await;

Expand Down Expand Up @@ -74,7 +77,9 @@ async fn test_get_friends_when_active() {
let app_components = AppComponents::new(Some(config)).await;
let app_data = Data::new(app_components);

let router = get_app_router(&app_data);
let http_metrics_collector = Data::new(HttpMetricsCollectorBuilder::default().build());

let router = get_app_router(&app_data, &http_metrics_collector);

let app = test::init_service(router).await;

Expand Down Expand Up @@ -120,7 +125,9 @@ async fn test_get_friends_when_inactive() {
let app_components = AppComponents::new(Some(config)).await;
let app_data = Data::new(app_components);

let router = get_app_router(&app_data);
let http_metrics_collector = Data::new(HttpMetricsCollectorBuilder::default().build());

let router = get_app_router(&app_data, &http_metrics_collector);

let app = test::init_service(router).await;

Expand Down Expand Up @@ -188,7 +195,9 @@ async fn test_get_user_friends_database_error_should_return_unknown_error() {
let app_components = AppComponents::new(Some(config)).await;
let app_data = Data::new(app_components);

let router = get_app_router(&app_data);
let http_metrics_collector = Data::new(HttpMetricsCollectorBuilder::default().build());

let router = get_app_router(&app_data, &http_metrics_collector);

app_data.db.close().await;
let app = test::init_service(router).await;
Expand Down Expand Up @@ -224,7 +233,9 @@ async fn test_get_user_friends_should_return_the_address_list() {
let app_components = AppComponents::new(Some(config)).await;
let app_data = Data::new(app_components);

let router = get_app_router(&app_data);
let http_metrics_collector = Data::new(HttpMetricsCollectorBuilder::default().build());

let router = get_app_router(&app_data, &http_metrics_collector);

let app = test::init_service(router).await;

Expand Down
5 changes: 4 additions & 1 deletion tests/routes/v1/friendships/mutuals.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use std::collections::HashMap;
use actix_http::StatusCode;

use actix_web::{test, web::Data};
use dcl_http_prom_metrics::HttpMetricsCollectorBuilder;
use social_service::{
api::{
app::get_app_router,
Expand Down Expand Up @@ -36,7 +37,9 @@ async fn test_get_mutual_friends() {
let app_components = AppComponents::new(Some(config)).await;
let app_data = Data::new(app_components);

let router = get_app_router(&app_data);
let http_metrics_collector = Data::new(HttpMetricsCollectorBuilder::default().build());

let router = get_app_router(&app_data, &http_metrics_collector);

let app = test::init_service(router).await;

Expand Down

0 comments on commit 539170b

Please sign in to comment.