|
| 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.spark.status.api.v1 |
| 18 | + |
| 19 | +import javax.ws.rs._ |
| 20 | +import javax.ws.rs.core.MediaType |
| 21 | + |
| 22 | +import org.eclipse.jetty.servlet.{ServletContextHandler, ServletHolder} |
| 23 | +import org.glassfish.jersey.server.ServerProperties |
| 24 | +import org.glassfish.jersey.servlet.ServletContainer |
| 25 | + |
| 26 | +import org.apache.spark.ui.SparkUI |
| 27 | + |
| 28 | +/** |
| 29 | + * This aims to expose Executor metrics like REST API which is documented in |
| 30 | + * |
| 31 | + * https://spark.apache.org/docs/3.0.0/monitoring.html#executor-metrics |
| 32 | + * |
| 33 | + * Note that this is based on ExecutorSummary which is different from ExecutorSource. |
| 34 | + */ |
| 35 | +@Path("/executors") |
| 36 | +private[v1] class PrometheusResource extends ApiRequestContext { |
| 37 | + @GET |
| 38 | + @Path("prometheus") |
| 39 | + @Produces(Array(MediaType.TEXT_PLAIN)) |
| 40 | + def executors(): String = { |
| 41 | + val sb = new StringBuilder |
| 42 | + val store = uiRoot.asInstanceOf[SparkUI].store |
| 43 | + val appId = store.applicationInfo.id.replaceAll("[^a-zA-Z0-9]", "_") |
| 44 | + store.executorList(true).foreach { executor => |
| 45 | + val prefix = s"metrics_${appId}_${executor.id}_executor_" |
| 46 | + sb.append(s"${prefix}rddBlocks_Count ${executor.rddBlocks}\n") |
| 47 | + sb.append(s"${prefix}memoryUsed_Count ${executor.memoryUsed}\n") |
| 48 | + sb.append(s"${prefix}diskUsed_Count ${executor.diskUsed}\n") |
| 49 | + sb.append(s"${prefix}totalCores_Count ${executor.totalCores}\n") |
| 50 | + sb.append(s"${prefix}maxTasks_Count ${executor.maxTasks}\n") |
| 51 | + sb.append(s"${prefix}activeTasks_Count ${executor.activeTasks}\n") |
| 52 | + sb.append(s"${prefix}failedTasks_Count ${executor.failedTasks}\n") |
| 53 | + sb.append(s"${prefix}completedTasks_Count ${executor.completedTasks}\n") |
| 54 | + sb.append(s"${prefix}totalTasks_Count ${executor.totalTasks}\n") |
| 55 | + sb.append(s"${prefix}totalDuration_Value ${executor.totalDuration}\n") |
| 56 | + sb.append(s"${prefix}totalGCTime_Value ${executor.totalGCTime}\n") |
| 57 | + sb.append(s"${prefix}totalInputBytes_Count ${executor.totalInputBytes}\n") |
| 58 | + sb.append(s"${prefix}totalShuffleRead_Count ${executor.totalShuffleRead}\n") |
| 59 | + sb.append(s"${prefix}totalShuffleWrite_Count ${executor.totalShuffleWrite}\n") |
| 60 | + sb.append(s"${prefix}maxMemory_Count ${executor.maxMemory}\n") |
| 61 | + executor.executorLogs.foreach { case (k, v) => } |
| 62 | + executor.memoryMetrics.foreach { m => |
| 63 | + sb.append(s"${prefix}usedOnHeapStorageMemory_Count ${m.usedOnHeapStorageMemory}\n") |
| 64 | + sb.append(s"${prefix}usedOffHeapStorageMemory_Count ${m.usedOffHeapStorageMemory}\n") |
| 65 | + sb.append(s"${prefix}totalOnHeapStorageMemory_Count ${m.totalOnHeapStorageMemory}\n") |
| 66 | + sb.append(s"${prefix}totalOffHeapStorageMemory_Count ${m.totalOffHeapStorageMemory}\n") |
| 67 | + } |
| 68 | + executor.peakMemoryMetrics.foreach { m => |
| 69 | + val names = Array( |
| 70 | + "JVMHeapMemory", |
| 71 | + "JVMOffHeapMemory", |
| 72 | + "OnHeapExecutionMemory", |
| 73 | + "OffHeapExecutionMemory", |
| 74 | + "OnHeapStorageMemory", |
| 75 | + "OffHeapStorageMemory", |
| 76 | + "OnHeapUnifiedMemory", |
| 77 | + "OffHeapUnifiedMemory", |
| 78 | + "DirectPoolMemory", |
| 79 | + "MappedPoolMemory", |
| 80 | + "ProcessTreeJVMVMemory", |
| 81 | + "ProcessTreeJVMRSSMemory", |
| 82 | + "ProcessTreePythonVMemory", |
| 83 | + "ProcessTreePythonRSSMemory", |
| 84 | + "ProcessTreeOtherVMemory", |
| 85 | + "ProcessTreeOtherRSSMemory", |
| 86 | + "MinorGCCount", |
| 87 | + "MinorGCTime", |
| 88 | + "MajorGCCount", |
| 89 | + "MajorGCTime" |
| 90 | + ) |
| 91 | + names.foreach { name => |
| 92 | + sb.append(s"$prefix${name}_Count ${m.getMetricValue(name)}\n") |
| 93 | + } |
| 94 | + } |
| 95 | + } |
| 96 | + sb.toString |
| 97 | + } |
| 98 | +} |
| 99 | + |
| 100 | +private[spark] object PrometheusResource { |
| 101 | + def getServletHandler(uiRoot: UIRoot): ServletContextHandler = { |
| 102 | + val jerseyContext = new ServletContextHandler(ServletContextHandler.NO_SESSIONS) |
| 103 | + jerseyContext.setContextPath("/metrics") |
| 104 | + val holder: ServletHolder = new ServletHolder(classOf[ServletContainer]) |
| 105 | + holder.setInitParameter(ServerProperties.PROVIDER_PACKAGES, "org.apache.spark.status.api.v1") |
| 106 | + UIRootFromServletContext.setUiRoot(jerseyContext, uiRoot) |
| 107 | + jerseyContext.addServlet(holder, "/*") |
| 108 | + jerseyContext |
| 109 | + } |
| 110 | +} |
0 commit comments