Small knowledge, big challenge! This article is participating in the creation activity of “Essential Tips for Programmers”.

This article has participated in the “Digitalstar Project” and won a creative gift package to challenge the creative incentive money.

One, foreword

Prometheus provides the official Golang library for collecting and exposing monitoring data, and this article gives you a quick guide to using the official Golang Library to expose Golang Runtime data and some basic examples. And use Prometheus monitoring service to collect indicator presentation data.

TIPS:

For comprehensive API documentation, see the GoDoc documentation for Prometheus’ various Go libraries.

Second, the installation

To install the dependent libraries, run the go get command as shown in the following example:

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
Copy the code

Install the Prometheus, Promauto, and Promhttp libraries as required.

$time go get github.com/prometheus/client_golang/prometheus real 0 m2. The user 0 m0. 054 s, 000 s sys 0 m0. 015 s $time go a get M4 github.com/prometheus/client_golang/prometheus/promauto real 0. 368 s user 0 m0. 000 s sys 0 m0. 015 s $time go a get M3 github.com/prometheus/client_golang/prometheus/promhttp real 0. 109 s user 0 m0. 000 s sys 0 m0. 047 sCopy the code

Fast start (Runtime metrics)

Prepare an HTTP service, usually using /metrics. You can use the Handler functions provided in Prometheus /promhttp directly. The following is a simple example of application, through http://localhost:2112/metrics exposed Golang application some default index data (including runtime index, process index and related building related indicators) :

package main

import (
	"net/http"

	"github.com/prometheus/client_golang/prometheus/promhttp"
)

func main(a) {
	// Provide the /metrics HTTP endpoint
	http.Handle("/metrics", promhttp.Handler())
	/ / the port number
	http.ListenAndServe(": 2112".nil)}Copy the code

Run the following command to start the application:

go run main.go
Copy the code

Run the following command to access the basic built-in indicator data:

curl http://localhost:2112/metrics
Copy the code
# HELP go_gc_duration_seconds A summary of the pause duration of garbage collection cycles.
# TYPE go_gc_duration_seconds summary
go_gc_duration_seconds{quantile="0"} 0
go_gc_duration_seconds{quantile="0.25"} 0
go_gc_duration_seconds{quantile="0.5"} 0
go_gc_duration_seconds{quantile="0.75"} 0
go_gc_duration_seconds{quantile="1"} 0
go_gc_duration_seconds_sum 0
go_gc_duration_seconds_count 0
# HELP go_goroutines Number of goroutines that currently exist.
# TYPE go_goroutines gauge
go_goroutines 7
# HELP go_info Information about the Go environment.
# TYPE go_info gauge
go_info{version="go1.16.7"1}# HELP go_memstats_alloc_bytes Number of bytes allocated and still in use.
# TYPE go_memstats_alloc_bytes gauge
go_memstats_alloc_bytes 611920
# HELP go_memstats_alloc_bytes_total Total number of bytes allocated, even if freed.
# TYPE go_memstats_alloc_bytes_total counter
go_memstats_alloc_bytes_total 611920
# HELP go_memstats_buck_hash_sys_bytes Number of bytes used by the profiling bucket hash table.
# TYPE go_memstats_buck_hash_sys_bytes gauge
go_memstats_buck_hash_sys_bytes 4081
# HELP go_memstats_frees_total Total number of frees.
# TYPE go_memstats_frees_total counter
go_memstats_frees_total 112
# HELP go_memstats_gc_cpu_fraction The fraction of this program's available CPU time used by the GC since the program started.
# TYPE go_memstats_gc_cpu_fraction gauge
go_memstats_gc_cpu_fraction 0
# HELP go_memstats_gc_sys_bytes Number of bytes used for garbage collection system metadata.
# TYPE go_memstats_gc_sys_bytes gauge3.962144 e+06 go_memstats_gc_sys_bytes# HELP go_memstats_heap_alloc_bytes Number of heap bytes allocated and still in use.
# TYPE go_memstats_heap_alloc_bytes gauge
go_memstats_heap_alloc_bytes 611920
# HELP go_memstats_heap_idle_bytes Number of heap bytes waiting to be used.
# TYPE go_memstats_heap_idle_bytes gauge6.5273856 e+07 go_memstats_heap_idle_bytes# HELP go_memstats_heap_inuse_bytes Number of heap bytes that are in use.
# TYPE go_memstats_heap_inuse_bytes gauge1.47456 e+06 go_memstats_heap_inuse_bytes# HELP go_memstats_heap_objects Number of allocated objects.
# TYPE go_memstats_heap_objects gauge
go_memstats_heap_objects 2296
# HELP go_memstats_heap_released_bytes Number of heap bytes released to OS.
# TYPE go_memstats_heap_released_bytes gauge6.5273856 e+07 go_memstats_heap_released_bytes# HELP go_memstats_heap_sys_bytes Number of heap bytes obtained from system.
# TYPE go_memstats_heap_sys_bytes gauge6.6748416 e+07 go_memstats_heap_sys_bytes# HELP go_memstats_last_gc_time_seconds Number of seconds since 1970 of last garbage collection.
# TYPE go_memstats_last_gc_time_seconds gauge
go_memstats_last_gc_time_seconds 0
# HELP go_memstats_lookups_total Total number of pointer lookups.
# TYPE go_memstats_lookups_total counter
go_memstats_lookups_total 0
# HELP go_memstats_mallocs_total Total number of mallocs.
# TYPE go_memstats_mallocs_total counter
go_memstats_mallocs_total 2408
# HELP go_memstats_mcache_inuse_bytes Number of bytes in use by mcache structures.
# TYPE go_memstats_mcache_inuse_bytes gauge
go_memstats_mcache_inuse_bytes 14400
# HELP go_memstats_mcache_sys_bytes Number of bytes used for mcache structures obtained from system.
# TYPE go_memstats_mcache_sys_bytes gauge
go_memstats_mcache_sys_bytes 16384
# HELP go_memstats_mspan_inuse_bytes Number of bytes in use by mspan structures.
# TYPE go_memstats_mspan_inuse_bytes gauge
go_memstats_mspan_inuse_bytes 37672
# HELP go_memstats_mspan_sys_bytes Number of bytes used for mspan structures obtained from system.
# TYPE go_memstats_mspan_sys_bytes gauge
go_memstats_mspan_sys_bytes 49152
# HELP go_memstats_next_gc_bytes Number of heap bytes when next garbage collection will take place.
# TYPE go_memstats_next_gc_bytes gauge4.473924 e+06 go_memstats_next_gc_bytes# HELP go_memstats_other_sys_bytes Number of bytes used for other system allocations.
# TYPE go_memstats_other_sys_bytes gauge1.033983 e+06 go_memstats_other_sys_bytes# HELP go_memstats_stack_inuse_bytes Number of bytes in use by the stack allocator.
# TYPE go_memstats_stack_inuse_bytes gauge
go_memstats_stack_inuse_bytes 360448
# HELP go_memstats_stack_sys_bytes Number of bytes obtained from system for stack allocator.
# TYPE go_memstats_stack_sys_bytes gauge
go_memstats_stack_sys_bytes 360448
# HELP go_memstats_sys_bytes Number of bytes obtained from system.
# TYPE go_memstats_sys_bytes gauge7.2174608 e+07 go_memstats_sys_bytes# HELP go_threads Number of OS threads created.
# TYPE go_threads gauge
go_threads 7
# HELP promhttp_metric_handler_requests_in_flight Current number of scrapes being served.
# TYPE promhttp_metric_handler_requests_in_flight gauge
promhttp_metric_handler_requests_in_flight 1
# HELP promhttp_metric_handler_requests_total Total number of scrapes by HTTP status code.
# TYPE promhttp_metric_handler_requests_total counter
promhttp_metric_handler_requests_total{code="200"} 0
promhttp_metric_handler_requests_total{code="500"} 0
promhttp_metric_handler_requests_total{code="503"} 0
Copy the code

