Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ public class MicrometerMetricsV2 implements Metrics {

public static final String RECONCILIATION_EXECUTION_DURATION =
RECONCILIATIONS + "execution.duration";
public static final String NO_NAMESPACE_TAG = "no_namespace";
public static final String UNKNOWN_ACTION_TAG = "unknown";

private final MeterRegistry registry;
private final Map<String, AtomicInteger> gauges = new ConcurrentHashMap<>();
Expand Down Expand Up @@ -176,7 +178,11 @@ public void eventReceived(Event event, Map<String, Object> metadata) {
Tag.of(ACTION, resourceEvent.getAction().toString()));
} else {
incrementCounter(
EVENTS_RECEIVED, null, metadata, Tag.of(EVENT, event.getClass().getSimpleName()));
EVENTS_RECEIVED,
event.getRelatedCustomResourceID().getNamespace().orElse(null),
metadata,
Tag.of(EVENT, event.getClass().getSimpleName()),
Tag.of(ACTION, UNKNOWN_ACTION_TAG));
}
}

Expand Down Expand Up @@ -244,8 +250,12 @@ private static void addControllerNameTag(String name, List<Tag> tags) {
}

private void addNamespaceTag(String namespace, List<Tag> tags) {
if (includeNamespaceTag && namespace != null && !namespace.isBlank()) {
addTag(NAMESPACE, namespace, tags);
if (includeNamespaceTag) {
if (namespace != null && !namespace.isBlank()) {
addTag(NAMESPACE, namespace, tags);
} else {
addTag(NAMESPACE, NO_NAMESPACE_TAG, tags);
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,42 @@
*/
package io.javaoperatorsdk.operator.sample.metrics;

import java.util.List;

import io.javaoperatorsdk.operator.api.reconciler.Context;
import io.javaoperatorsdk.operator.api.reconciler.ControllerConfiguration;
import io.javaoperatorsdk.operator.api.reconciler.EventSourceContext;
import io.javaoperatorsdk.operator.api.reconciler.UpdateControl;
import io.javaoperatorsdk.operator.processing.event.ResourceID;
import io.javaoperatorsdk.operator.processing.event.source.EventSource;
import io.javaoperatorsdk.operator.processing.event.source.timer.TimerEventSource;
import io.javaoperatorsdk.operator.sample.metrics.customresource.MetricsHandlingCustomResource1;

@ControllerConfiguration
public class MetricsHandlingReconciler1
extends AbstractMetricsHandlingReconciler<MetricsHandlingCustomResource1> {

private static final long TIMER_DELAY = 5000;

private final TimerEventSource<MetricsHandlingCustomResource1> timerEventSource;

public MetricsHandlingReconciler1() {
super(100);
timerEventSource = new TimerEventSource<>();
}

@SuppressWarnings("unchecked")
@Override
public List<EventSource<?, MetricsHandlingCustomResource1>> prepareEventSources(
EventSourceContext<MetricsHandlingCustomResource1> context) {
return List.of((EventSource) timerEventSource);
}

@Override
public UpdateControl<MetricsHandlingCustomResource1> reconcile(
MetricsHandlingCustomResource1 resource, Context<MetricsHandlingCustomResource1> context) {
var result = super.reconcile(resource, context);
timerEventSource.scheduleOnce(ResourceID.fromResource(resource), TIMER_DELAY);
return result;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ public Duration step() {
new ProcessorMetrics().bindTo(compositeRegistry);
new UptimeMetrics().bindTo(compositeRegistry);

return MicrometerMetricsV2.newBuilder(compositeRegistry).build();
return MicrometerMetricsV2.newBuilder(compositeRegistry).withNamespaceAsTag().build();
}

@SuppressWarnings("unchecked")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.time.Duration;
import java.util.ArrayDeque;
Expand Down Expand Up @@ -226,24 +227,48 @@ private void verifyPrometheusMetrics() {
"reconciliations_execution_duration_milliseconds_count",
Duration.ofSeconds(30));

// First verify events_received_total exists at all (from ResourceEvents)
assertMetricPresent(prometheusUrl, "events_received_total", Duration.ofSeconds(30));

// Verify timer event source events are recorded.
// Timer events are not ResourceEvents, so they get action="unknown".
// The namespace comes from the event's ResourceID (same as the associated resource).
// The "exported_namespace" label is used because OTel collector's
// resource_to_telemetry_conversion renames Micrometer's "namespace" tag.
assertMetricPresent(
prometheusUrl,
"events_received_total{action=\"unknown\"}",
Duration.ofSeconds(30),
"events_received_total",
"unknown");

log.info("All metrics verified successfully in Prometheus");
}

private void assertMetricPresent(String prometheusUrl, String metricName, Duration timeout) {
assertMetricPresent(prometheusUrl, metricName, timeout, metricName);
}

private void assertMetricPresent(
String prometheusUrl, String query, Duration timeout, String... expectedSubstrings) {
await()
.atMost(timeout)
.pollInterval(Duration.ofSeconds(5))
.untilAsserted(
() -> {
String result = queryPrometheus(prometheusUrl, metricName);
log.info("{}: {}", metricName, result);
String result = queryPrometheus(prometheusUrl, query);
log.info("{}: {}", query, result);
assertThat(result).contains("\"status\":\"success\"");
assertThat(result).contains(metricName);
for (String expected : expectedSubstrings) {
log.info("Checking if result: {} contains expected: {}", result, expected);
assertThat(result).contains(expected);
Comment on lines +262 to +264
Copy link

Copilot AI Mar 27, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

log.info("Checking if result: {} contains expected: {}", result, expected); logs the full Prometheus JSON response once per expected substring on every poll iteration. This can create extremely noisy logs and slow CI. Prefer logging only the query / expected substring (or log the full result once at debug level).

Copilot uses AI. Check for mistakes.
}
});
}

private String queryPrometheus(String prometheusUrl, String query) throws IOException {
String urlString = prometheusUrl + "/api/v1/query?query=" + query;
String urlString =
prometheusUrl + "/api/v1/query?query=" + URLEncoder.encode(query, StandardCharsets.UTF_8);
URL url = new URL(urlString);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
Expand Down
Loading