diff options
author | Ivan Topolnjak <ivantopo@gmail.com> | 2017-06-11 11:03:56 +0200 |
---|---|---|
committer | Ivan Topolnjak <ivantopo@gmail.com> | 2017-06-11 11:24:29 +0200 |
commit | a78ce66cbe149ec7f71b7912e9f7df427688aa15 (patch) | |
tree | 71be4f7a70168799bc490ff0811a5bb40ecbbe29 /kamon-core/src/main/scala/kamon/ReporterRegistry.scala | |
parent | e8d3e612dcf0fa396a25920a23f108f6ab8c2e61 (diff) | |
download | Kamon-a78ce66cbe149ec7f71b7912e9f7df427688aa15.tar.gz Kamon-a78ce66cbe149ec7f71b7912e9f7df427688aa15.tar.bz2 Kamon-a78ce66cbe149ec7f71b7912e9f7df427688aa15.zip |
dont start tickers until a reporter is added
Diffstat (limited to 'kamon-core/src/main/scala/kamon/ReporterRegistry.scala')
-rw-r--r-- | kamon-core/src/main/scala/kamon/ReporterRegistry.scala | 60 |
1 files changed, 41 insertions, 19 deletions
diff --git a/kamon-core/src/main/scala/kamon/ReporterRegistry.scala b/kamon-core/src/main/scala/kamon/ReporterRegistry.scala index 27a4a6ea..66193619 100644 --- a/kamon-core/src/main/scala/kamon/ReporterRegistry.scala +++ b/kamon-core/src/main/scala/kamon/ReporterRegistry.scala @@ -15,6 +15,7 @@ package kamon +import java.time.Duration import java.util.concurrent.atomic.{AtomicLong, AtomicReference} import java.util.concurrent._ @@ -64,6 +65,7 @@ trait SpanReporter { class ReporterRegistryImpl(metrics: MetricsSnapshotGenerator, initialConfig: Config) extends ReporterRegistry { private val registryExecutionContext = Executors.newScheduledThreadPool(2, threadFactory("kamon-reporter-registry")) private val reporterCounter = new AtomicLong(0L) + private var registryConfiguration = readRegistryConfiguration(initialConfig) private val metricReporters = TrieMap[Long, MetricReporterEntry]() private val metricReporterTickerSchedule = new AtomicReference[ScheduledFuture[_]]() @@ -97,6 +99,9 @@ class ReporterRegistryImpl(metrics: MetricsSnapshotGenerator, initialConfig: Con executionContext = ExecutionContext.fromExecutorService(executor) ) + if(metricReporters.isEmpty) + reStartMetricTicker() + metricReporters.put(reporterEntry.id, reporterEntry) createRegistration(reporterEntry.id, metricReporters) } @@ -110,6 +115,9 @@ class ReporterRegistryImpl(metrics: MetricsSnapshotGenerator, initialConfig: Con executionContext = ExecutionContext.fromExecutorService(executor) ) + if(spanReporters.isEmpty) + reStartTraceTicker() + spanReporters.put(reporterEntry.id, reporterEntry) createRegistration(reporterEntry.id, spanReporters) } @@ -141,43 +149,49 @@ class ReporterRegistryImpl(metrics: MetricsSnapshotGenerator, initialConfig: Con } private[kamon] def reconfigure(config: Config): Unit = synchronized { - val tickIntervalMillis = config.getDuration("kamon.metric.tick-interval", TimeUnit.MILLISECONDS) - val traceTickIntervalMillis = config.getDuration("kamon.trace.tick-interval", TimeUnit.MILLISECONDS) + val newConfig = readRegistryConfiguration(config) - val currentMetricTicker = metricReporterTickerSchedule.get() - if(currentMetricTicker != null) { - currentMetricTicker.cancel(true) - } + if(newConfig.metricTickInterval != registryConfiguration.metricTickInterval && metricReporters.nonEmpty) + reStartMetricTicker() + + if(newConfig.traceTickInterval != registryConfiguration.metricTickInterval && spanReporters.nonEmpty) + reStartTraceTicker() - val currentSpanTicker = spanReporterTickerSchedule.get() - if(currentSpanTicker != null) { - currentSpanTicker.cancel(true) - } // Reconfigure all registered reporters - metricReporters.foreach { case (_, entry) => - Future(entry.reporter.reconfigure(config))(entry.executionContext) - } + metricReporters.foreach { case (_, entry) => Future(entry.reporter.reconfigure(config))(entry.executionContext) } + spanReporters.foreach { case (_, entry) => Future(entry.reporter.reconfigure(config))(entry.executionContext) } + registryConfiguration = newConfig + } - spanReporters.foreach { case (_, entry) => - Future(entry.reporter.reconfigure(config))(entry.executionContext) - } + + private def reStartMetricTicker(): Unit = { + val tickIntervalMillis = registryConfiguration.metricTickInterval.toMillis + val currentMetricTicker = metricReporterTickerSchedule.get() + + if(currentMetricTicker != null) + currentMetricTicker.cancel(false) metricReporterTickerSchedule.set { registryExecutionContext.scheduleAtFixedRate( new MetricReporterTicker(metrics, metricReporters), tickIntervalMillis, 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), traceTickIntervalMillis, traceTickIntervalMillis, TimeUnit.MILLISECONDS + new SpanReporterTicker(spanReporters), tickIntervalMillis, tickIntervalMillis, TimeUnit.MILLISECONDS ) } } - - private[kamon] def reportSpan(span: Span.CompletedSpan): Unit = { spanReporters.foreach { case (_, reporterEntry) => if(reporterEntry.isActive) @@ -258,4 +272,12 @@ class ReporterRegistryImpl(metrics: MetricsSnapshotGenerator, initialConfig: Con } } } + + private def readRegistryConfiguration(config: Config): Configuration = + Configuration( + metricTickInterval = config.getDuration("kamon.metric.tick-interval"), + traceTickInterval = config.getDuration("kamon.trace.tick-interval") + ) + + private case class Configuration(metricTickInterval: Duration, traceTickInterval: Duration) }
\ No newline at end of file |