Въведение в показателите на Dropwizard

1. Въведение

Metrics е Java библиотека, която предоставя измервателни уреди за Java приложения.

Той има няколко модула и в тази статия ще разработим модул metrics-core, модул metrics-healthchecks, модул metrics-servlets и модул metrics-servlet, и ще скицираме останалите за ваша справка.

2. Модул метрика-ядро

2.1. Зависимости на Maven

За да използвате модула metrics-core , има нужда само от една зависимост, която трябва да бъде добавена към файла pom.xml :

 io.dropwizard.metrics metrics-core 3.1.2  

И можете да намерите най-новата му версия тук.

2.2. MetricRegistry

Най-просто казано, ние ще използваме класа MetricRegistry, за да регистрираме една или няколко метрики.

Можем да използваме един регистър за показатели за всички наши показатели, но ако искаме да използваме различни методи за отчитане за различни показатели, можем също да разделим нашите показатели в групи и да използваме различни регистри за показатели за всяка група.

Нека създадем MetricRegistry сега:

MetricRegistry metricRegistry = new MetricRegistry();

И тогава можем да регистрираме някои показатели с този MetricRegistry :

Meter meter1 = new Meter(); metricRegistry.register("meter1", meter1); Meter meter2 = metricRegistry.meter("meter2"); 

Има два основни начина за създаване на нова метрика: инстанциране на такава самостоятелно или получаване на такава от регистъра на метриките. Както можете да видите, ние използвахме и двете в примера по-горе, създаваме инстанция на обекта Meter „meter1“ и получаваме друг обект на Meter „meter2“, който е създаден от metricRegistry .

В метричния регистър всеки показател има уникално име, тъй като използвахме „meter1“ и „meter2“ като имена на показатели по-горе. MetricRegistry също предоставя набор от статични помощни методи, които да ни помогнат да създадем правилни имена на метрики:

String name1 = MetricRegistry.name(Filter.class, "request", "count"); String name2 = MetricRegistry.name("CustomFilter", "response", "count"); 

Ако трябва да управляваме набор от метрични регистри, можем да използваме клас SharedMetricRegistries , който е сингъл и безопасен за нишки. Можем да добавим метричен регистър към него, да извлечем този метричен регистър от него и да го премахнем:

SharedMetricRegistries.add("default", metricRegistry); MetricRegistry retrievedMetricRegistry = SharedMetricRegistries.getOrCreate("default"); SharedMetricRegistries.remove("default"); 

3. Метрични концепции

Основният модул на метриките предоставя няколко често използвани типа метрики: измервателен уред , измервателен уред , брояч , хистограма и таймер и репортер за извеждане на стойности на показателите .

3.1. Метър

А Meter измерва събития събития разчитат и оценка:

Meter meter = new Meter(); long initCount = meter.getCount(); assertThat(initCount, equalTo(0L)); meter.mark(); assertThat(meter.getCount(), equalTo(1L)); meter.mark(20); assertThat(meter.getCount(), equalTo(21L)); double meanRate = meter.getMeanRate(); double oneMinRate = meter.getOneMinuteRate(); double fiveMinRate = meter.getFiveMinuteRate(); double fifteenMinRate = meter.getFifteenMinuteRate(); 

Методът getCount () връща броя събития на събития, а методът mark () добавя 1 или n към броя събития на събития. Обектът Meter осигурява четири ставки, които представляват съответно средни ставки за целия живот на измервателния уред , за последната една минута, за последните пет минути и за последното тримесечие.

3.2. Габарит

Gauge е интерфейс, който просто се използва за връщане на определена стойност. Основният модул на метриките предоставя няколко негови реализации: RatioGauge , CachedGauge , DerivativeGauge и JmxAttributeGauge .

RatioGauge е абстрактен клас и измерва съотношението на една стойност към друга.

Нека да видим как да го използваме. Първо, ние прилагаме клас AttendanceRatioGauge :

public class AttendanceRatioGauge extends RatioGauge { private int attendanceCount; private int courseCount; @Override protected Ratio getRatio() { return Ratio.of(attendanceCount, courseCount); } // standard constructors } 

И след това го тестваме:

RatioGauge ratioGauge = new AttendanceRatioGauge(15, 20); assertThat(ratioGauge.getValue(), equalTo(0.75)); 

CachedGauge е друг абстрактен клас, който може да кешира стойност, следователно е доста полезно, когато стойностите са скъпи за изчисляване. За да го използваме, трябва да внедрим клас ActiveUsersGauge :

public class ActiveUsersGauge extends CachedGauge
    
