Prometheus provides an official Go library to collect and expose the monitoring data. This document describes how to use it to expose the Go runtime data and use TMP to collect metrics and display data with some basic samples.
Note:For Go client API documentation, please see Prometheus Go client library.
You can run the following go get
commands to install the relevant dependencies:
go get github.com/prometheus/client_golang/prometheus
go get github.com/prometheus/client_golang/prometheus/promauto
go get github.com/prometheus/client_golang/prometheus/promhttp
/metrics
. You can directly use the Handler
function provided in prometheus/promhttp
.http://localhost:2112/metrics
:package main
import (
"net/http"
"github.com/prometheus/client_golang/prometheus/promhttp"
)
func main() {
http.Handle("/metrics", promhttp.Handler())
http.ListenAndServe(":2112", nil)
}
go run main.go
curl http://localhost:2112/metrics
myapp_processed_ops_total
to count the currently completed operations. The operation is performed once every 2 seconds, and the count increases by 1 each time:package main
import (
"net/http"
"time"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
"github.com/prometheus/client_golang/prometheus/promhttp"
)
func recordMetrics() {
go func() {
for {
opsProcessed.Inc()
time.Sleep(2 * time.Second)
}
}()
}
var (
opsProcessed = promauto.NewCounter(prometheus.CounterOpts{
Name: "myapp_processed_ops_total",
Help: "The total number of processed events",
})
)
func main() {
recordMetrics()
http.Handle("/metrics", promhttp.Handler())
http.ListenAndServe(":2112", nil)
}
go run main.go
curl http://localhost:2112/metrics
myapp_processed_ops_total
counter, including the help documentation, type information, metric name, and current value, as shown below:
# HELP myapp_processed_ops_total The total number of processed events
# TYPE myapp_processed_ops_total counter
myapp_processed_ops_total 666
Two samples are used above to show how to use the Prometheus Go library to expose application metric data. However, because the exposed data is in text format, you’ll need to set up and maintain an additional Prometheus service to collect metrics, which may require additional Grafana dashboards for visual display.
In contrast, if you use TMP, you can directly skip the above steps and achieve the same purpose with just a few clicks. For more information, please see Getting Started.
FROM golang:alpine AS builder
RUN apk add --no-cache ca-certificates \
make \
git
COPY . /go-build
RUN cd /go-build && \
export GO111MODULE=on && \
export GOPROXY=https://goproxy.io && \
go build -o 'golang-exe' path/to/main/
FROM alpine
RUN apk add --no-cache tzdata
COPY --from=builder /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs
COPY --from=builder /go-build/golang-exe /usr/bin/golang-exe
ENV TZ Asia/Shanghai
CMD ["golang-exe"]
apiVersion: apps/v1
kind: Deployment
metadata:
name: golang-app-demo
labels:
app: golang-app-demo
spec:
replicas: 3
selector:
matchLabels:
app: golang-app-demo
template:
metadata:
labels:
app: golang-app-demo
spec:
containers:
- name: golang-exe-demo:v1
image: nginx:1.14.2
ports:
- containerPort: 80
apiVersion: v1
kind: Service
metadata:
name: golang-app-demo
spec:
selector:
app: golang-app-demo
ports:
- protocol: TCP
port: 80
targetPort: 80
NoteYou must add a label to identify the current application. The label name doesn't necessarily need to be
app
, but there must be a label with the similar meaning. You can add other extended labels by relabeling when adding a data collection task subsequently.
After the service runs, you need to configure TMP to discover and collect the monitoring metrics in the following steps:
Note:The
port
value is thespec/ports/name
value in the Service YAML configuration file.
apiVersion: monitoring.coreos.com/v1
kind: ServiceMonitor
metadata:
name: go-demo # Enter a unique name
namespace: cm-prometheus # The namespace is fixed. Do not change it
spec:
endpoints:
- interval: 30s
# Enter the name of the corresponding port of the Prometheus exporter in the Service YAML configuration file
port: 2112
# Enter the value of the corresponding path of the Prometheus exporter. If it is not specified, it will be `/metrics` by default
path: /metrics
relabelings:
# ** There must be a label named `application`. Here, suppose that K8s has a label named `app`
# Use the `replace` action of `relabel` to replace it with `application`
- action: replace
sourceLabels: [__meta_kubernetes_pod_label_app]
targetLabel: application
# Select the namespace where the Service to be monitored resides
namespaceSelector:
matchNames:
- golang-demo
# Enter the label value of the Service to be monitored to locate the target Service
selector:
matchLabels:
app: golang-app-demo
Note:You must configure the label named
application
in the sample; otherwise, you cannot use some other out-of-the-box integration features of TMP. For more advanced usage, please see ServiceMonitor or PodMonitor.
This document uses two samples to describe how to expose Go metrics to TMP and how to use the built-in visual charts to view monitoring data. This document only uses the Counter metrics. In other scenarios, you many need to use Gauge, Histogram, and Summary metrics. For more information, please see Metric Types.
For other use cases, TMP will integrate more frameworks to provide more out-of-the-box monitoring metrics, visual dashboards, and alerting templates.
Was this page helpful?