feat(agent): unify service statuses across monitor impls
This commit is contained in:
@@ -1,6 +1,22 @@
|
|||||||
package models
|
package models
|
||||||
|
|
||||||
|
// ServiceStatus represents the unified status of a service across all monitor types.
|
||||||
|
type ServiceStatus string
|
||||||
|
|
||||||
|
const (
|
||||||
|
StatusRunning ServiceStatus = "running"
|
||||||
|
StatusStopped ServiceStatus = "stopped"
|
||||||
|
StatusDegraded ServiceStatus = "degraded"
|
||||||
|
StatusPending ServiceStatus = "pending"
|
||||||
|
StatusUnknown ServiceStatus = "unknown"
|
||||||
|
)
|
||||||
|
|
||||||
|
// IsHealthy reports whether the service is stable enough for dependents to rely on.
|
||||||
|
func (s ServiceStatus) IsHealthy() bool {
|
||||||
|
return s == StatusRunning
|
||||||
|
}
|
||||||
|
|
||||||
type Service struct {
|
type Service struct {
|
||||||
Name string
|
Name string
|
||||||
Status string
|
Status ServiceStatus
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -36,7 +36,23 @@ func (self *DockerMonitor) CheckServices(ctx context.Context) ([]models.Service,
|
|||||||
return lo.Map(ctrs.Items, func(item container.Summary, _ int) models.Service {
|
return lo.Map(ctrs.Items, func(item container.Summary, _ int) models.Service {
|
||||||
return models.Service{
|
return models.Service{
|
||||||
Name: lo.If(len(item.Names) > 0, item.Names[0]).Else(item.ID),
|
Name: lo.If(len(item.Names) > 0, item.Names[0]).Else(item.ID),
|
||||||
Status: string(item.State), // TODO: map to standartized states enum
|
Status: mapContainerState(string(item.State)),
|
||||||
}
|
}
|
||||||
}), nil
|
}), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// mapContainerState maps Docker container states to unified ServiceStatus.
|
||||||
|
func mapContainerState(state string) models.ServiceStatus {
|
||||||
|
switch state {
|
||||||
|
case "running":
|
||||||
|
return models.StatusRunning
|
||||||
|
case "exited", "dead":
|
||||||
|
return models.StatusStopped
|
||||||
|
case "paused":
|
||||||
|
return models.StatusDegraded
|
||||||
|
case "restarting", "created", "removing":
|
||||||
|
return models.StatusPending
|
||||||
|
default:
|
||||||
|
return models.StatusUnknown
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -44,7 +44,23 @@ func (self *KubesMonitor) CheckServices(ctx context.Context) ([]models.Service,
|
|||||||
return lo.Map(pods.Items, func(item corev1.Pod, _ int) models.Service {
|
return lo.Map(pods.Items, func(item corev1.Pod, _ int) models.Service {
|
||||||
return models.Service{
|
return models.Service{
|
||||||
Name: item.Name,
|
Name: item.Name,
|
||||||
Status: string(item.Status.Phase), // TODO: map to standartized states enum
|
Status: mapPodPhase(item.Status.Phase),
|
||||||
}
|
}
|
||||||
}), nil
|
}), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// mapPodPhase maps K8s pod phases to unified ServiceStatus.
|
||||||
|
func mapPodPhase(phase corev1.PodPhase) models.ServiceStatus {
|
||||||
|
switch phase {
|
||||||
|
case corev1.PodRunning:
|
||||||
|
return models.StatusRunning
|
||||||
|
case corev1.PodSucceeded:
|
||||||
|
return models.StatusStopped
|
||||||
|
case corev1.PodFailed:
|
||||||
|
return models.StatusStopped
|
||||||
|
case corev1.PodPending:
|
||||||
|
return models.StatusPending
|
||||||
|
default:
|
||||||
|
return models.StatusUnknown
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user