go get 命令来安装相关依赖,示例如下:go get github.com/prometheus/client_golang/prometheusgo get github.com/prometheus/client_golang/prometheus/promautogo get github.com/prometheus/client_golang/prometheus/promhttp
/metrics。可以直接使用 prometheus/promhttp 里提供的 Handler 函数。
如下是一个简单的示例应用,通过 http://localhost:2112/metrics 暴露 Golang 应用的一些默认指标数据(包括运行时指标、进程相关指标以及构建相关的指标)。package mainimport ("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 的 计数类型 指标,用于对目前已经完成的操作进行计数。如下每两秒操作一次,同时计数器加1。package mainimport ("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计数器相关的信息,包括帮助文档、类型信息、指标名和当前值,如下所示。# HELP myapp_processed_ops_total The total number of processed events# TYPE myapp_processed_ops_total countermyapp_processed_ops_total 666
FROM golang:alpine AS builderRUN apk add --no-cache ca-certificates \\make \\gitCOPY . /go-buildRUN cd /go-build && \\export GO111MODULE=on && \\export GOPROXY=https://goproxy.io && \\go build -o 'golang-exe' path/to/main/FROM alpineRUN apk add --no-cache tzdataCOPY --from=builder /etc/ssl/certs/ca-certificates.crt /etc/ssl/certsCOPY --from=builder /go-build/golang-exe /usr/bin/golang-exeENV TZ Asia/ShanghaiCMD ["golang-exe"]
apiVersion: apps/v1kind: Deploymentmetadata:name: golang-app-demolabels:app: golang-app-demospec:replicas: 3selector:matchLabels:app: golang-app-demotemplate:metadata:labels:app: golang-app-demospec:containers:- name: golang-exe-demo:v1image: nginx:1.14.2ports:- containerPort: 80
apiVersion: v1kind: Servicemetadata:name: golang-app-demospec:selector:app: golang-app-demoports:- protocol: TCPport: 80targetPort: 80
Service Monitor,目前支持基于 Labels 发现对应的目标实例地址,因此可以对一些服务添加特定的 K8S Labels,可以使 Labels 下的服务都会被 Prometheus 服务自动识别出来,不需要再为每个服务一一添加采取任务,以上面的例子配置信息如下:port 的取值为 service yaml 配置文件里的 spec/ports/name 对应的值。apiVersion: monitoring.coreos.com/v1kind: ServiceMonitormetadata:name: go-demo # 填写一个唯一名称namespace: cm-prometheus # namespace固定,不要修改spec:endpoints:- interval: 30s# 填写service yaml中Prometheus Exporter对应的Port的Nameport: 2112# 填写Prometheus Exporter对应的Path的值,不填默认/metricspath: /metricsrelabelings:# ** 必须要有一个 label 为 application,这里假设 k8s 有一个 label 为 app,# 我们通过 relabel 的 replace 动作把它替换成了 application- action: replacesourceLabels: [__meta_kubernetes_pod_label_app]targetLabel: application# 选择要监控service所在的namespacenamespaceSelector:matchNames:- golang-demo# 填写要监控service的Label值,以定位目标serviceselector:matchLabels:app: golang-app-demo




文档反馈