Minor POC to test time based metrics alternatives
Like a cake recipe.
- Simple Kotlin/Spring Boot app
- Prometheus server up and running
- Grafana container (because install in Mac sucks).
After a working rest endpoint just add those dependencies:
dependencies {
// ...
implementation("io.micrometer:micrometer-core")
implementation("io.micrometer:micrometer-registry-prometheus")
// ...
}
and this parameters to application.properties:
#Metrics related configurations
management.endpoint.metrics.enabled=true
management.endpoints.web.exposure.include=*
management.endpoint.prometheus.enabled=true
management.metrics.export.prometheus.enabled=true
This will add endpoints to actuar and expose metrics through a rest api.
In the code, create objects to collect the metrics like:
@Component
class PrometheusMetrics(@Autowired val meterRegistry: MeterRegistry) {
private final val callCounter : Counter = Counter
.builder("echocounter")
.description("this is an echo counter")
.register(meterRegistry)
fun count() {
println("Count!")
callCounter.increment()
}
}
once count()
is called, it will add ONE in the prometheus metrics.
Download Prometheus and create a config file as bellow:
#App Config file
#Global configurations
global:
scrape_interval: 5s # Set the scrape interval to every 5 seconds.
evaluation_interval: 5s # Evaluate rules every 5 seconds.
scrape_configs:
- job_name: 'echo-app'
metrics_path: '/actuator/prometheus'
static_configs:
- targets: ['localhost:8080']
now start it ./prometheus --config.file=app-config.yml
.
Start with docker-compose and add a new Prometheus Datasource. (simple right?)
Alex Rocha - about.me