Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

KamonMetric cast error #608

Closed
mikelalvarezgo opened this issue Sep 20, 2019 · 4 comments
Closed

KamonMetric cast error #608

mikelalvarezgo opened this issue Sep 20, 2019 · 4 comments

Comments

@mikelalvarezgo
Copy link

We are upgrading Kamon in our application to version 2.0. We are using the histogram and counter metrics for monitoring our application. I have found this exception during execution:


java.lang.ClassCastException: kamon.metric.MetricFactory$$anon$3 cannot be cast to kamon.metric.Metric$Counter
	at kamon.metric.MetricRegistry.counter(MetricRegistry.scala:50)
	at kamon.metric.MetricBuilding.counter(MetricBuilding.scala:32)
	at kamon.metric.MetricBuilding.counter$(MetricBuilding.scala:31)
	at kamon.Kamon$.counter(Kamon.scala:19)
	at com.letgo.chat.modules.shared.monitoring.infrastructure.kamon.KamonCounter.<init>(KamonCounter.scala:8)
	at com.letgo.chat.modules.shared.monitoring.infrastructure.kamon.KamonMonitoring$.counter(KamonMonitoring.scala:6)
	at com.letgo.chat.modules.shared.persistence.DbConnection.$anonfun$run$1(DbConnection.scala:37)
	at com.letgo.chat.modules.shared.persistence.DbConnection.$anonfun$run$1$adapted(DbConnection.scala:34)
	at scala.concurrent.impl.CallbackRunnable.run(Promise.scala:64)
	at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
	at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
	at java.lang.Thread.run(Unknown Source)

This method is called using an internal name, depending on the action we watch, using an internal class for monitoring:

Kamon.counter(name).withoutTags().record("appname.actionName.responseName")

In one case is throwing this exception, but changing the name of the counter metric solves the issue. Has anyone had this issue before?

@mladens
Copy link
Contributor

mladens commented Sep 20, 2019

This is a bit strange, counter doesn't have a record method, histogram does, but even that one will take only longs.
Can you paste part of your KamonCounter thats invoking Kamon apis

@mikelalvarezgo
Copy link
Author

mikelalvarezgo commented Sep 20, 2019

The exception is also throwed with histogram method.

ERROR - kamon.metric.MetricFactory$$anon$1 cannot be cast to kamon.metric.Metric$Histogram 
java.lang.ClassCastException: kamon.metric.MetricFactory$$anon$1 cannot be cast to kamon.metric.Metric$Histogram
	at kamon.metric.MetricRegistry.histogram(MetricRegistry.scala:82)
	at kamon.metric.MetricBuilding.histogram(MetricBuilding.scala:74)
	at kamon.metric.MetricBuilding.histogram$(MetricBuilding.scala:73)
	at kamon.Kamon$.histogram(Kamon.scala:19)
	at modules.shared.monitoring.infrastructure.kamon.KamonHistogram.record(KamonHistogram.scala:9)

The implementation we have for histogram and counter , that is invoking the Kamon apis is the next:

class KamonHistogram(private val name: String) extends Histogram {
  override def record(value: Long): Unit = {
    Kamon.histogram(name).withoutTags().record(value)
    ()
  }

class KamonCounter(name: String) extends Counter {
  override def increment(): Unit = {
    Kamon.counter(name).withoutTags().increment()
    ()
  }

  override def add(num: Int): Unit = {
    Kamon.counter(name).withoutTags().increment(num)
    ()
  }
}

} 

It is weird that just changing the name of the metric it works, but with the name we were using in the previous version is not working and throws that exception. It seems that the line it fails in MetricRegistry

 val metric = _metrics.atomicGetOrElseUpdate(name, _factory.counter(name, description, unit, autoUpdateInterval))
        .asInstanceOf[Metric.Counter]

So at some point, with that concrete name we used for the histogram or counter metric, it is taking an object that is not possible to cast to Metric.Counter . As I said before changing the name of metric fix the issue, anyway something is not working fine there.

@mladens
Copy link
Contributor

mladens commented Sep 24, 2019

Seems like metric is already registered under different instrument type, that's why only certain name values end up exploding. This here will log an error if it happens.

@ivantopo
Copy link
Contributor

I was taking a look into this and indeed, this happens when the user attempts to redefine a metric. The behavior is expected, just the exception thrown was not the right one and I just got it fixed here: #618.

Thanks for sharing this issue @mikelalvarezgo.

@ivantopo ivantopo changed the title KanonMetric cast error KamonMetric cast error Oct 18, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants