|
| 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 | + |
| 18 | +package org.apache.spark.metrics.sink |
| 19 | + |
| 20 | +import java.util.Properties |
| 21 | +import javax.servlet.http.HttpServletRequest |
| 22 | + |
| 23 | +import com.codahale.metrics.MetricRegistry |
| 24 | +import org.eclipse.jetty.servlet.ServletContextHandler |
| 25 | + |
| 26 | +import org.apache.spark.{SecurityManager, SparkConf} |
| 27 | +import org.apache.spark.ui.JettyUtils._ |
| 28 | + |
| 29 | +/** |
| 30 | + * This exposes the metrics of the given registry with Prometheus format. |
| 31 | + * |
| 32 | + * The output is consistent with /metrics/json result in terms of item ordering |
| 33 | + * and with the previous result of Spark JMX Sink + Prometheus JMX Converter combination |
| 34 | + * in terms of key string format. |
| 35 | + */ |
| 36 | +private[spark] class PrometheusServlet( |
| 37 | + val property: Properties, |
| 38 | + val registry: MetricRegistry, |
| 39 | + securityMgr: SecurityManager) |
| 40 | + extends Sink { |
| 41 | + |
| 42 | + val SERVLET_KEY_PATH = "path" |
| 43 | + |
| 44 | + val servletPath = property.getProperty(SERVLET_KEY_PATH) |
| 45 | + |
| 46 | + def getHandlers(conf: SparkConf): Array[ServletContextHandler] = { |
| 47 | + Array[ServletContextHandler]( |
| 48 | + createServletHandler(servletPath, |
| 49 | + new ServletParams(request => getMetricsSnapshot(request), "text/plain"), conf) |
| 50 | + ) |
| 51 | + } |
| 52 | + |
| 53 | + def getMetricsSnapshot(request: HttpServletRequest): String = { |
| 54 | + import scala.collection.JavaConverters._ |
| 55 | + |
| 56 | + val sb = new StringBuilder() |
| 57 | + registry.getGauges.asScala.foreach { case (k, v) => |
| 58 | + if (!v.getValue.isInstanceOf[String]) { |
| 59 | + sb.append(s"${normalizeKey(k)}Value ${v.getValue}\n") |
| 60 | + } |
| 61 | + } |
| 62 | + registry.getCounters.asScala.foreach { case (k, v) => |
| 63 | + sb.append(s"${normalizeKey(k)}Count ${v.getCount}\n") |
| 64 | + } |
| 65 | + registry.getHistograms.asScala.foreach { case (k, h) => |
| 66 | + val snapshot = h.getSnapshot |
| 67 | + val prefix = normalizeKey(k) |
| 68 | + sb.append(s"${prefix}Count ${h.getCount}\n") |
| 69 | + sb.append(s"${prefix}Max ${snapshot.getMax}\n") |
| 70 | + sb.append(s"${prefix}Mean ${snapshot.getMean}\n") |
| 71 | + sb.append(s"${prefix}Min ${snapshot.getMin}\n") |
| 72 | + sb.append(s"${prefix}50thPercentile ${snapshot.getMedian}\n") |
| 73 | + sb.append(s"${prefix}75thPercentile ${snapshot.get75thPercentile}\n") |
| 74 | + sb.append(s"${prefix}95thPercentile ${snapshot.get95thPercentile}\n") |
| 75 | + sb.append(s"${prefix}98thPercentile ${snapshot.get98thPercentile}\n") |
| 76 | + sb.append(s"${prefix}99thPercentile ${snapshot.get99thPercentile}\n") |
| 77 | + sb.append(s"${prefix}999thPercentile ${snapshot.get999thPercentile}\n") |
| 78 | + sb.append(s"${prefix}StdDev ${snapshot.getStdDev}\n") |
| 79 | + } |
| 80 | + registry.getMeters.entrySet.iterator.asScala.foreach { kv => |
| 81 | + val prefix = normalizeKey(kv.getKey) |
| 82 | + val meter = kv.getValue |
| 83 | + sb.append(s"${prefix}Count ${meter.getCount}\n") |
| 84 | + sb.append(s"${prefix}MeanRate ${meter.getMeanRate}\n") |
| 85 | + sb.append(s"${prefix}OneMinuteRate ${meter.getOneMinuteRate}\n") |
| 86 | + sb.append(s"${prefix}FiveMinuteRate ${meter.getFiveMinuteRate}\n") |
| 87 | + sb.append(s"${prefix}FifteenMinuteRate ${meter.getFifteenMinuteRate}\n") |
| 88 | + } |
| 89 | + registry.getTimers.entrySet.iterator.asScala.foreach { kv => |
| 90 | + val prefix = normalizeKey(kv.getKey) |
| 91 | + val timer = kv.getValue |
| 92 | + val snapshot = timer.getSnapshot |
| 93 | + sb.append(s"${prefix}Count ${timer.getCount}\n") |
| 94 | + sb.append(s"${prefix}Max ${snapshot.getMax}\n") |
| 95 | + sb.append(s"${prefix}Mean ${snapshot.getMax}\n") |
| 96 | + sb.append(s"${prefix}Min ${snapshot.getMin}\n") |
| 97 | + sb.append(s"${prefix}50thPercentile ${snapshot.getMedian}\n") |
| 98 | + sb.append(s"${prefix}75thPercentile ${snapshot.get75thPercentile}\n") |
| 99 | + sb.append(s"${prefix}95thPercentile ${snapshot.get95thPercentile}\n") |
| 100 | + sb.append(s"${prefix}98thPercentile ${snapshot.get98thPercentile}\n") |
| 101 | + sb.append(s"${prefix}99thPercentile ${snapshot.get99thPercentile}\n") |
| 102 | + sb.append(s"${prefix}999thPercentile ${snapshot.get999thPercentile}\n") |
| 103 | + sb.append(s"${prefix}StdDev ${snapshot.getStdDev}\n") |
| 104 | + sb.append(s"${prefix}FifteenMinuteRate ${timer.getFifteenMinuteRate}\n") |
| 105 | + sb.append(s"${prefix}FiveMinuteRate ${timer.getFiveMinuteRate}\n") |
| 106 | + sb.append(s"${prefix}OneMinuteRate ${timer.getOneMinuteRate}\n") |
| 107 | + sb.append(s"${prefix}MeanRate ${timer.getMeanRate}\n") |
| 108 | + } |
| 109 | + sb.toString() |
| 110 | + } |
| 111 | + |
| 112 | + private def normalizeKey(key: String): String = { |
| 113 | + s"metrics_${key.replaceAll("[^a-zA-Z0-9]", "_")}_" |
| 114 | + } |
| 115 | + |
| 116 | + override def start() { } |
| 117 | + |
| 118 | + override def stop() { } |
| 119 | + |
| 120 | + override def report() { } |
| 121 | +} |
0 commit comments