Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/maksymdolgykh/dropwizard-micrometer
https://github.com/maksymdolgykh/dropwizard-micrometer
dropwizard dropwizard-bundle dropwizard-micrometer dropwizard-prometheus java metrics prometheus
Last synced: about 1 month ago
JSON representation
- Host: GitHub
- URL: https://github.com/maksymdolgykh/dropwizard-micrometer
- Owner: MaksymDolgykh
- License: apache-2.0
- Created: 2020-09-04T16:59:23.000Z (over 4 years ago)
- Default Branch: master
- Last Pushed: 2022-12-16T08:32:38.000Z (about 2 years ago)
- Last Synced: 2023-12-24T00:41:43.717Z (about 1 year ago)
- Topics: dropwizard, dropwizard-bundle, dropwizard-micrometer, dropwizard-prometheus, java, metrics, prometheus
- Language: Java
- Homepage:
- Size: 46.9 KB
- Stars: 3
- Watchers: 1
- Forks: 0
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
# dropwizard-micrometer
[Dropwizard][dropwizard] bundle that enables your dropwizard application for exposition of [micrometer-like][micrometer]
metrics (system, jvm and http requests) as a [prometheus][prometheus] endpoint. In addition, if you use JDBI to manage
mapping of objects to database tables, you can utilize [dropwizad-micrometer-jdbi](#dropwizad-micrometer-jdbi) package to meter SQL request's latencies.## Packages
- `dropwizad-micrometer-core`
Dropwizard bundle that implements prometheus endpoint and exposes core system metrics and JVM metrics utilizing micrometer instrumentation.
Optionally, it provides servlet filter to record HTTP requests latencies and statuses within dimensional
[prometheus histogram][prometheus histogram].- `dropwizad-micrometer-jdbi`
An additional module to record latencies of [JDBI][jdbi] queries within dimensional [prometheus histogram][prometheus histogram].## Usage
You can find an example of usage in the [ExampleApplication][dropwizard example application].Below are the steps explained in more detail specifically for each package.
### dropwizad-micrometer-core
This package provides a minimal setup, i.e. it instantiates `/prometheus` endpoint, adds system and JVM metrics utilizing
micrometer instrumentation, and optionally you can set up servlet filter to record HTTP requests latencies/statuses.#### Add dependency into your `pom.xml`
If you use `maven`, you can simply reference it in the `` block as below.
The latest version can be found in [Releases](https://github.com/MaksymDolgykh/dropwizard-micrometer/releases) or in the maven [repository][dropwizard-micrometer-core maven repo]```xml
...
...
io.github.maksymdolgykh.dropwizard
dropwizard-micrometer-core
2.0.5
...
...
```
#### Import DropwizardMicrometer classes in your Application class
```java
import io.github.maksymdolgykh.dropwizard.micrometer.MicrometerBundle;
import io.github.maksymdolgykh.dropwizard.micrometer.MicrometerHttpFilter;
import javax.servlet.FilterRegistration;
import javax.servlet.DispatcherType;
import java.util.EnumSet;
```#### Add the bundle to your application
Add `MicrometerBundle` class to the bootstrapping phase of your Application class
```java
public class ExampleApplication extends Application {
//...
//...@Override
public void initialize(Bootstrap bootstrap) {
//...
//...
bootstrap.addBundle(new MicrometerBundle());
//...
//...
}
//...
//...
}
```You will also need to make your `ExampleConfiguration` class implement `MicrometerBundleConfiguration`
in order to provide the bundle with the configuration:
```java
public class ExampleConfiguration extends Configuration implements MicrometerBundleConfiguration {
@JsonProperty("prometheus")
private PrometheusConfiguration prometheusConfiguration = new PrometheusConfiguration();@Override
public PrometheusConfiguration getPrometheusConfiguration() {
return prometheusConfiguration;
}
//...
//...
}
```
Assuming, the above example, configuration element is `prometheus`, so you can use configuration block like this in a config file:
```yaml
prometheus:
endpoint: "/prometheus"
```This will expose `/prometheus` endpoint in admin connector, by default admin connector is exposed at port `8081`
in dropwizard apps.### Assign servlet filter to the environment
To leverage latency metrics per http endpoint you need to assign servlet filter to the environment
in your Application class within `run` method
```java
public class ExampleApplication extends Application {
//...
//...
@Override
public void run(ExampleConfiguration configuration, Environment environment) {
//...
//...
FilterRegistration.Dynamic micrometerFilter = environment.servlets().addFilter("MicrometerHttpFilter", new MicrometerHttpFilter());
micrometerFilter.addMappingForUrlPatterns(EnumSet.allOf(DispatcherType.class), true, "/*");
}
}
```
This way all http requests will be metered and metrics will be recorded within `http_server_requests_seconds` [histogram][prometheus histogram]
with the following labels:
- `method`, http method, i.e. `GET`, `POST` etc
- `status`, response status, i.e. `200`, `404`, `500` etc
- `uri`, request path### dropwizad-micrometer-jdbi
If you use [JDBI][jdbi] to manage mapping of objects to database tables, you can utilize
[dropwizad-micrometer-jdbi][dropwizard-micrometer-jdbi maven repo] package to meter SQL request's latencies.
It depends on `dropwizad-micrometer-core`, so that to use `dropwizad-micrometer-jdbi` package
the core package should be already installed and bundle should be added to your dropwizard application
(see how to install it in [dropwizad-micrometer-core](#dropwizad-micrometer-core) section).
Once `dropwizad-micrometer-core` is installed, the steps to install `dropwizad-micrometer-jdbi` package are:#### Add dependency into your `pom.xml`
If you use `maven`, you can simply reference it in the `` block as below.
The latest version can be found on in the maven [repository][dropwizard-micrometer-jdbi maven repo]```xml
...
...
io.github.maksymdolgykh.dropwizard
dropwizard-micrometer-jdbi
2.0.5
...
...
```
#### Import MicrometerJdbiTimingCollector class in your Application class
```java
import io.github.maksymdolgykh.dropwizard.micrometer.MicrometerJdbiTimingCollector;
```#### Set TimingColletor for Jdbi object
To use the class you just need to set `TimingColletor` for Jdbi object, where it should be done depends on how your
Application is organized - it might be in the `run` method of your `Application` class, or you might have separate
class to configure DAO.
```
database.setTimingCollector(new MicrometerJdbiTimingCollector());
```
With this setup all jdbi requests will be metered and metrics will be recorded within `jdbi_requests_seconds` [histogram][prometheus histogram]## License
This project is licensed under the Apache License 2.0 ([LICENSE](./LICENSE.TXT))[dropwizard]: https://www.dropwizard.io/en/latest/
[dropwizard example application]: https://github.com/MaksymDolgykh/dropwizard-example-app
[dropwizard-micrometer-core maven repo]: https://mvnrepository.com/artifact/io.github.maksymdolgykh.dropwizard/dropwizard-micrometer-core
[dropwizard-micrometer-jdbi maven repo]: https://mvnrepository.com/artifact/io.github.maksymdolgykh.dropwizard/dropwizard-micrometer-jdbi
[jdbi]: https://jdbi.org/
[micrometer]: http://micrometer.io/
[prometheus]: https://prometheus.io/
[prometheus histogram]: https://prometheus.io/docs/practices/histograms/