From a5f054d319db2a44e3561f255a126065b4d639c9 Mon Sep 17 00:00:00 2001 From: Malte Sander Date: Thu, 16 Oct 2025 15:09:17 +0200 Subject: [PATCH 1/2] add prometheus annotations to metrics service --- rust/operator-binary/src/controller.rs | 11 ++------ rust/operator-binary/src/crd/mod.rs | 17 +----------- rust/operator-binary/src/service.rs | 38 ++++++++++++++++++-------- 3 files changed, 29 insertions(+), 37 deletions(-) diff --git a/rust/operator-binary/src/controller.rs b/rust/operator-binary/src/controller.rs index 73186da2..fc9a88e2 100644 --- a/rust/operator-binary/src/controller.rs +++ b/rust/operator-binary/src/controller.rs @@ -92,7 +92,7 @@ use crate::{ authentication::resolve_authentication_classes, catalog, discovery::{TrinoDiscovery, TrinoDiscoveryProtocol, TrinoPodRef}, - rolegroup_headless_service_name, v1alpha1, + v1alpha1, }, listener::{ LISTENER_VOLUME_DIR, LISTENER_VOLUME_NAME, build_group_listener, build_group_listener_pvc, @@ -156,11 +156,6 @@ pub enum Error { source: stackable_operator::cluster_resources::Error, }, - #[snafu(display("failed to apply global Service"))] - ApplyRoleService { - source: stackable_operator::cluster_resources::Error, - }, - #[snafu(display("failed to apply Service for {}", rolegroup))] ApplyRoleGroupService { source: stackable_operator::cluster_resources::Error, @@ -1394,9 +1389,7 @@ fn build_rolegroup_statefulset( ), ..LabelSelector::default() }, - service_name: Some(rolegroup_headless_service_name( - &role_group_ref.object_name(), - )), + service_name: Some(role_group_ref.rolegroup_headless_service_name()), template: pod_template, volume_claim_templates: Some(persistent_volume_claims), ..StatefulSetSpec::default() diff --git a/rust/operator-binary/src/crd/mod.rs b/rust/operator-binary/src/crd/mod.rs index 16f85ae1..61e7522a 100644 --- a/rust/operator-binary/src/crd/mod.rs +++ b/rust/operator-binary/src/crd/mod.rs @@ -120,9 +120,6 @@ pub const MAX_TRINO_LOG_FILES_SIZE: MemoryQuantity = MemoryQuantity { unit: BinaryMultiple::Mebi, }; -pub const METRICS_SERVICE_SUFFIX: &str = "metrics"; -pub const HEADLESS_SERVICE_SUFFIX: &str = "headless"; - pub const JVM_HEAP_FACTOR: f32 = 0.8; pub const DEFAULT_COORDINATOR_GRACEFUL_SHUTDOWN_TIMEOUT: Duration = @@ -860,9 +857,7 @@ impl v1alpha1::TrinoCluster { let ns = ns.clone(); (0..rolegroup.replicas.unwrap_or(0)).map(move |i| TrinoPodRef { namespace: ns.clone(), - role_group_service_name: rolegroup_headless_service_name( - &role_group_ref.object_name(), - ), + role_group_service_name: role_group_ref.rolegroup_headless_service_name(), pod_name: format!( "{role_group}-{i}", role_group = role_group_ref.object_name() @@ -963,16 +958,6 @@ impl v1alpha1::TrinoCluster { } } -/// Returns the metrics rolegroup service name `---`. -pub fn rolegroup_metrics_service_name(role_group_ref_object_name: &str) -> String { - format!("{role_group_ref_object_name}-{METRICS_SERVICE_SUFFIX}") -} - -/// Returns the headless rolegroup service name `---`. -pub fn rolegroup_headless_service_name(role_group_ref_object_name: &str) -> String { - format!("{role_group_ref_object_name}-{HEADLESS_SERVICE_SUFFIX}") -} - fn extract_role_from_coordinator_config( fragment: Role, ) -> Role { diff --git a/rust/operator-binary/src/service.rs b/rust/operator-binary/src/service.rs index 39595443..3fbfe96d 100644 --- a/rust/operator-binary/src/service.rs +++ b/rust/operator-binary/src/service.rs @@ -4,14 +4,11 @@ use snafu::{ResultExt, Snafu}; use stackable_operator::{ builder::meta::ObjectMetaBuilder, k8s_openapi::api::core::v1::{Service, ServicePort, ServiceSpec}, - kvp::{Label, ObjectLabels}, + kvp::{Annotations, Labels, ObjectLabels}, role_utils::RoleGroupRef, }; -use crate::crd::{ - METRICS_PORT, METRICS_PORT_NAME, rolegroup_headless_service_name, - rolegroup_metrics_service_name, v1alpha1, -}; +use crate::crd::{METRICS_PORT, METRICS_PORT_NAME, v1alpha1}; #[derive(Snafu, Debug)] pub enum Error { @@ -42,9 +39,7 @@ pub fn build_rolegroup_headless_service( Ok(Service { metadata: ObjectMetaBuilder::new() .name_and_namespace(trino) - .name(rolegroup_headless_service_name( - &role_group_ref.object_name(), - )) + .name(role_group_ref.rolegroup_headless_service_name()) .ownerreference_from_resource(trino, None, Some(true)) .context(ObjectMissingMetadataForOwnerRefSnafu)? .with_recommended_labels(object_labels) @@ -73,14 +68,13 @@ pub fn build_rolegroup_metrics_service( Ok(Service { metadata: ObjectMetaBuilder::new() .name_and_namespace(trino) - .name(rolegroup_metrics_service_name( - &role_group_ref.object_name(), - )) + .name(role_group_ref.rolegroup_metrics_service_name()) .ownerreference_from_resource(trino, None, Some(true)) .context(ObjectMissingMetadataForOwnerRefSnafu)? .with_recommended_labels(object_labels) .context(MetadataBuildSnafu)? - .with_label(Label::try_from(("prometheus.io/scrape", "true")).context(LabelBuildSnafu)?) + .with_labels(prometheus_labels()) + .with_annotations(prometheus_annotations()) .build(), spec: Some(ServiceSpec { // Internal communication does not need to be exposed @@ -115,3 +109,23 @@ fn metrics_service_ports() -> Vec { ..ServicePort::default() }] } + +/// Common labels for Prometheus +fn prometheus_labels() -> Labels { + Labels::try_from([("prometheus.io/scrape", "true")]).expect("should be a valid label") +} + +/// Common annotations for Prometheus +/// +/// These annotations can be used in a ServiceMonitor. +/// +/// see also +fn prometheus_annotations() -> Annotations { + Annotations::try_from([ + ("prometheus.io/path".to_owned(), "/metrics".to_owned()), + ("prometheus.io/port".to_owned(), METRICS_PORT.to_string()), + ("prometheus.io/scheme".to_owned(), "http".to_owned()), + ("prometheus.io/scrape".to_owned(), "true".to_owned()), + ]) + .expect("should be valid annotations") +} From 57acf95d22e439a46b31699420100ff01bf17fda Mon Sep 17 00:00:00 2001 From: Malte Sander Date: Thu, 16 Oct 2025 15:15:32 +0200 Subject: [PATCH 2/2] adapted changelog --- CHANGELOG.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1a634586..149369e9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,6 +10,7 @@ All notable changes to this project will be documented in this file. - Support for the client spooling protocol ([#793]). - Helm: Allow Pod `priorityClassName` to be configured ([#798]). - Add support for Trino 477 ([#801]). +- Add `prometheus.io/path|port|scheme` annotations to metrics service ([#807]). ### Changed @@ -34,6 +35,7 @@ All notable changes to this project will be documented in this file. [#796]: https://github.com/stackabletech/trino-operator/pull/796 [#798]: https://github.com/stackabletech/trino-operator/pull/798 [#801]: https://github.com/stackabletech/trino-operator/pull/801 +[#807]: https://github.com/stackabletech/trino-operator/pull/807 ## [25.7.0] - 2025-07-23