aboutsummaryrefslogtreecommitdiff
path: root/kamon-core/src/main/scala/kamon/ReporterRegistry.scala
blob: 06b9761a040ca443152a8a759a572ccfd718c4cf (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
/* =========================================================================================
 * Copyright © 2013-2017 the kamon project <http://kamon.io/>
 *
 * Licensed 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 kamon

import java.time.{Duration, Instant}
import java.util.concurrent.atomic.{AtomicLong, AtomicReference}
import java.util.concurrent._

import com.typesafe.config.Config
import kamon.metric._
import kamon.trace.Span
import kamon.trace.Span.FinishedSpan
import kamon.util.{Clock, DynamicAccess, Registration}
import org.slf4j.LoggerFactory

import scala.concurrent.{ExecutionContext, ExecutionContextExecutorService, Future}
import scala.util.Try
import scala.util.control.NonFatal
import scala.collection.JavaConverters._
import scala.collection.concurrent.TrieMap
import scala.concurrent.duration._

sealed trait Reporter {
  def start(): Unit
  def stop(): Unit
  def reconfigure(config: Config): Unit
}

trait MetricReporter extends Reporter {
  def reportPeriodSnapshot(snapshot: PeriodSnapshot): Unit
}

trait SpanReporter extends Reporter {
  def reportSpans(spans: Seq[Span.FinishedSpan]): Unit
}

trait ReporterRegistry {
  def loadReportersFromConfig(): Unit

  def addReporter(reporter: MetricReporter): Registration
  def addReporter(reporter: MetricReporter, name: String): Registration
  def addReporter(reporter: MetricReporter, name: String, filter: String): Registration
  def addReporter(reporter: SpanReporter): Registration
  def addReporter(reporter: SpanReporter, name: String): Registration

  def stopAllReporters(): Future[Unit]
}

object ReporterRegistry {

  private[kamon] trait SpanSink {
    def reportSpan(finishedSpan: FinishedSpan): Unit
  }

  private[kamon] class Default(metrics: MetricsSnapshotGenerator, initialConfig: Config, clock: Clock) extends ReporterRegistry with SpanSink {
    private val logger = LoggerFactory.getLogger(classOf[ReporterRegistry])
    private val registryExecutionContext = Executors.newScheduledThreadPool(2, threadFactory("kamon-reporter-registry", daemon = true))
    private val reporterCounter = new AtomicLong(0L)
    private var registryConfiguration = readRegistryConfiguration(initialConfig)

    private val metricReporters = TrieMap[Long, MetricReporterEntry]()
    private val metricReporterTickerSchedule = new AtomicReference[ScheduledFuture[_]]()
    private val spanReporters = TrieMap[Long, SpanReporterEntry]()
    private val spanReporterTickerSchedule = new AtomicReference[ScheduledFuture[_]]()


    reconfigure(initialConfig)

    override def loadReportersFromConfig(): Unit = {
      if(registryConfiguration.configuredReporters.isEmpty)
        logger.info("The kamon.reporters setting is empty, no reporters have been started.")
      else {
        registryConfiguration.configuredReporters.foreach { reporterFQCN =>
          val dynamicAccess = new DynamicAccess(getClass.getClassLoader)
          dynamicAccess.createInstanceFor[Reporter](reporterFQCN, Nil).map({
            case mr: MetricReporter =>
              addMetricReporter(mr, "loaded-from-config: " + reporterFQCN)
              logger.info("Loaded metric reporter [{}]", reporterFQCN)

            case sr: SpanReporter =>
              addSpanReporter(sr, "loaded-from-config: " + reporterFQCN)
              logger.info("Loaded span reporter [{}]", reporterFQCN)

          }).failed.foreach {
            t => logger.error(s"Failed to load configured reporter [$reporterFQCN]", t)
          }
        }
      }
    }

    override def addReporter(reporter: MetricReporter): Registration =
      addMetricReporter(reporter, reporter.getClass.getName())

    override def addReporter(reporter: MetricReporter, name: String): Registration =
      addMetricReporter(reporter, name)

    override def addReporter(reporter: MetricReporter, name: String, filter: String): Registration =
      addMetricReporter(reporter, name, Some(filter))

    override def addReporter(reporter: SpanReporter): Registration =
      addSpanReporter(reporter, reporter.getClass.getName())

    override def addReporter(reporter: SpanReporter, name: String): Registration =
      addSpanReporter(reporter, name)


    private def addMetricReporter(reporter: MetricReporter, name: String, filter: Option[String] = None): Registration = synchronized {
      val executor = Executors.newSingleThreadExecutor(threadFactory(name))
      val reporterEntry = new MetricReporterEntry(
        id = reporterCounter.getAndIncrement(),
        name = name,
        reporter = reporter,
        filter = filter,
        executionContext = ExecutionContext.fromExecutorService(executor)
      )

      Future {
        Try {
          reporterEntry.reporter.start()
        }.failed.foreach { error =>
          logger.error(s"Metric reporter [$name] failed to start.", error)
        }
      }(reporterEntry.executionContext)

      if(metricReporters.isEmpty)
        reStartMetricTicker()

      metricReporters.put(reporterEntry.id, reporterEntry)
      createRegistration(reporterEntry.id, metricReporters)

    }

    private def addSpanReporter(reporter: SpanReporter, name: String): Registration = synchronized {
      val executor = Executors.newSingleThreadExecutor(threadFactory(name))
      val reporterEntry = new SpanReporterEntry(
        id = reporterCounter.incrementAndGet(),
        name = name,
        reporter = reporter,
        bufferCapacity = registryConfiguration.traceReporterQueueSize,
        executionContext = ExecutionContext.fromExecutorService(executor)
      )

      Future {
        Try {
          reporterEntry.reporter.start()
        }.failed.foreach { error =>
          logger.error(s"Span reporter [$name] failed to start.", error)
        }
      }(reporterEntry.executionContext)

      if(spanReporters.isEmpty)
        reStartTraceTicker()

      spanReporters.put(reporterEntry.id, reporterEntry)
      createRegistration(reporterEntry.id, spanReporters)
    }

    private def createRegistration(id: Long, target: TrieMap[Long, _]): Registration = new Registration {
      override def cancel(): Boolean =
        target.remove(id).nonEmpty
    }

    override def stopAllReporters(): Future[Unit] = {
      implicit val stopReporterExeContext = ExecutionContext.fromExecutor(registryExecutionContext)
      val reporterStopFutures = Vector.newBuilder[Future[Unit]]

      while(metricReporters.nonEmpty) {
        val (idToRemove, _) = metricReporters.head
        metricReporters.remove(idToRemove).foreach { entry =>
          reporterStopFutures += stopMetricReporter(entry)
        }
      }

      while(spanReporters.nonEmpty) {
        val (idToRemove, _) = spanReporters.head
        spanReporters.remove(idToRemove).foreach { entry =>
          reporterStopFutures += stopSpanReporter(entry)
        }
      }

      Future.sequence(reporterStopFutures.result()).map(_ => ())
    }

    private[kamon] def reconfigure(config: Config): Unit = synchronized {
      val oldConfig = registryConfiguration
      registryConfiguration = readRegistryConfiguration(config)

      if(oldConfig.metricTickInterval != registryConfiguration.metricTickInterval && metricReporters.nonEmpty)
        reStartMetricTicker()

      if(oldConfig.traceTickInterval != registryConfiguration.traceTickInterval && spanReporters.nonEmpty)
        reStartTraceTicker()

      // Reconfigure all registered reporters
      metricReporters.foreach {
        case (_, entry) =>
          Future {
            Try {
              entry.reporter.reconfigure(config)
            }.failed.foreach { error =>
              logger.error(s"Metric reporter [${entry.name}] failed to reconfigure.", error)
            }
          }(entry.executionContext)
      }
      spanReporters.foreach   {
        case (_, entry) =>
          Future {
            Try {
              entry.reporter.reconfigure(config)
            }.failed.foreach { error =>
              logger.error(s"Span reporter [${entry.name}] failed to reconfigure.", error)
            }
          }(entry.executionContext)
      }
    }


    private def reStartMetricTicker(): Unit = {
      val tickIntervalMillis = registryConfiguration.metricTickInterval.toMillis
      val currentMetricTicker = metricReporterTickerSchedule.get()

      if(currentMetricTicker != null)
        currentMetricTicker.cancel(false)

      metricReporterTickerSchedule.set {
        val initialDelay =
          if(registryConfiguration.optimisticMetricTickAlignment) {
            val now = clock.instant()
            val nextTick = Clock.nextTick(now, registryConfiguration.metricTickInterval)
            Duration.between(now, nextTick).toMillis

          } else tickIntervalMillis

        registryExecutionContext.scheduleAtFixedRate(
          new MetricReporterTicker(metrics, metricReporters, clock), initialDelay, tickIntervalMillis, TimeUnit.MILLISECONDS
        )
      }
    }

    private def reStartTraceTicker(): Unit = {
      val tickIntervalMillis = registryConfiguration.traceTickInterval.toMillis
      val currentSpanTicker = spanReporterTickerSchedule.get()
      if(currentSpanTicker  != null)
        currentSpanTicker.cancel(false)

      spanReporterTickerSchedule.set {
        registryExecutionContext.scheduleAtFixedRate(
          new SpanReporterTicker(spanReporters), tickIntervalMillis, tickIntervalMillis, TimeUnit.MILLISECONDS
        )
      }
    }

    def reportSpan(span: Span.FinishedSpan): Unit = {
      spanReporters.foreach { case (_, reporterEntry) =>
        if(reporterEntry.isActive)
          reporterEntry.buffer.offer(span)
      }
    }

    private def stopMetricReporter(entry: MetricReporterEntry): Future[Unit] = {
      entry.isActive = false

      Future {
        Try {
          entry.reporter.stop()
        }.failed.foreach { error =>
          logger.error(s"Metric reporter [${entry.name}] failed to stop.", error)
        }
      }(entry.executionContext).andThen {
        case _ => entry.executionContext.shutdown()
      }(ExecutionContext.fromExecutor(registryExecutionContext))
    }

    private def stopSpanReporter(entry: SpanReporterEntry): Future[Unit] = {
      entry.isActive = false

      Future {
        Try {
          entry.reporter.stop()
        }.failed.foreach { error =>
          logger.error(s"Span reporter [${entry.name}] failed to stop.", error)
        }
      }(entry.executionContext).andThen {
        case _ => entry.executionContext.shutdown()
      }(ExecutionContext.fromExecutor(registryExecutionContext))
    }

    private class MetricReporterEntry(
      @volatile var isActive: Boolean = true,
      val id: Long,
      val name: String,
      val reporter: MetricReporter,
      val filter: Option[String],
      val executionContext: ExecutionContextExecutorService
    )

    private class SpanReporterEntry(
      @volatile var isActive: Boolean = true,
      val id: Long,
      val name: String,
      val reporter: SpanReporter,
      val bufferCapacity: Int,
      val executionContext: ExecutionContextExecutorService
    ) {
      val buffer = new ArrayBlockingQueue[Span.FinishedSpan](bufferCapacity)
    }

    private class MetricReporterTicker(snapshotGenerator: MetricsSnapshotGenerator, reporterEntries: TrieMap[Long, MetricReporterEntry],
        clock: Clock) extends Runnable {

      val logger = LoggerFactory.getLogger(classOf[MetricReporterTicker])
      var lastInstant = Instant.now(clock)

      def run(): Unit = try {
        val currentInstant = Instant.now(clock)
        val periodSnapshot = PeriodSnapshot(
          from = lastInstant,
          to = currentInstant,
          metrics = snapshotGenerator.snapshot()
        )

        reporterEntries.foreach { case (_, entry) =>
          Future {
            Try {
              if (entry.isActive) {
                val filteredSnapshot = entry.filter
                  .map(f => filterMetrics(f, periodSnapshot))
                  .getOrElse(periodSnapshot)

                entry.reporter.reportPeriodSnapshot(filteredSnapshot)
              }

            }.failed.foreach { error =>
              logger.error(s"Reporter [${entry.name}] failed to process a metrics tick.", error)
            }

          }(entry.executionContext)
        }

        lastInstant = currentInstant

      } catch {
        case NonFatal(t) => logger.error("Error while running a tick", t)
      }

      private def filterMetrics(filterName: String, periodSnapshot: PeriodSnapshot): PeriodSnapshot = {
        val metricFilter = Kamon.filter(filterName)
        val counters = periodSnapshot.metrics.counters.filter(c => metricFilter.accept(c.name))
        val gauges = periodSnapshot.metrics.gauges.filter(g => metricFilter.accept(g.name))
        val histograms = periodSnapshot.metrics.histograms.filter(h => metricFilter.accept(h.name))
        val rangeSamplers = periodSnapshot.metrics.rangeSamplers.filter(rs => metricFilter.accept(rs.name))

        periodSnapshot.copy(metrics = MetricsSnapshot(
          histograms, rangeSamplers, gauges, counters
        ))
      }
    }


    private class SpanReporterTicker(spanReporters: TrieMap[Long, SpanReporterEntry]) extends Runnable {
      override def run(): Unit = {
        spanReporters.foreach {
          case (_, entry) =>

            val spanBatch = new java.util.ArrayList[Span.FinishedSpan](entry.bufferCapacity)
            entry.buffer.drainTo(spanBatch, entry.bufferCapacity)

            Future {
              Try {
                entry.reporter.reportSpans(spanBatch.asScala)
              }.failed.foreach { error =>
                logger.error(s"Reporter [${entry.name}] failed to report spans.", error)
              }
            }(entry.executionContext)
        }
      }
    }

    private def readRegistryConfiguration(config: Config): Configuration =
      Configuration(
        metricTickInterval = config.getDuration("kamon.metric.tick-interval"),
        optimisticMetricTickAlignment = config.getBoolean("kamon.metric.optimistic-tick-alignment"),
        traceTickInterval = config.getDuration("kamon.trace.tick-interval"),
        traceReporterQueueSize = config.getInt("kamon.trace.reporter-queue-size"),
        configuredReporters = config.getStringList("kamon.reporters").asScala
      )

    private case class Configuration(metricTickInterval: Duration, optimisticMetricTickAlignment: Boolean,
      traceTickInterval: Duration, traceReporterQueueSize: Int, configuredReporters: Seq[String])
  }
}