      { @Override protected List loadValue() { return getActiveUserCount(); } private List getActiveUserCount() { List result = new ArrayList(); result.add(12L); return result; } // standard constructors }
    

След това го тестваме, за да видим дали работи както се очаква:

Gauge
    
      activeUsersGauge = new ActiveUsersGauge(15, TimeUnit.MINUTES); List expected = new ArrayList(); expected.add(12L); assertThat(activeUsersGauge.getValue(), equalTo(expected)); 
    

Зададохме времето на изтичане на кеша на 15 минути при създаване на екземпляр на ActiveUsersGauge .

DerivativeGauge също е абстрактен клас и ви позволява да извлечете стойност от друг Gauge като негова стойност.

Нека разгледаме един пример:

public class ActiveUserCountGauge extends DerivativeGauge
    
      { @Override protected Integer transform(List value) { return value.size(); } // standard constructors }
    

This Gauge derives its value from an ActiveUsersGauge, so we expect it to be the value from the base list's size:

Gauge
    
      activeUsersGauge = new ActiveUsersGauge(15, TimeUnit.MINUTES); Gauge activeUserCountGauge = new ActiveUserCountGauge(activeUsersGauge); assertThat(activeUserCountGauge.getValue(), equalTo(1)); 
    

JmxAttributeGauge is used when we need to access other libraries' metrics exposed via JMX.

3.3. Counter

The Counter is used for recording incrementations and decrementations:

Counter counter = new Counter(); long initCount = counter.getCount(); assertThat(initCount, equalTo(0L)); counter.inc(); assertThat(counter.getCount(), equalTo(1L)); counter.inc(11); assertThat(counter.getCount(), equalTo(12L)); counter.dec(); assertThat(counter.getCount(), equalTo(11L)); counter.dec(6); assertThat(counter.getCount(), equalTo(5L));

3.4. Histogram

Histogram is used for keeping track of a stream of Long values and it analyzes their statistical characteristics such as max, min, mean, median, standard deviation, 75th percentile and so on:

Histogram histogram = new Histogram(new UniformReservoir()); histogram.update(5); long count1 = histogram.getCount(); assertThat(count1, equalTo(1L)); Snapshot snapshot1 = histogram.getSnapshot(); assertThat(snapshot1.getValues().length, equalTo(1)); assertThat(snapshot1.getValues()[0], equalTo(5L)); histogram.update(20); long count2 = histogram.getCount(); assertThat(count2, equalTo(2L)); Snapshot snapshot2 = histogram.getSnapshot(); assertThat(snapshot2.getValues().length, equalTo(2)); assertThat(snapshot2.getValues()[1], equalTo(20L)); assertThat(snapshot2.getMax(), equalTo(20L)); assertThat(snapshot2.getMean(), equalTo(12.5)); assertEquals(10.6, snapshot2.getStdDev(), 0.1); assertThat(snapshot2.get75thPercentile(), equalTo(20.0)); assertThat(snapshot2.get999thPercentile(), equalTo(20.0)); 

Histogram samples the data by using reservoir sampling, and when we instantiate a Histogram object, we need to set its reservoir explicitly.

Reservoir is an interface and metrics-core provides four implementations of them: ExponentiallyDecayingReservoir, UniformReservoir, SlidingTimeWindowReservoir, SlidingWindowReservoir.

In the section above, we mentioned that a metric can also be created by MetricRegistry, besides using a constructor. When we use metricRegistry.histogram(), it returns a Histogram instance with ExponentiallyDecayingReservoir implementation.

3.5. Timer

Timer is used for keeping track of multiple timing durations which are represented by Context objects, and it also provides their statistical data:

Timer timer = new Timer(); Timer.Context context1 = timer.time(); TimeUnit.SECONDS.sleep(5); long elapsed1 = context1.stop(); assertEquals(5000000000L, elapsed1, 1000000); assertThat(timer.getCount(), equalTo(1L)); assertEquals(0.2, timer.getMeanRate(), 0.1); Timer.Context context2 = timer.time(); TimeUnit.SECONDS.sleep(2); context2.close(); assertThat(timer.getCount(), equalTo(2L)); assertEquals(0.3, timer.getMeanRate(), 0.1); 

3.6. Reporter

When we need to output our measurements, we can use Reporter. This is an interface, and the metrics-core module provides several implementations of it, such as ConsoleReporter, CsvReporter, Slf4jReporter, JmxReporter and so on.

Here we use ConsoleReporter as an example:

MetricRegistry metricRegistry = new MetricRegistry(); Meter meter = metricRegistry.meter("meter"); meter.mark(); meter.mark(200); Histogram histogram = metricRegistry.histogram("histogram"); histogram.update(12); histogram.update(17); Counter counter = metricRegistry.counter("counter"); counter.inc(); counter.dec(); ConsoleReporter reporter = ConsoleReporter.forRegistry(metricRegistry).build(); reporter.start(5, TimeUnit.MICROSECONDS); reporter.report(); 

Here is the sample output of the ConsoleReporter:

-- Histograms ------------------------------------------------------------------ histogram count = 2 min = 12 max = 17 mean = 14.50 stddev = 2.50 median = 17.00 75% <= 17.00 95% <= 17.00 98% <= 17.00 99% <= 17.00 99.9% <= 17.00 -- Meters ---------------------------------------------------------------------- meter count = 201 mean rate = 1756.87 events/second 1-minute rate = 0.00 events/second 5-minute rate = 0.00 events/second 15-minute rate = 0.00 events/second 

4. Module metrics-healthchecks

Metrics has an extension metrics-healthchecks module for dealing with health checks.

4.1. Maven Dependencies

To use the metrics-healthchecks module, we need to add this dependency to the pom.xml file:

