metrics examples
This commit is contained in:
@@ -0,0 +1,17 @@
|
||||
package com.baeldung.metrics.core;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import com.codahale.metrics.DerivativeGauge;
|
||||
import com.codahale.metrics.Gauge;
|
||||
|
||||
public class ActiveUserCountGauge extends DerivativeGauge<List<Long>, Integer> {
|
||||
public ActiveUserCountGauge(Gauge<List<Long>> base) {
|
||||
super(base);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Integer transform(List<Long> value) {
|
||||
return value.size();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
package com.baeldung.metrics.core;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import com.codahale.metrics.CachedGauge;
|
||||
|
||||
public class ActiveUsersGauge extends CachedGauge<List<Long>> {
|
||||
public ActiveUsersGauge(long timeout, TimeUnit timeoutUnit) {
|
||||
super(timeout, timeoutUnit);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected List<Long> loadValue() {
|
||||
return getActiveUserCount();
|
||||
}
|
||||
|
||||
private List<Long> getActiveUserCount() {
|
||||
// mock reading from database and count the active users, return a fixed value
|
||||
List<Long> result = new ArrayList<Long>();
|
||||
result.add(12L);
|
||||
return result;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
package com.baeldung.metrics.core;
|
||||
|
||||
import com.codahale.metrics.RatioGauge;
|
||||
|
||||
public class AttendanceRatioGauge extends RatioGauge {
|
||||
private int attendanceCount;
|
||||
private int courseCount;
|
||||
|
||||
public AttendanceRatioGauge(int attendanceCount, int courseCount) {
|
||||
this.attendanceCount = attendanceCount;
|
||||
this.courseCount = courseCount;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Ratio getRatio() {
|
||||
return Ratio.of(attendanceCount, courseCount);
|
||||
}
|
||||
}
|
||||
@@ -1,4 +0,0 @@
|
||||
package com.baeldung.metrics.core;
|
||||
|
||||
public class MetricsDemo {
|
||||
}
|
||||
@@ -1,6 +0,0 @@
|
||||
package com.baeldung.metrics.core;
|
||||
|
||||
public class MetricsRegistryDemo {
|
||||
public static void main(String[] args) {
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
package com.baeldung.metrics.healthchecks;
|
||||
|
||||
import com.codahale.metrics.health.HealthCheck;
|
||||
|
||||
public class DatabaseHealthCheck extends HealthCheck {
|
||||
@Override
|
||||
protected Result check() throws Exception {
|
||||
return HealthCheck.Result.healthy();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
package com.baeldung.metrics.healthchecks;
|
||||
|
||||
import com.codahale.metrics.health.HealthCheck;
|
||||
|
||||
public class UserCenterHealthCheck extends HealthCheck {
|
||||
@Override
|
||||
protected Result check() throws Exception {
|
||||
return Result.healthy();
|
||||
}
|
||||
}
|
||||
+13
@@ -0,0 +1,13 @@
|
||||
package com.baeldung.metrics.servlet;
|
||||
|
||||
import com.codahale.metrics.MetricRegistry;
|
||||
import com.codahale.metrics.servlet.InstrumentedFilterContextListener;
|
||||
|
||||
public class MyInstrumentedFilterContextListener extends InstrumentedFilterContextListener {
|
||||
public static final MetricRegistry REGISTRY = new MetricRegistry();
|
||||
|
||||
@Override
|
||||
protected MetricRegistry getMetricRegistry() {
|
||||
return REGISTRY;
|
||||
}
|
||||
}
|
||||
+18
@@ -0,0 +1,18 @@
|
||||
package com.baeldung.metrics.servlets;
|
||||
|
||||
import com.baeldung.metrics.healthchecks.DatabaseHealthCheck;
|
||||
import com.codahale.metrics.health.HealthCheckRegistry;
|
||||
import com.codahale.metrics.servlets.HealthCheckServlet;
|
||||
|
||||
public class MyHealthCheckServletContextListener extends HealthCheckServlet.ContextListener {
|
||||
public static final HealthCheckRegistry HEALTH_CHECK_REGISTRY = new HealthCheckRegistry();
|
||||
|
||||
static {
|
||||
HEALTH_CHECK_REGISTRY.register("db", new DatabaseHealthCheck());
|
||||
}
|
||||
|
||||
@Override
|
||||
protected HealthCheckRegistry getHealthCheckRegistry() {
|
||||
return HEALTH_CHECK_REGISTRY;
|
||||
}
|
||||
}
|
||||
+25
@@ -0,0 +1,25 @@
|
||||
package com.baeldung.metrics.servlets;
|
||||
|
||||
import com.codahale.metrics.Counter;
|
||||
import com.codahale.metrics.Histogram;
|
||||
import com.codahale.metrics.MetricRegistry;
|
||||
import com.codahale.metrics.servlets.MetricsServlet;
|
||||
|
||||
public class MyMetricsServletContextListener extends MetricsServlet.ContextListener {
|
||||
private static final 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;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user