Unable to see metrics captured with spring metrics

2019-04-12 10:26发布

问题:

How can I do the equivalent of:

  @Override
  public void init(final ServletConfig config) throws ServletException {
    super.init(config);
    CsvReporter.enable(new File("/tmp/measurements"), 1, TimeUnit.MINUTES);
    GraphiteReporter.enable(1, TimeUnit.MINUTES, "my.host.name", 2003);
  }

  @Override
  protected void doGet(final HttpServletRequest req,
          final HttpServletResponse resp) throws ServletException,
          IOException {
    final TimerContext timerContext = Metrics.newMeter(CreateSessionServlet.class,"myservlet-meter", "requests", TimeUnit.SECONDS).time();
    try {
...
  } finally {
      timerContext.stop();
    }

with spring annotations and codahale metrics as mentioned here?

I thought it would be as simple as:

-annotating my servlet like this (I will need gauges and metering eventually):

@Timed
@Gauge
@Metered
@Override
protected void doGet(final HttpServletRequest req,
        final HttpServletResponse resp) throws ServletException, IOException {

-and updating my spring-servlet to enable the spring annotations as explained on the page mentioned above.

But when I use jconsole, I do not see in the MBeans section any additional entry for that servlet that I instrumented than for other servlets that do not use any annotation

So my two questions:

  1. Is there anything I am missing so that my web app actually sends metric data via JMX?

  2. If I want the code with spring annotations to start reporting to a CSV file or to graphite, what do I need to add?

Surprisingly I found no complete examples on the web or in the doc from codahale on this.

回答1:

Unfortunately metrics only is based on AOP so that it could be used only for public method