 io.dropwizard.metrics metrics-healthchecks 3.1.2 

And you can find its latest version here.

4.2. Usage

First, we need several classes which are responsible for specific health check operations, and these classes must implement HealthCheck.

For example, we use DatabaseHealthCheck and UserCenterHealthCheck:

public class DatabaseHealthCheck extends HealthCheck { @Override protected Result check() throws Exception { return Result.healthy(); } } 
public class UserCenterHealthCheck extends HealthCheck { @Override protected Result check() throws Exception { return Result.healthy(); } } 

Then, we need a HealthCheckRegistry (which is just like MetricRegistry), and register the DatabaseHealthCheck and UserCenterHealthCheck with it:

HealthCheckRegistry healthCheckRegistry = new HealthCheckRegistry(); healthCheckRegistry.register("db", new DatabaseHealthCheck()); healthCheckRegistry.register("uc", new UserCenterHealthCheck()); assertThat(healthCheckRegistry.getNames().size(), equalTo(2)); 

We can also unregister the HealthCheck:

healthCheckRegistry.unregister("uc"); assertThat(healthCheckRegistry.getNames().size(), equalTo(1)); 

We can run all the HealthCheck instances:

Map results = healthCheckRegistry.runHealthChecks(); for (Map.Entry entry : results.entrySet()) { assertThat(entry.getValue().isHealthy(), equalTo(true)); } 

Finally, we can run a specific HealthCheck instance:

healthCheckRegistry.runHealthCheck("db"); 

5. Module metrics-servlets

Metrics provides us a handful of useful servlets which allow us to access metrics related data through HTTP requests.

5.1. Maven Dependencies

To use the metrics-servlets module, we need to add this dependency to the pom.xml file:

 io.dropwizard.metrics metrics-servlets 3.1.2 

And you can find its latest version here.

5.2. HealthCheckServlet Usage

HealthCheckServlet provides health check results. First, we need to create a ServletContextListener which exposes our HealthCheckRegistry:

public class MyHealthCheckServletContextListener extends HealthCheckServlet.ContextListener { public static HealthCheckRegistry HEALTH_CHECK_REGISTRY = new HealthCheckRegistry(); static { HEALTH_CHECK_REGISTRY.register("db", new DatabaseHealthCheck()); } @Override protected HealthCheckRegistry getHealthCheckRegistry() { return HEALTH_CHECK_REGISTRY; } } 

Then, we add both this listener and HealthCheckServlet into the web.xml file:

 com.baeldung.metrics.servlets.MyHealthCheckServletContextListener   healthCheck com.codahale.metrics.servlets.HealthCheckServlet   healthCheck /healthcheck 

Now we can start the web application, and send a GET request to “//localhost:8080/healthcheck” to get health check results. Its response should be like this:

{ "db": { "healthy": true } }

5.3. ThreadDumpServlet Usage

ThreadDumpServlet provides information about all live threads in the JVM, their states, their stack traces, and the state of any locks they may be waiting for.

If we want to use it, we simply need to add these into the web.xml file:

