Files
elmprodvpn/selective-vpn-api/app/transport_client_runtime_health.go

86 lines
2.2 KiB
Go

package app
import (
"strings"
"time"
)
func buildTransportHealthResponse(it TransportClient, now time.Time) TransportClientHealthResponse {
health := it.Health
if strings.TrimSpace(health.LastCheck) == "" {
if strings.TrimSpace(it.UpdatedAt) != "" {
health.LastCheck = it.UpdatedAt
} else {
health.LastCheck = now.Format(time.RFC3339)
}
}
if strings.TrimSpace(health.LastError) == "" {
health.LastError = strings.TrimSpace(it.Runtime.LastError.Message)
}
rt := transportRuntimeSnapshot(it, now)
code := ""
if it.Status == TransportClientDegraded || strings.TrimSpace(health.LastError) != "" {
code = "TRANSPORT_CLIENT_DEGRADED"
}
return TransportClientHealthResponse{
OK: true,
Message: "ok",
Code: code,
ClientID: it.ID,
Kind: it.Kind,
Status: it.Status,
Latency: health.LatencyMS,
LastErr: health.LastError,
Health: health,
Runtime: rt,
}
}
func buildTransportMetricsResponse(it TransportClient, now time.Time) TransportClientMetricsResponse {
rt := transportRuntimeSnapshot(it, now)
return TransportClientMetricsResponse{
OK: true,
Message: "ok",
ClientID: it.ID,
Kind: it.Kind,
Status: it.Status,
Metrics: rt.Metrics,
Runtime: rt,
}
}
func applyTransportHealthProbeSnapshot(it TransportClient, backendID string, probe transportBackendHealthResult, now time.Time) TransportClient {
snapshot := it
ts := now.Format(time.RFC3339)
if strings.TrimSpace(backendID) != "" {
snapshot.Runtime.Backend = backendID
}
snapshot.Health.LastCheck = ts
if probe.LatencyMS > 0 {
snapshot.Health.LatencyMS = probe.LatencyMS
}
if probe.Status != "" {
snapshot.Status = normalizeTransportStatus(probe.Status)
}
if probe.OK {
if snapshot.Status != TransportClientDegraded {
snapshot.Health.LastError = ""
}
snapshot.Runtime.LastError = TransportClientError{}
} else {
msg := strings.TrimSpace(probe.Message)
if msg == "" {
msg = "transport health probe failed"
}
snapshot.Health.LastError = msg
snapshot.Runtime.LastError = TransportClientError{
Code: strings.TrimSpace(probe.Code),
Message: msg,
Retryable: probe.Retryable,
At: ts,
}
}
snapshot.Runtime = transportRuntimeSnapshot(snapshot, now)
return snapshot
}