Skip to content

Commit ea2534a

Browse files
committed
Add namespace-level metrics
1 parent acbcb6b commit ea2534a

File tree

2 files changed

+44
-0
lines changed

2 files changed

+44
-0
lines changed
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
// Copyright (c) 2025, Oracle and/or its affiliates.
2+
// Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl.
3+
4+
package oracle.kubernetes.operator;
5+
6+
import java.util.ArrayList;
7+
import java.util.Collections;
8+
import java.util.List;
9+
import java.util.Map;
10+
import java.util.Optional;
11+
import java.util.Set;
12+
13+
import io.prometheus.client.Collector;
14+
import io.prometheus.client.GaugeMetricFamily;
15+
import oracle.kubernetes.operator.helpers.DomainPresenceInfo;
16+
17+
public class NamespaceCollector extends Collector {
18+
private static final String WKO_NAMESPACE_COUNT = "wko_namespace_count";
19+
private static final String WKO_DOMAIN_COUNT = "wko_domain_count";
20+
21+
private final MainDelegate mainDelegate;
22+
23+
public NamespaceCollector(MainDelegate mainDelegate) {
24+
this.mainDelegate = mainDelegate;
25+
}
26+
27+
@Override
28+
public List<MetricFamilySamples> collect() {
29+
List<MetricFamilySamples> mfs = new ArrayList<>();
30+
Set<String> namespaces = mainDelegate.getDomainNamespaces().getNamespaces();
31+
mfs.add(new GaugeMetricFamily(WKO_NAMESPACE_COUNT, "Count of managed namespaces", namespaces.size()));
32+
Map<String, Map<String, DomainPresenceInfo>> dps = mainDelegate.getDomainProcessor().getDomainPresenceInfoMap();
33+
GaugeMetricFamily domains = new GaugeMetricFamily(
34+
WKO_DOMAIN_COUNT, "Count of WebLogic domains", Collections.singletonList("namespace"));
35+
mfs.add(domains);
36+
for (String ns : namespaces) {
37+
Map<String, DomainPresenceInfo> dp = dps.get(ns);
38+
int size = Optional.ofNullable(dp).map(Map::size).orElse(0);
39+
domains.addMetric(Collections.singletonList(ns), size);
40+
}
41+
return mfs;
42+
}
43+
}

operator/src/main/java/oracle/kubernetes/operator/OperatorMain.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -297,6 +297,7 @@ public NextAction onSuccess(Packet packet, CallResponse<V1NamespaceList> callRes
297297

298298
void completeBegin() {
299299
try {
300+
new NamespaceCollector(mainDelegate).register();
300301
startMetricsServer(container);
301302
startRestServer(container);
302303

0 commit comments

Comments
 (0)