Skip to content

Commit afd759b

Browse files
author
wlatif
committed
Apply PR requested changes
1 parent 2eef998 commit afd759b

File tree

12 files changed

+147
-142
lines changed

12 files changed

+147
-142
lines changed

plume-web-jersey-info/README.md

Lines changed: 26 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,15 @@ Setup
1515
</dependency>
1616
```
1717

18-
Services
18+
Features
1919
-------
2020

21-
### HealthCheckService
22-
The `HealthCheckService` provides a simple API to monitor the health status of your application.
21+
### HealthCheckBuilder
22+
The `HealthCheckBuilder` provides a simple API to monitor the health status of your application.
2323

2424
- `registerHealthCheck`: Register your health checks
25-
- `isHealthy`: run the registered health checks and return the health status.
25+
- `registerDatabaseHealthCheck`: Register the built-in database health check
26+
- `build`: create a health status provider
2627

2728
Usage example:
2829

@@ -33,25 +34,25 @@ Usage example:
3334
public class HealthWs {
3435
private static final Logger logger = LoggerFactory.getLogger(HealthWs.class);
3536

36-
private final HealthCheckService healthCheckService;
37-
private final ObjectMapper objectMapper = PlmWebJerseyInfoObjectMapperProvider.get();
37+
private final Provider<HealthStatus> healthStatusProvider;
3838

3939
@Inject
4040
public HealthWs(TransactionManager transactionManager, HealthCheckService healthCheckService) {
41-
this.healthCheckService = healthCheckService;
42-
healthCheckService.registerHealthCheck("database", new DatabaseHealthCheck(transactionManager));
41+
this.healthStatusProvider = new HealthCheckBuilder()
42+
.registerDatabaseHealthCheck()
43+
.build();
4344
}
4445

4546
@GET
4647
@Produces(MediaType.APPLICATION_JSON)
47-
public HealthStatus get(@Context ContainerRequestContext requestContext) throws JsonProcessingException {
48-
return this.healthCheckService.isHealthy();
48+
public HealthStatus get(@Context ContainerRequestContext requestContext) {
49+
return this.healthStatusProvider.get();
4950
}
5051
}
5152
```
5253

53-
### InfoCheckService
54-
The `InfoCheckService` provides a unique getter that retrieves the basic application information from the `Pom.xml` :
54+
### InfoService
55+
The `InfoService` provides a unique getter that retrieves the basic application information from the `Pom.xml` :
5556
- name
5657
- description
5758
- version
@@ -87,13 +88,14 @@ public class InfoWs {
8788
}
8889
```
8990

90-
### MetricsService
91-
The `MetricsService` uses the [io.dropwizard.metrics](https://github.com/dropwizard/metrics) library
91+
### MetricsCheckBuilder
92+
The `MetricsCheckBuilder` uses the [io.dropwizard.metrics](https://github.com/dropwizard/metrics) library
9293
to provide some basic functionality for monitoring your application's metrics (CPU usage, memory usage, ...).
9394

9495
Exposed API :
9596
- `registerMetric`: Register metrics to monitor
96-
- `getMetrics`: Provides the metrics that are monitored.
97+
- `registerJvmMetrics`: Register the basic JVM metrics to monitor
98+
- `build`: create a metrics provider that provides the status of the metrics that are monitored.
9799

98100
Usage example:
99101

@@ -102,50 +104,31 @@ Usage example:
102104
@Path("/monitor/metrics")
103105
@Singleton
104106
public class MetricsWs {
105-
private final MetricsService metricsService;
106-
private final PlmWebJerseyInfoObjectMapper objectMapper = new PlmWebJerseyInfoObjectMapper();
107+
private final JerseyInfoObjectMapper objectMapper = JerseyInfoObjectMapper.get();
108+
private final Provider<Map<String, Metric>> metricsStatusProvider;
107109

108110
@Inject
109111
public MetricsWs(MetricsService metricsService) {
110-
this.metricsService = metricsService;
111-
112-
metricsService.registerMetric("memory-usage", new MemoryUsageGaugeSet());
113-
metricsService.registerMetric("thread-states", new ThreadStatesGaugeSet());
112+
this.metricsStatusProvider = new MetricsCheckBuilder()
113+
.registerJvmMetrics()
114+
.build();
114115
}
115-
116-
116+
117117
@GET
118118
@Produces(MediaType.APPLICATION_JSON)
119119
public String get(@Context ContainerRequestContext requestContext) throws JsonProcessingException {
120120
return objectMapper.writeValueAsString(Map.of(
121-
"metrics", metricsService.getMetrics()
121+
"metrics", metricsStatusProvider.get()
122122
));
123123
}
124124
}
125125
```
126126

