Github user steveloughran commented on a diff in the pull request:

    https://github.com/apache/spark/pull/9518#discussion_r47142484
  
    --- Diff: 
core/src/main/scala/org/apache/spark/metrics/sink/StatsdReporter.scala ---
    @@ -0,0 +1,143 @@
    +/*
    + * Licensed to the Apache Software Foundation (ASF) under one or more
    + * contributor license agreements.  See the NOTICE file distributed with
    + * this work for additional information regarding copyright ownership.
    + * The ASF licenses this file to You under the Apache License, Version 2.0
    + * (the "License"); you may not use this file except in compliance with
    + * the License.  You may obtain a copy of the License at
    + *
    + *    http://www.apache.org/licenses/LICENSE-2.0
    + *
    + * Unless required by applicable law or agreed to in writing, software
    + * distributed under the License is distributed on an "AS IS" BASIS,
    + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    + * See the License for the specific language governing permissions and
    + * limitations under the License.
    + */
    +
    +package org.apache.spark.metrics.sink
    +
    +import java.net.{DatagramPacket, InetSocketAddress, DatagramSocket}
    +import java.nio.charset.StandardCharsets.UTF_8
    +import java.util.SortedMap
    +import java.util.concurrent.TimeUnit
    +
    +import com.codahale.metrics._
    +import org.apache.spark.Logging
    +
    +import scala.collection.JavaConverters._
    +import scala.util.{Failure, Success, Try}
    +
    +/**
    +  * @see <a 
href="https://github.com/etsy/statsd/blob/master/docs/metric_types.md";>
    +  *        StatsD metric types</a>
    +  */
    +private[spark] sealed trait StatsdMetricType {
    +  val COUNTER = "c"
    +  val GAUGE = "g"
    +  val TIMER = "ms"
    +  val Set = "s"
    +}
    +
    +private[spark] class StatsdReporter(registry: MetricRegistry,
    +                                    host: String = "127.0.0.1",
    +                                    port: Int = 8125,
    +                                    prefix: String = "",
    +                                    filter: MetricFilter = 
MetricFilter.ALL,
    +                                    rateUnit: TimeUnit = TimeUnit.SECONDS,
    +                                    durationUnit: TimeUnit = 
TimeUnit.MILLISECONDS)
    +    extends ScheduledReporter(registry, "statsd-reporter", filter, 
rateUnit, durationUnit)
    +    with StatsdMetricType with Logging {
    +
    +  private val address = new InetSocketAddress(host, port)
    +  private val whitespace = "[\\s]+".r
    +
    +  override def report(gauges: SortedMap[String, Gauge[_]], counters: 
SortedMap[String, Counter],
    +                      histograms: SortedMap[String, Histogram], meters: 
SortedMap[String, Meter],
    +                      timers: SortedMap[String, Timer]): Unit =
    +    Try(new DatagramSocket) match {
    +      case Failure(e) => logWarning("StatsD datagram socket construction 
failed", e)
    +      case Success(s) =>
    +        implicit val socket = s
    +        Try {
    +          gauges.entrySet.asScala.foreach(e => reportGauge(e.getKey, 
e.getValue))
    +          counters.entrySet.asScala.foreach(e => reportCounter(e.getKey, 
e.getValue))
    +          histograms.entrySet.asScala.foreach(e => 
reportHistogram(e.getKey, e.getValue))
    +          meters.entrySet.asScala.foreach(e => reportMetered(e.getKey, 
e.getValue))
    +          timers.entrySet.asScala.foreach(e => reportTimer(e.getKey, 
e.getValue))
    +        } recover {
    +          case _ => logDebug(s"Unable to send packet to StatsD at 
'$host:$port'")
    +        }
    +        Try(socket.close()) recover {
    +          case e => logDebug("Error disconnecting from StatsD", e)
    +        }
    +    }
    +
    +  private def reportGauge(name: String, gauge: Gauge[_])(implicit socket: 
DatagramSocket) =
    +    formatAny(gauge.getValue).foreach(v => send(fullName(name), v, GAUGE))
    +
    +  private def reportCounter(name: String, counter: Counter)(implicit 
socket: DatagramSocket) =
    +    send(fullName(name), format(counter.getCount), COUNTER)
    +
    +  private def reportHistogram(name: String, histogram: Histogram)
    +                             (implicit socket: DatagramSocket) = {
    +    val snapshot = histogram.getSnapshot
    +    send(fullName(name, "count"), format(histogram.getCount), GAUGE)
    +    send(fullName(name, "max"), format(snapshot.getMax), TIMER)
    +    send(fullName(name, "mean"), format(snapshot.getMean), TIMER)
    +    send(fullName(name, "min"), format(snapshot.getMin), TIMER)
    +    send(fullName(name, "stddev"), format(snapshot.getStdDev), TIMER)
    +    send(fullName(name, "p50"), format(snapshot.getMedian), TIMER)
    +    send(fullName(name, "p75"), format(snapshot.get75thPercentile), TIMER)
    +    send(fullName(name, "p95"), format(snapshot.get95thPercentile), TIMER)
    +    send(fullName(name, "p98"), format(snapshot.get98thPercentile), TIMER)
    +    send(fullName(name, "p99"), format(snapshot.get99thPercentile), TIMER)
    +    send(fullName(name, "p999"), format(snapshot.get999thPercentile), 
TIMER)
    +  }
    +
    +  private def reportMetered(name: String, meter: Metered)(implicit socket: 
DatagramSocket) = {
    +    send(fullName(name, "count"), format(meter.getCount), GAUGE)
    +    send(fullName(name, "m1_rate"), 
format(convertRate(meter.getOneMinuteRate)), TIMER)
    +    send(fullName(name, "m5_rate"), 
format(convertRate(meter.getFiveMinuteRate)), TIMER)
    +    send(fullName(name, "m15_rate"), 
format(convertRate(meter.getFifteenMinuteRate)), TIMER)
    +    send(fullName(name, "mean_rate"), 
format(convertRate(meter.getMeanRate)), TIMER)
    +  }
    +
    +  private def reportTimer(name: String, timer: Timer)(implicit socket: 
DatagramSocket) = {
    +    val snapshot = timer.getSnapshot
    +    send(fullName(name, "max"), format(convertDuration(snapshot.getMax)), 
TIMER)
    +    send(fullName(name, "mean"), 
format(convertDuration(snapshot.getMean)), TIMER)
    +    send(fullName(name, "min"), format(convertDuration(snapshot.getMin)), 
TIMER)
    +    send(fullName(name, "stddev"), 
format(convertDuration(snapshot.getStdDev)), TIMER)
    +    send(fullName(name, "p50"), 
format(convertDuration(snapshot.getMedian)), TIMER)
    +    send(fullName(name, "p75"), 
format(convertDuration(snapshot.get75thPercentile)), TIMER)
    +    send(fullName(name, "p95"), 
format(convertDuration(snapshot.get95thPercentile)), TIMER)
    +    send(fullName(name, "p98"), 
format(convertDuration(snapshot.get98thPercentile)), TIMER)
    +    send(fullName(name, "p99"), 
format(convertDuration(snapshot.get99thPercentile)), TIMER)
    +    send(fullName(name, "p999"), 
format(convertDuration(snapshot.get999thPercentile)), TIMER)
    +
    +    reportMetered(name, timer)
    +  }
    +
    +  private def send(name: String, value: String, metricType: String)
    +                  (implicit socket: DatagramSocket) = {
    +    val bytes = sanitize(s"$name:$value|$metricType").getBytes(UTF_8)
    +    val packet = new DatagramPacket(bytes, bytes.length, address)
    +    socket.send(packet)
    --- End diff --
    
    I'd recommend wrapping any failure here with some details on destination 
host+port. Hadoop's {{NetUtils.wrapException}} can do this if people don't mind 
using a class that is nominally internal


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at [email protected] or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to