Skip to content

Commit 2bed4f5

Browse files
authored
chore: ensure metrics are correctly exposed (#807)
* add prometheus annotations to metrics service * adapted changelog
1 parent 06dab8c commit 2bed4f5

File tree

4 files changed

+31
-37
lines changed

4 files changed

+31
-37
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ All notable changes to this project will be documented in this file.
1111
- Helm: Allow Pod `priorityClassName` to be configured ([#798]).
1212
- Add support for Trino 477 ([#801]).
1313
- Add support for Hive 4.1.0 ([#805]).
14+
- Add `prometheus.io/path|port|scheme` annotations to metrics service ([#807]).
1415

1516
### Changed
1617

@@ -36,6 +37,7 @@ All notable changes to this project will be documented in this file.
3637
[#798]: https://github.com/stackabletech/trino-operator/pull/798
3738
[#801]: https://github.com/stackabletech/trino-operator/pull/801
3839
[#805]: https://github.com/stackabletech/trino-operator/pull/805
40+
[#807]: https://github.com/stackabletech/trino-operator/pull/807
3941

4042
## [25.7.0] - 2025-07-23
4143

rust/operator-binary/src/controller.rs

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ use crate::{
9292
authentication::resolve_authentication_classes,
9393
catalog,
9494
discovery::{TrinoDiscovery, TrinoDiscoveryProtocol, TrinoPodRef},
95-
rolegroup_headless_service_name, v1alpha1,
95+
v1alpha1,
9696
},
9797
listener::{
9898
LISTENER_VOLUME_DIR, LISTENER_VOLUME_NAME, build_group_listener, build_group_listener_pvc,
@@ -156,11 +156,6 @@ pub enum Error {
156156
source: stackable_operator::cluster_resources::Error,
157157
},
158158

159-
#[snafu(display("failed to apply global Service"))]
160-
ApplyRoleService {
161-
source: stackable_operator::cluster_resources::Error,
162-
},
163-
164159
#[snafu(display("failed to apply Service for {}", rolegroup))]
165160
ApplyRoleGroupService {
166161
source: stackable_operator::cluster_resources::Error,
@@ -1394,9 +1389,7 @@ fn build_rolegroup_statefulset(
13941389
),
13951390
..LabelSelector::default()
13961391
},
1397-
service_name: Some(rolegroup_headless_service_name(
1398-
&role_group_ref.object_name(),
1399-
)),
1392+
service_name: Some(role_group_ref.rolegroup_headless_service_name()),
14001393
template: pod_template,
14011394
volume_claim_templates: Some(persistent_volume_claims),
14021395
..StatefulSetSpec::default()

rust/operator-binary/src/crd/mod.rs

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -120,9 +120,6 @@ pub const MAX_TRINO_LOG_FILES_SIZE: MemoryQuantity = MemoryQuantity {
120120
unit: BinaryMultiple::Mebi,
121121
};
122122

123-
pub const METRICS_SERVICE_SUFFIX: &str = "metrics";
124-
pub const HEADLESS_SERVICE_SUFFIX: &str = "headless";
125-
126123
pub const JVM_HEAP_FACTOR: f32 = 0.8;
127124

128125
pub const DEFAULT_COORDINATOR_GRACEFUL_SHUTDOWN_TIMEOUT: Duration =
@@ -860,9 +857,7 @@ impl v1alpha1::TrinoCluster {
860857
let ns = ns.clone();
861858
(0..rolegroup.replicas.unwrap_or(0)).map(move |i| TrinoPodRef {
862859
namespace: ns.clone(),
863-
role_group_service_name: rolegroup_headless_service_name(
864-
&role_group_ref.object_name(),
865-
),
860+
role_group_service_name: role_group_ref.rolegroup_headless_service_name(),
866861
pod_name: format!(
867862
"{role_group}-{i}",
868863
role_group = role_group_ref.object_name()
@@ -963,16 +958,6 @@ impl v1alpha1::TrinoCluster {
963958
}
964959
}
965960

966-
/// Returns the metrics rolegroup service name `<cluster>-<role>-<rolegroup>-<METRICS_SERVICE_SUFFIX>`.
967-
pub fn rolegroup_metrics_service_name(role_group_ref_object_name: &str) -> String {
968-
format!("{role_group_ref_object_name}-{METRICS_SERVICE_SUFFIX}")
969-
}
970-
971-
/// Returns the headless rolegroup service name `<cluster>-<role>-<rolegroup>-<HEADLESS_SERVICE_SUFFIX>`.
972-
pub fn rolegroup_headless_service_name(role_group_ref_object_name: &str) -> String {
973-
format!("{role_group_ref_object_name}-{HEADLESS_SERVICE_SUFFIX}")
974-
}
975-
976961
fn extract_role_from_coordinator_config(
977962
fragment: Role<TrinoConfigFragment, TrinoCoordinatorRoleConfig, JavaCommonConfig>,
978963
) -> Role<TrinoConfigFragment, GenericRoleConfig, JavaCommonConfig> {

rust/operator-binary/src/service.rs

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,11 @@ use snafu::{ResultExt, Snafu};
44
use 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

11-
use crate::crd::{
12-
METRICS_PORT, METRICS_PORT_NAME, rolegroup_headless_service_name,
13-
rolegroup_metrics_service_name, v1alpha1,
14-
};
11+
use crate::crd::{METRICS_PORT, METRICS_PORT_NAME, v1alpha1};
1512

1613
#[derive(Snafu, Debug)]
1714
pub enum Error {
@@ -42,9 +39,7 @@ pub fn build_rolegroup_headless_service(
4239
Ok(Service {
4340
metadata: ObjectMetaBuilder::new()
4441
.name_and_namespace(trino)
45-
.name(rolegroup_headless_service_name(
46-
&role_group_ref.object_name(),
47-
))
42+
.name(role_group_ref.rolegroup_headless_service_name())
4843
.ownerreference_from_resource(trino, None, Some(true))
4944
.context(ObjectMissingMetadataForOwnerRefSnafu)?
5045
.with_recommended_labels(object_labels)
@@ -73,14 +68,13 @@ pub fn build_rolegroup_metrics_service(
7368
Ok(Service {
7469
metadata: ObjectMetaBuilder::new()
7570
.name_and_namespace(trino)
76-
.name(rolegroup_metrics_service_name(
77-
&role_group_ref.object_name(),
78-
))
71+
.name(role_group_ref.rolegroup_metrics_service_name())
7972
.ownerreference_from_resource(trino, None, Some(true))
8073
.context(ObjectMissingMetadataForOwnerRefSnafu)?
8174
.with_recommended_labels(object_labels)
8275
.context(MetadataBuildSnafu)?
83-
.with_label(Label::try_from(("prometheus.io/scrape", "true")).context(LabelBuildSnafu)?)
76+
.with_labels(prometheus_labels())
77+
.with_annotations(prometheus_annotations())
8478
.build(),
8579
spec: Some(ServiceSpec {
8680
// Internal communication does not need to be exposed
@@ -115,3 +109,23 @@ fn metrics_service_ports() -> Vec<ServicePort> {
115109
..ServicePort::default()
116110
}]
117111
}
112+
113+
/// Common labels for Prometheus
114+
fn prometheus_labels() -> Labels {
115+
Labels::try_from([("prometheus.io/scrape", "true")]).expect("should be a valid label")
116+
}
117+
118+
/// Common annotations for Prometheus
119+
///
120+
/// These annotations can be used in a ServiceMonitor.
121+
///
122+
/// see also <https://github.com/prometheus-community/helm-charts/blob/prometheus-27.32.0/charts/prometheus/values.yaml#L983-L1036>
123+
fn prometheus_annotations() -> Annotations {
124+
Annotations::try_from([
125+
("prometheus.io/path".to_owned(), "/metrics".to_owned()),
126+
("prometheus.io/port".to_owned(), METRICS_PORT.to_string()),
127+
("prometheus.io/scheme".to_owned(), "http".to_owned()),
128+
("prometheus.io/scrape".to_owned(), "true".to_owned()),
129+
])
130+
.expect("should be valid annotations")
131+
}

0 commit comments

Comments
 (0)