Skip to content

Commit 5a44c3c

Browse files
committed
wip
Signed-off-by: Attila Mészáros <a_meszaros@apple.com>
1 parent 258fc0c commit 5a44c3c

File tree

4 files changed

+55
-9
lines changed

4 files changed

+55
-9
lines changed

micrometer-support/src/main/java/io/javaoperatorsdk/operator/monitoring/micrometer/MicrometerMetricsV2.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,8 @@ public class MicrometerMetricsV2 implements Metrics {
6262

6363
public static final String RECONCILIATION_EXECUTION_DURATION =
6464
RECONCILIATIONS + "execution.duration";
65-
public static final String NO_NAMESPACE = "NO_NAMESPACE";
66-
public static final String UNKNOWN_ACTION = "UNKNOWN";
65+
public static final String NO_NAMESPACE_TAG = "no_namespace";
66+
public static final String UNKNOWN_ACTION_TAG = "unknown";
6767

6868
private final MeterRegistry registry;
6969
private final Map<String, AtomicInteger> gauges = new ConcurrentHashMap<>();
@@ -182,7 +182,7 @@ public void eventReceived(Event event, Map<String, Object> metadata) {
182182
null,
183183
metadata,
184184
Tag.of(EVENT, event.getClass().getSimpleName()),
185-
Tag.of(ACTION, UNKNOWN_ACTION));
185+
Tag.of(ACTION, UNKNOWN_ACTION_TAG));
186186
}
187187
}
188188

@@ -254,7 +254,7 @@ private void addNamespaceTag(String namespace, List<Tag> tags) {
254254
if (namespace != null && !namespace.isBlank()) {
255255
addTag(NAMESPACE, namespace, tags);
256256
} else {
257-
addTag(NAMESPACE, NO_NAMESPACE, tags);
257+
addTag(NAMESPACE, NO_NAMESPACE_TAG, tags);
258258
}
259259
}
260260
}

sample-operators/metrics-processing/src/main/java/io/javaoperatorsdk/operator/sample/metrics/MetricsHandlingReconciler1.java

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,42 @@
1515
*/
1616
package io.javaoperatorsdk.operator.sample.metrics;
1717

18+
import java.util.List;
19+
20+
import io.javaoperatorsdk.operator.api.reconciler.Context;
1821
import io.javaoperatorsdk.operator.api.reconciler.ControllerConfiguration;
22+
import io.javaoperatorsdk.operator.api.reconciler.EventSourceContext;
23+
import io.javaoperatorsdk.operator.api.reconciler.UpdateControl;
24+
import io.javaoperatorsdk.operator.processing.event.ResourceID;
25+
import io.javaoperatorsdk.operator.processing.event.source.EventSource;
26+
import io.javaoperatorsdk.operator.processing.event.source.timer.TimerEventSource;
1927
import io.javaoperatorsdk.operator.sample.metrics.customresource.MetricsHandlingCustomResource1;
2028

2129
@ControllerConfiguration
2230
public class MetricsHandlingReconciler1
2331
extends AbstractMetricsHandlingReconciler<MetricsHandlingCustomResource1> {
2432

33+
private static final long TIMER_DELAY = 5000;
34+
35+
private final TimerEventSource<MetricsHandlingCustomResource1> timerEventSource;
36+
2537
public MetricsHandlingReconciler1() {
2638
super(100);
39+
timerEventSource = new TimerEventSource<>();
40+
}
41+
42+
@SuppressWarnings("unchecked")
43+
@Override
44+
public List<EventSource<?, MetricsHandlingCustomResource1>> prepareEventSources(
45+
EventSourceContext<MetricsHandlingCustomResource1> context) {
46+
return List.of((EventSource) timerEventSource);
47+
}
48+
49+
@Override
50+
public UpdateControl<MetricsHandlingCustomResource1> reconcile(
51+
MetricsHandlingCustomResource1 resource, Context<MetricsHandlingCustomResource1> context) {
52+
var result = super.reconcile(resource, context);
53+
timerEventSource.scheduleOnce(ResourceID.fromResource(resource), TIMER_DELAY);
54+
return result;
2755
}
2856
}

sample-operators/metrics-processing/src/main/java/io/javaoperatorsdk/operator/sample/metrics/MetricsHandlingSampleOperator.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ public Duration step() {
123123
new ProcessorMetrics().bindTo(compositeRegistry);
124124
new UptimeMetrics().bindTo(compositeRegistry);
125125

126-
return MicrometerMetricsV2.newBuilder(compositeRegistry).build();
126+
return MicrometerMetricsV2.newBuilder(compositeRegistry).withNamespaceAsTag().build();
127127
}
128128

129129
@SuppressWarnings("unchecked")

sample-operators/metrics-processing/src/test/java/io/javaoperatorsdk/operator/sample/metrics/MetricsHandlingE2E.java

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import java.io.*;
1919
import java.net.HttpURLConnection;
2020
import java.net.URL;
21+
import java.net.URLEncoder;
2122
import java.nio.charset.StandardCharsets;
2223
import java.time.Duration;
2324
import java.util.ArrayDeque;
@@ -226,24 +227,41 @@ private void verifyPrometheusMetrics() {
226227
"reconciliations_execution_duration_milliseconds_count",
227228
Duration.ofSeconds(30));
228229

230+
// Verify timer event source events are recorded with "no_namespace" tag
231+
// Timer events are not ResourceEvents so they have no namespace
232+
assertMetricPresent(
233+
prometheusUrl,
234+
"events_received_total{namespace=\"no_namespace\"}",
235+
Duration.ofSeconds(30),
236+
"events_received_total",
237+
"no_namespace");
238+
229239
log.info("All metrics verified successfully in Prometheus");
230240
}
231241

232242
private void assertMetricPresent(String prometheusUrl, String metricName, Duration timeout) {
243+
assertMetricPresent(prometheusUrl, metricName, timeout, metricName);
244+
}
245+
246+
private void assertMetricPresent(
247+
String prometheusUrl, String query, Duration timeout, String... expectedSubstrings) {
233248
await()
234249
.atMost(timeout)
235250
.pollInterval(Duration.ofSeconds(5))
236251
.untilAsserted(
237252
() -> {
238-
String result = queryPrometheus(prometheusUrl, metricName);
239-
log.info("{}: {}", metricName, result);
253+
String result = queryPrometheus(prometheusUrl, query);
254+
log.info("{}: {}", query, result);
240255
assertThat(result).contains("\"status\":\"success\"");
241-
assertThat(result).contains(metricName);
256+
for (String expected : expectedSubstrings) {
257+
assertThat(result).contains(expected);
258+
}
242259
});
243260
}
244261

245262
private String queryPrometheus(String prometheusUrl, String query) throws IOException {
246-
String urlString = prometheusUrl + "/api/v1/query?query=" + query;
263+
String urlString =
264+
prometheusUrl + "/api/v1/query?query=" + URLEncoder.encode(query, StandardCharsets.UTF_8);
247265
URL url = new URL(urlString);
248266
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
249267
connection.setRequestMethod("GET");

0 commit comments

Comments
 (0)