Skip to content

Commit 3ecace4

Browse files
checkettsbrian-brazil
authored andcommitted
Add metrics registration and 'child' explanation to readme (#188)
Add metrics registration explanation, add TOC
1 parent 0b14621 commit 3ecace4

File tree

1 file changed

+81
-6
lines changed

1 file changed

+81
-6
lines changed

README.md

Lines changed: 81 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,31 @@ It supports Java, Clojure, Scala, JRuby, and anything else that runs on the JVM.
33

44
[![Build Status](https://travis-ci.org/prometheus/client_java.png?branch=master)](https://travis-ci.org/prometheus/client_java)
55

6+
Table of Contents
7+
=================
8+
9+
* [Using](#using)
10+
* [Assets](#assets)
11+
* [Javadocs](#javadocs)
12+
* [Instrumenting](#instrumenting)
13+
* [Counter](#counter)
14+
* [Gauge](#gauge)
15+
* [Summary](#summary)
16+
* [Histogram](#histogram)
17+
* [Labels](#labels)
18+
* [Registering Metrics](#registering-metrics)
19+
* [Included Collectors](#included-collectors)
20+
* [Logging](#logging)
21+
* [Caches](#caches)
22+
* [Hibernate](#hibernate)
23+
* [Exporting](#exporting)
24+
* [HTTP](#http)
25+
* [Exporting to a Pushgateway](#exporting-to-a-pushgateway)
26+
* [Bridges](#bridges)
27+
* [Graphite](#graphite)
28+
* [Custom Collectors](#custom-collectors)
29+
* [Contact](#contact)
30+
631
## Using
732
### Assets
833
If you use Maven, you can simply reference the assets below. The latest
@@ -42,7 +67,6 @@ There are canonical examples defined in the class definition Javadoc of the clie
4267
Documentation can be found at the [Java Client
4368
Github Project Page](http://prometheus.github.io/client_java).
4469

45-
4670
## Instrumenting
4771

4872
Four types of metrics are offered: Counter, Gauge, Summary and Histogram.
@@ -205,7 +229,7 @@ Taking a counter as an example:
205229
```java
206230
class YourClass {
207231
static final Counter requests = Counter.build()
208-
.name("requests_total").help("Total requests.")
232+
.name("my_library_requests_total").help("Total requests.")
209233
.labelNames("method").register();
210234

211235
void processGetRequest() {
@@ -215,7 +239,58 @@ class YourClass {
215239
}
216240
```
217241

218-
### Included Collectors
242+
### Registering Metrics
243+
244+
The best way to register a metric is via a `static final` class variable as is common with loggers.
245+
246+
```java
247+
static final Counter requests = Counter.build()
248+
.name("my_library_requests_total").help("Total requests.").labelNames("path").register();
249+
```
250+
251+
Using the default registry with variables that are `static` is ideal since registering a metric with the same name
252+
is not allowed and the default registry is also itself static. You can think of registering a metric, more like
253+
registering a definition (as in the `TYPE` and `HELP` sections). The metric 'definition' internally holds the samples
254+
that are reported and pulled out by Prometheus. Here is an example of registering a metric that has no labels.
255+
256+
```java
257+
class YourClass {
258+
static final Gauge activeTransactions = Gauge.build()
259+
.name("my_library_transactions_active")
260+
.help("Active transactions.")
261+
.register();
262+
263+
void processThatCalculates(String key) {
264+
activeTransactions.inc();
265+
try {
266+
// Perform work.
267+
} finally{
268+
activeTransactions.dec();
269+
}
270+
}
271+
}
272+
```
273+
274+
To create timeseries with labels, include `labelNames()` with the builder. The `labels()` method looks up or creates
275+
the corresponding labelled timeseries. You might also consider storing the labelled timeseries as an instance variable if it is
276+
appropriate. It is thread safe and can be used multiple times, which can help performance.
277+
278+
279+
```java
280+
class YourClass {
281+
static final Counter calculationsCounter = Counter.build()
282+
.name("my_library_calculations_total").help("Total calls.")
283+
.labelNames("key").register();
284+
285+
void processThatCalculates(String key) {
286+
calculationsCounter.labels(key).inc();
287+
// Run calculations.
288+
}
289+
}
290+
```
291+
292+
293+
## Included Collectors
219294

220295
The Java client includes collectors for garbage collection, memory pools, JMX, classloading, and thread counts.
221296
These can be added individually or just use the `DefaultExports` to conveniently register them.
@@ -224,7 +299,7 @@ These can be added individually or just use the `DefaultExports` to conveniently
224299
DefaultExports.initialize();
225300
```
226301

227-
####Logging
302+
###Logging
228303

229304
There are logging collectors for log4j, log4j2 and logback.
230305

@@ -274,7 +349,7 @@ To register the log4j2 collector at root level:
274349
</Configuration>
275350
```
276351

277-
#### Caches
352+
### Caches
278353

279354
To register the Guava cache collector, be certain to add `recordStats()` when building
280355
the cache and adding it to the registered collector.
@@ -296,7 +371,7 @@ Cache<String, String> cache = Caffeine.newBuilder().recordStats().build();
296371
cacheMetrics.addCache("myCacheLabel", cache);
297372
```
298373

299-
#### Hibernate
374+
### Hibernate
300375

301376
There is a collector for Hibernate which allows to collect metrics from one or more
302377
`SessionFactory` instances.

0 commit comments

Comments
 (0)