Junior — Middle
How do you monitor and track the status of a service or application during its operation?
sobes.tech AI
Answer from AI
To control and track the state of a service or application in Go, several approaches are commonly used:
- Logging — recording important events and errors in log files or external systems (e.g., ELK, Graylog).
- Metrics — collecting statistics (CPU load, memory, request count, errors) using libraries like Prometheus client.
- Tracing — tracking call chains and delays with OpenTelemetry or Jaeger.
- Health Checks — endpoints that return the service status (e.g.,
/healthz) so orchestration systems (like Kubernetes) can check availability.
Example of a simple health check in Go:
http.HandleFunc("/healthz", func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
w.Write([]byte("OK"))
})
log.Fatal(http.ListenAndServe(":8080", nil))
By combining logs, metrics, and health checks, you can effectively monitor the application's state during operation.