Skip to content

Commit

Permalink
Apply PR requested changes
Browse files Browse the repository at this point in the history
  • Loading branch information
wlatif committed Jul 20, 2023
1 parent afd759b commit 6d9432b
Show file tree
Hide file tree
Showing 17 changed files with 176 additions and 141 deletions.
2 changes: 1 addition & 1 deletion plume-framework-dependencies/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@
</dependency>
<dependency>
<groupId>com.coreoz</groupId>
<artifactId>plume-web-jersey-info</artifactId>
<artifactId>plume-web-jersey-monitoring</artifactId>
<version>${project.version}</version>
</dependency>

Expand Down

This file was deleted.

File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,26 @@ This module provides utilities to expose backend monitoring API similarly to wha

Setup
-----
### Maven dependency
Add the dependency to the `pom.xml` file
```xml
<dependency>
<groupId>com.coreoz</groupId>
<artifactId>plume-web-jersey-info</artifactId>
<artifactId>plume-web-jersey-monitoring</artifactId>
</dependency>
```

### Install Guice modules
In your application module :
1. replace `GuiceJacksonModule`by the `GuiceJacksonWithMetricsModule`
2. install the `GuiceJerseyMonitoringModule`

~~`install(new GuiceJacksonModule());`~~
```java
install(new GuiceJacksonWithMetricsModule());
install(new GuiceJerseyMonitoringModule());
```

Features
-------

Expand All @@ -25,34 +38,12 @@ The `HealthCheckBuilder` provides a simple API to monitor the health status of y
- `registerDatabaseHealthCheck`: Register the built-in database health check
- `build`: create a health status provider

Usage example:

**Web-service**
```java
@Path("/monitor/health")
@Singleton
public class HealthWs {
private static final Logger logger = LoggerFactory.getLogger(HealthWs.class);

private final Provider<HealthStatus> healthStatusProvider;
### ApplicationInfo
The `ApplicationInfo` is a singleton object available through dependency injection.
It contains the basic application information retrieved from the `Pom.xml` file.

@Inject
public HealthWs(TransactionManager transactionManager, HealthCheckService healthCheckService) {
this.healthStatusProvider = new HealthCheckBuilder()
.registerDatabaseHealthCheck()
.build();
}

@GET
@Produces(MediaType.APPLICATION_JSON)
public HealthStatus get(@Context ContainerRequestContext requestContext) {
return this.healthStatusProvider.get();
}
}
```

### InfoService
The `InfoService` provides a unique getter that retrieves the basic application information from the `Pom.xml` :
`ApplicationInfo` content:
- name
- description
- version
Expand All @@ -67,27 +58,6 @@ plm-web-jersey-info = {
}
```

Usage example:

**Web-service**
```java
@Path("/monitor/info")
@Singleton
public class InfoWs {
private final InfoService infoService;

@Inject
public InfoWs(InfoService infoService) {
this.infoService = infoService;
}

@GET
public String get(@Context ContainerRequestContext requestContext) {
return this.infoService.getAppInfo();
}
}
```

### MetricsCheckBuilder
The `MetricsCheckBuilder` uses the [io.dropwizard.metrics](https://github.com/dropwizard/metrics) library
to provide some basic functionality for monitoring your application's metrics (CPU usage, memory usage, ...).
Expand All @@ -97,38 +67,68 @@ Exposed API :
- `registerJvmMetrics`: Register the basic JVM metrics to monitor
- `build`: create a metrics provider that provides the status of the metrics that are monitored.

Usage example:

Usage example
-------

**Web-service**

```java
@Path("/monitor/metrics")
import com.coreoz.plume.jersey.security.permission.PublicApi;

