Annotation processor which automatically generates Grafana Dashboard JSON model based on metrics declared in application.
run command
./mvnw clean install
- Add next dependencies to pom.xml
<dependency>
<groupId>io.github.ilya-rochev</groupId>
<artifactId>grafana-dashboard-annotations-processor</artifactId>
<version>1.0</version>
</dependency>
<dependency>
<groupId>io.github.ilya-rochev</groupId>
<artifactId>prometheus-metric-spring-boot-starter</artifactId>
<version>1.0</version>
</dependency>
- Migrate creating
Counter
andTimer
beans to processed byprometheus-metric-spring-boot-starter
annotationi.r.grafana.json.annotations.promethues.@Metric
.
If you create metrics like this
@Configuration
@RequiredArgsConstructor
public class MetricConfig {
private final MeterRegistry meterRegistry;
@Bean
public Counter savedObjectsCounter() {
return meterRegistry.counter("counter_as_field", "type", "saved");
}
...
}
Change creating metrics declarative way using @Metric
annotation
@Service
@RequiredArgsConstructor
@ParametersAreNonnullByDefault
public class ExternalCounterServiceStub {
@Metric(value = "counter_as_field", extraTags = {"type", "saved"}, description = "Saved object counter")
private final Counter savedObjectsCounter;
...
}
Notice: prometheus-metric-spring-boot-starter
processes annotation @Metric
during starting application.
It creates Timer
or Counter
bean with name and tags you provided as annotation parameters.
- Declare you Grafana dashboard using annotations from package
i.r.grafana.json.annotations
@Dashboard(title = "Demo application metrics", panels = {
@Panel(metric = "timer_as_field", title = "External service invocation time (FIELDS)"),
@Panel(metric = "timer_as_annotation", title = "External service invocation time (ANNOTATIONS)"),
@Panel(metric = "counter_as_field", title = "External objects counters (FIELDS)"),
@Panel(metric = "counter_as_annotation", title = "External objects counters (ANNOTATION)")}
)
public class GrafanaDashboardConfiguration { }
Notice: grafana-dashboard-annotations-processor
processes annotations from package i.r.grafana.json.annotations
during compile.
It generates dashboard model and injects metrics you declared using @Metric
annotation;
-
Build your project. Created grafana json model will be stored in
target\classes\dasboard_{uuid}.json
file, for example -dashboard_221a4f4c-2d60-4ec0-9fa5-7c828fee81eb.json
-
Run your project and import generated json file to grafana. Enjoy to view you custom application metrics.
Project provides demo application to be informed how to use library in practice
Requires:
- "docker" runned at your machine;
- Start locally grafana and prometheus in docker:
docker-compose up -d