From ac4d0127f43918b694f0bbb4f37a0c1a22a3dca3 Mon Sep 17 00:00:00 2001 From: "Ryan, Jonathan" Date: Thu, 30 Jun 2022 12:27:47 -0400 Subject: [PATCH 1/3] Update metrics --- project/Dependencies.scala | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/project/Dependencies.scala b/project/Dependencies.scala index 25906cc3..1510634c 100644 --- a/project/Dependencies.scala +++ b/project/Dependencies.scala @@ -1,6 +1,6 @@ import sbt._ object Dependencies { - val metricsV = "3.2.6" + val metricsV = "4.2.10" val apacheHttpClientV = "4.5.6" val akkaV = "2.5.32" From 5cb1646ea792838910e0556f76a87f6a38ccd15c Mon Sep 17 00:00:00 2001 From: "Ryan, Jonathan" Date: Thu, 30 Jun 2022 12:41:09 -0400 Subject: [PATCH 2/3] Update metrics --- build.sbt | 2 ++ .../com/comcast/money/core/metrics/MetricRegistryFactory.scala | 3 ++- project/Dependencies.scala | 1 + 3 files changed, 5 insertions(+), 1 deletion(-) diff --git a/build.sbt b/build.sbt index 85d8bef4..82ef4c34 100644 --- a/build.sbt +++ b/build.sbt @@ -59,6 +59,7 @@ lazy val moneyCore = slf4j, log4jbinding, metricsCore, + metricsJmx, openTelemetryApi, openTelemetrySemConv, typesafeConfig @@ -75,6 +76,7 @@ lazy val moneyOtelFormatters = slf4j, log4jbinding, metricsCore, + metricsJmx, openTelemetryApi, openTelemetryProp, typesafeConfig diff --git a/money-core/src/main/scala/com/comcast/money/core/metrics/MetricRegistryFactory.scala b/money-core/src/main/scala/com/comcast/money/core/metrics/MetricRegistryFactory.scala index bf4394cc..fbdea8ea 100644 --- a/money-core/src/main/scala/com/comcast/money/core/metrics/MetricRegistryFactory.scala +++ b/money-core/src/main/scala/com/comcast/money/core/metrics/MetricRegistryFactory.scala @@ -16,7 +16,8 @@ package com.comcast.money.core.metrics -import com.codahale.metrics.{ JmxReporter, MetricRegistry } +import com.codahale.metrics.MetricRegistry +import com.codahale.metrics.jmx.JmxReporter import com.typesafe.config.Config import org.slf4j.LoggerFactory diff --git a/project/Dependencies.scala b/project/Dependencies.scala index 1510634c..bf42566e 100644 --- a/project/Dependencies.scala +++ b/project/Dependencies.scala @@ -36,6 +36,7 @@ object Dependencies { // Codahale metrics val metricsCore = "io.dropwizard.metrics" % "metrics-core" % metricsV + val metricsJmx = "io.dropwizard.metrics" % "metrics-jmx" % metricsV // Apache http client val apacheHttpClient = "org.apache.httpcomponents" % "httpclient" % apacheHttpClientV From 4ee16ca4b6323089c73b1c7eb15782e9931e3cd3 Mon Sep 17 00:00:00 2001 From: "Ryan, Jonathan" Date: Thu, 30 Jun 2022 12:54:49 -0400 Subject: [PATCH 3/3] Just remove metrics handlers --- build.sbt | 4 - .../core/handlers/MetricsSpanHandler.scala | 55 ------------- .../core/metrics/MetricRegistryFactory.scala | 72 ----------------- .../core/handlers/MetricsHandlerSpec.scala | 70 ---------------- .../metrics/MetricRegistryFactorySpec.scala | 79 ------------------- project/Dependencies.scala | 4 - 6 files changed, 284 deletions(-) delete mode 100644 money-core/src/main/scala/com/comcast/money/core/handlers/MetricsSpanHandler.scala delete mode 100644 money-core/src/main/scala/com/comcast/money/core/metrics/MetricRegistryFactory.scala delete mode 100644 money-core/src/test/scala/com/comcast/money/core/handlers/MetricsHandlerSpec.scala delete mode 100644 money-core/src/test/scala/com/comcast/money/core/metrics/MetricRegistryFactorySpec.scala diff --git a/build.sbt b/build.sbt index 82ef4c34..48b9eb9b 100644 --- a/build.sbt +++ b/build.sbt @@ -58,8 +58,6 @@ lazy val moneyCore = Seq( slf4j, log4jbinding, - metricsCore, - metricsJmx, openTelemetryApi, openTelemetrySemConv, typesafeConfig @@ -75,8 +73,6 @@ lazy val moneyOtelFormatters = Seq( slf4j, log4jbinding, - metricsCore, - metricsJmx, openTelemetryApi, openTelemetryProp, typesafeConfig diff --git a/money-core/src/main/scala/com/comcast/money/core/handlers/MetricsSpanHandler.scala b/money-core/src/main/scala/com/comcast/money/core/handlers/MetricsSpanHandler.scala deleted file mode 100644 index 340ea6fa..00000000 --- a/money-core/src/main/scala/com/comcast/money/core/handlers/MetricsSpanHandler.scala +++ /dev/null @@ -1,55 +0,0 @@ -/* - * Copyright 2012 Comcast Cable Communications Management, LLC - * - * 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 com.comcast.money.core.handlers - -import com.codahale.metrics.{ Histogram, Meter, MetricRegistry } -import com.comcast.money.api.{ SpanHandler, SpanInfo } -import com.comcast.money.core.metrics.MetricRegistryFactory -import com.typesafe.config.Config - -import scala.collection.concurrent.TrieMap - -case class SpanMetrics(latencyMetric: Histogram, errorMetric: Meter) { - - def record(spanInfo: SpanInfo): Unit = { - if (!spanInfo.success) - errorMetric.mark() - - latencyMetric.update(spanInfo.durationMicros) - } -} - -object MetricsSpanHandler { - def apply(conf: Config): MetricsSpanHandler = { - val metricRegistry = MetricRegistryFactory.metricRegistry(conf) - new MetricsSpanHandler(metricRegistry) - } -} - -class MetricsSpanHandler(val metricRegistry: MetricRegistry) extends SpanHandler { - - private[handlers] val spans = new TrieMap[String, SpanMetrics]() - - def handle(span: SpanInfo): Unit = - spans.getOrElseUpdate(span.name, spanMetrics(span.name)).record(span) - - private def spanMetrics(spanName: String): SpanMetrics = { - val latencyMetric: Histogram = metricRegistry.histogram(s"/money/$spanName:latency") - val errorMetric: Meter = metricRegistry.meter(s"/money/$spanName:error") - SpanMetrics(latencyMetric, errorMetric) - } -} diff --git a/money-core/src/main/scala/com/comcast/money/core/metrics/MetricRegistryFactory.scala b/money-core/src/main/scala/com/comcast/money/core/metrics/MetricRegistryFactory.scala deleted file mode 100644 index fbdea8ea..00000000 --- a/money-core/src/main/scala/com/comcast/money/core/metrics/MetricRegistryFactory.scala +++ /dev/null @@ -1,72 +0,0 @@ -/* - * Copyright 2012 Comcast Cable Communications Management, LLC - * - * 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 com.comcast.money.core.metrics - -import com.codahale.metrics.MetricRegistry -import com.codahale.metrics.jmx.JmxReporter -import com.typesafe.config.Config -import org.slf4j.LoggerFactory - -/* - * Simple factory that tries to delegate to an implementation of this trait itself and announced via - * the configuration 'metricRegistryFactory' - requiring that this implementation has a default constructor. - * - * It is up to the custom metricRegistryFactory what kind of MetricRegistry is created. No action is - * performed on this MetricRegistry is performed (i.e. registering reporters) but used/passed back right away. - * - * If any error occurs (configuration is not set, class can't be loaded etc.) the default behavior - * is performed and a fresh MetricRegistry is created and registered with the JmxReporter. - * - * Note: This trait should be kept as simple as possible so that the resulting interface can also be implemented - * by a Java client custom factory. - */ -object MetricRegistryFactory { - - private val logger = LoggerFactory.getLogger("com.comcast.money.core.metrics.MetricRegistryFactory") - - def metricRegistry(config: Config): MetricRegistry = { - try { - val realFactory = - if (config.hasPath("metrics-registry.class-name")) - Class.forName(config.getString("metrics-registry.class-name")).newInstance.asInstanceOf[MetricRegistryFactory] - else - new DefaultMetricRegistryFactory() - - // Ask the custom factory for an MetricRegistry - and pass in our configuration so that an implementation - // can add their settings in the application.conf, too. - realFactory.metricRegistry(config) - } catch { - case e: Throwable => - logger.error("Unable to create actual factory instance", e) - throw e - } - } -} - -class DefaultMetricRegistryFactory extends MetricRegistryFactory { - override def metricRegistry(config: Config): MetricRegistry = { - val registry = new MetricRegistry - val jmxReporter = JmxReporter.forRegistry(registry).build() - jmxReporter.start() - - registry - } -} - -trait MetricRegistryFactory { - def metricRegistry(config: Config): MetricRegistry -} diff --git a/money-core/src/test/scala/com/comcast/money/core/handlers/MetricsHandlerSpec.scala b/money-core/src/test/scala/com/comcast/money/core/handlers/MetricsHandlerSpec.scala deleted file mode 100644 index aab5681e..00000000 --- a/money-core/src/test/scala/com/comcast/money/core/handlers/MetricsHandlerSpec.scala +++ /dev/null @@ -1,70 +0,0 @@ -/* - * Copyright 2012 Comcast Cable Communications Management, LLC - * - * 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 com.comcast.money.core.handlers - -import com.codahale.metrics.{ Histogram, Meter, MetricRegistry } -import com.typesafe.config.Config -import io.opentelemetry.api.trace.StatusCode -import org.mockito.Mockito._ -import org.mockito.ArgumentMatchers._ -import org.scalatest.wordspec.AnyWordSpec -import org.scalatest.matchers.should.Matchers -import org.scalatestplus.mockito.MockitoSugar -import org.scalatest.OneInstancePerTest - -class MetricsHandlerSpec extends AnyWordSpec with Matchers with MockitoSugar with TestData with OneInstancePerTest { - - val conf = mock[Config] - when(conf.hasPath("metrics-registry.class-name")).thenReturn(true) - when(conf.getString("metrics-registry.class-name")).thenReturn("com.comcast.money.core.metrics.MockMetricRegistryFactory") - - "MetricsSpanHandler" should { - "configure the metrics registry" in { - val underTest = MetricsSpanHandler(conf) - - underTest.metricRegistry shouldBe a[MetricRegistry] - } - - "save latency metric" in { - val underTest = MetricsSpanHandler(conf) - - val latencyMetric = mock[Histogram] - val errorMetric = mock[Meter] - when(underTest.metricRegistry.histogram(anyString())).thenReturn(latencyMetric) - when(underTest.metricRegistry.meter(anyString())).thenReturn(errorMetric) - - underTest.handle(testSpanInfo) - - verify(latencyMetric).update(testSpanInfo.durationMicros) - verifyNoMoreInteractions(errorMetric) - } - - "update the error metric" in { - val underTest = MetricsSpanHandler(conf) - - val latencyMetric = mock[Histogram] - val errorMetric = mock[Meter] - when(underTest.metricRegistry.histogram(anyString())).thenReturn(latencyMetric) - when(underTest.metricRegistry.meter(anyString())).thenReturn(errorMetric) - - underTest.handle(testSpanInfo.copy(status = StatusCode.ERROR)) - - verify(latencyMetric).update(testSpanInfo.durationMicros) - verify(errorMetric).mark() - } - } -} diff --git a/money-core/src/test/scala/com/comcast/money/core/metrics/MetricRegistryFactorySpec.scala b/money-core/src/test/scala/com/comcast/money/core/metrics/MetricRegistryFactorySpec.scala deleted file mode 100644 index a1312e11..00000000 --- a/money-core/src/test/scala/com/comcast/money/core/metrics/MetricRegistryFactorySpec.scala +++ /dev/null @@ -1,79 +0,0 @@ -/* - * Copyright 2012 Comcast Cable Communications Management, LLC - * - * 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 com.comcast.money.core.metrics - -import com.codahale.metrics.MetricRegistry -import com.typesafe.config.Config -import org.mockito.Mockito._ -import org.scalatestplus.mockito.MockitoSugar -import org.scalatest.wordspec.AnyWordSpec -import org.scalatest.BeforeAndAfter -import org.scalatest.matchers.should.Matchers - -object MockMetricRegistryFactory extends MetricRegistryFactory with MockitoSugar { - lazy val mockRegistry = mock[MetricRegistry] - - def metricRegistry(config: Config): MetricRegistry = mockRegistry - -} - -class MockMetricRegistryFactory extends MetricRegistryFactory { - def metricRegistry(config: Config): MetricRegistry = MockMetricRegistryFactory.mockRegistry -} - -class MetricRegistryFactorySpec extends AnyWordSpec with BeforeAndAfter with MockitoSugar with Matchers { - - private val conf = mock[Config] - - "The MetricRegistry" when { - "use the DefaultMetricRegistryFactory" should { - "creating MetricRegistries" in { - - when(conf.getString("metrics-registry.class-name")).thenReturn("com.comcast.money.metrics.DefaultMetricRegistryFactory") - - val registry = MetricRegistryFactory.metricRegistry(conf) - - registry shouldNot be(null) - } - } - } - - "fall back to the DefaultMetricRegistryFactory" should { - "when the config is broken" in { - - when(conf.hasPath("metrics-registry.class-name")).thenReturn(true) - when(conf.getString("metrics-registry.class-name")).thenReturn("lorem ipsum") - - intercept[ClassNotFoundException] { - val registry = MetricRegistryFactory.metricRegistry(conf) - } - } - } - - "use the MockMetricRegistryFactory" should { - "when configured so" in { - - when(conf.hasPath("metrics-registry.class-name")).thenReturn(true) - when(conf.getString("metrics-registry.class-name")).thenReturn("com.comcast.money.core.metrics.MockMetricRegistryFactory") - - val registry1 = MetricRegistryFactory.metricRegistry(conf) - val registry2 = MetricRegistryFactory.metricRegistry(conf) - - registry1 should be theSameInstanceAs registry2 - } - } -} diff --git a/project/Dependencies.scala b/project/Dependencies.scala index bf42566e..18d0f664 100644 --- a/project/Dependencies.scala +++ b/project/Dependencies.scala @@ -34,10 +34,6 @@ object Dependencies { // Typseafe config def typesafeConfig = "com.typesafe" % "config" % typesafeConfigV - // Codahale metrics - val metricsCore = "io.dropwizard.metrics" % "metrics-core" % metricsV - val metricsJmx = "io.dropwizard.metrics" % "metrics-jmx" % metricsV - // Apache http client val apacheHttpClient = "org.apache.httpcomponents" % "httpclient" % apacheHttpClientV