|
| 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 | +} |
0 commit comments