本文整理汇总了Scala中com.codahale.metrics.Gauge类的典型用法代码示例。如果您正苦于以下问题:Scala Gauge类的具体用法?Scala Gauge怎么用?Scala Gauge使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Gauge类的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Scala代码示例。
示例1: CHMetricsStatsReceiver
//设置package包名称以及导入依赖的类
package org.http4s.finagle
import com.codahale.metrics.{ Gauge, MetricRegistry }
import com.twitter.finagle.stats.{ Counter, Stat, StatsReceiver, Gauge => FGauge }
import scalaz.syntax.id._
class CHMetricsStatsReceiver(registry: MetricRegistry, prefix: String) extends StatsReceiver {
override val repr: AnyRef = this
override def counter(names: String*): Counter =
registry.counter(format(names)) |> { c =>
new Counter {
override def incr(delta: Int): Unit = c.inc(delta.toLong)
}
}
override def addGauge(names: String*)(f: => Float): FGauge =
format(names) |> { name =>
registry.register(format(names), new Gauge[Float] {
override def getValue: Float = f
}) |> { g =>
new FGauge {
override def remove(): Unit = { registry.remove(name); () }
}
}
}
override def stat(names: String*): Stat =
registry.histogram(format(names)) |> { h =>
new Stat {
override def add(value: Float): Unit = h.update(value.toLong)
}
}
private def format(names: Seq[String]): String =
(prefix +: names).mkString(".")
}
示例2: GaugeInjectionListener
//设置package包名称以及导入依赖的类
package de.khamrakulov.play.metrics.annotation.guice.listener
import java.lang.reflect.Method
import com.codahale.metrics.{Gauge, MetricRegistry}
import com.google.inject.spi.InjectionListener
private[annotation] object GaugeInjectionListener {
def apply(metricRegistry: MetricRegistry, metricName: String, method: Method) =
new GaugeInjectionListener(metricRegistry, metricName, method)
}
private[annotation] class GaugeInjectionListener[I](metricRegistry: MetricRegistry, metricName: String, method: Method) extends InjectionListener[I] {
def afterInjection(i: I) = if (metricRegistry.getGauges.get(metricName) == null) {
metricRegistry.register(metricName, new Gauge[AnyRef]() {
def getValue = try method.invoke(i) catch {
case e: Exception => new RuntimeException(e)
}
})
}
}
示例3: TestMetricsReporter
//设置package包名称以及导入依赖的类
import java.util.concurrent.TimeUnit
import com.codahale.metrics.{Gauge, ScheduledReporter}
import eu.inn.metrics.MetricsTracker
import eu.inn.metrics.modules.ConsoleReporterModule
import org.scalatest.concurrent.ScalaFutures
import org.scalatest.{FreeSpec, Matchers}
import scaldi.Injectable
import scala.concurrent.ExecutionContext.Implicits.global
import scala.concurrent.Future
import scala.concurrent.duration.Duration
class TestMetricsReporter extends FreeSpec with Matchers with Injectable with ScalaFutures {
implicit val injector = new ConsoleReporterModule(Duration.Undefined)
"MetricsReporter should instantiate" - {
val metrics = inject[MetricsTracker]
metrics.counter("counter").inc(100500)
metrics.meter("meter").mark(15)
metrics.histogram("histogram").update(13)
metrics.histogram("histogram").update(12)
metrics.histogram("histogram").update(13)
metrics.histogram("histogram").update(15)
metrics.timer("timer1").update(10, TimeUnit.SECONDS)
metrics.timeOf("measure-sync") {
Thread.sleep(10)
}
metrics.timeOfFuture("measure-async") {
Future {
Thread.sleep(10)
}
}.futureValue
metrics.gauge("gauge1") {
100500
}
metrics.gauge("gauge1") {
100501
}
metrics.gauge("gauge2", new Gauge[Int] {
override def getValue = 3
})
val reporter = inject[ScheduledReporter]
reporter shouldNot equal (null)
reporter.report()
}
}
示例4: MetricRegistries
//设置package包名称以及导入依赖的类
package com.rbmhtechnology.eventuate.tools.metrics.dropwizard
import com.codahale.metrics.Gauge
import com.codahale.metrics.MetricRegistry
object MetricRegistries {
implicit class RichMetricRegistry(metricRegistry: MetricRegistry) {
def setGaugeValue[A](name: String, value: A): Unit = {
val oldEntry = Option(metricRegistry.getGauges().get(name))
if (oldEntry.forall(_.getValue != value)) {
metricRegistry.remove(name)
metricRegistry.register(name, fixValueGauge(value))
}
}
private def fixValueGauge[A](value: A): Gauge[A] = new Gauge[A] {
override def getValue: A = value
}
}
}
示例5: MetricHelper
//设置package包名称以及导入依赖的类
package com.evolutiongaming.util
import com.codahale.metrics.{Gauge, Histogram, MetricRegistry, Timer}
import scala.compat.Platform
import scala.concurrent.duration._
import scala.concurrent.{ExecutionContext, Future}
import scala.util.control.NonFatal
object MetricHelper {
object GaugeF {
def apply[T](f: => T): Gauge[T] = new Gauge[T] { def getValue: T = f }
}
implicit class RichTimer(val timer: Timer) extends AnyVal {
def timeExceed[T](limit: FiniteDuration)(f: => T): T = {
val start = Platform.currentTime
try f finally {
val stop = Platform.currentTime
val duration = stop - start
if (duration >= limit.toMillis) timer.update(duration, MILLISECONDS)
}
}
def timeFuture[T](f: => Future[T])(implicit ec: ExecutionContext): Future[T] = {
val time = timer.time()
try f andThen { case _ => time.stop() } catch {
case NonFatal(e) => time.stop(); throw e
}
}
def timeFunc[T](f: => T): T = {
val time = timer.time()
try f finally time.stop()
}
}
implicit class HistogramOps(val histogram: Histogram) extends AnyVal {
def timeFunc[T](f: => T): T = {
val start = Platform.currentTime
try f finally histogram.update(Platform.currentTime - start)
}
def timeFuture[T](f: => Future[T])(implicit ec: ExecutionContext): Future[T] = {
val start = Platform.currentTime
f andThen { case _ => histogram.update(Platform.currentTime - start) }
}
}
implicit class MetricRegistryOps(val self: MetricRegistry) extends AnyVal {
def gauge[T](name: String, f: => T): Gauge[T] = {
self remove name
self.register(name, GaugeF(f))
}
}
}