Sobes.tech
Senior

How were the metrics configured? Did the service push metrics or pull them?

sobes.tech AI

Answer from AI

Metrics in services are usually configured in two main ways: push and pull.

  • Push model: services themselves send (push) metrics to the monitoring system or an intermediate collector (e.g., Prometheus Pushgateway). This is convenient for short-term or unstable services that cannot be constantly available for polling.

  • Pull model: the monitoring system periodically polls (pulls) services via HTTP or another protocol to obtain current metrics. This is the standard approach in Prometheus, where services expose metrics on a special endpoint (/metrics).

In my projects, the pull model with Prometheus was more commonly used: services in Go exposed metrics via HTTP, and Prometheus collected them regularly. This simplifies architecture and reduces load on services.

Example of exposing metrics in Go using the Prometheus library:

import (
    "github.com/prometheus/client_golang/prometheus"
    "github.com/prometheus/client_golang/prometheus/promhttp"
    "net/http"
)

var opsProcessed = prometheus.NewCounter(prometheus.CounterOpts{
    Name: "myapp_processed_ops_total",
    Help: "The total number of processed events",
})

func init() {
    prometheus.MustRegister(opsProcessed)
}

func main() {
    http.Handle("/metrics", promhttp.Handler())
    go func() {
        for {
            opsProcessed.Inc()
            time.Sleep(time.Second)
        }
    }()
    http.ListenAndServe(":2112", nil)
}

Thus, the service itself provides metrics, and the monitoring system collects them.