Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,10 @@ path="src/bin/reporter.rs"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
anyhow = { version = "~1.0" }
axum = { version="~0.6" }
axum-macros = { version="~0.3" }
chrono = "~0.4"
chrono = { version = "~0.4", features = ["serde"] }
config = "~0.13"
evalexpr = "~9.0"
glob = "~0.3"
Expand Down
110 changes: 110 additions & 0 deletions src/bin/mock_convertor.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
use axum::{
extract::{Query, State},
http::StatusCode,
response::Json,
routing::get,
Router,
};
use chrono::Utc;
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::net::SocketAddr;
use std::sync::{Arc, Mutex};
use tokio::signal;

#[derive(Deserialize, Debug)]
struct HealthQuery {
environment: String,
service: String,
#[serde(default)]
_from: String,
#[serde(default)]
_to: String,
}

/// Structure of the response which waits reporter.rs
#[derive(Serialize, Debug)]
struct ServiceHealthPoint {
ts: u32,
value: u8,
#[serde(default)]
triggered: Vec<String>,
}

#[derive(Serialize, Debug)]
struct ServiceHealthResponse {
name: String,
service_category: String,
environment: String,
metrics: Vec<ServiceHealthPoint>,
}

/// State of the mock server. Mutex provides live changes.
type AppState = Arc<Mutex<HashMap<String, u8>>>;

#[tokio::main]
async fn main() {
let health_statuses: AppState = Arc::new(Mutex::new(HashMap::new()));

// 0 = OK, >0 = Problem
health_statuses
.lock()
.unwrap()
.insert("production_eu-de:test".to_string(), 2); // Imitate a problem (impact = 2)

let app = Router::new()
.route("/api/v1/health", get(health_handler))
.with_state(health_statuses);

let addr = SocketAddr::from(([127, 0, 0, 1], 3005));
println!("Mock convertor listening on {}", addr);

axum::Server::bind(&addr)
.serve(app.into_make_service())
.with_graceful_shutdown(shutdown_signal())
.await
.unwrap();
}

async fn health_handler(
State(state): State<AppState>,
Query(params): Query<HealthQuery>,
) -> (StatusCode, Json<ServiceHealthResponse>) {
let key = format!("{}:{}", params.environment, params.service);
println!("Received request for: {}", key);

let statuses = state.lock().unwrap();
let status_value = statuses.get(&key).cloned().unwrap_or(0);

let metric_time = Utc::now().timestamp() as u32;

let triggered = if status_value > 0 {
vec![format!("{}.{}", params.service, "api_down")]
} else {
Vec::new()
};

let response = ServiceHealthResponse {
name: params.service.clone(),
service_category: "mock_category".to_string(),
environment: params.environment.clone(),
metrics: vec![ServiceHealthPoint {
ts: metric_time,
value: status_value,
triggered,
}],
};

println!(
"Responding with status: {}, time: {}",
status_value, metric_time
);
(StatusCode::OK, Json(response))
}

async fn shutdown_signal() {
signal::ctrl_c()
.await
.expect("failed to install Ctrl+C handler");
println!("Signal received, shutting down mock server.");
}
Loading