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

1
gcloud services enable monitoring.googleapis.com
Copied!

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:
Maven
Gradle
1
<dependency>
2
<groupId>org.springframework.boot</groupId>
3
<artifactId>spring-boot-starter-actuator</artifactId>
4
</dependency>
5
<dependency>
6
<groupId>io.micrometer</groupId>
7
<artifactId>micrometer-registry-prometheus</artifactId>
8
</dependency>
Copied!
1
2
compile group: 'org.springframework.boot', name: 'spring-boot-starter-actuator'
3
compile group: 'io.micrometer', name: 'micrometer-registry-prometheus'
Copied!

Configuration

Configure Spring Boot Actuator to expose the Prometheus endpoint:
application.properties
1
management.endpoints.web.exposure.include=health,info,prometheus
Copied!
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.
1
$ curl http://localhost:8080/actuator/prometheus
2
3
# HELP jvm_memory_committed_bytes The amount of memory in bytes that is committed for the Java virtual machine to use
4
# TYPE jvm_memory_committed_bytes gauge
5
jvm_memory_committed_bytes{area="nonheap",id="Code Cache",} 1.8284544E7
6
jvm_memory_committed_bytes{area="nonheap",id="Metaspace",} 6.6281472E7
7
jvm_memory_committed_bytes{area="nonheap",id="Compressed Class Space",} 8609792.0
8
jvm_memory_committed_bytes{area="heap",id="PS Eden Space",} 6.01358336E8
9
jvm_memory_committed_bytes{area="heap",id="PS Survivor Space",} 2.2020096E7
10
jvm_memory_committed_bytes{area="heap",id="PS Old Gen",} 1.1010048E8
11
# HELP tomcat_global_sent_bytes_total
12
...
Copied!

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

1
kubectl apply -f \
2
https://raw.githubusercontent.com/coreos/prometheus-operator/v0.38.1/bundle.yaml
Copied!

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.
prometheus.yaml
1
# This config is cooked based on following resource:
2
# https://godoc.org/github.com/coreos/prometheus-operator/pkg/apis/monitoring/v1#Prometheus
3
# https://github.com/istio/installer/blob/master/istio-telemetry/prometheus-operator/templates/prometheus.yaml
4
apiVersion: monitoring.coreos.com/v1
5
kind: Prometheus
6
metadata:
7
name: prometheus
8
labels:
9
spec:
10
image: "docker.io/prom/prometheus:v2.12.0"
11
version: v2.12.0
12
retention: 720h
13
scrapeInterval: 15s
14
serviceAccountName: prometheus
15
serviceMonitorSelector:
16
any: true
17
serviceMonitorNamespaceSelector:
18
any: true
19
podMonitorSelector:
20
any: true
21
podMonitorNamespaceSelector:
22
any: true
23
enableAdminAPI: false
24
podMetadata:
25
labels:
26
app: prometheus
27
containers:
28
- name: sd-sidecar
29
image: gcr.io/stackdriver-prometheus/stackdriver-prometheus-sidecar:0.7.3
30
args:
31
- --stackdriver.project-id=${PROJECT_ID}
32
- --stackdriver.kubernetes.location=${LOCATION}
33
- --stackdriver.kubernetes.cluster-name=${CLUSTER_NAME}
34
- --prometheus.wal-directory=/prometheus/wal
35
ports:
36
- name: sidecar
37
containerPort: 9091
38
volumeMounts:
39
- name: prometheus-prometheus-db
40
mountPath: /prometheus
41
---
42
apiVersion: rbac.authorization.k8s.io/v1
43
kind: ClusterRole
44
metadata:
45
name: prometheus
46
labels:
47
app: prometheus
48
rules:
49
- apiGroups: [""]
50
resources:
51
- nodes
52
- services
53
- endpoints
54
- pods
55
- nodes/proxy
56
verbs: ["get", "list", "watch"]
57
- apiGroups: [""]
58
resources:
59
- configmaps
60
verbs: ["get"]
61
- nonResourceURLs: ["/metrics"]
62
verbs: ["get"]
63
---
64
apiVersion: rbac.authorization.k8s.io/v1
65
kind: ClusterRoleBinding
66
metadata:
67
name: prometheus-default
68
labels:
69
app: prometheus
70
roleRef:
71
apiGroup: rbac.authorization.k8s.io
72
kind: ClusterRole
73
name: prometheus
74
subjects:
75
- kind: ServiceAccount
76
name: prometheus
77
namespace: default
78
---
79
apiVersion: v1
80
kind: Service
81
metadata:
82
name: prometheus
83
annotations:
84
prometheus.io/scrape: 'true'
85
labels:
86
app: prometheus
87
spec:
88
selector:
89
app: prometheus
90
ports:
91
- name: http-prometheus
92
protocol: TCP
93
port: 9090
94
---
95
apiVersion: v1
96
kind: ServiceAccount
97
metadata:
98
name: prometheus
99
namespace: default
100
labels:
101
app: prometheus
Copied!
Apply prometheus.yamlto the Kubernetes cluster to provision a new instance using the Prometheus Operator.
1
kubectl apply -f prometheus.yaml
Copied!
Configure Prometheus server to scrape the metrics. Create a pod-monitors.yaml:
pod-monitors.yaml
1
apiVersion: monitoring.coreos.com/v1
2
kind: PodMonitor
3
metadata:
4
name: order-service
5
labels:
6
app: order-service
7
spec:
8
namespaceSelector:
9
any: true
10
selector:
11
matchLabels:
12
app: order-service
13
podMetricsEndpoints:
14
- targetPort: 8080
15
path: /actuator/prometheus
16
interval: 15s
Copied!

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:
Maven
Gradle
1
<dependency>
2
<groupId>org.springframework.boot</groupId>
3
<artifactId>spring-boot-starter-actuator</artifactId>
4
</dependency>
5
<dependency>
6
<groupId>io.micrometer</groupId>
7
<artifactId>micrometer-registry-stackdriver</artifactId>
8
</dependency>
Copied!
1
2
compile group: 'org.springframework.boot', name: 'spring-boot-starter-actuator'
3
compile group: 'io.micrometer', name: 'micrometer-registry-stackdriver'
Copied!

Configuration

Configure the Project ID.
application.properties
1
management.metrics.export.stackdriver.project-id=<PROJECT_ID>
Copied!
Better integration is coming soon with Spring Cloud GCP.

Learn More

Last modified 1yr ago