diff options
author | Ivan Topolnak <itopolnak@despegar.com> | 2014-03-06 18:08:59 -0300 |
---|---|---|
committer | Ivan Topolnak <itopolnak@despegar.com> | 2014-03-06 18:08:59 -0300 |
commit | eb7c41c17ced2a749fe2c63794531f067d76ea7b (patch) | |
tree | 2c1cca7523c67d79c5be63f3cd9fac70aed09645 /kamon-core/src | |
parent | da2ba240b2202639cae75928232f3f39228dbc3d (diff) | |
download | Kamon-eb7c41c17ced2a749fe2c63794531f067d76ea7b.tar.gz Kamon-eb7c41c17ced2a749fe2c63794531f067d76ea7b.tar.bz2 Kamon-eb7c41c17ced2a749fe2c63794531f067d76ea7b.zip |
support for custom metrics
Diffstat (limited to 'kamon-core/src')
6 files changed, 126 insertions, 6 deletions
diff --git a/kamon-core/src/main/scala/kamon/metrics/ActorMetrics.scala b/kamon-core/src/main/scala/kamon/metrics/ActorMetrics.scala index 4c8752e5..b3685a93 100644 --- a/kamon-core/src/main/scala/kamon/metrics/ActorMetrics.scala +++ b/kamon-core/src/main/scala/kamon/metrics/ActorMetrics.scala @@ -32,7 +32,7 @@ object ActorMetrics extends MetricGroupCategory { case object TimeInMailbox extends MetricIdentity { val name, tag = "TimeInMailbox" } case class ActorMetricRecorder(processingTime: MetricRecorder, mailboxSize: MetricRecorder, timeInMailbox: MetricRecorder) - extends MetricGroupRecorder { + extends MetricMultiGroupRecorder { def record(identity: MetricIdentity, value: Long): Unit = identity match { case ProcessingTime ⇒ processingTime.record(value) diff --git a/kamon-core/src/main/scala/kamon/metrics/CustomMetric.scala b/kamon-core/src/main/scala/kamon/metrics/CustomMetric.scala new file mode 100644 index 00000000..11cb6c3d --- /dev/null +++ b/kamon-core/src/main/scala/kamon/metrics/CustomMetric.scala @@ -0,0 +1,51 @@ +/* + * ========================================================================================= + * Copyright © 2013 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.metrics + +import kamon.metrics.instruments.ContinuousHighDynamicRangeRecorder +import org.HdrHistogram.HighDynamicRangeRecorder.Configuration +import org.HdrHistogram.HighDynamicRangeRecorder +import com.typesafe.config.Config + +case class CustomMetric(name: String) extends MetricGroupIdentity { + val category = CustomMetric +} + +object CustomMetric extends MetricGroupCategory { + val name = "custom-metric" + private val identity = new MetricIdentity { val name, tag = "RecordedValues" } + + def withConfig(highestTrackableValue: Long, significantValueDigits: Int, continuous: Boolean = false) = new MetricGroupFactory { + type GroupRecorder = CustomMetricRecorder + + def create(config: Config): CustomMetricRecorder = + if (continuous) + new CustomMetricRecorder(identity, ContinuousHighDynamicRangeRecorder(Configuration(highestTrackableValue, significantValueDigits))) + else + new CustomMetricRecorder(identity, HighDynamicRangeRecorder(Configuration(highestTrackableValue, significantValueDigits))) + } + + class CustomMetricRecorder(identity: MetricIdentity, underlyingRecorder: HighDynamicRangeRecorder) + extends MetricSingleGroupRecorder { + + def collect: MetricGroupSnapshot = new MetricGroupSnapshot { + val metrics: Map[MetricIdentity, MetricSnapshot] = Map((identity, underlyingRecorder.collect())) + } + + def record(value: Long): Unit = underlyingRecorder.record(value) + } +} diff --git a/kamon-core/src/main/scala/kamon/metrics/Metrics.scala b/kamon-core/src/main/scala/kamon/metrics/Metrics.scala index 81475d52..f7ee833b 100644 --- a/kamon-core/src/main/scala/kamon/metrics/Metrics.scala +++ b/kamon-core/src/main/scala/kamon/metrics/Metrics.scala @@ -34,11 +34,18 @@ trait MetricIdentity { def tag: String } -trait MetricGroupRecorder { - def record(identity: MetricIdentity, value: Long) +sealed trait MetricGroupRecorder { def collect: MetricGroupSnapshot } +trait MetricMultiGroupRecorder extends MetricGroupRecorder { + def record(identity: MetricIdentity, value: Long) +} + +trait MetricSingleGroupRecorder extends MetricGroupRecorder { + def record(value: Long) +} + trait MetricGroupSnapshot { def metrics: Map[MetricIdentity, MetricSnapshot] } diff --git a/kamon-core/src/main/scala/kamon/metrics/TraceMetrics.scala b/kamon-core/src/main/scala/kamon/metrics/TraceMetrics.scala index ccffe382..abea3d82 100644 --- a/kamon-core/src/main/scala/kamon/metrics/TraceMetrics.scala +++ b/kamon-core/src/main/scala/kamon/metrics/TraceMetrics.scala @@ -31,7 +31,7 @@ object TraceMetrics extends MetricGroupCategory { case class HttpClientRequest(name: String, tag: String) extends MetricIdentity class TraceMetricRecorder(val elapsedTime: HighDynamicRangeRecorder, private val segmentRecorderFactory: () ⇒ HighDynamicRangeRecorder) - extends MetricGroupRecorder { + extends MetricMultiGroupRecorder { private val segments = TrieMap[MetricIdentity, HighDynamicRangeRecorder]() diff --git a/kamon-core/src/main/scala/kamon/trace/TraceContext.scala b/kamon-core/src/main/scala/kamon/trace/TraceContext.scala index dd4a25f8..ee1f949e 100644 --- a/kamon-core/src/main/scala/kamon/trace/TraceContext.scala +++ b/kamon-core/src/main/scala/kamon/trace/TraceContext.scala @@ -18,10 +18,11 @@ package kamon.trace import akka.actor.ActorSystem import kamon.Kamon -import kamon.metrics.{ MetricGroupRecorder, MetricIdentity, TraceMetrics, Metrics } +import kamon.metrics._ import java.util.concurrent.ConcurrentLinkedQueue import kamon.trace.TraceContextAware.DefaultTraceContextAware import kamon.trace.TraceContext.SegmentIdentity +import kamon.trace.SegmentData trait TraceContext { def name: String @@ -97,7 +98,7 @@ class SimpleMetricCollectionContext(@volatile private var _name: String, val tok } } - private def drainFinishedSegments(metricRecorder: MetricGroupRecorder): Unit = { + private def drainFinishedSegments(metricRecorder: MetricMultiGroupRecorder): Unit = { while (!finishedSegments.isEmpty) { val segmentData = finishedSegments.poll() metricRecorder.record(segmentData.identity, segmentData.duration) diff --git a/kamon-core/src/test/scala/kamon/metrics/CustomMetricSpec.scala b/kamon-core/src/test/scala/kamon/metrics/CustomMetricSpec.scala new file mode 100644 index 00000000..f5caf6e9 --- /dev/null +++ b/kamon-core/src/test/scala/kamon/metrics/CustomMetricSpec.scala @@ -0,0 +1,61 @@ +/* + * ========================================================================================= + * Copyright © 2013 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.metrics + +import akka.testkit.TestKitBase +import org.scalatest.{ Matchers, WordSpecLike } +import akka.actor.ActorSystem +import scala.concurrent.duration._ +import com.typesafe.config.ConfigFactory +import kamon.Kamon +import kamon.metrics.Subscriptions.TickMetricSnapshot + +class CustomMetricSpec extends TestKitBase with WordSpecLike with Matchers { + implicit lazy val system: ActorSystem = ActorSystem("actor-metrics-spec", ConfigFactory.parseString( + """ + |kamon.metrics { + | filters = [ + | { + | custom-metric { + | includes = [ "test/*" ] + | excludes = [ ] + | } + | } + | ] + |} + """.stripMargin)) + + "the Kamon custom metrics support" should { + "allow registering a custom metric with the Metrics extension" in { + val recorder = Kamon(Metrics).register(CustomMetric("test/sample-counter"), CustomMetric.withConfig(100, 2)) + + recorder should be('defined) + } + + "allow subscriptions to custom metrics using the default subscription protocol" in { + val recorder = Kamon(Metrics).register(CustomMetric("test/sample-counter"), CustomMetric.withConfig(100, 2)) + recorder.map(_.record(100)) + + Kamon(Metrics).subscribe(CustomMetric, "test/sample-counter", testActor) + + println(within(5 seconds) { + expectMsgType[TickMetricSnapshot] + }.metrics(CustomMetric("test/sample-counter"))) + } + } + +} |