@@ -4,7 +4,7 @@ use snafu::{ResultExt, Snafu};
44use stackable_operator:: {
55 builder:: meta:: ObjectMetaBuilder ,
66 k8s_openapi:: api:: core:: v1:: { Service , ServicePort , ServiceSpec } ,
7- kvp:: { Label , ObjectLabels } ,
7+ kvp:: { Annotations , Labels , ObjectLabels } ,
88 role_utils:: RoleGroupRef ,
99} ;
1010
@@ -78,17 +78,15 @@ pub fn build_rolegroup_metrics_service(
7878) -> Result < Service , Error > {
7979 let ports = metrics_service_ports ( ) ;
8080
81- let prometheus_label =
82- Label :: try_from ( ( "prometheus.io/scrape" , "true" ) ) . context ( LabelBuildSnafu ) ?;
83-
8481 let metadata = ObjectMetaBuilder :: new ( )
8582 . name_and_namespace ( airflow)
8683 . name ( rolegroup_metrics_service_name ( & rolegroup_ref. object_name ( ) ) )
8784 . ownerreference_from_resource ( airflow, None , Some ( true ) )
8885 . context ( ObjectMissingMetadataForOwnerRefSnafu ) ?
8986 . with_recommended_labels ( object_labels)
9087 . context ( MetadataBuildSnafu ) ?
91- . with_label ( prometheus_label)
88+ . with_labels ( prometheus_labels ( ) )
89+ . with_annotations ( prometheus_annotations ( ) )
9290 . build ( ) ;
9391
9492 let service_spec = ServiceSpec {
@@ -145,3 +143,23 @@ fn metrics_service_ports() -> Vec<ServicePort> {
145143 ..ServicePort :: default ( )
146144 } ]
147145}
146+
147+ /// Common labels for Prometheus
148+ fn prometheus_labels ( ) -> Labels {
149+ Labels :: try_from ( [ ( "prometheus.io/scrape" , "true" ) ] ) . expect ( "should be a valid label" )
150+ }
151+
152+ /// Common annotations for Prometheus
153+ ///
154+ /// These annotations can be used in a ServiceMonitor.
155+ ///
156+ /// see also <https://github.com/prometheus-community/helm-charts/blob/prometheus-27.32.0/charts/prometheus/values.yaml#L983-L1036>
157+ fn prometheus_annotations ( ) -> Annotations {
158+ Annotations :: try_from ( [
159+ ( "prometheus.io/path" . to_owned ( ) , "/metrics" . to_owned ( ) ) ,
160+ ( "prometheus.io/port" . to_owned ( ) , METRICS_PORT . to_string ( ) ) ,
161+ ( "prometheus.io/scheme" . to_owned ( ) , "http" . to_owned ( ) ) ,
162+ ( "prometheus.io/scrape" . to_owned ( ) , "true" . to_owned ( ) ) ,
163+ ] )
164+ . expect ( "should be valid annotations" )
165+ }
0 commit comments