Cloud Monitoring provides visibility into the performance, uptime, and overall health of your applications. It can collect metrics, events, and metadata from Google Cloud, Amazon Web Services, hosted uptime probes, application instrumentation, Metrics data can be used to generate insights via dashboards, charts, and alerts. Cloud Monitoring alerting helps you collaborate by integrating with Slack, PagerDuty, and more.
Enable API
gcloudservicesenablemonitoring.googleapis.com
Micrometer
Micrometer is the de-facto metrics collector for Spring Boot applications. Micrometer can export JVM metrics, Spring Boot metrics, and also application metrics with Counters, Gauges, and Timers. You can export the metrics to Cloud Monitoring with two methods:
Method
Description
When to use?
Export metrics using the Prometheus format from a Spring Boot Actuator endpoint (/actuator/prometheus).
A Prometheus agent will need to be configured to scrape the metrics.
Great option when running in Kubernetes, where metrics are usually collected using Prometheus operator.
Export the metrics directly to Cloud Monitoring using the API.
This is needed whenever Prometheus scraping is not possible, such as Serverless environments like Cloud Run and App Engine.
Prometheus
Dependency
Add Spring Boot Actuator and Micrometer Prometheus dependencies:
Notice that there is no explicit configuration for username/password. Cloud Trace authentication uses the GCP credential (either your user credential, or Service Account credential), and authorization is configured via Identity Access Management (IAM).
Prometheus Endpoint
You should be able to access the metrics in Prometheus format from /actuator/prometheus.
$ curl http://localhost:8080/actuator/prometheus
# HELP jvm_memory_committed_bytes The amount of memory in bytes that is committed for the Java virtual machine to use
# TYPE jvm_memory_committed_bytes gauge
jvm_memory_committed_bytes{area="nonheap",id="Code Cache",} 1.8284544E7
jvm_memory_committed_bytes{area="nonheap",id="Metaspace",} 6.6281472E7
jvm_memory_committed_bytes{area="nonheap",id="Compressed Class Space",} 8609792.0
jvm_memory_committed_bytes{area="heap",id="PS Eden Space",} 6.01358336E8
jvm_memory_committed_bytes{area="heap",id="PS Survivor Space",} 2.2020096E7
jvm_memory_committed_bytes{area="heap",id="PS Old Gen",} 1.1010048E8
# HELP tomcat_global_sent_bytes_total
...
Prometheus Scraper
If you are running in Kubernetes Engine, you can use Prometheus Operator to install a Prometheus instance. You also need to configure Prometheus with a sidecar that can propagate Prometheus metrics to Cloud Monitoring.
Create a prometheus.yaml for the Prometheus Operator, but replace the variables for ${RPOJECT_ID},${LOCATION}, and ${CLUSTER_NAME}with your Kubernetes Engine cluster information.