4. Application level indicators

The application exposes only the default Go metric. We can also register custom application specifications for metrics. The following example application exposes the myapp_processed_OPs_total counter, which counts the number of operations that have been processed so far. Every 2 seconds, the counter increases by 1.

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(a) {
	// Every 2 seconds, the counter increases by 1.
	go func(a) {
		for {
			opsProcessed.Inc()
			time.Sleep(2 * time.Second)
		}
	}()
}

// Public myapp_processed_ops_total counter
var (
	opsProcessed = promauto.NewCounter(prometheus.CounterOpts{
		Name: "myapp_processed_ops_total",
		Help: "The total number of processed events",}))func main(a) {
	recordMetrics()

	http.Handle("/metrics", promhttp.Handler())
	http.ListenAndServe(": 2112".nil)}Copy the code

Run the following command to start the application:

go run main.go
Copy the code

Execute the following command to access exposed metrics:

curl http://localhost:2112/metrics
Copy the code

From the output, you can see information about the myapp_processed_OPs_total counter, including help text, type information, indicator name, and current value.

As follows:

.# HELP myapp_processed_ops_total The total number of processed events
# TYPE myapp_processed_ops_total counter
myapp_processed_ops_total 4
......
Copy the code

We can configure an instance of Prometheus to get metrics from the application. Here is an example of a Promethe.yml configuration:

scrape_configs:
- job_name: myapp
  scrape_interval: 10s
  static_configs:
  - targets:
    - 172.16106.251.: 2112
Copy the code

Five, the summary

This article showed two examples of how to expose Golang-related metrics to the Prometheus monitoring service and how to view monitoring data using built-in visual charts.

In this article, only indicators of the Counter type are used. For other scenarios, indicators of the Gauge, Histgram, and Summary types may also be required.

Source code address:

  • ​Github.com/zuozewei/bl…

References:

  • [1] : Prometheus. IO/docs/guides…