Skip to content

Commit

Permalink
Merge pull request #37 from DeyanZhelyazkov/master
Browse files Browse the repository at this point in the history
Add dropwizard based java client for custom metrics. Excluding jackson dependency.
  • Loading branch information
KarstenSchnitter authored Jul 12, 2019
2 parents 1ecc179 + f81216b commit fabbc5d
Show file tree
Hide file tree
Showing 40 changed files with 1,525 additions and 92 deletions.
63 changes: 58 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,11 @@ All in all, you should do the following:
And
4. Adjust your logging configuration accordingly.

Let's say you want to make use of the *servlet filter* feature, then you need to add the following dependency to your POM with property `cf-logging-version` referring to the latest nexus version (currently `3.0.1`):
Let's say you want to make use of the *servlet filter* feature, then you need to add the following dependency to your POM with property `cf-logging-version` referring to the latest nexus version (currently `3.0.2`):

```xml
<properties>
<cf-logging-version>3.0.1</cf-logging-version>
<cf-logging-version>3.0.2</cf-logging-version>
</properties>
```

Expand All @@ -48,7 +48,9 @@ Let's say you want to make use of the *servlet filter* feature, then you need to

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).

If you want to use the `custom metrics` `spring-boot client`, just define the following dependency:
If you want to use the `custom metrics`, just define the following dependency:

* `spring-boot client`:

``` xml

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

* `java client`:

``` xml

<dependency>
<groupId>com.sap.hcp.cf.logging</groupId>
<artifactId>cf-custom-metrics-clients-java</artifactId>
<version>${cf-logging-version}</version>
</dependency>
```


## Implementation variants and logging configurations

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:
Expand Down Expand Up @@ -160,7 +174,7 @@ Here are the minimal configurations you'd need:
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.
To use the clients you'd need:

*Using spring-boot client in Spring Boot 2 application:*
1. *Using spring-boot client in Spring Boot 2 application:*

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

2. *Using java client in Java application:*

``` xml
<dependency>
<groupId>com.sap.hcp.cf.logging</groupId>
<artifactId>cf-custom-metrics-clients-java</artifactId>
<version>${cf-logging-version}</version>
</dependency>
```

The java client uses [Dropwizard](https://metrics.dropwizard.io) which allows to define all kind of metrics supports by Dropwizard. 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:

``` java
import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.codahale.metrics.Counter;
import com.codahale.metrics.Meter;
import com.sap.cloud.cf.monitoring.java.CustomMetricRegistry;

public class CustomMetricsServlet extends HttpServlet {
private static Counter counter = CustomMetricRegistry.get().counter("custom.metric.request.count");
private static Meter meter = CustomMetricRegistry.get().meter("custom.metric.request.meter");

@Override
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
counter.inc(3);
meter.mark();
response.getWriter().println("<p>Greetings from Custom Metrics</p>");
}
}
```

## Custom metrics client configurations

This client library supports the following configurations regarding sending custom metrics:
* `interval`: the interval for sending metrics, in millis. **Default value: `60000`**
* `enabled`: enables or disables the sending of metrics. **Default value: `true`**
* `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: `[]`**
* `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`**

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:

