Akka-streams:如何获取kamon-akka

时间:2016-11-24 18:12:57

标签: scala akka-stream akka-monitoring kamon

我一直在尝试为Akka流设置一些仪器。有了它的工作,但是,即使我将所有流量命名为流的一部分,我仍然会在指标中得到这样的名称:flow-0-0-unknown-operation

我正在尝试做的一个简单示例:

val myflow = Flow[String].named("myflow").map(println)

Source.via(myflow).to(Sink.ignore).run()

我基本上希望看到为“myflow”创建的Actor的指标,并使用正确的名称。

这甚至可能吗?我错过了什么吗?

1 个答案:

答案 0 :(得分:0)

我在项目中遇到了挑战,并通过使用Kamon + Prometheus解决了问题。但是,我必须创建一个Akka流Flow,可以设置其名称metricName并使用val kamonThroughputGauge: Metric.Gauge从中导出度量值。

class MonitorProcessingTimerFlow[T](interval: FiniteDuration)(metricName: String = "monitorFlow") extends GraphStage[FlowShape[T, T]] {
  val in = Inlet[T]("MonitorProcessingTimerFlow.in")
  val out = Outlet[T]("MonitorProcessingTimerFlow.out")

  Kamon.init()
  val kamonThroughputGauge: Metric.Gauge = Kamon.gauge("akka-stream-throughput-monitor")
  override def createLogic(inheritedAttributes: Attributes): GraphStageLogic = new TimerGraphStageLogic(shape) {
    // mutable state
    var open = false
    var count = 0
    var start = System.nanoTime
    setHandler(in, new InHandler {
      override def onPush(): Unit = {
        try {
          push(out, grab(in))
          count += 1
          if (!open) {
            open = true
            scheduleOnce(None, interval)
          }
        } catch {
          case e: Throwable => failStage(e)
        }
      }
    })
    setHandler(out, new OutHandler {
      override def onPull(): Unit = {
        pull(in)
      }
    })

    override protected def onTimer(timerKey: Any): Unit = {
      open = false
      val duration = (System.nanoTime - start) / 1e9d
      val throughput = count / duration
      kamonThroughputGauge.withTag("name", metricName).update(throughput)
      count = 0
      start = System.nanoTime
    }
  }
  override def shape: FlowShape[T, T] = FlowShape[T, T](in, out)
}

然后,我创建了一个简单的流,该流使用MonitorProcessingTimerFlow导出指标:

implicit val system = ActorSystem("FirstStreamMonitoring")
val source = Source(Stream.from(1)).throttle(1, 1 second)
/** Simulating workload fluctuation: A Flow that expand the event to a random number of multiple events */
val flow = Flow[Int].extrapolate { element =>
  Stream.continually(Random.nextInt(100)).take(Random.nextInt(100)).iterator
}
val monitorFlow = Flow.fromGraph(new MonitorProcessingTimerFlow[Int](5 seconds)("monitorFlow"))
val sink = Sink.foreach[Int](println)

val graph = source
  .via(flow)
  .via(monitorFlow)
  .to(sink)
graph.run()

application.conf进行了正确的配置:

kamon.instrumentation.akka.filters {
  actors.track {
    includes = [ "FirstStreamMonitoring/user/*" ]
  }
}

我可以在名称为name="monitorFlow"的prometheus控制台上看到吞吐量指标: enter image description here