Skip to content

Commit 08a780f

Browse files
committed
wip
Signed-off-by: Attila Mészáros <a_meszaros@apple.com>
1 parent f4b2882 commit 08a780f

File tree

8 files changed

+169
-283
lines changed

8 files changed

+169
-283
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939

4040
import static io.javaoperatorsdk.operator.api.reconciler.Constants.CONTROLLER_NAME;
4141

42-
@Deprecated(forRemoval = true)
42+
@Deprecated
4343
public class MicrometerMetrics implements Metrics {
4444

4545
private static final String PREFIX = "operator.sdk.";

sample-operators/metrics-processing/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@
104104
<image>gcr.io/distroless/java17-debian11</image>
105105
</from>
106106
<to>
107-
<image>webpage-operator</image>
107+
<image>metrics-processing-operator</image>
108108
</to>
109109
</configuration>
110110
</plugin>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
/*
2+
* Copyright Java Operator SDK Authors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package io.javaoperatorsdk.operator.sample.metrics;
17+
18+
import java.util.concurrent.ThreadLocalRandom;
19+
20+
import org.slf4j.Logger;
21+
import org.slf4j.LoggerFactory;
22+
23+
import io.fabric8.kubernetes.client.CustomResource;
24+
import io.javaoperatorsdk.operator.api.reconciler.Context;
25+
import io.javaoperatorsdk.operator.api.reconciler.Reconciler;
26+
import io.javaoperatorsdk.operator.api.reconciler.UpdateControl;
27+
import io.javaoperatorsdk.operator.sample.metrics.customresource.MetricsHandlingSpec;
28+
import io.javaoperatorsdk.operator.sample.metrics.customresource.MetricsHandlingStatus;
29+
30+
public abstract class AbstractMetricsHandlingReconciler<
31+
R extends CustomResource<MetricsHandlingSpec, MetricsHandlingStatus>>
32+
implements Reconciler<R> {
33+
34+
private static final Logger log =
35+
LoggerFactory.getLogger(AbstractMetricsHandlingReconciler.class);
36+
37+
private final long sleepMillis;
38+
39+
protected AbstractMetricsHandlingReconciler(long sleepMillis) {
40+
this.sleepMillis = sleepMillis;
41+
}
42+
43+
@Override
44+
public UpdateControl<R> reconcile(R resource, Context<R> context) {
45+
String name = resource.getMetadata().getName();
46+
log.info("Reconciling resource: {}", name);
47+
48+
try {
49+
Thread.sleep(sleepMillis + ThreadLocalRandom.current().nextLong(sleepMillis));
50+
} catch (InterruptedException e) {
51+
Thread.currentThread().interrupt();
52+
throw new RuntimeException("Interrupted during reconciliation", e);
53+
}
54+
55+
if (name.toLowerCase().contains("fail")) {
56+
log.error("Simulating failure for resource: {}", name);
57+
throw new IllegalStateException("Simulated reconciliation failure for resource: " + name);
58+
}
59+
60+
var status = resource.getStatus();
61+
if (status == null) {
62+
status = new MetricsHandlingStatus();
63+
resource.setStatus(status);
64+
}
65+
66+
var spec = resource.getSpec();
67+
if (spec != null) {
68+
status.setObservedNumber(spec.getNumber());
69+
}
70+
71+
log.info("Successfully reconciled resource: {}", name);
72+
return UpdateControl.patchStatus(resource);
73+
}
74+
}

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

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

18-
import java.util.List;
19-
20-
import org.slf4j.Logger;
21-
import org.slf4j.LoggerFactory;
22-
23-
import io.javaoperatorsdk.operator.api.reconciler.*;
24-
import io.javaoperatorsdk.operator.api.reconciler.Context;
25-
import io.javaoperatorsdk.operator.processing.event.source.EventSource;
18+
import io.javaoperatorsdk.operator.api.reconciler.ControllerConfiguration;
2619
import io.javaoperatorsdk.operator.sample.metrics.customresource.MetricsHandlingCustomResource1;
27-
import io.javaoperatorsdk.operator.sample.metrics.customresource.MetricsHandlingStatus;
2820

2921
@ControllerConfiguration
30-
public class MetricsHandlingReconciler1 implements Reconciler<MetricsHandlingCustomResource1> {
31-
32-
private static final Logger log = LoggerFactory.getLogger(MetricsHandlingReconciler1.class);
33-
34-
public MetricsHandlingReconciler1() {}
35-
36-
@Override
37-
public List<EventSource<?, MetricsHandlingCustomResource1>> prepareEventSources(
38-
EventSourceContext<MetricsHandlingCustomResource1> context) {
39-
return List.of();
40-
}
41-
42-
@Override
43-
public UpdateControl<MetricsHandlingCustomResource1> reconcile(
44-
MetricsHandlingCustomResource1 resource, Context<MetricsHandlingCustomResource1> context) {
45-
46-
String name = resource.getMetadata().getName();
47-
log.info("Reconciling resource: {}", name);
48-
49-
// Simulate some work
50-
try {
51-
Thread.sleep(100);
52-
} catch (InterruptedException e) {
53-
Thread.currentThread().interrupt();
54-
throw new RuntimeException("Interrupted during reconciliation", e);
55-
}
56-
57-
// Throw exception for resources with names containing "fail" or "error"
58-
if (name.toLowerCase().contains("fail") || name.toLowerCase().contains("error")) {
59-
log.error("Simulating failure for resource: {}", name);
60-
throw new IllegalStateException("Simulated reconciliation failure for resource: " + name);
61-
}
62-
63-
// Update status
64-
var status = resource.getStatus();
65-
if (status == null) {
66-
status = new MetricsHandlingStatus();
67-
resource.setStatus(status);
68-
}
69-
70-
var spec = resource.getSpec();
71-
if (spec != null) {
72-
status.setObservedNumber(spec.getNumber());
73-
}
22+
public class MetricsHandlingReconciler1
23+
extends AbstractMetricsHandlingReconciler<MetricsHandlingCustomResource1> {
7424

75-
log.info("Successfully reconciled resource: {}", name);
76-
return UpdateControl.patchStatus(resource);
25+
public MetricsHandlingReconciler1() {
26+
super(100);
7727
}
7828
}

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

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

18-
import org.slf4j.Logger;
19-
import org.slf4j.LoggerFactory;
20-
21-
import io.javaoperatorsdk.operator.api.reconciler.Context;
2218
import io.javaoperatorsdk.operator.api.reconciler.ControllerConfiguration;
23-
import io.javaoperatorsdk.operator.api.reconciler.Reconciler;
24-
import io.javaoperatorsdk.operator.api.reconciler.UpdateControl;
2519
import io.javaoperatorsdk.operator.sample.metrics.customresource.MetricsHandlingCustomResource2;
26-
import io.javaoperatorsdk.operator.sample.metrics.customresource.MetricsHandlingStatus;
2720

2821
@ControllerConfiguration
29-
public class MetricsHandlingReconciler2 implements Reconciler<MetricsHandlingCustomResource2> {
30-
31-
private static final Logger log = LoggerFactory.getLogger(MetricsHandlingReconciler2.class);
32-
33-
public MetricsHandlingReconciler2() {}
34-
35-
@Override
36-
public UpdateControl<MetricsHandlingCustomResource2> reconcile(
37-
MetricsHandlingCustomResource2 resource, Context<MetricsHandlingCustomResource2> context) {
38-
39-
String name = resource.getMetadata().getName();
40-
log.info("Reconciling resource: {}", name);
41-
42-
// Simulate some work (slightly different timing than reconciler1)
43-
try {
44-
Thread.sleep(150);
45-
} catch (InterruptedException e) {
46-
Thread.currentThread().interrupt();
47-
throw new RuntimeException("Interrupted during reconciliation", e);
48-
}
49-
50-
// Throw exception for resources with names containing "fail" or "error"
51-
if (name.toLowerCase().contains("fail") || name.toLowerCase().contains("error")) {
52-
log.error("Simulating failure for resource: {}", name);
53-
throw new IllegalStateException("Simulated reconciliation failure for resource: " + name);
54-
}
55-
56-
// Update status
57-
var status = resource.getStatus();
58-
if (status == null) {
59-
status = new MetricsHandlingStatus();
60-
resource.setStatus(status);
61-
}
62-
63-
var spec = resource.getSpec();
64-
if (spec != null) {
65-
status.setObservedNumber(spec.getNumber());
66-
}
22+
public class MetricsHandlingReconciler2
23+
extends AbstractMetricsHandlingReconciler<MetricsHandlingCustomResource2> {
6724

68-
log.info("Successfully reconciled resource: {}", name);
69-
return UpdateControl.patchStatus(resource);
25+
public MetricsHandlingReconciler2() {
26+
super(150);
7027
}
7128
}

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

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -62,14 +62,11 @@ public static boolean isLocal() {
6262
public static void main(String[] args) {
6363
log.info("Metrics Handling Sample Operator starting!");
6464

65-
// Load configuration from config.yaml
6665
Metrics metrics = initOTLPMetrics(isLocal());
6766
Operator operator =
6867
new Operator(o -> o.withStopOnInformerErrorDuringStartup(false).withMetrics(metrics));
69-
7068
operator.register(new MetricsHandlingReconciler1());
7169
operator.register(new MetricsHandlingReconciler2());
72-
7370
operator.start();
7471
}
7572

@@ -88,7 +85,6 @@ public static void main(String[] args) {
8885
return configProperties.get(key);
8986
}
9087

91-
// these should come from env variables
9288
@Override
9389
public Map<String, String> resourceAttributes() {
9490
return Map.of("service.name", "josdk", "operator", "metrics-processing");
@@ -98,6 +94,7 @@ public Map<String, String> resourceAttributes() {
9894
MeterRegistry otlpRegistry = new OtlpMeterRegistry(otlpConfig, Clock.SYSTEM);
9995
compositeRegistry.add(otlpRegistry);
10096

97+
// enable to easily see propagated metrics
10198
String enableConsoleLogging = System.getenv("METRICS_CONSOLE_LOGGING");
10299
if (!"true".equalsIgnoreCase(enableConsoleLogging)) {
103100
log.info("Console metrics logging enabled");
@@ -119,7 +116,6 @@ public Duration step() {
119116
}
120117
// Register JVM and system metrics
121118
log.info("Registering JVM and system metrics...");
122-
123119
new JvmMemoryMetrics().bindTo(compositeRegistry);
124120
new JvmGcMetrics().bindTo(compositeRegistry);
125121
new JvmThreadMetrics().bindTo(compositeRegistry);
@@ -140,16 +136,13 @@ private static Map<String, String> loadConfigFromYaml() {
140136
log.warn("otlp-config.yaml not found in resources, using default OTLP configuration");
141137
return configMap;
142138
}
143-
144139
Yaml yaml = new Yaml();
145140
Map<String, Object> yamlData = yaml.load(inputStream);
146-
147141
// Navigate to otlp section and map properties directly
148142
Map<String, Object> otlp = (Map<String, Object>) yamlData.get("otlp");
149143
if (otlp != null) {
150144
otlp.forEach((key, value) -> configMap.put("otlp." + key, value.toString()));
151145
}
152-
153146
log.info("Loaded OTLP configuration from otlp-config.yaml: {}", configMap);
154147
} catch (IOException e) {
155148
log.error("Error loading otlp-config.yaml", e);

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,6 @@ public class MetricsHandlingCustomResource2
2727

2828
@Override
2929
public String toString() {
30-
return "MetricsHandlingCustomResource1{" + "spec=" + spec + ", status=" + status + '}';
30+
return "MetricsHandlingCustomResource2{" + "spec=" + spec + ", status=" + status + '}';
3131
}
3232
}

0 commit comments

Comments
 (0)