Junior — Middle
Have you had experience using libraries for integration with Prometheus?
sobes.tech AI
Answer from AI
Yes, there is an official client library for integrating with Prometheus in Go — the prometheus/client_golang package. It allows you to create and register metrics (counters, histograms, summaries, etc.), which can then be exported for Prometheus collection.
Example usage:
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 handler(w http.ResponseWriter, r *http.Request) {
opsProcessed.Inc()
w.Write([]byte("Hello, Prometheus!"))
}
func main() {
http.Handle("/metrics", promhttp.Handler())
http.HandleFunc("/", handler)
http.ListenAndServe(":8080", nil)
}
This approach makes it easy to integrate metrics into your application and collect them with Prometheus.