api: remove service health from prometheus, implement godoxy metrics

This commit is contained in:
yusing
2025-02-12 05:30:34 +08:00
parent 72dc76ec74
commit c807b30c8f
21 changed files with 531 additions and 152 deletions

View File

@@ -11,6 +11,7 @@ import (
config "github.com/yusing/go-proxy/internal/config/types"
"github.com/yusing/go-proxy/internal/logging"
"github.com/yusing/go-proxy/internal/logging/memlogger"
"github.com/yusing/go-proxy/internal/metrics/uptime"
"github.com/yusing/go-proxy/internal/utils/strutils"
)
@@ -39,8 +40,10 @@ func NewHandler(cfg config.ConfigInstance) http.Handler {
mux.HandleFunc("GET", "/v1/logs/ws", auth.RequireAuth(memlogger.LogsWS(cfg)))
mux.HandleFunc("GET", "/v1/favicon", auth.RequireAuth(favicon.GetFavIcon))
mux.HandleFunc("POST", "/v1/homepage/set", auth.RequireAuth(v1.SetHomePageOverrides))
mux.HandleFunc("GET", "/v1/system_info", auth.RequireAuth(useCfg(cfg, v1.SystemInfo)))
mux.HandleFunc("GET", "/v1/system_info/{agent_name}", auth.RequireAuth(useCfg(cfg, v1.SystemInfo)))
mux.HandleFunc("GET", "/v1/metrics/system_info", auth.RequireAuth(useCfg(cfg, v1.SystemInfo)))
mux.HandleFunc("GET", "/v1/metrics/system_info/ws", auth.RequireAuth(useCfg(cfg, v1.SystemInfo)))
mux.HandleFunc("GET", "/v1/metrics/uptime", auth.RequireAuth(uptime.Poller.ServeHTTP))
mux.HandleFunc("GET", "/v1/metrics/uptime/ws", auth.RequireAuth(useCfg(cfg, uptime.Poller.ServeWS)))
if common.PrometheusEnabled {
mux.Handle("GET /v1/metrics", promhttp.Handler())

View File

@@ -2,29 +2,34 @@ package v1
import (
"net/http"
"strings"
"github.com/coder/websocket/wsjson"
agentPkg "github.com/yusing/go-proxy/agent/pkg/agent"
U "github.com/yusing/go-proxy/internal/api/v1/utils"
config "github.com/yusing/go-proxy/internal/config/types"
"github.com/yusing/go-proxy/internal/metrics"
"github.com/yusing/go-proxy/internal/metrics/systeminfo"
)
func SystemInfo(cfg config.ConfigInstance, w http.ResponseWriter, r *http.Request) {
agentName := r.FormValue("agent_name")
isWS := strings.HasSuffix(r.URL.Path, "/ws")
agentName := r.URL.Query().Get("agent_name")
if agentName == "" {
info, err := metrics.GetSystemInfo(r.Context())
if err != nil {
U.HandleErr(w, r, err)
return
if isWS {
systeminfo.Poller.ServeWS(cfg, w, r)
} else {
systeminfo.Poller.ServeHTTP(w, r)
}
U.RespondJSON(w, r, info)
} else {
agent, ok := cfg.GetAgent(agentName)
if !ok {
U.HandleErr(w, r, U.ErrInvalidKey("agent_name"), http.StatusNotFound)
return
}
respData, status, err := agent.Fetch(r.Context(), agentPkg.EndpointSystemInfo)
return
}
agent, ok := cfg.GetAgent(agentName)
if !ok {
U.HandleErr(w, r, U.ErrInvalidKey("agent_name"), http.StatusNotFound)
return
}
if !isWS {
respData, status, err := agent.Forward(r, agentPkg.EndpointSystemInfo)
if err != nil {
U.HandleErr(w, r, err)
return
@@ -33,6 +38,35 @@ func SystemInfo(cfg config.ConfigInstance, w http.ResponseWriter, r *http.Reques
http.Error(w, string(respData), status)
return
}
U.RespondJSON(w, r, respData)
U.WriteBody(w, respData)
} else {
clientConn, err := U.InitiateWS(cfg, w, r)
if err != nil {
U.HandleErr(w, r, err)
return
}
agentConn, _, err := agent.Websocket(r.Context(), agentPkg.EndpointSystemInfo)
if err != nil {
U.HandleErr(w, r, err)
return
}
//nolint:errcheck
defer agentConn.CloseNow()
var data []byte
for {
select {
case <-r.Context().Done():
return
default:
err := wsjson.Read(r.Context(), agentConn, &data)
if err == nil {
err = wsjson.Write(r.Context(), clientConn, data)
}
if err != nil {
U.HandleErr(w, r, err)
return
}
}
}
}
}

View File

@@ -1,7 +1,9 @@
package utils
import (
"context"
"encoding/json"
"errors"
"net/http"
E "github.com/yusing/go-proxy/internal/error"
@@ -17,7 +19,13 @@ func HandleErr(w http.ResponseWriter, r *http.Request, err error, code ...int) {
if err == nil {
return
}
if errors.Is(err, context.Canceled) {
return
}
LogError(r).Msg(err.Error())
if r.Header.Get("Upgrade") == "websocket" {
return
}
if len(code) == 0 {
code = []int{http.StatusInternalServerError}
}

View File

@@ -60,7 +60,7 @@ func PeriodicWS(cfg config.ConfigInstance, w http.ResponseWriter, r *http.Reques
return
case <-ticker.C:
if err := do(conn); err != nil {
LogError(r).Msg(err.Error())
HandleErr(w, r, err)
return
}
}