Metrics

Cloud Monitoring

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

gcloud services enable monitoring.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:

<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
  <groupId>io.micrometer</groupId>
  <artifactId>micrometer-registry-prometheus</artifactId>
</dependency>

Configuration

Configure Spring Boot Actuator to expose the Prometheus endpoint:

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.

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.

Install Prometheus Operator

Provisioning Prometheus server with Sidecar

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.

Apply prometheus.yamlto the Kubernetes cluster to provision a new instance using the Prometheus Operator.

Configure Prometheus server to scrape the metrics. Create a pod-monitors.yaml:

Cloud Monitoring API

To export metrics directly to Cloud Monitoring, you can use the Micrometer Stackdriver registry.

Dependency

Add Spring Boot Actuator and Micrometer Stackdriver dependencies:

Configuration

Configure the Project ID.

Learn More

Last updated

Was this helpful?