Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: Add usage metric #58

Merged
merged 1 commit into from
Jun 5, 2024
Merged
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
29 changes: 27 additions & 2 deletions operator/src/metrics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ use crate::{get_config, Config, Error, KupoPort, State};
#[derive(Clone)]
pub struct Metrics {
pub dcu: IntCounterVec,
pub usage: IntCounterVec,
pub reconcile_failures: IntCounterVec,
pub metrics_failures: IntCounterVec,
}
Expand All @@ -27,6 +28,12 @@ impl Default for Metrics {
)
.unwrap();

let usage = IntCounterVec::new(
opts!("usage", "Feature usage",),
&["feature", "project", "resource_name", "tier"],
)
.unwrap();

let reconcile_failures = IntCounterVec::new(
opts!(
"kupo_operator_crd_reconciliation_errors_total",
Expand All @@ -47,6 +54,7 @@ impl Default for Metrics {

Metrics {
dcu,
usage,
reconcile_failures,
metrics_failures,
}
Expand All @@ -58,6 +66,7 @@ impl Metrics {
registry.register(Box::new(self.reconcile_failures.clone()))?;
registry.register(Box::new(self.metrics_failures.clone()))?;
registry.register(Box::new(self.dcu.clone()))?;
registry.register(Box::new(self.usage.clone()))?;

Ok(self)
}
Expand Down Expand Up @@ -85,6 +94,15 @@ impl Metrics {
.with_label_values(&[project, &service, &service_type, tenancy])
.inc_by(dcu);
}

pub fn count_usage(&self, project: &str, resource_name: &str, tier: &str, value: f64) {
let feature = &KupoPort::kind(&());
let value: u64 = value.ceil() as u64;

self.usage
.with_label_values(&[feature, project, resource_name, tier])
.inc_by(value);
}
}

async fn api_get_metrics(
Expand Down Expand Up @@ -180,7 +198,7 @@ pub fn run_metrics_collector(state: Arc<State>) {
info!("collecting metrics running");

let config = get_config();
let project_regex = Regex::new(r"prj-(.+)\..+").unwrap();
let project_regex = Regex::new(r"prj-(.+)\.(.+)$").unwrap();
let network_regex = Regex::new(r"kupo-([\w-]+)-.+").unwrap();
let mut last_execution = Utc::now();

Expand All @@ -193,7 +211,7 @@ pub fn run_metrics_collector(state: Arc<State>) {
last_execution = end;

let query = format!(
"sum by (consumer, exported_instance) (increase(kupo_proxy_http_total_request{{status_code!~\"401|429|503\"}}[{start}s] @ {}))",
"sum by (consumer, exported_instance, tier) (increase(kupo_proxy_http_total_request{{status_code!~\"401|429|503\"}}[{start}s] @ {}))",
end.timestamp_millis() / 1000
);

Expand All @@ -209,6 +227,7 @@ pub fn run_metrics_collector(state: Arc<State>) {
if result.value == 0.0
|| result.metric.consumer.is_none()
|| result.metric.exported_instance.is_none()
|| result.metric.tier.is_none()
{
continue;
}
Expand All @@ -221,6 +240,7 @@ pub fn run_metrics_collector(state: Arc<State>) {
}
let project_captures = project_captures.unwrap();
let project = project_captures.get(1).unwrap().as_str();
let resource_name = project_captures.get(2).unwrap().as_str();

let instance = result.metric.exported_instance.unwrap();
let network_captures = network_regex.captures(&instance);
Expand All @@ -230,6 +250,7 @@ pub fn run_metrics_collector(state: Arc<State>) {
}
let network_captures = network_captures.unwrap();
let network = network_captures.get(1).unwrap().as_str();
let tier = result.metric.tier.unwrap();

let dcu_per_request = config.dcu_per_request.get(network);
if dcu_per_request.is_none() {
Expand All @@ -245,6 +266,9 @@ pub fn run_metrics_collector(state: Arc<State>) {

let dcu = result.value * dcu_per_request;
state.metrics.count_dcu_consumed(project, network, dcu);
state
.metrics
.count_usage(project, resource_name, &tier, result.value);
}
}
});
Expand Down Expand Up @@ -322,6 +346,7 @@ pub fn run_kong_metrics_collector(state: Arc<State>) {
struct PrometheusDataResultMetric {
consumer: Option<String>,
exported_instance: Option<String>,
tier: Option<String>,
}

#[derive(Debug, Deserialize)]
Expand Down
12 changes: 8 additions & 4 deletions proxy/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ impl Metrics {
pub fn new() -> Self {
let http_total_request = register_int_counter_vec!(
opts!("kupo_proxy_http_total_request", "Total http request",),
&["consumer", "namespace", "instance", "status_code",]
&["consumer", "namespace", "instance", "status_code", "tier"]
)
.unwrap();

Expand All @@ -178,10 +178,14 @@ impl Metrics {
instance: &str,
status: &u16,
) {
let consumer = &consumer.to_string();

self.http_total_request
.with_label_values(&[consumer, namespace, instance, &status.to_string()])
.with_label_values(&[
&consumer.to_string(),
namespace,
instance,
&status.to_string(),
&consumer.tier,
])
.inc()
}
}
Expand Down
Loading