问题
How can I monitor JDBC calls(Oracle/MySql/Postgre etc.) statistics for my spring boot application with Grafana/Premetheus?.
- Do we have any plugin for it ?.
- is it possible to capture this statistics through Grafana/Prometheus?
Specifically I am looking for statistics like, i.e.
- How many open connection I have with database?
- Whether my database is up or not?
- What queries are causing longest response time etc ?.
回答1:
You can try using micrometer. This is the metrics provider for spring boot. I'm currently working on the same thing, and I'm not completely aware of all the way one can use micrometer. So, hopefully the below information will be useful for you.
There are ways to expose spring boot application endpoints to prometheus, and then you can link up grafana to use prometheus. Checkout this link for an example on how you can do it
By default, micrometer will include the JvmMemoryMetrics, UptimeMetrics, etc,. But you can use the binders provided in the below package in your application by adding it as a bean.
io.micrometer.core.instrument.binder
For example (code written in Kotlin),
import io.micrometer.core.instrument.binder.db.PostgreSQLDatabaseMetrics
import io.micrometer.core.instrument.binder.jvm.ClassLoaderMetrics
import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.Configuration
import javax.sql.DataSource
/**
* Class to configure binders for micrometer metrics for this application
*/
@Configuration
open class MetricsConfiguration(private val dataSource: DataSource) {
@Bean
open fun jvmMemoryMetrics(): ClassLoaderMetrics {
return ClassLoaderMetrics()
}
@Bean
open fun dbMetrics(): PostgreSQLDatabaseMetrics {
return PostgreSQLDatabaseMetrics(dataSource, "database-name")
}
}
You can also write custom binders & metrics if needed. Refer to the micrometer docs for more information on those.
回答2:
Exporters help in exposing the metrics of the hosts to the endpoint.
The endpoints can either be /metrics
or /prometheus
You would need to configure your prometheus.yml
to scrape the metrics from the endpoints
Prometheus official documentation has a list of exporters
https://prometheus.io/docs/instrumenting/exporters/
You can find exporters for Oracle,MySql and PostGresSQL and a generic JDBC exporter.
You would need to install this exporter on your database machine to obtain DB specific metrics.
For spring boot application related metrics like "Number of queries to the database", you can write custom metrics using Micrometer for spring boot 2.x
来源:https://stackoverflow.com/questions/51641960/how-to-monitor-jdbc-calls-and-statistics-with-grafana