Skip to content

Commit a728c7f

Browse files
Add functions to create Prometheus labels and annotations
1 parent 951b386 commit a728c7f

2 files changed

Lines changed: 78 additions & 0 deletions

File tree

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
pub mod meta;
22
pub mod pdb;
33
pub mod pod;
4+
pub mod service;
45
pub mod statefulset;
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
use crate::{
2+
kvp::{Annotations, Labels},
3+
v2::types::common::Port,
4+
};
5+
6+
pub enum Scraping {
7+
Enabled,
8+
Disabled,
9+
}
10+
11+
pub enum Scheme {
12+
Http,
13+
Https,
14+
}
15+
16+
/// Common labels for Prometheus
17+
pub fn prometheus_labels(scraping_enabled: &Scraping) -> Labels {
18+
Labels::try_from([(
19+
"prometheus.io/scrape",
20+
// "true" and "false" are valid label values.
21+
match scraping_enabled {
22+
Scraping::Enabled => "true".to_owned(),
23+
Scraping::Disabled => "false".to_owned(),
24+
},
25+
)])
26+
.expect("should be a valid label")
27+
}
28+
29+
/// Common annotations for Prometheus
30+
///
31+
/// These annotations can be used in a ServiceMonitor.
32+
///
33+
/// see also <https://github.com/prometheus-community/helm-charts/blob/prometheus-27.32.0/charts/prometheus/values.yaml#L983-L1036>
34+
///
35+
/// # Example
36+
///
37+
/// ```rust
38+
/// # use stackable_operator::v2::{
39+
/// # builder::service::{Scheme, Scraping, prometheus_annotations},
40+
/// # types::common::Port,
41+
/// # };
42+
///
43+
/// prometheus_annotations(
44+
/// &Scraping::Enabled,
45+
/// &Scheme::Https,
46+
/// "/_prometheus/metrics",
47+
/// &Port(9200),
48+
/// );
49+
/// ```
50+
pub fn prometheus_annotations(
51+
scraping_enabled: &Scraping,
52+
scheme: &Scheme,
53+
path: &str,
54+
port: &Port,
55+
) -> Annotations {
56+
// There are no restrictions on annotation values, so it is not necessary to check the given
57+
// parameters.
58+
Annotations::try_from([
59+
("prometheus.io/path".to_owned(), path.to_owned()),
60+
("prometheus.io/port".to_owned(), port.to_string()),
61+
(
62+
"prometheus.io/scheme".to_owned(),
63+
match scheme {
64+
Scheme::Http => "http".to_owned(),
65+
Scheme::Https => "https".to_owned(),
66+
},
67+
),
68+
(
69+
"prometheus.io/scrape".to_owned(),
70+
match scraping_enabled {
71+
Scraping::Enabled => "true".to_owned(),
72+
Scraping::Disabled => "false".to_owned(),
73+
},
74+
),
75+
])
76+
.expect("should be valid annotations")
77+
}

0 commit comments

Comments
 (0)