|
| 1 | +/** |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | + * contributor license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright ownership. |
| 5 | + * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | + * (the "License"); you may not use this file except in compliance with |
| 7 | + * the License. You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | +package org.apache.activemq.prometheus; |
| 18 | + |
| 19 | +import java.io.IOException; |
| 20 | +import java.io.PrintWriter; |
| 21 | +import java.io.StringWriter; |
| 22 | +import java.lang.management.ManagementFactory; |
| 23 | +import java.util.Set; |
| 24 | + |
| 25 | +import jakarta.servlet.http.HttpServlet; |
| 26 | +import jakarta.servlet.http.HttpServletRequest; |
| 27 | +import jakarta.servlet.http.HttpServletResponse; |
| 28 | +import javax.management.MBeanServer; |
| 29 | +import javax.management.ObjectName; |
| 30 | + |
| 31 | +public class PrometheusMetricsServlet extends HttpServlet { |
| 32 | + |
| 33 | + private static final long serialVersionUID = 1L; |
| 34 | + private static final String CONTENT_TYPE = "text/plain; version=0.0.4; charset=utf-8"; |
| 35 | + |
| 36 | + // Metrics can be easily extended by adding them here |
| 37 | + private static final MetricDefinition[] BROKER_METRICS = { |
| 38 | + new MetricDefinition("connections", "Current number of connections", "CurrentConnectionsCount", MetricType.GAUGE), |
| 39 | + new MetricDefinition("connections_total", "Total connections since last start", "TotalConnectionsCount", MetricType.COUNTER), |
| 40 | + new MetricDefinition("messages_enqueued_total", "Total messages enqueued since last start", "TotalEnqueueCount", MetricType.COUNTER), |
| 41 | + new MetricDefinition("messages_dequeued_total", "Total messages dequeued since last start", "TotalDequeueCount", MetricType.COUNTER), |
| 42 | + new MetricDefinition("consumers", "Current number of consumers", "TotalConsumerCount", MetricType.GAUGE), |
| 43 | + new MetricDefinition("producers", "Current number of producers", "TotalProducerCount", MetricType.GAUGE), |
| 44 | + new MetricDefinition("messages", "Current number of messages across all destinations", "TotalMessageCount", MetricType.GAUGE), |
| 45 | + new MetricDefinition("memory_percent_usage", "Percent (0-100) of memory limit used", "MemoryPercentUsage", MetricType.GAUGE), |
| 46 | + new MetricDefinition("memory_limit_bytes", "Memory limit in bytes", "MemoryLimit", MetricType.GAUGE), |
| 47 | + new MetricDefinition("store_percent_usage", "Percent (0-100) of store limit used", "StorePercentUsage", MetricType.GAUGE), |
| 48 | + new MetricDefinition("store_limit_bytes", "Store limit in bytes", "StoreLimit", MetricType.GAUGE), |
| 49 | + new MetricDefinition("temp_percent_usage", "Percent (0-100) of temp limit used", "TempPercentUsage", MetricType.GAUGE), |
| 50 | + new MetricDefinition("temp_limit_bytes", "Temp limit in bytes", "TempLimit", MetricType.GAUGE), |
| 51 | + new MetricDefinition("uptime_milliseconds", "Broker uptime in milliseconds", "UptimeMillis", MetricType.GAUGE) |
| 52 | + }; |
| 53 | + |
| 54 | + private static final MetricDefinition[] DESTINATION_METRICS = { |
| 55 | + new MetricDefinition("messages", "Number of messages in this destination", "QueueSize", MetricType.GAUGE), |
| 56 | + new MetricDefinition("enqueued_total", "Total messages enqueued to this destination since last start", "EnqueueCount", MetricType.COUNTER), |
| 57 | + new MetricDefinition("dequeued_total", "Total messages dequeued from destination since last start", "DequeueCount", MetricType.COUNTER), |
| 58 | + new MetricDefinition("dispatched_total", "Total messages dispatched from destination since last start", "DispatchCount", MetricType.COUNTER), |
| 59 | + new MetricDefinition("message_inflight_count", "Messages dispatched but not acknowledged", "InFlightCount", MetricType.GAUGE), |
| 60 | + new MetricDefinition("expired_total", "Total messages expired since last start", "ExpiredCount", MetricType.COUNTER), |
| 61 | + new MetricDefinition("consumers", "Number of consumers", "ConsumerCount", MetricType.GAUGE), |
| 62 | + new MetricDefinition("producers", "Number of producers", "ProducerCount", MetricType.GAUGE), |
| 63 | + new MetricDefinition("memory_percent_usage", "Percent (0-100) of destination memory limit used", "MemoryPercentUsage", MetricType.GAUGE), |
| 64 | + new MetricDefinition("memory_limit_bytes", "Memory limit for this destination in bytes", "MemoryLimit", MetricType.GAUGE), |
| 65 | + new MetricDefinition("memory_usage_bytes", "Memory used by this destination in bytes", "MemoryUsageByteCount", MetricType.GAUGE), |
| 66 | + new MetricDefinition("store_message_size_bytes", "Store message size in bytes", "StoreMessageSize", MetricType.GAUGE), |
| 67 | + new MetricDefinition("average_enqueue_time_milliseconds", "Average time (since last start) messages waited before dispatch", "AverageEnqueueTime", MetricType.GAUGE) |
| 68 | + }; |
| 69 | + |
| 70 | + @Override |
| 71 | + protected void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException { |
| 72 | + boolean perObject = request != null && "true".equalsIgnoreCase(request.getParameter("per_object")); |
| 73 | + |
| 74 | + StringWriter output = new StringWriter(); |
| 75 | + PrintWriter writer = new PrintWriter(output); |
| 76 | + |
| 77 | + try { |
| 78 | + writeMetrics(ManagementFactory.getPlatformMBeanServer(), writer, perObject); |
| 79 | + } catch (Exception exception) { |
| 80 | + response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, "Metrics collection failed"); |
| 81 | + return; |
| 82 | + } |
| 83 | + |
| 84 | + response.setContentType(CONTENT_TYPE); |
| 85 | + response.setStatus(HttpServletResponse.SC_OK); |
| 86 | + response.getWriter().write(output.toString()); |
| 87 | + } |
| 88 | + |
| 89 | + void writeMetrics(MBeanServer mBeanServer, PrintWriter writer, boolean perObject) throws Exception { |
| 90 | + writeBrokerMetrics(mBeanServer, writer); |
| 91 | + // Scraping this by default on brokers with lots of queues or topics might be expensive |
| 92 | + if (perObject) { |
| 93 | + writeDestinationMetrics(mBeanServer, writer, "Queue"); |
| 94 | + writeDestinationMetrics(mBeanServer, writer, "Topic"); |
| 95 | + } |
| 96 | + writer.flush(); |
| 97 | + } |
| 98 | + |
| 99 | + private void writeBrokerMetrics(MBeanServer mBeanServer, PrintWriter writer) throws Exception { |
| 100 | + ObjectName pattern = new ObjectName("org.apache.activemq:type=Broker,brokerName=*"); |
| 101 | + Set<ObjectName> brokers = mBeanServer.queryNames(pattern, null); |
| 102 | + |
| 103 | + for (MetricDefinition metric : BROKER_METRICS) { |
| 104 | + String metricName = "activemq_broker_" + metric.name; |
| 105 | + writeMetadata(writer, metricName, metric); |
| 106 | + for (ObjectName broker : brokers) { |
| 107 | + String brokerName = sanitizeLabel((String) mBeanServer.getAttribute(broker, "BrokerName")); |
| 108 | + String labels = "broker=\"" + brokerName + "\""; |
| 109 | + writeSample(writer, metricName, labels, getNumber(mBeanServer, broker, metric.attribute)); |
| 110 | + } |
| 111 | + } |
| 112 | + } |
| 113 | + |
| 114 | + private void writeDestinationMetrics(MBeanServer mBeanServer, PrintWriter writer, String type) throws Exception { |
| 115 | + String queryPattern = "org.apache.activemq:type=Broker,brokerName=*,destinationType=" + type + ",destinationName=*"; |
| 116 | + Set<ObjectName> destinations = mBeanServer.queryNames(new ObjectName(queryPattern), null); |
| 117 | + String typeLower = type.toLowerCase(); |
| 118 | + |
| 119 | + for (MetricDefinition metric : DESTINATION_METRICS) { |
| 120 | + String metricName = "activemq_" + typeLower + "_" + metric.name; |
| 121 | + writeMetadata(writer, metricName, metric.withFormattedHelp(typeLower)); |
| 122 | + for (ObjectName destination : destinations) { |
| 123 | + String brokerName = sanitizeLabel(destination.getKeyProperty("brokerName")); |
| 124 | + String destinationName = sanitizeLabel(destination.getKeyProperty("destinationName")); |
| 125 | + String labels = String.format("broker=\"%s\",destination=\"%s\"", brokerName, destinationName); |
| 126 | + writeSample(writer, metricName, labels, getNumber(mBeanServer, destination, metric.attribute)); |
| 127 | + } |
| 128 | + } |
| 129 | + } |
| 130 | + |
| 131 | + private double getNumber(MBeanServer mBeanServer, ObjectName name, String attribute) { |
| 132 | + try { |
| 133 | + Object value = mBeanServer.getAttribute(name, attribute); |
| 134 | + if (value instanceof Number) { |
| 135 | + return ((Number) value).doubleValue(); |
| 136 | + } |
| 137 | + } catch (Exception ignored) { |
| 138 | + // Some attributes are not available on every ActiveMQ deployment (eg: bridge metrics) |
| 139 | + } |
| 140 | + return 0; |
| 141 | + } |
| 142 | + |
| 143 | + private void writeMetadata(PrintWriter writer, String metric, MetricDefinition def) { |
| 144 | + writer.println("# HELP " + metric + " " + def.help); |
| 145 | + writer.println("# TYPE " + metric + " " + def.type.prometheusName()); |
| 146 | + } |
| 147 | + |
| 148 | + private void writeSample(PrintWriter writer, String metric, String labels, double value) { |
| 149 | + if (value == (long) value) { |
| 150 | + writer.println(metric + "{" + labels + "} " + (long) value); |
| 151 | + } else { |
| 152 | + writer.println(metric + "{" + labels + "} " + value); |
| 153 | + } |
| 154 | + } |
| 155 | + |
| 156 | + static String sanitizeLabel(String value) { |
| 157 | + if (value == null) { |
| 158 | + return "unknown"; |
| 159 | + } |
| 160 | + return value.replace("\\", "\\\\").replace("\"", "\\\"").replace("\n", "\\n"); |
| 161 | + } |
| 162 | + |
| 163 | + private static final class MetricDefinition { |
| 164 | + private final String name; |
| 165 | + private final String help; |
| 166 | + private final String attribute; |
| 167 | + private final MetricType type; |
| 168 | + |
| 169 | + private MetricDefinition(String name, String help, String attribute, MetricType type) { |
| 170 | + this.name = name; |
| 171 | + this.help = help; |
| 172 | + this.attribute = attribute; |
| 173 | + this.type = type; |
| 174 | + } |
| 175 | + |
| 176 | + private MetricDefinition withFormattedHelp(String arg) { |
| 177 | + return new MetricDefinition(name, String.format(help, arg), attribute, type); |
| 178 | + } |
| 179 | + } |
| 180 | + |
| 181 | + enum MetricType { |
| 182 | + GAUGE("gauge"), |
| 183 | + COUNTER("counter"); |
| 184 | + |
| 185 | + private final String prometheusName; |
| 186 | + |
| 187 | + MetricType(String prometheusName) { |
| 188 | + this.prometheusName = prometheusName; |
| 189 | + } |
| 190 | + |
| 191 | + String prometheusName() { |
| 192 | + return prometheusName; |
| 193 | + } |
| 194 | + } |
| 195 | +} |
0 commit comments