127-
128-
Built-in HealthChecks
129-
-------
130-
131-
### DatabaseHealthCheck
132-
Check the health of your connection to the database.
133-
134-
Usage example:
135-
136-
```java
137-
@Inject
138-
public HealthWs(TransactionManager transactionManager, HealthCheckService healthCheckService) {
139-
this.healthCheckService = healthCheckService;
140-
healthCheckService.registerHealthCheck("database", new DatabaseHealthCheck(transactionManager));
141-
}
142-
```
143-
144-
### PlmWebJerseyInfoObjectMapperProvider
127+
### JerseyInfoObjectMapper
145128
Provides an ObjectMapper to serialize the types provided by plume-web-jersey-info services.
146129

147130
Usage example:
148131

149132
```java
150-
private final PlmWebJerseyInfoObjectMapper objectMapper = new PlmWebJerseyInfoObjectMapper();
133+
private final ObjectMapper objectMapper = JerseyInfoObjectMapper.get();
151134
```
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,12 @@
88
import java.util.Map;
99

1010
@Singleton
11-
public class PlmWebJerseyInfoConfigurationService {
11+
public class JerseyInfoConfigurationService {
1212

1313
private final Config config;
1414

1515
@Inject
16-
public PlmWebJerseyInfoConfigurationService(Config config) {
16+
public JerseyInfoConfigurationService(Config config) {
1717
this.config = config.withFallback(ConfigFactory.parseResources("plume-web-jersey-info.conf"));
1818
}
1919

plume-web-jersey-info/src/main/java/com/coreoz/plume/jersey/info/health/HealthCheckService.java

Lines changed: 0 additions & 34 deletions
This file was deleted.
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package com.coreoz.plume.jersey.info.json;
2+
3+
import com.codahale.metrics.json.HealthCheckModule;
4+
import com.codahale.metrics.json.MetricsModule;
5+
import com.fasterxml.jackson.annotation.JsonInclude;
6+
import com.fasterxml.jackson.databind.ObjectMapper;
7+
import com.fasterxml.jackson.databind.SerializationFeature;
8+
9+
import java.util.concurrent.TimeUnit;
10+
11+
public class JerseyInfoObjectMapper {
12+
private static final HealthCheckModule healthCheckModule = new HealthCheckModule();
13+
private static final MetricsModule metricsModule = new MetricsModule(
14+
TimeUnit.SECONDS,
15+
TimeUnit.SECONDS,
16+
false
17+
);
18+
19+
private JerseyInfoObjectMapper() {
20+
}
21+
22+
public static ObjectMapper get() {
23+
return new ObjectMapper()
24+
.registerModule(healthCheckModule)
25+
.registerModule(metricsModule)
26+
.disable(SerializationFeature.FAIL_ON_EMPTY_BEANS)
27+
.enable(SerializationFeature.INDENT_OUTPUT)
28+
.setSerializationInclusion(JsonInclude.Include.NON_EMPTY);
29+
}
30+
}

plume-web-jersey-info/src/main/java/com/coreoz/plume/jersey/info/metrics/MetricsService.java

Lines changed: 0 additions & 29 deletions
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package com.coreoz.plume.jersey.info.monitoring.health;
2+
3+
import com.coreoz.plume.db.transaction.TransactionManager;
4+
import com.coreoz.plume.jersey.info.monitoring.health.beans.HealthStatus;
5+
import com.codahale.metrics.health.HealthCheck;
6+
import com.codahale.metrics.health.HealthCheckRegistry;
7+
import com.coreoz.plume.jersey.info.monitoring.health.healthchecks.DatabaseHealthCheck;
8+
9+
import javax.inject.Provider;
10+
import java.util.SortedMap;
11+
12+
public class HealthCheckBuilder {
13+
private final HealthCheckRegistry healthCheckRegistry = new HealthCheckRegistry();
14+
15+
private HealthCheckBuilder() {
16+
}
17+
18+
public HealthCheckBuilder registerHealthCheck(String name, HealthCheck healthCheck) {
19+
this.healthCheckRegistry.register(name, healthCheck);
20+
return this;
21+
}
22+
23+
public HealthCheckBuilder registerDatabaseHealthCheck(TransactionManager transactionManager) {
24+
this.healthCheckRegistry.register("database", new DatabaseHealthCheck(transactionManager));
25+
return this;
26+
}
27+
28+
public Provider<HealthStatus> build() {
29+
return this::isHealthy;
30+
}
31+
32+
/* PRIVATE */
33+
private HealthStatus isHealthy() {
34+
SortedMap<String, HealthCheck.Result> healthChecksResult = healthCheckRegistry.runHealthChecks();
35+
boolean isAppHealthy = healthChecksResult.values()
36+
.stream()
37+
.reduce(true, (isHealthy, healthCheck) -> isHealthy && healthCheck.isHealthy(), Boolean::logicalAnd);
38+
39+
return new HealthStatus(isAppHealthy, healthChecksResult);
40+
}
41+
}

plume-web-jersey-info/src/main/java/com/coreoz/plume/jersey/info/health/beans/HealthStatus.java renamed to plume-web-jersey-info/src/main/java/com/coreoz/plume/jersey/info/monitoring/health/beans/HealthStatus.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.coreoz.plume.jersey.info.health.beans;
1+
package com.coreoz.plume.jersey.info.monitoring.health.beans;
22

33
import com.codahale.metrics.health.HealthCheck;
44

plume-web-jersey-info/src/main/java/com/coreoz/plume/jersey/info/health/healthchecks/DatabaseHealthCheck.java renamed to plume-web-jersey-info/src/main/java/com/coreoz/plume/jersey/info/monitoring/health/healthchecks/DatabaseHealthCheck.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.coreoz.plume.jersey.info.health.healthchecks;
1+
package com.coreoz.plume.jersey.info.monitoring.health.healthchecks;
22

33
import com.codahale.metrics.health.HealthCheck;
44
import com.coreoz.plume.db.transaction.TransactionManager;

plume-web-jersey-info/src/main/java/com/coreoz/plume/jersey/info/info/InfoService.java renamed to plume-web-jersey-info/src/main/java/com/coreoz/plume/jersey/info/monitoring/info/InfoService.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
package com.coreoz.plume.jersey.info.info;
1+
package com.coreoz.plume.jersey.info.monitoring.info;
22

3-
import com.coreoz.plume.jersey.info.configuration.PlmWebJerseyInfoConfigurationService;
4-
import com.coreoz.plume.jersey.info.info.beans.AppInformation;
5-
import com.coreoz.plume.jersey.info.objectmapper.PlmWebJerseyInfoObjectMapperProvider;
3+
import com.coreoz.plume.jersey.info.configuration.JerseyInfoConfigurationService;
4+
import com.coreoz.plume.jersey.info.monitoring.info.beans.AppInformation;
5+
import com.coreoz.plume.jersey.info.json.JerseyInfoObjectMapper;
66
import com.fasterxml.jackson.databind.ObjectMapper;
77
import org.apache.maven.model.Model;
88
import org.apache.maven.model.io.xpp3.MavenXpp3Reader;
@@ -14,11 +14,11 @@
1414

1515
@Singleton
1616
public class InfoService {
17-
private final PlmWebJerseyInfoConfigurationService configurationService;
18-
private final ObjectMapper objectMapper = PlmWebJerseyInfoObjectMapperProvider.get();
17+
private final JerseyInfoConfigurationService configurationService;
18+
private final ObjectMapper objectMapper = JerseyInfoObjectMapper.get();
1919

2020
@Inject
21-
private InfoService(PlmWebJerseyInfoConfigurationService configurationService) {
21+
private InfoService(JerseyInfoConfigurationService configurationService) {
2222
this.configurationService = configurationService;
2323
}
2424

plume-web-jersey-info/src/main/java/com/coreoz/plume/jersey/info/info/beans/AppInformation.java renamed to plume-web-jersey-info/src/main/java/com/coreoz/plume/jersey/info/monitoring/info/beans/AppInformation.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.coreoz.plume.jersey.info.info.beans;
1+
package com.coreoz.plume.jersey.info.monitoring.info.beans;
22

33
import java.util.Map;
44

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package com.coreoz.plume.jersey.info.monitoring.metrics;
2+
3+
import com.codahale.metrics.Metric;
4+
import com.codahale.metrics.MetricRegistry;
5+
import com.codahale.metrics.MetricSet;
6+
import com.codahale.metrics.jvm.MemoryUsageGaugeSet;
7+
import com.codahale.metrics.jvm.ThreadStatesGaugeSet;
8+
9+
import javax.inject.Provider;
10+
import java.util.Map;
11+
12+
public class MetricsCheckBuilder {
13+
private final MetricRegistry metricRegistry = new MetricRegistry();
14+
15+
public MetricsCheckBuilder registerMetric(String name, Metric metric) {
16+
if (metric instanceof MetricSet) {
17+
this.metricRegistry.registerAll(name, (MetricSet) metric);
18+
} else {
19+
this.metricRegistry.register(name, metric);
20+
}
21+
return this;
22+
}
23+
24+
public MetricsCheckBuilder registerJvmMetrics() {
25+
this.metricRegistry.registerAll("memory-usage", new MemoryUsageGaugeSet());
26+
this.metricRegistry.registerAll("thread-states", new ThreadStatesGaugeSet());
27+
return this;
28+
}
29+
30+
public Provider<Map<String, Metric>> build() {
31+
return this::getMetrics;
32+
}
33+
34+
/* PRIVATE */
35+
private Map<String, Metric> getMetrics() {
36+
return this.metricRegistry.getMetrics();
37+
}
38+
}

plume-web-jersey-info/src/main/java/com/coreoz/plume/jersey/info/objectmapper/PlmWebJerseyInfoObjectMapperProvider.java

Lines changed: 0 additions & 24 deletions
This file was deleted.

0 commit comments

Comments
 (0)