|
| 1 | +/* |
| 2 | + * Copyright 2024 Aiven Oy |
| 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 | + |
| 17 | +package io.aiven.kafka.tieredstorage.misc; |
| 18 | + |
| 19 | +import java.util.LinkedHashMap; |
| 20 | +import java.util.Map; |
| 21 | +import java.util.TreeMap; |
| 22 | + |
| 23 | +import org.apache.kafka.common.MetricName; |
| 24 | +import org.apache.kafka.common.MetricNameTemplate; |
| 25 | +import org.apache.kafka.common.metrics.Metrics; |
| 26 | +import org.apache.kafka.common.utils.Sanitizer; |
| 27 | + |
| 28 | +import io.aiven.kafka.tieredstorage.fetch.cache.ChunkCache; |
| 29 | +import io.aiven.kafka.tieredstorage.fetch.index.MemorySegmentIndexesCache; |
| 30 | +import io.aiven.kafka.tieredstorage.fetch.manifest.MemorySegmentManifestCache; |
| 31 | +import io.aiven.kafka.tieredstorage.metrics.CaffeineMetricsRegistry; |
| 32 | +import io.aiven.kafka.tieredstorage.metrics.MetricsRegistry; |
| 33 | +import io.aiven.kafka.tieredstorage.metrics.ThreadPoolMonitorMetricsRegistry; |
| 34 | + |
| 35 | +public class MetricsDocs { |
| 36 | + public static void main(final String[] args) { |
| 37 | + printSectionTitle("Core components metrics"); |
| 38 | + System.out.println(); |
| 39 | + printSubsectionTitle("RemoteStorageManager metrics"); |
| 40 | + System.out.println(); |
| 41 | + System.out.println(toRstTable(MetricsRegistry.METRIC_CONTEXT, new MetricsRegistry().all())); |
| 42 | + |
| 43 | + System.out.println(); |
| 44 | + printSubsectionTitle("SegmentManifestCache metrics"); |
| 45 | + System.out.println(); |
| 46 | + System.out.println(toRstTable( |
| 47 | + CaffeineMetricsRegistry.METRIC_CONTEXT, |
| 48 | + new CaffeineMetricsRegistry(MemorySegmentManifestCache.METRIC_GROUP).all())); |
| 49 | + System.out.println(); |
| 50 | + System.out.println(toRstTable( |
| 51 | + ThreadPoolMonitorMetricsRegistry.METRIC_CONFIG, |
| 52 | + new ThreadPoolMonitorMetricsRegistry(MemorySegmentManifestCache.THREAD_POOL_METRIC_GROUP).all())); |
| 53 | + |
| 54 | + System.out.println(); |
| 55 | + printSubsectionTitle("SegmentIndexesCache metrics"); |
| 56 | + System.out.println(toRstTable( |
| 57 | + CaffeineMetricsRegistry.METRIC_CONTEXT, |
| 58 | + new CaffeineMetricsRegistry(MemorySegmentIndexesCache.METRIC_GROUP).all())); |
| 59 | + System.out.println(toRstTable( |
| 60 | + ThreadPoolMonitorMetricsRegistry.METRIC_CONFIG, |
| 61 | + new ThreadPoolMonitorMetricsRegistry(MemorySegmentIndexesCache.THREAD_POOL_METRIC_GROUP).all())); |
| 62 | + System.out.println(); |
| 63 | + printSubsectionTitle("ChunkCache metrics"); |
| 64 | + System.out.println(); |
| 65 | + System.out.println(toRstTable( |
| 66 | + CaffeineMetricsRegistry.METRIC_CONTEXT, |
| 67 | + new CaffeineMetricsRegistry(ChunkCache.METRIC_GROUP).all())); |
| 68 | + System.out.println(); |
| 69 | + System.out.println(toRstTable( |
| 70 | + ThreadPoolMonitorMetricsRegistry.METRIC_CONFIG, |
| 71 | + new ThreadPoolMonitorMetricsRegistry(ChunkCache.THREAD_POOL_METRIC_GROUP).all())); |
| 72 | + |
| 73 | + System.out.println(); |
| 74 | + printSectionTitle("Storage Backend metrics"); |
| 75 | + System.out.println(); |
| 76 | + printSubsectionTitle("AzureBlobStorage metrics"); |
| 77 | + System.out.println(); |
| 78 | + System.out.println(toRstTable( |
| 79 | + io.aiven.kafka.tieredstorage.storage.azure.MetricRegistry.METRIC_CONTEXT, |
| 80 | + new io.aiven.kafka.tieredstorage.storage.azure.MetricRegistry().all())); |
| 81 | + System.out.println(); |
| 82 | + printSubsectionTitle("GcsStorage metrics"); |
| 83 | + System.out.println(); |
| 84 | + System.out.println(toRstTable( |
| 85 | + io.aiven.kafka.tieredstorage.storage.gcs.MetricRegistry.METRIC_CONTEXT, |
| 86 | + new io.aiven.kafka.tieredstorage.storage.gcs.MetricRegistry().all())); |
| 87 | + System.out.println(); |
| 88 | + printSubsectionTitle("S3Storage metrics"); |
| 89 | + System.out.println(); |
| 90 | + System.out.println(toRstTable( |
| 91 | + io.aiven.kafka.tieredstorage.storage.s3.MetricRegistry.METRIC_CONTEXT, |
| 92 | + new io.aiven.kafka.tieredstorage.storage.s3.MetricRegistry().all())); |
| 93 | + } |
| 94 | + |
| 95 | + // o.a.k.common.metrics.Metrics does only have generation of Html documentation. |
| 96 | + // as there is no plans to publish HTML docs, this util method is added to generate RST. |
| 97 | + // may be upstreamed. |
| 98 | + static String toRstTable(final String domain, final Iterable<MetricNameTemplate> allMetrics) { |
| 99 | + final Map<String, Map<String, String>> beansAndAttributes = new TreeMap<>(); |
| 100 | + |
| 101 | + try (final Metrics metrics = new Metrics()) { |
| 102 | + for (final MetricNameTemplate template : allMetrics) { |
| 103 | + final Map<String, String> tags = new LinkedHashMap<>(); |
| 104 | + for (final String s : template.tags()) { |
| 105 | + tags.put(s, "{" + s + "}"); |
| 106 | + } |
| 107 | + |
| 108 | + final MetricName metricName = metrics.metricName( |
| 109 | + template.name(), |
| 110 | + template.group(), |
| 111 | + template.description(), |
| 112 | + tags |
| 113 | + ); |
| 114 | + final String beanName = getMBeanName(domain, metricName); |
| 115 | + beansAndAttributes.computeIfAbsent(beanName, k -> new TreeMap<>()); |
| 116 | + final Map<String, String> attrAndDesc = beansAndAttributes.get(beanName); |
| 117 | + if (!attrAndDesc.containsKey(template.name())) { |
| 118 | + attrAndDesc.put(template.name(), template.description()); |
| 119 | + } else { |
| 120 | + throw new IllegalArgumentException( |
| 121 | + "mBean '" + beanName |
| 122 | + + "' attribute '" |
| 123 | + + template.name() |
| 124 | + + "' is defined twice." |
| 125 | + ); |
| 126 | + } |
| 127 | + } |
| 128 | + } |
| 129 | + |
| 130 | + final StringBuilder b = new StringBuilder(); |
| 131 | + |
| 132 | + for (final Map.Entry<String, Map<String, String>> e : beansAndAttributes.entrySet()) { |
| 133 | + // Add mBean name as a section title |
| 134 | + b.append(e.getKey()).append("\n"); |
| 135 | + b.append("=".repeat(e.getKey().length())).append("\n\n"); |
| 136 | + |
| 137 | + // Determine the maximum lengths for each column |
| 138 | + final int maxAttrLength = Math.max("Attribute name".length(), |
| 139 | + e.getValue().keySet().stream().mapToInt(String::length).max().orElse(0)); |
| 140 | + final int maxDescLength = Math.max("Description".length(), |
| 141 | + e.getValue().values().stream().mapToInt(String::length).max().orElse(0)); |
| 142 | + |
| 143 | + // Create the table header |
| 144 | + final String headerFormat = "%-" + maxAttrLength + "s %-" + maxDescLength + "s\n"; |
| 145 | + final String separatorLine = "=" + "=".repeat(maxAttrLength) + " " + "=".repeat(maxDescLength) + "\n"; |
| 146 | + |
| 147 | + b.append(separatorLine); |
| 148 | + b.append(String.format(headerFormat, "Attribute name", "Description")); |
| 149 | + b.append(separatorLine); |
| 150 | + |
| 151 | + // Add table rows |
| 152 | + for (final Map.Entry<String, String> e2 : e.getValue().entrySet()) { |
| 153 | + b.append(String.format(headerFormat, e2.getKey(), e2.getValue())); |
| 154 | + } |
| 155 | + |
| 156 | + // Close the table |
| 157 | + b.append(separatorLine); |
| 158 | + b.append("\n"); // Add an empty line between tables |
| 159 | + } |
| 160 | + |
| 161 | + return b.toString(); |
| 162 | + } |
| 163 | + |
| 164 | + // same as o.a.k.common.metrics.JmxReporter#getMBeanName but copy/pasted |
| 165 | + // to avoid adding another dependency to this module. |
| 166 | + static String getMBeanName(final String prefix, final MetricName metricName) { |
| 167 | + final StringBuilder beanName = new StringBuilder(); |
| 168 | + beanName.append(prefix); |
| 169 | + beanName.append(":type="); |
| 170 | + beanName.append(metricName.group()); |
| 171 | + for (final Map.Entry<String, String> entry : metricName.tags().entrySet()) { |
| 172 | + if (entry.getKey().length() <= 0 || entry.getValue().length() <= 0) { |
| 173 | + continue; |
| 174 | + } |
| 175 | + beanName.append(","); |
| 176 | + beanName.append(entry.getKey()); |
| 177 | + beanName.append("="); |
| 178 | + beanName.append(Sanitizer.jmxSanitize(entry.getValue())); |
| 179 | + } |
| 180 | + return beanName.toString(); |
| 181 | + } |
| 182 | + |
| 183 | + static void printSectionTitle(final String title) { |
| 184 | + System.out.println("=================\n" |
| 185 | + + title + "\n" |
| 186 | + + "================="); |
| 187 | + } |
| 188 | + |
| 189 | + static void printSubsectionTitle(final String title) { |
| 190 | + System.out.println("-----------------\n" |
| 191 | + + title + "\n" |
| 192 | + + "-----------------"); |
| 193 | + } |
| 194 | +} |
0 commit comments