Golang Metrics with Prometheus and Grafana
Collecting server metrics is crucial for maintaining optimal performance, security, and resource utilization in IT infrastructures. This document outlines the process of implementing server metrics collection using Prometheus and Grafana in a Go project.
Prometheus
Prometheus stands out as the premier tool for collecting metrics in GoLang. Its robust features make it ideal for monitoring various aspects of system performance. To integrate Prometheus into your project, follow the instructions provided here.
Grafana
Grafana offers a powerful monitoring and observability solution with its user-friendly interface and scalability. It is extensively utilized across industries for monitoring infrastructure, applications, and more. To leverage Grafana for visualizing server metrics, refer to its documentation here.
Getting Started
To begin collecting metrics in your Go project, start by importing the required library,
go get github.com/prometheus/client_golang
Attach relevant metrics to functions within your project to evaluate performance. Refer to the example code provided here for guidance.
Counter and Gauge metrics are define in example code as follow,
var (
opsProcessed = promauto.NewCounter(prometheus.CounterOpts{
Namespace: "go_metrics",
Subsystem: "prometheus",
Name: "processed_record_total",
Help: "process metrics count",
})
opsRequested = promauto.NewGauge(prometheus.GaugeOpts{
Namespace: "go_metrics",
Subsystem: "prometheus",
Name: "processed_record_count",
Help: "request record count",
})
)
name space is define as go_metrics and subsystem as promethues.
Starting the Metrics Server
Initiate the metrics server in your Go project by executing the following command,
export METRICS_PORT=:8181
go run main.go
Access the metrics via the following URL
http://localhost:8181/metrics
Metrics will be prefixed with go_metrics_*
.
Configuring Prometheus and Grafana with Docker
Ensure Docker and Docker Compose are installed on your system. Follow the instructions here for installation.
Docker Network Setup
After setup the docker in your environment. Let’s create a docker network mode as metrics,
docker network create metrics
Then run below docker compose,
version: "2"
services:
prometheus:
image: prom/prometheus:v2.45.3
container_name: prometheus
network_mode: metrics
ports:
- "9090:9090"
volumes:
- "${PROJECT_PATH}/prometheus.yml:/etc/prometheus/prometheus.yml"
grafana:
image: grafana/grafana:10.2.4
container_name: grafana
network_mode: metrics
ports:
- "3000:3000"
depends_on:
- prometheus
by using command:
./metrics-run.sh
Access the Prometheus dashboard at:
http://localhost:9090/graph
Server targets are already configured in prometheus.yml
.
Grafana Configuration
Access the Grafana dashboard at:
http://localhost:3000/
Configure Grafana by adding a prometheus database. Then import this dashboard json to your grafana.
Summary
Implementing server metrics collection with Prometheus and Grafana enhances the monitoring capabilities of your Go project. By following the steps outlined in this document, you can efficiently collect, visualize, and analyze server metrics to ensure optimal performance and resource utilization.