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

code example for Reporters #63

Closed
hhadind opened this issue Aug 3, 2015 · 8 comments
Closed

code example for Reporters #63

hhadind opened this issue Aug 3, 2015 · 8 comments
Labels

Comments

@hhadind
Copy link

hhadind commented Aug 3, 2015

any examples for using reporters like the ConsoleReporter or the CvsReporter ?
thanks in advance.

@scullxbones
Copy link
Collaborator

They're fairly straight-forward to set up. Enhancing the manual example a little bit:

object YourApplication {
  /** The application wide metrics registry. */
  val metricRegistry = { 
    val registry = new com.codahale.metrics.MetricRegistry()
    ConsoleReporter.forRegistry(registry)
      .convertRatesTo(TimeUnit.SECONDS)
      .convertDurationsTo(TimeUnit.MILLISECONDS)
      .build()
      .start(1, TimeUnit.MINUTES)
    registry
  }
}

@hhadind
Copy link
Author

hhadind commented Aug 4, 2015

thank you @scullxbones for responding. I am still new to this package so excuse the basic question, when I integrated your code above into the example, I still do not see anything being sent to the console , here is the code :

import com.codahale.metrics.ConsoleReporter
import com.codahale.metrics.Timer

import java.util.concurrent.TimeUnit;

object YourApplication {
  /** The application wide metrics registry. */
  val metricRegistry = {
    val registry = new com.codahale.metrics.MetricRegistry()
    ConsoleReporter.forRegistry(registry)
      .convertRatesTo(TimeUnit.SECONDS)
      .convertDurationsTo(TimeUnit.MILLISECONDS)
      .build()
      .start(1, TimeUnit.MINUTES)
    registry
  }
}

trait Instrumented extends nl.grons.metrics.scala.InstrumentedBuilder {
  val metricRegistry = YourApplication.metricRegistry

}

class Example(db: List[Int]) extends Instrumented {
  private[this] val loading = metrics.timer("loading")
  def loadStuff(): Seq[Int] = loading.time {
    val k = (1 to 100)
    k.toSeq
  }
}

object metric extends Instrumented {

  def main(args: Array[String]) {

    val o = new Example(List(1)).loadStuff()

  }
}

@scullxbones
Copy link
Collaborator

Sure, happy to help.

Note the timing of the reporter - .start(1, TimeUnit.MINUTES). I believe this will only write to console every minute. If your example doesn't run that long, you wouldn't see anything. You could try a shorter interval and see if that improves things.

@hhadind
Copy link
Author

hhadind commented Aug 4, 2015

uh... that was really a rocky mistake :) modified my code as such and it worked fine :

import com.codahale.metrics.ConsoleReporter
import com.codahale.metrics.Timer

import java.util.concurrent.TimeUnit;

object YourApplication {
  /** The application wide metrics registry. */
  val metricRegistry = {
    val registry = new com.codahale.metrics.MetricRegistry()
    ConsoleReporter.forRegistry(registry)
      .convertRatesTo(TimeUnit.SECONDS)
      .convertDurationsTo(TimeUnit.MILLISECONDS)
      .build()
      .start(1, TimeUnit.SECONDS) //changed the interval to seconds instead of minutes
    registry
  }
}

trait Instrumented extends nl.grons.metrics.scala.InstrumentedBuilder {

  val metricRegistry = YourApplication.metricRegistry

}

class Example(db: List[Int]) extends Instrumented {
  private[this] val loading = metrics.timer("loading")

  def loadStuff(): Seq[Int] = loading.time {
    val k = (1 to 100)
    k.toSeq
  }
}

object metric extends Instrumented {
  def wait5Seconds() {
    try {
      Thread.sleep(5 * 1000);
    } catch {
      case e: InterruptedException =>
    }

  }

  def main(args: Array[String]) {

    val o = new Example(List(1)).loadStuff()
    wait5Seconds //forced program to wait for 5 seconds 
  }
}

thanks again.

@erikvanoosten
Copy link
Owner

Hi @hhadind , can we close this issue?

@hhadind
Copy link
Author

hhadind commented Aug 7, 2015

sure thanks again.

@atz
Copy link

atz commented Sep 7, 2017

Very helpful. Recommend reopening this, since no Reporter examples are currently in the Manual.

@erikvanoosten
Copy link
Owner

erikvanoosten commented Sep 8, 2017

Hi @atz, a pull request would be amazing!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants