Skip to content

Commit aff063f

Browse files
authored
DCU compute metrics (#20)
1 parent 06b8692 commit aff063f

File tree

9 files changed

+477
-317
lines changed

9 files changed

+477
-317
lines changed

Cargo.lock

Lines changed: 168 additions & 264 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

examples/manifest.yaml

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,9 @@ data:
9292
- job_name: proxy
9393
static_configs:
9494
- targets: ["proxy:9187"]
95+
- job_name: operator
96+
static_configs:
97+
- targets: ["operator:9187"]
9598
kind: ConfigMap
9699
metadata:
97100
name: prometheus-vol
@@ -310,6 +313,12 @@ spec:
310313
env:
311314
- name: ADDR
312315
value: "0.0.0.0:9187"
316+
- name: METRICS_DELAY
317+
value: "60"
318+
- name: PROMETHEUS_URL
319+
value: "http://prometheus/api/v1"
320+
- name: DCU_PER_PACKAGE
321+
value: "preview=5,preprod=5,mainnet=5"
313322
---
314323
apiVersion: v1
315324
kind: Service
@@ -324,7 +333,7 @@ spec:
324333
type: ClusterIP
325334
ports:
326335
- name: operator
327-
port: 80
336+
port: 9187
328337
targetPort: 9187
329338
protocol: TCP
330339
---

examples/setup

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#!/bin/bash
22

3-
~/go/bin/kind create cluster
3+
kind create cluster
44

55
echo "Building operator CRD"
66
cargo run --bin=crdgen > env-crd.yaml --manifest-path ../operator/Cargo.toml
@@ -14,9 +14,9 @@ echo "Building operator image"
1414
docker build -t operator:1.0 -f ../docker/dockerfile.operator ../
1515

1616
echo "Loading proxy image"
17-
~/go/bin/kind load docker-image proxy:1.0
17+
kind load docker-image proxy:1.0
1818

1919
echo "Loading operator image"
20-
~/go/bin/kind load docker-image operator:1.0
20+
kind load docker-image operator:1.0
2121

2222
kubectl apply -f manifest.yaml

operator/Cargo.toml

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ default-run = "controller"
77
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
88

99
[dependencies]
10-
actix-web = "4.4.1"
1110
argon2 = "0.5.3"
1211
bech32 = "0.11.0"
1312
dotenv = "0.15.0"
@@ -24,6 +23,12 @@ thiserror = "1.0.52"
2423
tokio = { version = "1.35.1", features = ["macros", "rt-multi-thread"] }
2524
tracing = "0.1.40"
2625
tracing-subscriber = "0.3.18"
26+
http-body-util = "0.1.0"
27+
hyper = { version = "1.1.0", features = ["full"] }
28+
hyper-util = { version = "0.1.3", features = ["full"] }
29+
reqwest = { version = "0.11.23", features = ["json"] }
30+
regex = "1.10.2"
31+
chrono = "0.4.31"
2732

2833
[[bin]]
2934
name="controller"

operator/src/config.rs

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use lazy_static::lazy_static;
2-
use std::env;
2+
use std::{collections::HashMap, env, time::Duration};
33

44
lazy_static! {
55
static ref CONTROLLER_CONFIG: Config = Config::from_env();
@@ -14,6 +14,9 @@ pub struct Config {
1414
pub dns_zone: String,
1515
pub extension_name: String,
1616
pub api_key_salt: String,
17+
pub metrics_delay: Duration,
18+
pub prometheus_url: String,
19+
pub dcu_per_package: HashMap<String, f64>,
1720
}
1821

1922
impl Config {
@@ -22,6 +25,25 @@ impl Config {
2225
dns_zone: env::var("DNS_ZONE").unwrap_or("demeter.run".into()),
2326
extension_name: env::var("EXTENSION_NAME").unwrap_or("node-m1".into()),
2427
api_key_salt: env::var("API_KEY_SALT").unwrap_or("cardano-node-salt".into()),
28+
metrics_delay: Duration::from_secs(
29+
std::env::var("METRICS_DELAY")
30+
.expect("METRICS_DELAY must be set")
31+
.parse::<u64>()
32+
.expect("METRICS_DELAY must be a number"),
33+
),
34+
prometheus_url: env::var("PROMETHEUS_URL").expect("PROMETHEUS_URL must be set"),
35+
dcu_per_package: env::var("DCU_PER_PACKAGE")
36+
.expect("DCU_PER_PACKAGE must be set")
37+
.split(',')
38+
.map(|pair| {
39+
let parts: Vec<&str> = pair.split('=').collect();
40+
let dcu = parts[1]
41+
.parse::<f64>()
42+
.expect("DCU_PER_PACKAGE must be NETWORK=NUMBER");
43+
44+
(parts[0].into(), dcu)
45+
})
46+
.collect(),
2547
}
2648
}
2749
}

operator/src/lib.rs

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,12 @@ pub enum Error {
1818

1919
#[error("Bech32 Error: {0}")]
2020
Bech32Error(String),
21+
22+
#[error("Http Request error: {0}")]
23+
HttpError(String),
24+
25+
#[error("Config Error: {0}")]
26+
ConfigError(String),
2127
}
2228
impl Error {
2329
pub fn metric_label(&self) -> String {
@@ -50,7 +56,7 @@ impl From<bech32::primitives::hrp::Error> for Error {
5056
}
5157
}
5258

53-
#[derive(Clone, Default)]
59+
#[derive(Clone)]
5460
pub struct State {
5561
registry: Registry,
5662
pub metrics: Metrics,
@@ -66,6 +72,11 @@ impl State {
6672
self.registry.gather()
6773
}
6874
}
75+
impl Default for State {
76+
fn default() -> Self {
77+
Self::new()
78+
}
79+
}
6980

7081
#[derive(Debug, Clone, Deserialize, Serialize, JsonSchema)]
7182
pub enum Network {
@@ -89,8 +100,8 @@ impl Display for Network {
89100
}
90101
}
91102

92-
pub use kube;
93103
pub use k8s_openapi;
104+
pub use kube;
94105

95106
pub type Result<T, E = Error> = std::result::Result<T, E>;
96107

operator/src/main.rs

Lines changed: 4 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,9 @@
1-
use actix_web::{
2-
get, middleware, web::Data, App, HttpRequest, HttpResponse, HttpServer, Responder,
3-
};
41
use dotenv::dotenv;
5-
use prometheus::{Encoder, TextEncoder};
62
use std::{io, sync::Arc};
7-
use tracing::{info, Level};
3+
use tracing::Level;
84

95
use operator::{controller, metrics as metrics_collector, State};
106

11-
#[get("/metrics")]
12-
async fn metrics(c: Data<Arc<State>>, _req: HttpRequest) -> impl Responder {
13-
let metrics = c.metrics_collected();
14-
let encoder = TextEncoder::new();
15-
let mut buffer = vec![];
16-
encoder.encode(&metrics, &mut buffer).unwrap();
17-
HttpResponse::Ok().body(buffer)
18-
}
19-
20-
#[get("/health")]
21-
async fn health(_: HttpRequest) -> impl Responder {
22-
HttpResponse::Ok().json("healthy")
23-
}
24-
257
#[tokio::main]
268
async fn main() -> io::Result<()> {
279
dotenv().ok();
@@ -30,22 +12,10 @@ async fn main() -> io::Result<()> {
3012

3113
let state = Arc::new(State::default());
3214

33-
let controller = controller::run(state.clone());
34-
let metrics_collector = metrics_collector::run_metrics_collector(state.clone());
35-
36-
let addr = std::env::var("ADDR").unwrap_or("0.0.0.0:8080".into());
37-
38-
let server = HttpServer::new(move || {
39-
App::new()
40-
.app_data(Data::new(state.clone()))
41-
.wrap(middleware::Logger::default())
42-
.service(health)
43-
.service(metrics)
44-
})
45-
.bind(&addr)?;
46-
info!({ addr }, "metrics server running");
15+
metrics_collector::run_metrics_collector(state.clone());
16+
metrics_collector::run_metrics_server(state.clone());
4717

48-
tokio::join!(server.run(), controller, metrics_collector).0?;
18+
controller::run(state.clone()).await;
4919

5020
Ok(())
5121
}

0 commit comments

Comments
 (0)