@Path("/monitor")
// Authentication is done directly by the web service without any annotation
@PublicApi
@Singleton
public class MetricsWs {
private final JerseyInfoObjectMapper objectMapper = JerseyInfoObjectMapper.get();
public class MonitoringWs {
private final ApplicationInfo applicationInfo;
private final Provider<HealthStatus> healthStatusProvider;
private final Provider<Map<String, Metric>> metricsStatusProvider;

private final BasicAuthenticator<String> basicAuthenticator;

@Inject
public MetricsWs(MetricsService metricsService) {
public MonitoringWs(ApplicationInfo applicationInfo) {
this.applicationInfo = applicationInfo;
// Registering health checks
this.healthStatusProvider = new HealthCheckBuilder()
.registerDatabaseHealthCheck()
.build();

// Registering metrics to monitor
this.metricsStatusProvider = new MetricsCheckBuilder()
.registerJvmMetrics()
.build();

// require authentication to access the supervision URL
this.basicAuthenticator = BasicAuthenticator.fromSingleCredentials(
"SINGLE_USERNAME",
"PASSWORD",
"MY_REALM"
);
}

@GET
@Produces(MediaType.APPLICATION_JSON)
public String get(@Context ContainerRequestContext requestContext) throws JsonProcessingException {
return objectMapper.writeValueAsString(Map.of(
"metrics", metricsStatusProvider.get()
));
@Path("/info")
public ApplicationInfo get(@Context ContainerRequestContext requestContext) {
basicAuthenticator.requireAuthentication(requestContext);
return this.infoService.getAppInfo();
}
}
```

### JerseyInfoObjectMapper
Provides an ObjectMapper to serialize the types provided by plume-web-jersey-info services.

Usage example:
@GET
@Path("/health")
@Produces(MediaType.APPLICATION_JSON)
public HealthStatus get(@Context ContainerRequestContext requestContext) {
basicAuthenticator.requireAuthentication(requestContext);
return this.healthStatusProvider.get();
}

```java
private final ObjectMapper objectMapper = JerseyInfoObjectMapper.get();
@GET
@Path("/metrics")
@Produces(MediaType.APPLICATION_JSON)
public String get(@Context ContainerRequestContext requestContext) {
basicAuthenticator.requireAuthentication(requestContext);
return metricsStatusProvider.get();
}
}
```
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@
<version>4.1.2-SNAPSHOT</version>
</parent>

<artifactId>plume-web-jersey-info</artifactId>
<artifactId>plume-web-jersey-monitoring</artifactId>
<packaging>jar</packaging>
<name>Plume Jersey info services</name>
<name>Plume Jersey monitoring</name>

<build>
<plugins>
Expand Down Expand Up @@ -66,6 +66,15 @@
<dependency>
<groupId>com.coreoz</groupId>
<artifactId>plume-db</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>com.google.inject</groupId>
<artifactId>guice</artifactId>
</dependency>
<dependency>
<groupId>com.coreoz</groupId>
<artifactId>plume-web-jersey</artifactId>
</dependency>
</dependencies>

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.coreoz.plume.jersey.info.configuration;
package com.coreoz.plume.jersey.monitoring.configuration;

import com.typesafe.config.Config;
import com.typesafe.config.ConfigFactory;
Expand All @@ -8,13 +8,12 @@
import java.util.Map;

@Singleton
public class JerseyInfoConfigurationService {

public class JerseyMonitoringConfigurationService {
private final Config config;

@Inject
public JerseyInfoConfigurationService(Config config) {
this.config = config.withFallback(ConfigFactory.parseResources("plume-web-jersey-info.conf"));
public JerseyMonitoringConfigurationService(Config config) {
this.config = config.withFallback(ConfigFactory.parseResources("plume-web-jersey-monitoring.conf"));
}

public Map<String, Object> getCustomInfo() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.coreoz.plume.jersey.monitoring.guice;

import com.coreoz.plume.jersey.monitoring.json.JerseyMonitoringObjectMapperProvider;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.inject.AbstractModule;

public class GuiceJacksonWithMetricsModule extends AbstractModule {
@Override
protected void configure() {
bind(ObjectMapper.class).toProvider(JerseyMonitoringObjectMapperProvider.class);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.coreoz.plume.jersey.monitoring.guice;

import com.coreoz.plume.jersey.monitoring.utils.info.ApplicationInfoProvider;
import com.coreoz.plume.jersey.monitoring.utils.info.beans.ApplicationInfo;
import com.google.inject.AbstractModule;

public class GuiceJerseyMonitoringModule extends AbstractModule {
@Override
protected void configure() {
bind(ApplicationInfo.class).toProvider(ApplicationInfoProvider.class);
}
}
Original file line number Diff line number Diff line change
@@ -1,30 +1,38 @@
package com.coreoz.plume.jersey.info.json;
package com.coreoz.plume.jersey.monitoring.json;

import com.codahale.metrics.json.HealthCheckModule;
import com.codahale.metrics.json.MetricsModule;
import com.coreoz.plume.jersey.jackson.ObjectMapperProvider;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;

import javax.inject.Inject;
import javax.inject.Provider;
import javax.inject.Singleton;
import java.util.concurrent.TimeUnit;

public class JerseyInfoObjectMapper {
@Singleton
public class JerseyMonitoringObjectMapperProvider implements Provider<ObjectMapper> {
private final ObjectMapper objectMapper;
private static final HealthCheckModule healthCheckModule = new HealthCheckModule();
private static final MetricsModule metricsModule = new MetricsModule(
TimeUnit.SECONDS,
TimeUnit.SECONDS,
false
);

private JerseyInfoObjectMapper() {
}

public static ObjectMapper get() {
return new ObjectMapper()
@Inject
public JerseyMonitoringObjectMapperProvider(ObjectMapperProvider objectMapperProvider) {
this.objectMapper = objectMapperProvider.get()
.registerModule(healthCheckModule)
.registerModule(metricsModule)
.disable(SerializationFeature.FAIL_ON_EMPTY_BEANS)
.enable(SerializationFeature.INDENT_OUTPUT)
.setSerializationInclusion(JsonInclude.Include.NON_EMPTY);
}

public ObjectMapper get() {
return this.objectMapper;
}
}
Original file line number Diff line number Diff line change
@@ -1,20 +1,17 @@
package com.coreoz.plume.jersey.info.monitoring.health;
package com.coreoz.plume.jersey.monitoring.utils.health;

import com.coreoz.plume.db.transaction.TransactionManager;
import com.coreoz.plume.jersey.info.monitoring.health.beans.HealthStatus;
import com.coreoz.plume.jersey.monitoring.utils.health.beans.HealthStatus;
import com.codahale.metrics.health.HealthCheck;
import com.codahale.metrics.health.HealthCheckRegistry;
import com.coreoz.plume.jersey.info.monitoring.health.healthchecks.DatabaseHealthCheck;
import com.coreoz.plume.jersey.monitoring.utils.health.healthchecks.DatabaseHealthCheck;

import javax.inject.Provider;
import java.util.SortedMap;

public class HealthCheckBuilder {
private final HealthCheckRegistry healthCheckRegistry = new HealthCheckRegistry();

private HealthCheckBuilder() {
}

public HealthCheckBuilder registerHealthCheck(String name, HealthCheck healthCheck) {
this.healthCheckRegistry.register(name, healthCheck);
return this;
Expand Down
Loading

0 comments on commit 6d9432b

Please sign in to comment.