Skip to content

Commit 8397856

Browse files
Update README.MD with dropwizard based java client for custom metrics
1 parent a69c74f commit 8397856

File tree

10 files changed

+86
-32
lines changed

10 files changed

+86
-32
lines changed

README.md

Lines changed: 56 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,9 @@ Let's say you want to make use of the *servlet filter* feature, then you need to
4848

4949
This feature only depends on the servlet API which you have included in your POM anyhow. You can find more information about the *servlet filter* feature (like e.g. how to adjust the web.xml) in the [Wiki](https://github.com/SAP/cf-java-logging-support/wiki/Instrumenting-Servlets).
5050

51-
If you want to use the `custom metrics` `spring-boot client`, just define the following dependency:
51+
If you want to use the `custom metrics`, just define the following dependency:
52+
53+
* `spring-boot client`:
5254

5355
``` xml
5456

@@ -59,6 +61,18 @@ If you want to use the `custom metrics` `spring-boot client`, just define the fo
5961
</dependency>
6062
```
6163

64+
* `java client`:
65+
66+
``` xml
67+
68+
<dependency>
69+
<groupId>com.sap.hcp.cf.logging</groupId>
70+
<artifactId>cf-custom-metrics-clients-java</artifactId>
71+
<version>${cf-logging-version}</version>
72+
</dependency>
73+
```
74+
75+
6276
## Implementation variants and logging configurations
6377

6478
The *core* feature (on which all other features rely) is just using the `org.slf4j` API, but to actually get logs written, you need to pick an implementation feature. As stated above, we have two implementations:
@@ -160,7 +174,7 @@ Here are the minimal configurations you'd need:
160174
With the custom metrics clients you can send metrics defined inside your code. If you choose not to use one of these clients, you can still directly push the metrics using the REST API. Once send the metrics can be consumed in Kibana.
161175
To use the clients you'd need:
162176

163-
*Using spring-boot client in Spring Boot 2 application:*
177+
1. *Using spring-boot client in Spring Boot 2 application:*
164178

165179
``` xml
166180
<dependency>
@@ -221,12 +235,50 @@ public class DemoController {
221235
```
222236
In the example above, three custom metrics are defined and used. The metrics are `Counter`, `LongTaskTimer` and `Gauge`.
223237

238+
2. *Using java client in Java application:*
239+
240+
``` xml
241+
<dependency>
242+
<groupId>com.sap.hcp.cf.logging</groupId>
243+
<artifactId>cf-custom-metrics-clients-java</artifactId>
244+
<version>${cf-logging-version}</version>
245+
</dependency>
246+
```
247+
248+
The java client uses [Dropwizard](https://metrics.dropwizard.io) which allows to define all kind of metrics supports by Dropwizar. The following metrics are available: com.codahale.metrics.Gauge, com.codahale.metrics.Counter, com.codahale.metrics.Histogram, com.codahale.metrics.Meter and com.codahale.metrics.Timer. More information about the [metric types and their usage](https://metrics.dropwizard.io/4.0.0/getting-started.html). Define your custom metrics and iterate with them:
249+
250+
``` java
251+
import java.io.IOException;
252+
253+
import javax.servlet.ServletException;
254+
import javax.servlet.http.HttpServlet;
255+
import javax.servlet.http.HttpServletRequest;
256+
import javax.servlet.http.HttpServletResponse;
257+
258+
import com.codahale.metrics.Counter;
259+
import com.codahale.metrics.Meter;
260+
import com.sap.cloud.cf.monitoring.java.CustomMetricRegistry;
261+
262+
public class CustomMetricsServlet extends HttpServlet {
263+
private static Counter counter = CustomMetricRegistry.get().counter("custom.metric.request.count");
264+
private static Meter meter = CustomMetricRegistry.get().meter("custom.metric.request.meter");
265+
266+
@Override
267+
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
268+
counter.inc(3);
269+
meter.mark();
270+
response.getWriter().println("<p>Greetings from Custom Metrics</p>");
271+
}
272+
}
273+
```
274+
224275
## Custom metrics client configurations
225276

226277
This client library supports the following configurations regarding sending custom metrics:
227278
* `interval`: the interval for sending metrics, in millis. **Default value: `60000`**
228279
* `enabled`: enables or disables the sending of metrics. **Default value: `true`**
229280
* `metrics`: array of whitelisted metric names. Only mentioned metrics would be processed and sent. If it is an empty array all metrics are being sent. **Default value: `[]`**
281+
* `metricQuantiles`: enables or disables the sending of metric's [quantiles](https://en.wikipedia.org/wiki/Quantile) like median, 95th percentile, 99th percentile. Spring-boot client does not support this configuration. **Default value: `false`**
230282

231283
Configurations are read from environment variable named `CUSTOM_METRICS`. To change the default values, you should override the environment variable with your custom values. Example:
232284

@@ -237,7 +289,8 @@ This client library supports the following configurations regarding sending cust
237289
"metrics": [
238290
"my.whitelist.metric.1",
239291
"my.whitelist.metric.2"
240-
]
292+
],
293+
"metricQuantiles":true
241294
}
242295
```
243296

cf-java-monitoring-custom-metrics-clients/cf-custom-metrics-clients-java/pom.xml

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,19 @@
22
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
33
<modelVersion>4.0.0</modelVersion>
44

5-
<parent>
6-
<groupId>com.sap.hcp.cf.logging</groupId>
7-
<artifactId>cf-java-monitoring-custom-metrics-clients</artifactId>
8-
<version>3.0.0</version>
9-
</parent>
5+
<parent>
6+
<groupId>com.sap.hcp.cf.logging</groupId>
7+
<artifactId>cf-java-monitoring-custom-metrics-clients</artifactId>
8+
<version>3.0.0</version>
9+
</parent>
1010

1111
<artifactId>cf-custom-metrics-clients-java</artifactId>
1212
<packaging>jar</packaging>
1313
<name>cf-java-monitoring-custom-metrics-clients-dropwizard-java</name>
1414

1515
<properties>
1616
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
17-
<dropwizard.version>3.2.6</dropwizard.version>
17+
<dropwizard.version>3.2.6</dropwizard.version>
1818
</properties>
1919

2020
<build>
@@ -31,11 +31,11 @@
3131
</build>
3232

3333
<dependencies>
34-
<dependency>
35-
<groupId>com.sap.hcp.cf.logging</groupId>
36-
<artifactId>cf-custom-metrics-clients-core</artifactId>
37-
<version>${project.version}</version>
38-
</dependency>
34+
<dependency>
35+
<groupId>com.sap.hcp.cf.logging</groupId>
36+
<artifactId>cf-custom-metrics-clients-core</artifactId>
37+
<version>${project.version}</version>
38+
</dependency>
3939
<dependency>
4040
<groupId>io.dropwizard.metrics</groupId>
4141
<artifactId>metrics-core</artifactId>

cf-java-monitoring-custom-metrics-clients/cf-custom-metrics-clients-java/src/main/java/com/sap/cloud/cf/monitoring/java/CustomMetricRegistry.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,7 @@ private CFConfigurationProvider getConfigProvider() {
5353

5454
private void initializeAndStartReporter() {
5555
if (configProvider == null || customMetricsConfig == null || !customMetricsConfig.isEnabled()) {
56-
LOGGER.error(
57-
"Custom Metrics reporter will not start since required ENVs are missing or environment variable 'enable' is false.");
56+
LOGGER.error("Custom Metrics reporter will not start since required ENVs are missing or environment variable 'enable' is false.");
5857
return;
5958
}
6059
MonitoringClient client = new MonitoringClientBuilder().setConfigurationProvider(configProvider).create();

cf-java-monitoring-custom-metrics-clients/cf-custom-metrics-clients-java/src/main/java/com/sap/cloud/cf/monitoring/java/CustomMetricsReporter.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,16 +45,17 @@ public boolean matches(String name, com.codahale.metrics.Metric metric) {
4545
}
4646

4747
public CustomMetricsReporter(MetricRegistry registry, MonitoringClient client,
48-
CustomMetricsConfiguration customMetricsConfig) {
48+
CustomMetricsConfiguration customMetricsConfig) {
4949
super(registry, "custom-metrics-reporter", getFilter(customMetricsConfig.getMetrics()), TimeUnit.SECONDS,
50-
TimeUnit.MILLISECONDS);
50+
TimeUnit.MILLISECONDS);
5151
this.client = client;
5252
this.customMetricsConfig = customMetricsConfig;
5353
}
5454

5555
@Override
5656
public void report(SortedMap<String, Gauge> gauges, SortedMap<String, Counter> counters,
57-
SortedMap<String, Histogram> histograms, SortedMap<String, Meter> meters, SortedMap<String, Timer> timers) {
57+
SortedMap<String, Histogram> histograms, SortedMap<String, Meter> meters,
58+
SortedMap<String, Timer> timers) {
5859
try {
5960
List<Metric> convertedMetrics = convert(gauges, counters, histograms, meters, timers);
6061
if (convertedMetrics.isEmpty()) {
@@ -68,7 +69,8 @@ public void report(SortedMap<String, Gauge> gauges, SortedMap<String, Counter> c
6869
}
6970

7071
private List<Metric> convert(SortedMap<String, Gauge> gauges, SortedMap<String, Counter> counters,
71-
SortedMap<String, Histogram> histograms, SortedMap<String, Meter> meters, SortedMap<String, Timer> timers) {
72+
SortedMap<String, Histogram> histograms, SortedMap<String, Meter> meters,
73+
SortedMap<String, Timer> timers) {
7274
List<Metric> result = new ArrayList<>();
7375
long timestamp = System.currentTimeMillis();
7476
boolean metricQuantiles = customMetricsConfig.metricQuantiles();

cf-java-monitoring-custom-metrics-clients/cf-custom-metrics-clients-java/src/main/java/com/sap/cloud/cf/monitoring/java/converter/CounterConverter.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ public final class CounterConverter extends MetricConverter<Counter> {
1212
@Override
1313
List<Metric> convertMetricEntry(Entry<String, Counter> metricEntry, long timestamp) {
1414
Counter counter = metricEntry.getValue();
15-
return Arrays.asList(
16-
buildCustomMetric(metricEntry.getKey() + ".count", counter.getCount(), MetricType.COUNTER, timestamp));
15+
return Arrays.asList(buildCustomMetric(metricEntry.getKey() + ".count", counter.getCount(), MetricType.COUNTER,
16+
timestamp));
1717
}
18-
}
18+
}

cf-java-monitoring-custom-metrics-clients/cf-custom-metrics-clients-java/src/main/java/com/sap/cloud/cf/monitoring/java/converter/GaugeConverter.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,11 @@ List<Metric> convertMetricEntry(Entry<String, Gauge> metricEntry, long timestamp
1515
Object gaugeValue = metricEntry.getValue().getValue();
1616
if (gaugeValue instanceof Number) {
1717
Number number = (Number) gaugeValue;
18-
result.add(
19-
buildCustomMetric(metricEntry.getKey() + ".value", number.doubleValue(), MetricType.GAUGE, timestamp));
18+
result.add(buildCustomMetric(metricEntry.getKey() + ".value", number.doubleValue(), MetricType.GAUGE,
19+
timestamp));
2020
} else {
2121
throw new IllegalArgumentException();
2222
}
2323
return result;
2424
}
25-
}
25+
}

cf-java-monitoring-custom-metrics-clients/cf-custom-metrics-clients-java/src/main/java/com/sap/cloud/cf/monitoring/java/converter/HistogramConverter.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,4 +41,4 @@ List<Metric> convertMetricEntry(Entry<String, Histogram> metricEntry, long times
4141

4242
return result;
4343
}
44-
}
44+
}

cf-java-monitoring-custom-metrics-clients/cf-custom-metrics-clients-java/src/main/java/com/sap/cloud/cf/monitoring/java/converter/MeterConverter.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,4 +33,4 @@ List<Metric> convertMetricEntry(Entry<String, Meter> metricEntry, long timestamp
3333

3434
return result;
3535
}
36-
}
36+
}

cf-java-monitoring-custom-metrics-clients/cf-custom-metrics-clients-java/src/main/java/com/sap/cloud/cf/monitoring/java/converter/MetricConverter.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ public abstract class MetricConverter<T> {
1212
private static final String KEY_TYPE = "type";
1313

1414
enum MetricType {
15-
TIMER("timer"), HISTOGRAM("histogram"), GAUGE("gauge"), METER("meter"), COUNTER("counter");
15+
TIMER("timer"), HISTOGRAM("histogram"), GAUGE("gauge"), METER("meter"), COUNTER("counter");
1616

1717
private final String metricTypeName;
1818

@@ -27,7 +27,7 @@ private MetricType(String metricTypeName) {
2727

2828
public List<Metric> convert(Map<String, T> metrics, long timestamp) {
2929
ArrayList<Metric> result = new ArrayList<>();
30-
for (Entry<String, T> entry : metrics.entrySet()) {
30+
for (Entry<String, T> entry: metrics.entrySet()) {
3131
result.addAll(convertMetricEntry(entry, timestamp));
3232
}
3333
return result;
@@ -40,4 +40,4 @@ Metric buildCustomMetric(String name, double value, MetricType type, long timest
4040
tags.put(KEY_TYPE, type.getMetricTypeName());
4141
return new Metric(name, value, timestamp, tags);
4242
}
43-
}
43+
}

cf-java-monitoring-custom-metrics-clients/cf-custom-metrics-clients-java/src/main/java/com/sap/cloud/cf/monitoring/java/converter/TimerConverter.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,4 +44,4 @@ List<Metric> convertMetricEntry(Entry<String, Timer> metricEntry, long timestamp
4444
}
4545
return result;
4646
}
47-
}
47+
}

0 commit comments

Comments
 (0)