Senior
Have you ever described anything for observability — metrics, alerts?
sobes.tech AI
Answer from AI
For observability in Go projects, I described metrics using the Prometheus client_golang library. I usually created counters for counting events, histograms for measuring execution time, and gauges for current values.
Example of creating and registering a metric:
var httpRequestsTotal = prometheus.NewCounterVec(
prometheus.CounterOpts{
Name: "http_requests_total",
Help: "Number of HTTP requests",
},
[]string{"method", "endpoint"},
)
func init() {
prometheus.MustRegister(httpRequestsTotal)
}
func handler(w http.ResponseWriter, r *http.Request) {
httpRequestsTotal.WithLabelValues(r.Method, r.URL.Path).Inc()
// handle request
}
For alerts, I usually set up rules in Prometheus Alertmanager, for example, if the number of errors exceeds a threshold over a certain period. This allows for prompt response to service issues.