Code Monkey home page Code Monkey logo

Comments (8)

scullxbones avatar scullxbones commented on August 24, 2024

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
  }
}

from metrics-scala.

hhadind avatar hhadind commented on August 24, 2024

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()

  }
}

from metrics-scala.

scullxbones avatar scullxbones commented on August 24, 2024

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.

from metrics-scala.

hhadind avatar hhadind commented on August 24, 2024

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.

from metrics-scala.

erikvanoosten avatar erikvanoosten commented on August 24, 2024

Hi @hhadind , can we close this issue?

from metrics-scala.

hhadind avatar hhadind commented on August 24, 2024

sure thanks again.

from metrics-scala.

atz avatar atz commented on August 24, 2024

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

from metrics-scala.

erikvanoosten avatar erikvanoosten commented on August 24, 2024

Hi @atz, a pull request would be amazing!

from metrics-scala.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.