go get commands to install the relevant dependencies: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. You can directly use the Handler function provided in prometheus/promhttp.
The following is a sample Go application, which exposes some default metrics (including runtime, process, and build metrics) through http://localhost:2112/metrics.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 to count the currently completed operations. The operation is performed once every 2 seconds, and the count increases by 1 each time.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_totalcounter, 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 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
port value is the spec/ports/name value in the Service YAML configuration file.apiVersion: monitoring.coreos.com/v1kind: ServiceMonitormetadata:name: go-demo # Enter a unique namenamespace: cm-prometheus # The namespace is fixed. Do not change itspec:endpoints:- interval: 30s# Enter the name of the corresponding port of the Prometheus exporter in the Service YAML configuration fileport: 2112# Enter the value of the corresponding path of the Prometheus exporter. If it is not specified, it will be `/metrics` by defaultpath: /metricsrelabelings:# ** 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: replacesourceLabels: [__meta_kubernetes_pod_label_app]targetLabel: application# Select the namespace where the Service to be monitored residesnamespaceSelector:matchNames:- golang-demo# Enter the label value of the Service to be monitored to locate the target Serviceselector:matchLabels:app: golang-app-demo
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.



Feedback