|
| 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