From 2fdd0125c48a703bd44ff93072c859b4fc65b766 Mon Sep 17 00:00:00 2001 From: Ivan Topolnjak Date: Sun, 11 Jun 2017 16:03:53 +0200 Subject: use java.util.Duration --- kamon-core/src/main/scala/kamon/Kamon.scala | 2 +- .../src/main/scala/kamon/metric/Counter.scala | 2 +- .../scala/kamon/metric/InstrumentFactory.scala | 8 +-- .../src/main/scala/kamon/metric/Metric.scala | 2 +- .../src/main/scala/kamon/metric/MetricLookup.scala | 2 +- .../main/scala/kamon/metric/MetricRegistry.scala | 2 +- .../main/scala/kamon/metric/MinMaxCounter.scala | 11 ++-- .../kamon/metric/instrument/CounterSpec.scala | 52 ------------------ .../metric/instrument/LongAdderCounterSpec.scala | 64 ++++++++++++++++++++++ .../metric/instrument/MinMaxCounterSpec.scala | 16 +++--- .../kamon/testkit/DefaultInstrumentFactory.scala | 28 ---------- 11 files changed, 86 insertions(+), 103 deletions(-) delete mode 100644 kamon-core/src/test/scala/kamon/metric/instrument/CounterSpec.scala create mode 100644 kamon-core/src/test/scala/kamon/metric/instrument/LongAdderCounterSpec.scala delete mode 100644 kamon-core/src/test/scala/kamon/testkit/DefaultInstrumentFactory.scala (limited to 'kamon-core') diff --git a/kamon-core/src/main/scala/kamon/Kamon.scala b/kamon-core/src/main/scala/kamon/Kamon.scala index 50124e28..f58f056e 100644 --- a/kamon-core/src/main/scala/kamon/Kamon.scala +++ b/kamon-core/src/main/scala/kamon/Kamon.scala @@ -25,7 +25,7 @@ import kamon.trace.Tracer import kamon.util.MeasurementUnit import scala.concurrent.Future -import scala.concurrent.duration.Duration +import java.time.Duration object Kamon extends MetricLookup with ReporterRegistry with io.opentracing.Tracer { diff --git a/kamon-core/src/main/scala/kamon/metric/Counter.scala b/kamon-core/src/main/scala/kamon/metric/Counter.scala index d2481fa5..043ce6e6 100644 --- a/kamon-core/src/main/scala/kamon/metric/Counter.scala +++ b/kamon-core/src/main/scala/kamon/metric/Counter.scala @@ -39,7 +39,7 @@ class LongAdderCounter(name: String, tags: Map[String, String], val unit: Measur else logger.warn(s"Ignored attempt to decrement counter [$name]") } - def snapshot(resetState: Boolean): MetricValue = + def snapshot(resetState: Boolean = true): MetricValue = if(resetState) MetricValue(name, tags, unit, adder.sumAndReset()) else diff --git a/kamon-core/src/main/scala/kamon/metric/InstrumentFactory.scala b/kamon-core/src/main/scala/kamon/metric/InstrumentFactory.scala index 2869595a..d6aa9fb9 100644 --- a/kamon-core/src/main/scala/kamon/metric/InstrumentFactory.scala +++ b/kamon-core/src/main/scala/kamon/metric/InstrumentFactory.scala @@ -23,7 +23,7 @@ import com.typesafe.config.Config import kamon.metric.InstrumentFactory.CustomInstrumentSettings import kamon.util.MeasurementUnit -import scala.concurrent.duration._ +import java.time.Duration private[kamon] class InstrumentFactory private (defaultHistogramDynamicRange: DynamicRange, defaultMMCounterDynamicRange: DynamicRange, @@ -80,14 +80,14 @@ object InstrumentFactory { val factoryConfig = config.getConfig("kamon.metric.instrument-factory") val histogramDynamicRange = readDynamicRange(factoryConfig.getConfig("default-settings.histogram")) val mmCounterDynamicRange = readDynamicRange(factoryConfig.getConfig("default-settings.min-max-counter")) - val mmCounterSampleInterval = factoryConfig.getDuration("default-settings.min-max-counter.sample-interval", TimeUnit.MILLISECONDS) + val mmCounterSampleInterval = factoryConfig.getDuration("default-settings.min-max-counter.sample-interval") val customSettings = factoryConfig.getConfig("custom-settings") .configurations .filter(nonEmptySection) .map(readCustomInstrumentSettings) - new InstrumentFactory(histogramDynamicRange, mmCounterDynamicRange, mmCounterSampleInterval.millis, customSettings) + new InstrumentFactory(histogramDynamicRange, mmCounterDynamicRange, mmCounterSampleInterval, customSettings) } private def nonEmptySection(entry: (String, Config)): Boolean = entry match { @@ -100,7 +100,7 @@ object InstrumentFactory { if (metricConfig.hasPath("lowest-discernible-value")) Some(metricConfig.getLong("lowest-discernible-value")) else None, if (metricConfig.hasPath("highest-trackable-value")) Some(metricConfig.getLong("highest-trackable-value")) else None, if (metricConfig.hasPath("significant-value-digits")) Some(metricConfig.getInt("significant-value-digits")) else None, - if (metricConfig.hasPath("sample-interval")) Some(metricConfig.getDuration("sample-interval", TimeUnit.MILLISECONDS).millis) else None + if (metricConfig.hasPath("sample-interval")) Some(metricConfig.getDuration("sample-interval")) else None ) metricName -> customSettings } diff --git a/kamon-core/src/main/scala/kamon/metric/Metric.scala b/kamon-core/src/main/scala/kamon/metric/Metric.scala index 89c0b5e9..dbac3878 100644 --- a/kamon-core/src/main/scala/kamon/metric/Metric.scala +++ b/kamon-core/src/main/scala/kamon/metric/Metric.scala @@ -23,7 +23,7 @@ import kamon.metric.InstrumentFactory.InstrumentTypes._ import kamon.util.MeasurementUnit import scala.collection.concurrent.TrieMap -import scala.concurrent.duration.Duration +import java.time.Duration diff --git a/kamon-core/src/main/scala/kamon/metric/MetricLookup.scala b/kamon-core/src/main/scala/kamon/metric/MetricLookup.scala index a09a8f02..7e58a722 100644 --- a/kamon-core/src/main/scala/kamon/metric/MetricLookup.scala +++ b/kamon-core/src/main/scala/kamon/metric/MetricLookup.scala @@ -18,7 +18,7 @@ package metric import kamon.util.MeasurementUnit -import scala.concurrent.duration.Duration +import java.time.Duration trait MetricLookup { diff --git a/kamon-core/src/main/scala/kamon/metric/MetricRegistry.scala b/kamon-core/src/main/scala/kamon/metric/MetricRegistry.scala index e47df88e..48ad29b7 100644 --- a/kamon-core/src/main/scala/kamon/metric/MetricRegistry.scala +++ b/kamon-core/src/main/scala/kamon/metric/MetricRegistry.scala @@ -24,7 +24,7 @@ import kamon.metric.InstrumentFactory.{InstrumentType, InstrumentTypes} import kamon.util.MeasurementUnit import scala.collection.concurrent.TrieMap -import scala.concurrent.duration.Duration +import java.time.Duration class MetricRegistry(initialConfig: Config) extends MetricsSnapshotGenerator { diff --git a/kamon-core/src/main/scala/kamon/metric/MinMaxCounter.scala b/kamon-core/src/main/scala/kamon/metric/MinMaxCounter.scala index 7dca3060..6acb7c54 100644 --- a/kamon-core/src/main/scala/kamon/metric/MinMaxCounter.scala +++ b/kamon-core/src/main/scala/kamon/metric/MinMaxCounter.scala @@ -16,12 +16,11 @@ package kamon.metric import java.lang.Math.abs +import java.time.Duration import java.util.concurrent.atomic.AtomicLong import kamon.util.{AtomicLongMaxUpdater, MeasurementUnit} -import scala.concurrent.duration.Duration - trait MinMaxCounter { def unit: MeasurementUnit def dynamicRange: DynamicRange @@ -35,7 +34,7 @@ trait MinMaxCounter { } class SimpleMinMaxCounter(name: String, tags: Map[String, String], underlyingHistogram: AtomicHdrHistogram, - val sampleInterval: Duration) extends MinMaxCounter{ + val sampleInterval: Duration) extends MinMaxCounter { private val min = AtomicLongMaxUpdater() private val max = AtomicLongMaxUpdater() @@ -47,9 +46,6 @@ class SimpleMinMaxCounter(name: String, tags: Map[String, String], underlyingHis def unit: MeasurementUnit = underlyingHistogram.unit - private[kamon] def snapshot(resetState: Boolean): MetricDistribution = - underlyingHistogram.snapshot(resetState) - def increment(): Unit = increment(1L) @@ -86,4 +82,7 @@ class SimpleMinMaxCounter(name: String, tags: Map[String, String], underlyingHis underlyingHistogram.record(currentMin) underlyingHistogram.record(currentMax) } + + private[kamon] def snapshot(resetState: Boolean = true): MetricDistribution = + underlyingHistogram.snapshot(resetState) } diff --git a/kamon-core/src/test/scala/kamon/metric/instrument/CounterSpec.scala b/kamon-core/src/test/scala/kamon/metric/instrument/CounterSpec.scala deleted file mode 100644 index b4e3fe96..00000000 --- a/kamon-core/src/test/scala/kamon/metric/instrument/CounterSpec.scala +++ /dev/null @@ -1,52 +0,0 @@ -/* ========================================================================================= - * Copyright © 2013-2017 the kamon project - * - * 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.metric.instrument - -import kamon.testkit.DefaultInstrumentFactory -import org.scalatest.{Matchers, WordSpec} - -class CounterSpec extends WordSpec with Matchers with DefaultInstrumentFactory { - - "a Counter" should { - - "allow unit and bundled increments" in { - val counter = buildCounter("unit-increments") - counter.increment() - counter.increment() - counter.increment(40) - - counter.snapshot().value shouldBe 42 - } - - "warn the user and ignore attempts to decrement the counter" in { - val counter = buildCounter("attempt-to-decrement") - counter.increment(100) - counter.increment(100) - counter.increment(100) - - counter.snapshot().value shouldBe 300 - } - - "reset the internal state to zero after taking snapshots" in { - val counter = buildCounter("reset-after-snapshot") - counter.increment() - counter.increment(10) - - counter.snapshot().value shouldBe 11 - counter.snapshot().value shouldBe 0 - } - } -} diff --git a/kamon-core/src/test/scala/kamon/metric/instrument/LongAdderCounterSpec.scala b/kamon-core/src/test/scala/kamon/metric/instrument/LongAdderCounterSpec.scala new file mode 100644 index 00000000..d742b54f --- /dev/null +++ b/kamon-core/src/test/scala/kamon/metric/instrument/LongAdderCounterSpec.scala @@ -0,0 +1,64 @@ +/* ========================================================================================= + * Copyright © 2013-2017 the kamon project + * + * 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 +package metric + +import kamon.util.MeasurementUnit +import org.scalatest.{Matchers, WordSpec} + +class LongAdderCounterSpec extends WordSpec with Matchers { + + "a LongAdderCounter" should { + "allow unit and bundled increments" in { + val counter = buildCounter("unit-increments") + counter.increment() + counter.increment() + counter.increment(40) + + counter.snapshot().value shouldBe 42 + } + + "warn the user and ignore attempts to decrement the counter" in { + val counter = buildCounter("attempt-to-decrement") + counter.increment(100) + counter.increment(100) + counter.increment(100) + + counter.snapshot().value shouldBe 300 + } + + "reset the internal state to zero after taking snapshots as a default behavior" in { + val counter = buildCounter("reset-after-snapshot") + counter.increment() + counter.increment(10) + + counter.snapshot().value shouldBe 11 + counter.snapshot().value shouldBe 0 + } + + "optionally leave the internal state unchanged" in { + val counter = buildCounter("reset-after-snapshot") + counter.increment() + counter.increment(10) + + counter.snapshot(resetState = false).value shouldBe 11 + counter.snapshot(resetState = false).value shouldBe 11 + } + } + + def buildCounter(name: String, tags: Map[String, String] = Map.empty, unit: MeasurementUnit = MeasurementUnit.none): LongAdderCounter = + new LongAdderCounter(name, tags, unit) +} diff --git a/kamon-core/src/test/scala/kamon/metric/instrument/MinMaxCounterSpec.scala b/kamon-core/src/test/scala/kamon/metric/instrument/MinMaxCounterSpec.scala index b4c11dc2..a21bcc96 100644 --- a/kamon-core/src/test/scala/kamon/metric/instrument/MinMaxCounterSpec.scala +++ b/kamon-core/src/test/scala/kamon/metric/instrument/MinMaxCounterSpec.scala @@ -16,16 +16,17 @@ package kamon.metric.instrument -import kamon.metric.Bucket -import kamon.testkit.DefaultInstrumentFactory +import java.time.Duration + +import kamon.metric.{AtomicHdrHistogram, Bucket, DynamicRange, SimpleMinMaxCounter} +import kamon.util.MeasurementUnit import org.scalatest.{Matchers, WordSpec} case class TemporalBucket(value: Long, frequency: Long) extends Bucket -class MinMaxCounterSpec extends WordSpec with Matchers with DefaultInstrumentFactory { +class MinMaxCounterSpec extends WordSpec with Matchers { "a MinMaxCounter" should { - "track ascending tendencies" in { val mmCounter = buildMinMaxCounter("track-ascending") mmCounter.increment() @@ -50,7 +51,6 @@ class MinMaxCounterSpec extends WordSpec with Matchers with DefaultInstrumentFac mmCounter.sample() val snapshot = mmCounter.snapshot() - snapshot.distribution.min should be(0) snapshot.distribution.max should be(5) } @@ -60,18 +60,15 @@ class MinMaxCounterSpec extends WordSpec with Matchers with DefaultInstrumentFac mmCounter.increment(5) mmCounter.decrement(3) - mmCounter.sample() val firstSnapshot = mmCounter.snapshot() - firstSnapshot.distribution.min should be(0) firstSnapshot.distribution.max should be(5) mmCounter.sample() val secondSnapshot = mmCounter.snapshot() - secondSnapshot.distribution.min should be(2) secondSnapshot.distribution.max should be(2) } @@ -89,4 +86,7 @@ class MinMaxCounterSpec extends WordSpec with Matchers with DefaultInstrumentFac snapshot.distribution.max should be(0) } } + + def buildMinMaxCounter(name: String, tags: Map[String, String] = Map.empty, unit: MeasurementUnit = MeasurementUnit.none): SimpleMinMaxCounter = + new SimpleMinMaxCounter(name, tags, new AtomicHdrHistogram(name, tags, unit, dynamicRange = DynamicRange.Default), Duration.ofMillis(100)) } \ No newline at end of file diff --git a/kamon-core/src/test/scala/kamon/testkit/DefaultInstrumentFactory.scala b/kamon-core/src/test/scala/kamon/testkit/DefaultInstrumentFactory.scala deleted file mode 100644 index 0778a3bc..00000000 --- a/kamon-core/src/test/scala/kamon/testkit/DefaultInstrumentFactory.scala +++ /dev/null @@ -1,28 +0,0 @@ -/* ========================================================================================= - * Copyright © 2013-2017 the kamon project - * - * 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.testkit - -import com.typesafe.config.ConfigFactory -import kamon.metric.InstrumentFactory -import kamon.util.MeasurementUnit -import scala.concurrent.duration._ - -trait DefaultInstrumentFactory { - val instrumentFactory: InstrumentFactory = InstrumentFactory.fromConfig(ConfigFactory.load()) - - def buildCounter(name: String) = instrumentFactory.buildCounter(name, Map.empty, MeasurementUnit.none) - def buildMinMaxCounter(name: String) = instrumentFactory.buildMinMaxCounter(None, Some(1 hour))(name, Map.empty, MeasurementUnit.none) -} -- cgit v1.2.3