prometheus metrics endpoint
This commit is contained in:
@@ -10,8 +10,12 @@ import (
|
||||
"net/http/httputil"
|
||||
"net/url"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"github.com/caarlos0/env/v11"
|
||||
"github.com/prometheus/client_golang/prometheus"
|
||||
"github.com/prometheus/client_golang/prometheus/promauto"
|
||||
"github.com/prometheus/client_golang/prometheus/promhttp"
|
||||
)
|
||||
|
||||
var cfg config
|
||||
@@ -23,12 +27,14 @@ type config struct {
|
||||
|
||||
func createProxy(target *url.URL) func(http.ResponseWriter, *http.Request) {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
r.URL.Path = strings.TrimPrefix(r.URL.Path, "/proxy")
|
||||
r.Host = target.Host
|
||||
r.URL.Scheme = target.Scheme
|
||||
r.URL.Host = target.Host
|
||||
|
||||
director := func(req *http.Request) {
|
||||
req.Header.Set("X-Forwarded-For", r.RemoteAddr)
|
||||
req.URL = r.URL
|
||||
req.Host = target.Host
|
||||
req.URL.Scheme = target.Scheme
|
||||
req.URL.Host = target.Host
|
||||
@@ -81,9 +87,15 @@ func handleJsonLine(line []byte) {
|
||||
if jsonData["done"].(bool) {
|
||||
duration := jsonData["eval_duration"].(float64)
|
||||
fmt.Printf("Duration: %.2f seconds\n", duration/1000000000.0)
|
||||
opsProcessed.Inc()
|
||||
}
|
||||
}
|
||||
|
||||
var opsProcessed = promauto.NewCounter(prometheus.CounterOpts{
|
||||
Name: "total_requests",
|
||||
Help: "The total number of processed events",
|
||||
})
|
||||
|
||||
func main() {
|
||||
err := env.Parse(&cfg)
|
||||
if err != nil {
|
||||
@@ -95,7 +107,8 @@ func main() {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
http.HandleFunc("/", createProxy(targetURL))
|
||||
http.HandleFunc("/proxy/", createProxy(targetURL))
|
||||
http.Handle("/metrics", promhttp.Handler())
|
||||
|
||||
log.Printf("Starting proxy server on :%s", strconv.Itoa(cfg.Port))
|
||||
err = http.ListenAndServe(fmt.Sprintf(":%d", cfg.Port), nil)
|
||||
|
||||
Reference in New Issue
Block a user