relay_server/endpoints/
autoscaling.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
use crate::http::StatusCode;
use crate::service::ServiceState;
use crate::services::autoscaling::{AutoscalingData, AutoscalingMessageKind};
use std::fmt::Display;
use std::fmt::Write;

/// Returns internal metrics data for relay.
pub async fn handle(state: ServiceState) -> (StatusCode, String) {
    let data = match state
        .autoscaling()
        .send(AutoscalingMessageKind::Check)
        .await
    {
        Ok(data) => data,
        Err(_) => {
            return (
                StatusCode::INTERNAL_SERVER_ERROR,
                "Failed to collect internal metrics".to_string(),
            )
        }
    };

    (StatusCode::OK, to_prometheus_string(&data))
}

/// Serializes the autoscaling data into a prometheus string.
fn to_prometheus_string(data: &AutoscalingData) -> String {
    let mut result = String::with_capacity(2048);

    append_data_row(&mut result, "memory_usage", data.memory_usage, &[]);
    append_data_row(&mut result, "up", data.up, &[]);
    append_data_row(&mut result, "spool_item_count", data.item_count, &[]);
    append_data_row(&mut result, "spool_total_size", data.total_size, &[]);
    for utilization in &data.services_metrics {
        let service_name = extract_service_name(utilization.0);
        append_data_row(
            &mut result,
            "utilization",
            utilization.1,
            &[("relay_service", service_name)],
        );
    }
    result
}

fn append_data_row(result: &mut String, label: &str, data: impl Display, tags: &[(&str, &str)]) {
    // Metrics are automatically prefixed with "relay_"
    write!(result, "relay_{label}").unwrap();
    if !tags.is_empty() {
        result.push('{');
        for (idx, (key, value)) in tags.iter().enumerate() {
            if idx > 0 {
                result.push_str(", ");
            }
            write!(result, "{key}=\"{value}\"").unwrap();
        }
        result.push('}');
    }
    writeln!(result, " {data}").unwrap();
}

/// Extracts the concrete Service name from a string with a namespace,
/// In case there are no ':' because a custom name is used, then the full name is returned.
/// For example:
/// * `relay::services::MyService` -> `MyService`.
/// * `aggregator_service` -> `aggregator_service`.
fn extract_service_name(full_name: &str) -> &str {
    full_name
        .rsplit_once(':')
        .map(|(_, s)| s)
        .unwrap_or(full_name)
}

#[cfg(test)]
mod test {
    use crate::endpoints::autoscaling::{append_data_row, extract_service_name};
    use crate::services::autoscaling::{AutoscalingData, ServiceUtilization};

    #[test]
    fn test_extract_service_with_namespace() {
        let service_name = extract_service_name("relay::services::MyService");
        assert_eq!(service_name, "MyService");
    }

    #[test]
    fn test_extract_service_without_namespace() {
        let service_name = extract_service_name("custom_service");
        assert_eq!(service_name, "custom_service");
    }

    #[test]
    fn test_append_no_labels() {
        let mut result = String::new();
        append_data_row(&mut result, "example", 200, &[]);
        assert_eq!(result, "relay_example 200\n");
    }

    #[test]
    fn test_append_single_label() {
        let mut result = String::new();
        append_data_row(&mut result, "example", 200, &[("key", "value")]);
        assert_eq!(result, "relay_example{key=\"value\"} 200\n");
    }

    #[test]
    fn test_append_multiple_labels() {
        let mut result = String::new();
        append_data_row(
            &mut result,
            "example",
            200,
            &[("first_key", "first_value"), ("second_key", "second_value")],
        );
        assert_eq!(
            result,
            "relay_example{first_key=\"first_value\", second_key=\"second_value\"} 200\n"
        );
    }

    #[test]
    fn test_prometheus_serialize() {
        let data = AutoscalingData {
            memory_usage: 0.75,
            up: 1,
            item_count: 10,
            total_size: 30,
            services_metrics: vec![
                ServiceUtilization("test", 10),
                ServiceUtilization("envelope", 50),
            ],
        };
        let result = super::to_prometheus_string(&data);
        assert_eq!(
            result,
            r#"relay_memory_usage 0.75
relay_up 1
relay_spool_item_count 10
relay_spool_total_size 30
relay_utilization{relay_service="test"} 10
relay_utilization{relay_service="envelope"} 50
"#
        );
    }
}