Skip to content

Commit b2bee76

Browse files
committed
Remove metrics cache since 4.x does it for us. Clean up and add to metrics post
1 parent d1e55fc commit b2bee76

File tree

2 files changed

+24
-29
lines changed

2 files changed

+24
-29
lines changed

stubbornjava-common/src/main/java/com/stubbornjava/common/Metrics.java

Lines changed: 3 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,12 @@
11
package com.stubbornjava.common;
22

3-
import java.util.Map;
4-
import java.util.concurrent.ConcurrentHashMap;
53
import java.util.concurrent.TimeUnit;
64

75
import org.slf4j.Logger;
86
import org.slf4j.LoggerFactory;
97

108
import com.amazonaws.util.EC2MetadataUtils;
119
import com.codahale.metrics.Meter;
12-
import com.codahale.metrics.Metric;
1310
import com.codahale.metrics.MetricFilter;
1411
import com.codahale.metrics.MetricRegistry;
1512
import com.codahale.metrics.Timer;
@@ -21,15 +18,10 @@
2118

2219
import ch.qos.logback.classic.LoggerContext;
2320
import okhttp3.OkHttpClient;
21+
2422
// {{start:metrics}}
2523
public class Metrics {
2624
private static final Logger log = LoggerFactory.getLogger(Metrics.class);
27-
28-
/*
29-
* Use a concurrent map to cache metrics we generate on the fly.
30-
* For example we generate status code metrics on the fly.
31-
*/
32-
private static final Map<String, Metric> metricCache = new ConcurrentHashMap<>();
3325
private static final MetricRegistry registry;
3426
static {
3527
registry = new MetricRegistry();
@@ -70,21 +62,11 @@ public static MetricRegistry registry() {
7062
}
7163

7264
public static Timer timer(String first, String... keys) {
73-
String key = MetricRegistry.name(first, keys);
74-
return (Timer) metricCache.computeIfAbsent(key, (String metricName) -> {
75-
Timer metric = new Timer();
76-
registry.register(metricName, metric);
77-
return metric;
78-
});
65+
return registry.timer(MetricRegistry.name(first, keys));
7966
}
8067

8168
public static Meter meter(String first, String... keys) {
82-
String key = MetricRegistry.name(first, keys);
83-
return (Meter) metricCache.computeIfAbsent(key, (String metricName) -> {
84-
Meter metric = new Meter();
85-
registry.register(metricName, metric);
86-
return metric;
87-
});
69+
return registry.meter(MetricRegistry.name(first, keys));
8870
}
8971

9072
private static String metricPrefix(String app) {

stubbornjava-webapp/ui/src/posts/monitoring-your-jvm-with-dropwizard-metrics.hbs

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,17 @@
1-
<p>We briefly mentioned <a href="http://metrics.dropwizard.io/">Dropwizard Metrics</a> in our <a href="/posts/lightweight-embedded-java-rest-server-without-a-framework#routing-server">Simple Embedded Java REST server</a> example. Let's see what else it can do.</p>
1+
<div class="anchored-md">
2+
{{#assign "markdown"}}
3+
4+
We briefly mentioned [Dropwizard Metrics](http://metrics.dropwizard.io/) in our [Simple Embedded Java REST server](/posts/lightweight-embedded-java-rest-server-without-a-framework#routing-server) example. Let's see what some of the built in metrics such as `GarbageCollectorMetricSet`, `CachedThreadStatesGaugeSet`, `MemoryUsageGaugeSet`, and a Logback `InstrumentedAppender` can offer us.
5+
6+
## Metrics Implementation
7+
[Live stats for this server.](/dev/metrics) Once we create our `MetricRegistry` we can register some built in metric sets. This also also where we can set up our metrics reporters where we can report and graph metrics to vairous systems such as [hosted grafana](https://grafana.com/cloud/grafana) used with [hosted metrics](https://grafana.com/cloud/metrics) or many self hosted solutions including but not limited to
8+
[Grafana](https://grafana.com), [InfluxDB](https://www.influxdata.com/), [Graphite](https://graphiteapp.org/), and [Prometheus](https://prometheus.io/).
29

3-
<h2 class="anchored">Metrics Implementation</h2>
4-
<p>Live stats for this server. <a rel="nofollow" href="/dev/metrics">Metrics</a></p>
510
{{> templates/src/widgets/code/code-snippet file=metrics section=metrics.sections.metrics}}
611

7-
<h2 class="anchored">Garbage Collector Stats</h2>
12+
## Garbage Collector Stats
13+
Garbage collector stats from `GarbageCollectorMetricSet` provide us with metrics that tell us how frequently we mark and sweep or scavenge as well as how long they take to run. Monitoring these over time can help identify unecessary object creation or anytime you introduce new code that incurs much higher garbage collection costs.
14+
815
<pre class="line-numbers"><code class="language-json">"gc.PS-MarkSweep.count": {
916
"value": 2
1017
},
@@ -18,7 +25,9 @@
1825
"value": 102
1926
}</code></pre>
2027

21-
<h2 class="anchored">Thread Stats</h2>
28+
## Thread State Stats
29+
The `CachedThreadStatesGaugeSet` provides us with snapshots of how many threads we have in which states. It's best to use the cached version since this requires traversing all threads and can be a little more expensive than some other metrics. These stats help identify whether or not the application is utilizing its threads well. It's not uncommon to see microservices these days running on 2 core machines with 200-400 threads. Get your services under control people.
30+
2231
<pre class="line-numbers"><code class="language-json">"threads.blocked.count": {
2332
"value": 0
2433
},
@@ -50,9 +59,9 @@
5059
"value": 20
5160
}</code></pre>
5261

62+
## Logging Level Hit Counts
63+
The `InstrumentedAppender` is a great way to monitor your logging levels over time. This can help track if you have increasing / decreasing errors over time or notice any bursts in one specific level. For instance If you log validation errors at the `INFO` level and notice a very large spike in `INFO` logs it might be worthwhile to check out what is going on.
5364

54-
<h2 class="anchored">Logging Level Hit Counts</h2>
55-
<p>Great for monitoring errors over time and early alerting if issues arise.</p>
5665
<pre class="line-numbers"><code class="language-json">"ch.qos.logback.core.Appender.all": {
5766
"count": 383,
5867
"m15_rate": 34.36128532787729,
@@ -102,8 +111,9 @@
102111
"units": "events/minute"
103112
}</code></pre>
104113

114+
## JVM Memory Stats
115+
The `MemoryUsageGaugeSet` gives you many useful metrics around the JVM's memory internals.
105116

106-
<h2 class="anchored">JVM Memory Stats</h2>
107117
<pre class="line-numbers"><code class="language-json">"memory.heap.committed": {
108118
"value": 294649856
109119
},
@@ -237,3 +247,6 @@
237247
"value": 191603408
238248
}</code></pre>
239249

250+
{{/assign}}
251+
{{md markdown}}
252+
</div>

0 commit comments

Comments
 (0)