@@ -2,8 +2,10 @@ use std::collections::BTreeMap;
22
33use stackable_operator:: {
44 k8s_openapi:: api:: core:: v1:: { Service , ServicePort , ServiceSpec } ,
5- kvp:: { Annotations , Labels } ,
6- v2:: types:: common:: Port ,
5+ v2:: {
6+ builder:: service:: { Scheme , Scraping , prometheus_annotations, prometheus_labels} ,
7+ types:: common:: Port ,
8+ } ,
79} ;
810
911use crate :: controller:: {
@@ -107,6 +109,11 @@ pub(crate) fn build_rolegroup_metrics_service(
107109 role_group_name : & RoleGroupName ,
108110) -> Service {
109111 let tls_enabled = cluster. is_tls_enabled ( ) ;
112+ let ( scheme, port) = if tls_enabled {
113+ ( Scheme :: Https , APP_TLS_PORT )
114+ } else {
115+ ( Scheme :: Http , APP_PORT )
116+ } ;
110117 let metadata = cluster
111118 . object_meta (
112119 cluster
@@ -115,8 +122,14 @@ pub(crate) fn build_rolegroup_metrics_service(
115122 . to_string ( ) ,
116123 role_group_name,
117124 )
118- . with_labels ( prometheus_labels ( ) )
119- . with_annotations ( prometheus_annotations ( tls_enabled) )
125+ . with_labels ( prometheus_labels ( & Scraping :: Enabled ) )
126+ // The metrics are served on the same port as the HTTP/HTTPS traffic, under `/metrics`.
127+ . with_annotations ( prometheus_annotations (
128+ & Scraping :: Enabled ,
129+ & scheme,
130+ "/metrics" ,
131+ & port,
132+ ) )
120133 . build ( ) ;
121134
122135 let service_spec = headless_cluster_ip_service_spec (
@@ -159,32 +172,6 @@ fn metrics_service_port(tls_enabled: bool) -> ServicePort {
159172 }
160173}
161174
162- /// Common labels for Prometheus
163- fn prometheus_labels ( ) -> Labels {
164- Labels :: try_from ( [ ( "prometheus.io/scrape" , "true" ) ] ) . expect ( "should be a valid label" )
165- }
166-
167- /// Common annotations for Prometheus
168- ///
169- /// These annotations can be used in a ServiceMonitor.
170- ///
171- /// see also <https://github.com/prometheus-community/helm-charts/blob/prometheus-27.32.0/charts/prometheus/values.yaml#L983-L1036>
172- fn prometheus_annotations ( tls_enabled : bool ) -> Annotations {
173- let ( port, scheme) = if tls_enabled {
174- ( APP_TLS_PORT , "https" )
175- } else {
176- ( APP_PORT , "http" )
177- } ;
178-
179- Annotations :: try_from ( [
180- ( "prometheus.io/path" . to_owned ( ) , "/metrics" . to_owned ( ) ) ,
181- ( "prometheus.io/port" . to_owned ( ) , port. to_string ( ) ) ,
182- ( "prometheus.io/scheme" . to_owned ( ) , scheme. to_owned ( ) ) ,
183- ( "prometheus.io/scrape" . to_owned ( ) , "true" . to_owned ( ) ) ,
184- ] )
185- . expect ( "should be valid annotations" )
186- }
187-
188175#[ cfg( test) ]
189176mod tests {
190177 use serde_json:: json;
0 commit comments