 threadDump com.codahale.metrics.servlets.ThreadDumpServlet   threadDump /threaddump 

Thread dump data will be available at “//localhost:8080/threaddump”.

5.4. PingServlet Usage

PingServlet can be used to test if the application is running. We add these into the web.xml file:

 ping com.codahale.metrics.servlets.PingServlet   ping /ping 

And then send a GET request to “//localhost:8080/ping”. The response's status code is 200 and its content is “pong”.

5.5. MetricsServlet Usage

MetricsServlet provides metrics data. First, we need to create a ServletContextListener which exposes our MetricRegistry:

public class MyMetricsServletContextListener extends MetricsServlet.ContextListener { private static MetricRegistry METRIC_REGISTRY = new MetricRegistry(); static { Counter counter = METRIC_REGISTRY.counter("m01-counter"); counter.inc(); Histogram histogram = METRIC_REGISTRY.histogram("m02-histogram"); histogram.update(5); histogram.update(20); histogram.update(100); } @Override protected MetricRegistry getMetricRegistry() { return METRIC_REGISTRY; } } 

Both this listener and MetricsServlet need to be added into web.xml:

 com.codahale.metrics.servlets.MyMetricsServletContextListener   metrics com.codahale.metrics.servlets.MetricsServlet   metrics /metrics 

This will be exposed in our web application at “//localhost:8080/metrics”. Its response should contain various metrics data:

{ "version": "3.0.0", "gauges": {}, "counters": { "m01-counter": { "count": 1 } }, "histograms": { "m02-histogram": { "count": 3, "max": 100, "mean": 41.66666666666666, "min": 5, "p50": 20, "p75": 100, "p95": 100, "p98": 100, "p99": 100, "p999": 100, "stddev": 41.69998667732268 } }, "meters": {}, "timers": {} } 

5.6. AdminServlet Usage

AdminServlet aggregates HealthCheckServlet, ThreadDumpServlet, MetricsServlet, and PingServlet.

Let's add these into the web.xml:

 admin com.codahale.metrics.servlets.AdminServlet   admin /admin/* 

It can now be accessed at “//localhost:8080/admin”. We will get a page containing four links, one for each of those four servlets.

Note that, if we want to do health check and access metrics data, those two listeners are still needed.

6. Module metrics-servlet

The metrics-servlet module provides a Filter which has several metrics: meters for status codes, a counter for the number of active requests, and a timer for request duration.

6.1. Maven Dependencies

To use this module, let's first add the dependency into the pom.xml:

 io.dropwizard.metrics metrics-servlet 3.1.2 

And you can find its latest version here.

6.2. Usage

To use it, we need to create a ServletContextListener which exposes our MetricRegistry to the InstrumentedFilter:

public class MyInstrumentedFilterContextListener extends InstrumentedFilterContextListener { public static MetricRegistry REGISTRY = new MetricRegistry(); @Override protected MetricRegistry getMetricRegistry() { return REGISTRY; } } 

Then, we add these into the web.xml:

  com.baeldung.metrics.servlet.MyInstrumentedFilterContextListener    instrumentFilter  com.codahale.metrics.servlet.InstrumentedFilter    instrumentFilter /* 

Now the InstrumentedFilter can work. If we want to access its metrics data, we can do it through its MetricRegistryREGISTRY.

7. Other Modules

Except for the modules we introduced above, Metrics has some other modules for different purposes:

  • metrics-jvm: provides several useful metrics for instrumenting JVM internals
  • metrics-ehcache: provides InstrumentedEhcache, a decorator for Ehcache caches
  • metrics-httpclient: provides classes for instrumenting Apache HttpClient (4.x version)
  • metrics-log4j: provides InstrumentedAppender, a Log4j Appender implementation for log4j 1.x which records the rate of logged events by their logging level
  • metrics-log4j2: is similar to metrics-log4j, just for log4j 2.x
  • metrics-logback: provides InstrumentedAppender, a Logback Appender implementation which records the rate of logged events by their logging level
  • metrics-json : предоставя HealthCheckModule и MetricsModule за Jackson

Нещо повече, освен тези основни модули на проекта, някои други библиотеки на трети страни осигуряват интеграция с други библиотеки и рамки.

8. Заключение

Инструментирането на приложения е често срещано изискване, затова в тази статия въведохме Metrics, надявайки се, че това може да ви помогне да решите проблема си.

Както винаги, пълният изходен код за примера е достъпен в GitHub.