Skip to content

Commit cbbe19e

Browse files
authored
chore: ensure metrics are correctly exposed (#761)
* chore: ensure metrics are correctly exposed * add changelog entry * small refactoring for service name functions
1 parent a6ad574 commit cbbe19e

File tree

3 files changed

+27
-29
lines changed

3 files changed

+27
-29
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ All notable changes to this project will be documented in this file.
88

99
- Helm: Allow Pod `priorityClassName` to be configured ([#752]).
1010
- Add support for `34.0.0` ([#755]).
11+
- Add `prometheus.io/path|port|scheme` annotations to metrics services ([#761]).
1112

1213
### Changed
1314

@@ -31,6 +32,7 @@ All notable changes to this project will be documented in this file.
3132
[#753]: https://github.com/stackabletech/druid-operator/pull/753
3233
[#755]: https://github.com/stackabletech/druid-operator/pull/755
3334
[#756]: https://github.com/stackabletech/druid-operator/pull/756
35+
[#761]: https://github.com/stackabletech/druid-operator/pull/761
3436

3537
## [25.7.0] - 2025-07-23
3638

rust/operator-binary/src/druid_controller.rs

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -73,10 +73,11 @@ use crate::{
7373
Container, DB_PASSWORD_ENV, DB_USERNAME_ENV, DRUID_CONFIG_DIRECTORY, DS_BUCKET,
7474
DeepStorageSpec, DruidClusterStatus, DruidRole, EXTENSIONS_LOADLIST, HDFS_CONFIG_DIRECTORY,
7575
JVM_CONFIG, JVM_SECURITY_PROPERTIES_FILE, LOG_CONFIG_DIRECTORY, MAX_DRUID_LOG_FILES_SIZE,
76-
OPERATOR_NAME, RUNTIME_PROPS, RW_CONFIG_DIRECTORY, S3_ACCESS_KEY, S3_ENDPOINT_URL,
77-
S3_PATH_STYLE_ACCESS, S3_SECRET_KEY, STACKABLE_LOG_DIR, ZOOKEEPER_CONNECTION_STRING,
78-
authentication::AuthenticationClassesResolved, authorization::DruidAuthorization,
79-
build_recommended_labels, build_string_list, security::DruidTlsSecurity, v1alpha1,
76+
METRICS_PORT, METRICS_PORT_NAME, OPERATOR_NAME, RUNTIME_PROPS, RW_CONFIG_DIRECTORY,
77+
S3_ACCESS_KEY, S3_ENDPOINT_URL, S3_PATH_STYLE_ACCESS, S3_SECRET_KEY, STACKABLE_LOG_DIR,
78+
ZOOKEEPER_CONNECTION_STRING, authentication::AuthenticationClassesResolved,
79+
authorization::DruidAuthorization, build_recommended_labels, build_string_list,
80+
security::DruidTlsSecurity, v1alpha1,
8081
},
8182
discovery::{self, build_discovery_configmaps},
8283
extensions::get_extension_list,
@@ -87,10 +88,7 @@ use crate::{
8788
},
8889
operations::{graceful_shutdown::add_graceful_shutdown_config, pdb::add_pdbs},
8990
product_logging::extend_role_group_config_map,
90-
service::{
91-
build_rolegroup_headless_service, build_rolegroup_metrics_service,
92-
rolegroup_headless_service_name,
93-
},
91+
service::{build_rolegroup_headless_service, build_rolegroup_metrics_service},
9492
};
9593

9694
pub const DRUID_CONTROLLER_NAME: &str = "druidcluster";
@@ -1078,6 +1076,7 @@ fn build_rolegroup_statefulset(
10781076
.args(vec![main_container_commands.join("\n")])
10791077
.add_env_vars(rest_env)
10801078
.add_container_ports(druid_tls_security.container_ports(role))
1079+
.add_container_port(METRICS_PORT_NAME, METRICS_PORT.into())
10811080
// 10s * 30 = 300s to come up
10821081
.startup_probe(druid_tls_security.get_tcp_socket_probe(30, 10, 30, 3))
10831082
// 10s * 1 = 10s to get removed from service
@@ -1220,9 +1219,7 @@ fn build_rolegroup_statefulset(
12201219
),
12211220
..LabelSelector::default()
12221221
},
1223-
service_name: Some(rolegroup_headless_service_name(
1224-
&rolegroup_ref.object_name(),
1225-
)),
1222+
service_name: Some(rolegroup_ref.rolegroup_headless_service_name()),
12261223
template: pod_template,
12271224
volume_claim_templates: pvcs,
12281225
..StatefulSetSpec::default()

rust/operator-binary/src/service.rs

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,14 @@ 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, Label, ObjectLabels},
88
role_utils::RoleGroupRef,
99
};
1010

1111
use crate::crd::{
1212
DruidRole, METRICS_PORT, METRICS_PORT_NAME, security::DruidTlsSecurity, v1alpha1,
1313
};
1414

15-
const METRICS_SERVICE_SUFFIX: &str = "metrics";
16-
const HEADLESS_SERVICE_SUFFIX: &str = "headless";
17-
1815
#[derive(Snafu, Debug)]
1916
pub enum Error {
2017
#[snafu(display("object is missing metadata to build owner reference"))]
@@ -46,9 +43,7 @@ pub fn build_rolegroup_headless_service(
4643
Ok(Service {
4744
metadata: ObjectMetaBuilder::new()
4845
.name_and_namespace(druid)
49-
.name(rolegroup_headless_service_name(
50-
&role_group_ref.object_name(),
51-
))
46+
.name(role_group_ref.rolegroup_headless_service_name())
5247
.ownerreference_from_resource(druid, None, Some(true))
5348
.context(ObjectMissingMetadataForOwnerRefSnafu)?
5449
.with_recommended_labels(object_labels)
@@ -77,14 +72,13 @@ pub fn build_rolegroup_metrics_service(
7772
Ok(Service {
7873
metadata: ObjectMetaBuilder::new()
7974
.name_and_namespace(druid)
80-
.name(rolegroup_metrics_service_name(
81-
&role_group_ref.object_name(),
82-
))
75+
.name(role_group_ref.rolegroup_metrics_service_name())
8376
.ownerreference_from_resource(druid, None, Some(true))
8477
.context(ObjectMissingMetadataForOwnerRefSnafu)?
8578
.with_recommended_labels(object_labels)
8679
.context(MetadataBuildSnafu)?
8780
.with_label(Label::try_from(("prometheus.io/scrape", "true")).context(LabelBuildSnafu)?)
81+
.with_annotations(prometheus_annotations())
8882
.build(),
8983
spec: Some(ServiceSpec {
9084
// Internal communication does not need to be exposed
@@ -108,12 +102,17 @@ fn metrics_service_ports() -> Vec<ServicePort> {
108102
}]
109103
}
110104

111-
/// Returns the metrics rolegroup service name `<cluster>-<role>-<rolegroup>-<METRICS_SERVICE_SUFFIX>`.
112-
fn rolegroup_metrics_service_name(role_group_ref_object_name: &str) -> String {
113-
format!("{role_group_ref_object_name}-{METRICS_SERVICE_SUFFIX}")
114-
}
115-
116-
/// Returns the headless rolegroup service name `<cluster>-<role>-<rolegroup>-<HEADLESS_SERVICE_SUFFIX>`.
117-
pub fn rolegroup_headless_service_name(role_group_ref_object_name: &str) -> String {
118-
format!("{role_group_ref_object_name}-{HEADLESS_SERVICE_SUFFIX}")
105+
/// Common annotations for Prometheus
106+
///
107+
/// These annotations can be used in a ServiceMonitor.
108+
///
109+
/// see also <https://github.com/prometheus-community/helm-charts/blob/prometheus-27.32.0/charts/prometheus/values.yaml#L983-L1036>
110+
fn prometheus_annotations() -> Annotations {
111+
Annotations::try_from([
112+
("prometheus.io/path".to_owned(), "/metrics".to_owned()),
113+
("prometheus.io/port".to_owned(), METRICS_PORT.to_string()),
114+
("prometheus.io/scheme".to_owned(), "http".to_owned()),
115+
("prometheus.io/scrape".to_owned(), "true".to_owned()),
116+
])
117+
.expect("should be valid annotations")
119118
}

0 commit comments

Comments
 (0)