Sobes.tech
Middle

What standard set of metrics is used in a Go program with Prometheus?

sobes.tech AI

Answer from AI

The most common standard set of metrics for monitoring Go programs using Prometheus includes:

  • process_cpu_seconds_total: Total CPU time consumed by the process.
  • process_virtual_memory_bytes: Virtual memory size of the process.
  • process_resident_memory_bytes: Resident memory size of the process.
  • go_goroutines: Number of active goroutines.
  • go_threads: Number of OS threads.
  • go_memstats_alloc_bytes: Number of bytes allocated for heap objects.
  • go_memstats_heap_alloc_bytes: Number of bytes allocated for heap objects.
  • go_memstats_stack_inuse_bytes: Number of bytes used for goroutine stacks.
  • go_memstats_gc_sys_bytes: Memory used for garbage collector metadata.
  • go_gc_duration_seconds: Time spent on garbage collection.

In addition to these basic metrics, application-specific metrics are often added, such as:

  • Counters for requests/errors to external services (database, API).
  • Duration of key operations.
  • Message queue metrics.

Example of exporting basic metrics:

package main

import (
	"net/http"

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

func main() {
	// Register the standard Prometheus metrics handler
	http.Handle("/metrics", promhttp.Handler())
	// Start the HTTP server for exporting metrics
	http.ListenAndServe(":2112", nil)
}