Files

53 lines
2.2 KiB
Go

package api
import (
"fmt"
"net/http"
"strconv"
"strings"
)
func (s *Server) prometheus(w http.ResponseWriter, r *http.Request) {
ps, err := s.store.List()
if err != nil {
fail(w, 500, "cannot load metrics")
return
}
var b strings.Builder
b.WriteString("# HELP linkport_gateway_running Gateway listener state.\n# TYPE linkport_gateway_running gauge\n")
for _, p := range ps {
v := 0
if s.manager.Status(p.ID).Running {
v = 1
}
fmt.Fprintf(&b, "linkport_gateway_running{gateway_id=%s,protocol=%s} %d\n", strconv.Quote(p.ID), strconv.Quote(p.Type), v)
}
fields := []struct {
name, help, kind string
value func(string) float64
}{
{"active_connections", "Accepted active connections.", "gauge", func(id string) float64 { return float64(s.manager.Metrics(id).ActiveConnections) }},
{"connection_attempts_total", "TCP connection attempts.", "counter", func(id string) float64 { return float64(s.manager.Metrics(id).ConnectionAttempts) }},
{"authentication_failures_total", "Gateway authentication failures.", "counter", func(id string) float64 { return float64(s.manager.Metrics(id).AuthenticationFailed) }},
{"authentication_successes_total", "Gateway authentication successes.", "counter", func(id string) float64 { return float64(s.manager.Metrics(id).AuthenticationSucceeded) }},
{"authentication_duration_seconds_sum", "Total authentication duration.", "counter", func(id string) float64 { return s.manager.Metrics(id).AuthenticationLatencyTotalMS / 1000 }},
}
for _, f := range fields {
fmt.Fprintf(&b, "# HELP linkport_%s %s\n# TYPE linkport_%s %s\n", f.name, f.help, f.name, f.kind)
for _, p := range ps {
fmt.Fprintf(&b, "linkport_%s{gateway_id=%s,protocol=%s} %g\n", f.name, strconv.Quote(p.ID), strconv.Quote(p.Type), f.value(p.ID))
}
}
_, health := s.manager.HealthSnapshot()
b.WriteString("# HELP linkport_target_healthy Last target health result.\n# TYPE linkport_target_healthy gauge\n")
for _, h := range health {
v := 0
if h.Healthy {
v = 1
}
fmt.Fprintf(&b, "linkport_target_healthy{target_id=%s,account_id=%s} %d\n", strconv.Quote(h.TargetID), strconv.Quote(h.AccountID), v)
}
w.Header().Set("Content-Type", "text/plain; version=0.0.4; charset=utf-8")
fmt.Fprint(w, b.String())
}