|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | + |
| 20 | +package org.apache.spark |
| 21 | + |
| 22 | +import org.apache.spark.metrics.source.Source |
| 23 | + |
| 24 | +import com.codahale.metrics.{Counter, Gauge, MetricRegistry} |
| 25 | + |
| 26 | +import org.apache.comet.CometCoverageStats |
| 27 | + |
| 28 | +/** |
| 29 | + * Exposes following metrics (hooked from CometCoverageStats) |
| 30 | + * - operators.native: Total operators executed natively |
| 31 | + * - operators.spark: Total operators that fell back to Spark |
| 32 | + * - queries.planned: Total queries processed |
| 33 | + * - transitions: Total Spark-to-Comet transitions |
| 34 | + * - acceleration.ratio: native / (native + spark) |
| 35 | + */ |
| 36 | +object CometSource extends Source { |
| 37 | + override val sourceName = "comet" |
| 38 | + override val metricRegistry = new MetricRegistry() |
| 39 | + |
| 40 | + val NATIVE_OPERATORS: Counter = |
| 41 | + metricRegistry.counter(MetricRegistry.name("operators", "native")) |
| 42 | + val SPARK_OPERATORS: Counter = metricRegistry.counter(MetricRegistry.name("operators", "spark")) |
| 43 | + val QUERIES_PLANNED: Counter = metricRegistry.counter(MetricRegistry.name("queries", "planned")) |
| 44 | + val TRANSITIONS: Counter = metricRegistry.counter(MetricRegistry.name("transitions")) |
| 45 | + |
| 46 | + metricRegistry.register( |
| 47 | + MetricRegistry.name("acceleration", "ratio"), |
| 48 | + new Gauge[Double] { |
| 49 | + override def getValue: Double = { |
| 50 | + val native = NATIVE_OPERATORS.getCount |
| 51 | + val total = native + SPARK_OPERATORS.getCount |
| 52 | + if (total > 0) native.toDouble / total else 0.0 |
| 53 | + } |
| 54 | + }) |
| 55 | + |
| 56 | + def recordStats(stats: CometCoverageStats): Unit = { |
| 57 | + NATIVE_OPERATORS.inc(stats.cometOperators) |
| 58 | + SPARK_OPERATORS.inc(stats.sparkOperators) |
| 59 | + TRANSITIONS.inc(stats.transitions) |
| 60 | + QUERIES_PLANNED.inc() |
| 61 | + } |
| 62 | +} |
0 commit comments