Expand All @@ -237,7 +289,8 @@ This client library supports the following configurations regarding sending cust
"metrics": [
"my.whitelist.metric.1",
"my.whitelist.metric.2"
]
],
"metricQuantiles":true
}
```

Expand Down
2 changes: 1 addition & 1 deletion cf-java-logging-support-core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
<parent>
<groupId>com.sap.hcp.cf.logging</groupId>
<artifactId>cf-java-logging-support-parent</artifactId>
<version>3.0.1</version>
<version>3.0.2</version>
<relativePath>../pom.xml</relativePath>
</parent>
<build>
Expand Down
2 changes: 1 addition & 1 deletion cf-java-logging-support-jersey/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<relativePath>../pom.xml</relativePath>
<groupId>com.sap.hcp.cf.logging</groupId>
<artifactId>cf-java-logging-support-parent</artifactId>
<version>3.0.1</version>
<version>3.0.2</version>
</parent>

<name>cf-java-logging-support-jersey</name>
Expand Down
2 changes: 1 addition & 1 deletion cf-java-logging-support-log4j2/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
<relativePath>../pom.xml</relativePath>
<groupId>com.sap.hcp.cf.logging</groupId>
<artifactId>cf-java-logging-support-parent</artifactId>
<version>3.0.1</version>
<version>3.0.2</version>
</parent>

<dependencies>
Expand Down
2 changes: 1 addition & 1 deletion cf-java-logging-support-logback/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
<relativePath>../pom.xml</relativePath>
<groupId>com.sap.hcp.cf.logging</groupId>
<artifactId>cf-java-logging-support-parent</artifactId>
<version>3.0.1</version>
<version>3.0.2</version>
</parent>

<dependencies>
Expand Down
14 changes: 7 additions & 7 deletions cf-java-logging-support-servlet/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@
<artifactId>cf-java-logging-support-servlet</artifactId>
<packaging>jar</packaging>

<name>cf-java-logging-support-servlet</name>
<parent>
<groupId>com.sap.hcp.cf.logging</groupId>
<artifactId>cf-java-logging-support-parent</artifactId>
<version>3.0.1</version>
<relativePath>../pom.xml</relativePath>
</parent>
<name>cf-java-logging-support-servlet</name>
<parent>
<groupId>com.sap.hcp.cf.logging</groupId>
<artifactId>cf-java-logging-support-parent</artifactId>
<version>3.0.2</version>
<relativePath>../pom.xml</relativePath>
</parent>

<properties>
<servlet.api.version>3.0.1</servlet.api.version>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,54 +1,56 @@
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.sap.hcp.cf.logging</groupId>
<artifactId>cf-java-monitoring-custom-metrics-clients</artifactId>
<version>3.0.1</version>
</parent>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.sap.hcp.cf.logging</groupId>
<artifactId>cf-java-monitoring-custom-metrics-clients</artifactId>
<version>3.0.2</version>
</parent>

<artifactId>cf-custom-metrics-clients-core</artifactId>
<packaging>jar</packaging>
<name>cf-java-monitoring-custom-metrics-clients-core</name>
<artifactId>cf-custom-metrics-clients-core</artifactId>
<packaging>jar</packaging>
<name>cf-java-monitoring-custom-metrics-clients-core</name>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
</plugins>
</build>

<dependencies>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<scope>provided</scope>
</dependency>
<!-- Test dependencies-->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-all</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<dependencies>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<scope>provided</scope>
</dependency>
<!-- Test dependencies -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-all</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.0.2</version>
<executions>
<execution>
<goals>
<goal>test-jar</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package com.sap.cloud.cf.monitoring.client.configuration;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.TimeUnit;

public class CustomMetricsConfiguration {

static final long DEFAULT_INTERVAL = TimeUnit.MINUTES.toMillis(1);
private long interval = DEFAULT_INTERVAL;
private boolean enabled = true;
private List<String> metrics;
private boolean metricQuantiles = false;

public long getInterval() {
return interval;
}

public boolean isEnabled() {
return enabled;
}

public List<String> getMetrics() {
if (this.metrics == null) {
return Collections.emptyList();
}
return new ArrayList<>(metrics);
}

public boolean metricQuantiles() {
return metricQuantiles;
}

@Override
public String toString() {
return new StringBuilder("CustomMetricsConfiguration[").append("interval=")
.append(interval)
.append(", enabled=")
.append(enabled)
.append(", metrics=")
.append(metrics)
.append("]")
.append(", metricQuantiles=")
.append(metricQuantiles)
.toString();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.sap.cloud.cf.monitoring.client.configuration;

import java.util.Map;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

public class CustomMetricsConfigurationFactory {

private static final String CUSTOM_METRICS_KEY = "CUSTOM_METRICS";
private static final Gson gson =
new GsonBuilder().registerTypeAdapter(long.class, new LongIntervalGsonTypeAdapter()).create();

public static CustomMetricsConfiguration create() {
return create(System.getenv());
}

public static CustomMetricsConfiguration create(Map<String, String> env) {
String customMetricsString = env.get(CUSTOM_METRICS_KEY);
if (customMetricsString == null || customMetricsString.isEmpty()) {
return new CustomMetricsConfiguration();
}
return gson.fromJson(customMetricsString, CustomMetricsConfiguration.class);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package com.sap.cloud.cf.monitoring.client.configuration;

import static com.sap.cloud.cf.monitoring.client.configuration.CustomMetricsConfiguration.DEFAULT_INTERVAL;

import java.lang.reflect.Type;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.google.gson.JsonDeserializationContext;
import com.google.gson.JsonDeserializer;
import com.google.gson.JsonElement;
import com.google.gson.JsonParseException;

final class LongIntervalGsonTypeAdapter implements JsonDeserializer<Long> {
private static final Logger LOGGER = LoggerFactory.getLogger(LongIntervalGsonTypeAdapter.class);
private static final long MINIMAL_INTERVAL = 20000L;

@Override
public Long deserialize(JsonElement json, Type type, JsonDeserializationContext context) throws JsonParseException {
long value = Long.parseLong(json.getAsString());
if (value < MINIMAL_INTERVAL) {
LOGGER.warn(String.format(
"The value of 'interval' property could not be less than %s. The default value %s will be used.",
MINIMAL_INTERVAL, DEFAULT_INTERVAL));
return DEFAULT_INTERVAL;
}
return value;
}
}
Loading

0 comments on commit fabbc5d

Please sign in to comment.