-
Notifications
You must be signed in to change notification settings - Fork 18
/
health.go
172 lines (149 loc) · 5.7 KB
/
health.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
/***************************************************************
*
* Copyright (C) 2024, Pelican Project, Morgridge Institute for Research
*
* Licensed under the Apache License, Version 2.0 (the "License"); you
* may not use this file except in compliance with the License. You may
* obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
***************************************************************/
package metrics
import (
"fmt"
"sync"
"time"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
)
type (
// This is for API response so we want to display string representation of status
ComponentStatus struct {
Status string `json:"status"`
Message string `json:"message,omitempty"`
LastUpdate int64 `json:"last_update"`
}
componentStatusInternal struct {
Status HealthStatusEnum
Message string
LastUpdate time.Time
}
HealthStatus struct {
OverallStatus string `json:"status"`
ComponentStatus map[HealthStatusComponent]ComponentStatus `json:"components"`
}
HealthStatusEnum int
HealthStatusComponent string
)
// HealthStatusEnum are stored as Prometheus values and internal struct
const (
StatusCritical HealthStatusEnum = iota + 1
StatusWarning
StatusOK
StatusUnknown // Do not abuse this enum. Use others when possible
)
const statusIndexErrorMessage = "Error: status string index out of range"
// Naming convention for components:
//
// ServiceName1Name2_ComponentName
//
// i.e. For ""OriginCache_XRootD", it means this component is available at both
// Origin and Cache. Please come up with the largest possible scope of the component
const (
OriginCache_XRootD HealthStatusComponent = "xrootd"
OriginCache_CMSD HealthStatusComponent = "cmsd"
OriginCache_Federation HealthStatusComponent = "federation" // Advertise to the director
OriginCache_Director HealthStatusComponent = "director" // File transfer tests with director
OriginCache_Registry HealthStatusComponent = "registry" // Register namespace at the registry
DirectorRegistry_Topology HealthStatusComponent = "topology" // Fetch data from OSDF topology
Server_WebUI HealthStatusComponent = "web-ui"
)
var (
healthStatus = sync.Map{} // In-memory map of component health status, key is HealthStatusComponent, value is componentStatusInternal
PelicanHealthStatus = promauto.NewGaugeVec(prometheus.GaugeOpts{
Name: "pelican_component_health_status",
Help: "The health status of various components",
}, []string{"component"})
PelicanHealthLastUpdate = promauto.NewGaugeVec(prometheus.GaugeOpts{
Name: "pelican_component_health_status_last_update",
Help: "Last update timestamp of components health status",
}, []string{"component"})
)
// Unfortunately we don't have a better way to ensure the enum constants always have
// matched string representation, so we will return "Error: status string index out of range"
// as an indicator
func (status HealthStatusEnum) String() string {
strings := [...]string{"critical", "warning", "ok", "unknown"}
if int(status) < 1 || int(status) > len(strings) {
return statusIndexErrorMessage
}
return strings[status-1]
}
func (component HealthStatusComponent) String() string {
return string(component)
}
// Add/update the component health status. If you have a new component to record,
// please go to metrics/health and register your component as a new constant of
// type HealthStatusComponent. Also note that StatusUnknown is mostly for internal
// use only, please try to avoid setting this as your component status
func SetComponentHealthStatus(name HealthStatusComponent, state HealthStatusEnum, msg string) {
now := time.Now()
healthStatus.Store(name, componentStatusInternal{state, msg, now})
PelicanHealthStatus.With(
prometheus.Labels{"component": name.String()}).
Set(float64(state))
PelicanHealthLastUpdate.With(prometheus.Labels{"component": name.String()}).
SetToCurrentTime()
}
func DeleteComponentHealthStatus(name HealthStatusComponent) {
healthStatus.Delete(name)
}
func GetHealthStatus() HealthStatus {
status := HealthStatus{}
status.OverallStatus = StatusUnknown.String()
overallStatus := StatusUnknown
healthStatus.Range(func(component, compstat any) bool {
componentStatus, ok := compstat.(componentStatusInternal)
if !ok {
return true
}
componentString, ok := component.(HealthStatusComponent)
if !ok {
return true
}
if status.ComponentStatus == nil {
status.ComponentStatus = make(map[HealthStatusComponent]ComponentStatus)
}
status.ComponentStatus[componentString] = ComponentStatus{
componentStatus.Status.String(),
componentStatus.Message,
componentStatus.LastUpdate.Unix(),
}
if componentStatus.Status < overallStatus {
overallStatus = componentStatus.Status
}
return true
})
status.OverallStatus = overallStatus.String()
return status
}
// Get the current health status of a component.
// Status can be critical|warning|ok|unknown
func GetComponentStatus(comp HealthStatusComponent) (status string, err error) {
component, ok := healthStatus.Load(comp)
if !ok {
return "", fmt.Errorf("component %s does not exist", comp.String())
}
statusInt, ok := component.(componentStatusInternal)
if !ok {
return "", fmt.Errorf("wrong format of component status for component %s", comp.String())
}
return statusInt.Status.String(), nil
}