mirror of
https://github.com/yusing/godoxy.git
synced 2026-04-25 09:48:32 +02:00
implement godoxy-agent
This commit is contained in:
75
internal/watcher/health/monitor/agent_route.go
Normal file
75
internal/watcher/health/monitor/agent_route.go
Normal file
@@ -0,0 +1,75 @@
|
||||
package monitor
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"net/http"
|
||||
"net/url"
|
||||
|
||||
agentPkg "github.com/yusing/go-proxy/agent/pkg/agent"
|
||||
"github.com/yusing/go-proxy/internal/net/types"
|
||||
"github.com/yusing/go-proxy/internal/watcher/health"
|
||||
)
|
||||
|
||||
type (
|
||||
AgentRouteMonior struct {
|
||||
agent *agentPkg.AgentConfig
|
||||
endpointURL string
|
||||
*monitor
|
||||
}
|
||||
AgentCheckHealthTarget struct {
|
||||
Scheme string
|
||||
Host string
|
||||
Path string
|
||||
}
|
||||
)
|
||||
|
||||
func AgentCheckHealthTargetFromURL(url *types.URL) *AgentCheckHealthTarget {
|
||||
return &AgentCheckHealthTarget{
|
||||
Scheme: url.Scheme,
|
||||
Host: url.Host,
|
||||
Path: url.Path,
|
||||
}
|
||||
}
|
||||
|
||||
func (target *AgentCheckHealthTarget) buildQuery() string {
|
||||
query := make(url.Values, 3)
|
||||
query.Set("scheme", target.Scheme)
|
||||
query.Set("host", target.Host)
|
||||
query.Set("path", target.Path)
|
||||
return query.Encode()
|
||||
}
|
||||
|
||||
func (target *AgentCheckHealthTarget) displayURL() *types.URL {
|
||||
return types.NewURL(&url.URL{
|
||||
Scheme: target.Scheme,
|
||||
Host: target.Host,
|
||||
Path: target.Path,
|
||||
})
|
||||
}
|
||||
|
||||
func NewAgentRouteMonitor(agent *agentPkg.AgentConfig, config *health.HealthCheckConfig, target *AgentCheckHealthTarget) *AgentRouteMonior {
|
||||
mon := &AgentRouteMonior{
|
||||
agent: agent,
|
||||
endpointURL: agentPkg.EndpointHealth + "?" + target.buildQuery(),
|
||||
}
|
||||
mon.monitor = newMonitor(target.displayURL(), config, mon.CheckHealth)
|
||||
return mon
|
||||
}
|
||||
|
||||
func (mon *AgentRouteMonior) CheckHealth() (result *health.HealthCheckResult, err error) {
|
||||
result = new(health.HealthCheckResult)
|
||||
ctx, cancel := mon.ContextWithTimeout("timeout querying agent")
|
||||
defer cancel()
|
||||
data, status, err := mon.agent.Fetch(ctx, mon.endpointURL)
|
||||
if err != nil {
|
||||
return result, err
|
||||
}
|
||||
switch status {
|
||||
case http.StatusOK:
|
||||
err = json.Unmarshal(data, result)
|
||||
default:
|
||||
err = errors.New(string(data))
|
||||
}
|
||||
return
|
||||
}
|
||||
@@ -12,10 +12,9 @@ type FileServerHealthMonitor struct {
|
||||
path string
|
||||
}
|
||||
|
||||
func NewFileServerHealthMonitor(alias string, config *health.HealthCheckConfig, path string) *FileServerHealthMonitor {
|
||||
func NewFileServerHealthMonitor(config *health.HealthCheckConfig, path string) *FileServerHealthMonitor {
|
||||
mon := &FileServerHealthMonitor{path: path}
|
||||
mon.monitor = newMonitor(nil, config, mon.CheckHealth)
|
||||
mon.service = alias
|
||||
return mon
|
||||
}
|
||||
|
||||
|
||||
@@ -11,9 +11,9 @@ import (
|
||||
|
||||
type (
|
||||
HealthCheckResult struct {
|
||||
Healthy bool
|
||||
Detail string
|
||||
Latency time.Duration
|
||||
Healthy bool `json:"healthy"`
|
||||
Detail string `json:"detail"`
|
||||
Latency time.Duration `json:"latency"`
|
||||
}
|
||||
WithHealthInfo interface {
|
||||
Status() Status
|
||||
|
||||
Reference in New Issue
Block a user