Skip to content

Commit

Permalink
Add metrics registration and 'child' explanation to readme (#188)
Browse files Browse the repository at this point in the history
Add metrics registration explanation, add TOC
  • Loading branch information
checketts authored and brian-brazil committed Feb 19, 2017
1 parent 0b14621 commit 3ecace4
Showing 1 changed file with 81 additions and 6 deletions.
87 changes: 81 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,31 @@ It supports Java, Clojure, Scala, JRuby, and anything else that runs on the JVM.

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

Table of Contents
=================

* [Using](#using)
* [Assets](#assets)
* [Javadocs](#javadocs)
* [Instrumenting](#instrumenting)
* [Counter](#counter)
* [Gauge](#gauge)
* [Summary](#summary)
* [Histogram](#histogram)
* [Labels](#labels)
* [Registering Metrics](#registering-metrics)
* [Included Collectors](#included-collectors)
* [Logging](#logging)
* [Caches](#caches)
* [Hibernate](#hibernate)
* [Exporting](#exporting)
* [HTTP](#http)
* [Exporting to a Pushgateway](#exporting-to-a-pushgateway)
* [Bridges](#bridges)
* [Graphite](#graphite)
* [Custom Collectors](#custom-collectors)
* [Contact](#contact)

## Using
### Assets
If you use Maven, you can simply reference the assets below. The latest
Expand Down Expand Up @@ -42,7 +67,6 @@ There are canonical examples defined in the class definition Javadoc of the clie
Documentation can be found at the [Java Client
Github Project Page](http://prometheus.github.io/client_java).


## Instrumenting

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

void processGetRequest() {
Expand All @@ -215,7 +239,58 @@ class YourClass {
}
```

### Included Collectors
### Registering Metrics

The best way to register a metric is via a `static final` class variable as is common with loggers.

```java
static final Counter requests = Counter.build()
.name("my_library_requests_total").help("Total requests.").labelNames("path").register();
```

Using the default registry with variables that are `static` is ideal since registering a metric with the same name
is not allowed and the default registry is also itself static. You can think of registering a metric, more like
registering a definition (as in the `TYPE` and `HELP` sections). The metric 'definition' internally holds the samples
that are reported and pulled out by Prometheus. Here is an example of registering a metric that has no labels.

```java
class YourClass {
static final Gauge activeTransactions = Gauge.build()
.name("my_library_transactions_active")
.help("Active transactions.")
.register();

void processThatCalculates(String key) {
activeTransactions.inc();
try {
// Perform work.
} finally{
activeTransactions.dec();
}
}
}
```

To create timeseries with labels, include `labelNames()` with the builder. The `labels()` method looks up or creates
the corresponding labelled timeseries. You might also consider storing the labelled timeseries as an instance variable if it is
appropriate. It is thread safe and can be used multiple times, which can help performance.


```java
class YourClass {
static final Counter calculationsCounter = Counter.build()
.name("my_library_calculations_total").help("Total calls.")
.labelNames("key").register();

void processThatCalculates(String key) {
calculationsCounter.labels(key).inc();
// Run calculations.
}
}
```


## Included Collectors

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

####Logging
###Logging

There are logging collectors for log4j, log4j2 and logback.

Expand Down Expand Up @@ -274,7 +349,7 @@ To register the log4j2 collector at root level:
</Configuration>
```

#### Caches
### Caches

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

#### Hibernate
### Hibernate

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

0 comments on commit 3ecace4

Please sign in to comment.