Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2019-present Open Networking Foundation |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | package probe |
| 17 | |
| 18 | import ( |
| 19 | "context" |
| 20 | "fmt" |
Girish Gowdra | 89c985b | 2020-10-14 15:02:09 -0700 | [diff] [blame] | 21 | "github.com/opencord/voltha-lib-go/v4/pkg/log" |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 22 | "net/http" |
| 23 | "sync" |
| 24 | ) |
| 25 | |
| 26 | // ProbeContextKey used to fetch the Probe instance from a context |
| 27 | type ProbeContextKeyType string |
| 28 | |
| 29 | // ServiceStatus typed values for service status |
| 30 | type ServiceStatus int |
| 31 | |
| 32 | const ( |
| 33 | // ServiceStatusUnknown initial state of services |
| 34 | ServiceStatusUnknown ServiceStatus = iota |
| 35 | |
| 36 | // ServiceStatusPreparing to optionally be used for prep, such as connecting |
| 37 | ServiceStatusPreparing |
| 38 | |
| 39 | // ServiceStatusPrepared to optionally be used when prep is complete, but before run |
| 40 | ServiceStatusPrepared |
| 41 | |
| 42 | // ServiceStatusRunning service is functional |
| 43 | ServiceStatusRunning |
| 44 | |
| 45 | // ServiceStatusStopped service has stopped, but not because of error |
| 46 | ServiceStatusStopped |
| 47 | |
| 48 | // ServiceStatusFailed service has stopped because of an error |
| 49 | ServiceStatusFailed |
Scott Baker | 104b67d | 2019-10-29 15:56:27 -0700 | [diff] [blame] | 50 | |
| 51 | // ServiceStatusNotReady service has started but is unable to accept requests |
| 52 | ServiceStatusNotReady |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 53 | ) |
| 54 | |
| 55 | const ( |
| 56 | // ProbeContextKey value of context key to fetch probe |
| 57 | ProbeContextKey = ProbeContextKeyType("status-update-probe") |
| 58 | ) |
| 59 | |
| 60 | // String convert ServiceStatus values to strings |
| 61 | func (s ServiceStatus) String() string { |
| 62 | switch s { |
| 63 | default: |
| 64 | fallthrough |
| 65 | case ServiceStatusUnknown: |
| 66 | return "Unknown" |
| 67 | case ServiceStatusPreparing: |
| 68 | return "Preparing" |
| 69 | case ServiceStatusPrepared: |
| 70 | return "Prepared" |
| 71 | case ServiceStatusRunning: |
| 72 | return "Running" |
| 73 | case ServiceStatusStopped: |
| 74 | return "Stopped" |
| 75 | case ServiceStatusFailed: |
| 76 | return "Failed" |
Scott Baker | 104b67d | 2019-10-29 15:56:27 -0700 | [diff] [blame] | 77 | case ServiceStatusNotReady: |
| 78 | return "NotReady" |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 79 | } |
| 80 | } |
| 81 | |
| 82 | // ServiceStatusUpdate status update event |
| 83 | type ServiceStatusUpdate struct { |
| 84 | Name string |
| 85 | Status ServiceStatus |
| 86 | } |
| 87 | |
| 88 | // Probe reciever on which to implement probe capabilities |
| 89 | type Probe struct { |
| 90 | readyFunc func(map[string]ServiceStatus) bool |
| 91 | healthFunc func(map[string]ServiceStatus) bool |
| 92 | |
| 93 | mutex sync.RWMutex |
| 94 | status map[string]ServiceStatus |
| 95 | isReady bool |
| 96 | isHealthy bool |
| 97 | } |
| 98 | |
| 99 | // WithReadyFunc override the default ready calculation function |
| 100 | func (p *Probe) WithReadyFunc(readyFunc func(map[string]ServiceStatus) bool) *Probe { |
| 101 | p.readyFunc = readyFunc |
| 102 | return p |
| 103 | } |
| 104 | |
| 105 | // WithHealthFunc override the default health calculation function |
| 106 | func (p *Probe) WithHealthFunc(healthFunc func(map[string]ServiceStatus) bool) *Probe { |
| 107 | p.healthFunc = healthFunc |
| 108 | return p |
| 109 | } |
| 110 | |
| 111 | // RegisterService register one or more service names with the probe, status will be track against service name |
Neha Sharma | 94f16a9 | 2020-06-26 04:17:55 +0000 | [diff] [blame] | 112 | func (p *Probe) RegisterService(ctx context.Context, names ...string) { |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 113 | p.mutex.Lock() |
| 114 | defer p.mutex.Unlock() |
| 115 | if p.status == nil { |
| 116 | p.status = make(map[string]ServiceStatus) |
| 117 | } |
| 118 | for _, name := range names { |
| 119 | if _, ok := p.status[name]; !ok { |
| 120 | p.status[name] = ServiceStatusUnknown |
Neha Sharma | 94f16a9 | 2020-06-26 04:17:55 +0000 | [diff] [blame] | 121 | logger.Debugw(ctx, "probe-service-registered", log.Fields{"service-name": name}) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 122 | } |
| 123 | } |
| 124 | |
| 125 | if p.readyFunc != nil { |
| 126 | p.isReady = p.readyFunc(p.status) |
| 127 | } else { |
| 128 | p.isReady = defaultReadyFunc(p.status) |
| 129 | } |
| 130 | |
| 131 | if p.healthFunc != nil { |
| 132 | p.isHealthy = p.healthFunc(p.status) |
| 133 | } else { |
| 134 | p.isHealthy = defaultHealthFunc(p.status) |
| 135 | } |
| 136 | } |
| 137 | |
| 138 | // UpdateStatus utility function to send a service update to the probe |
Neha Sharma | 94f16a9 | 2020-06-26 04:17:55 +0000 | [diff] [blame] | 139 | func (p *Probe) UpdateStatus(ctx context.Context, name string, status ServiceStatus) { |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 140 | p.mutex.Lock() |
| 141 | defer p.mutex.Unlock() |
| 142 | if p.status == nil { |
| 143 | p.status = make(map[string]ServiceStatus) |
| 144 | } |
Scott Baker | 104b67d | 2019-10-29 15:56:27 -0700 | [diff] [blame] | 145 | |
| 146 | // if status hasn't changed, avoid doing useless work |
| 147 | existingStatus, ok := p.status[name] |
| 148 | if ok && (existingStatus == status) { |
| 149 | return |
| 150 | } |
| 151 | |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 152 | p.status[name] = status |
| 153 | if p.readyFunc != nil { |
| 154 | p.isReady = p.readyFunc(p.status) |
| 155 | } else { |
| 156 | p.isReady = defaultReadyFunc(p.status) |
| 157 | } |
| 158 | |
| 159 | if p.healthFunc != nil { |
| 160 | p.isHealthy = p.healthFunc(p.status) |
| 161 | } else { |
| 162 | p.isHealthy = defaultHealthFunc(p.status) |
| 163 | } |
Neha Sharma | 94f16a9 | 2020-06-26 04:17:55 +0000 | [diff] [blame] | 164 | logger.Debugw(ctx, "probe-service-status-updated", |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 165 | log.Fields{ |
| 166 | "service-name": name, |
| 167 | "status": status.String(), |
| 168 | "ready": p.isReady, |
| 169 | "health": p.isHealthy, |
| 170 | }) |
| 171 | } |
| 172 | |
Scott Baker | 104b67d | 2019-10-29 15:56:27 -0700 | [diff] [blame] | 173 | func (p *Probe) GetStatus(name string) ServiceStatus { |
| 174 | p.mutex.Lock() |
| 175 | defer p.mutex.Unlock() |
| 176 | |
| 177 | if p.status == nil { |
| 178 | p.status = make(map[string]ServiceStatus) |
| 179 | } |
| 180 | |
| 181 | currentStatus, ok := p.status[name] |
| 182 | if ok { |
| 183 | return currentStatus |
| 184 | } |
| 185 | |
| 186 | return ServiceStatusUnknown |
| 187 | } |
| 188 | |
| 189 | func GetProbeFromContext(ctx context.Context) *Probe { |
| 190 | if ctx != nil { |
| 191 | if value := ctx.Value(ProbeContextKey); value != nil { |
| 192 | if p, ok := value.(*Probe); ok { |
| 193 | return p |
| 194 | } |
| 195 | } |
| 196 | } |
| 197 | return nil |
| 198 | } |
| 199 | |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 200 | // UpdateStatusFromContext a convenience function to pull the Probe reference from the |
| 201 | // Context, if it exists, and then calling UpdateStatus on that Probe reference. If Context |
| 202 | // is nil or if a Probe reference is not associated with the ProbeContextKey then nothing |
| 203 | // happens |
| 204 | func UpdateStatusFromContext(ctx context.Context, name string, status ServiceStatus) { |
Scott Baker | 104b67d | 2019-10-29 15:56:27 -0700 | [diff] [blame] | 205 | p := GetProbeFromContext(ctx) |
| 206 | if p != nil { |
Neha Sharma | 94f16a9 | 2020-06-26 04:17:55 +0000 | [diff] [blame] | 207 | p.UpdateStatus(ctx, name, status) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 208 | } |
| 209 | } |
| 210 | |
| 211 | // pulled out to a function to help better enable unit testing |
| 212 | func (p *Probe) readzFunc(w http.ResponseWriter, req *http.Request) { |
| 213 | p.mutex.RLock() |
| 214 | defer p.mutex.RUnlock() |
| 215 | if p.isReady { |
| 216 | w.WriteHeader(http.StatusOK) |
| 217 | } else { |
| 218 | w.WriteHeader(http.StatusTeapot) |
| 219 | } |
| 220 | } |
| 221 | func (p *Probe) healthzFunc(w http.ResponseWriter, req *http.Request) { |
| 222 | p.mutex.RLock() |
| 223 | defer p.mutex.RUnlock() |
| 224 | if p.isHealthy { |
| 225 | w.WriteHeader(http.StatusOK) |
| 226 | } else { |
| 227 | w.WriteHeader(http.StatusTeapot) |
| 228 | } |
| 229 | } |
| 230 | func (p *Probe) detailzFunc(w http.ResponseWriter, req *http.Request) { |
Neha Sharma | 94f16a9 | 2020-06-26 04:17:55 +0000 | [diff] [blame] | 231 | ctx := context.Background() |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 232 | p.mutex.RLock() |
| 233 | defer p.mutex.RUnlock() |
| 234 | w.Header().Set("Content-Type", "application/json") |
David K. Bainbridge | 7c75cac | 2020-02-19 08:53:46 -0800 | [diff] [blame] | 235 | if _, err := w.Write([]byte("{")); err != nil { |
Neha Sharma | 94f16a9 | 2020-06-26 04:17:55 +0000 | [diff] [blame] | 236 | logger.Errorw(ctx, "write-response", log.Fields{"error": err}) |
David K. Bainbridge | 7c75cac | 2020-02-19 08:53:46 -0800 | [diff] [blame] | 237 | w.WriteHeader(http.StatusInternalServerError) |
| 238 | return |
| 239 | } |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 240 | comma := "" |
| 241 | for c, s := range p.status { |
David K. Bainbridge | 7c75cac | 2020-02-19 08:53:46 -0800 | [diff] [blame] | 242 | if _, err := w.Write([]byte(fmt.Sprintf("%s\"%s\": \"%s\"", comma, c, s.String()))); err != nil { |
Neha Sharma | 94f16a9 | 2020-06-26 04:17:55 +0000 | [diff] [blame] | 243 | logger.Errorw(ctx, "write-response", log.Fields{"error": err}) |
David K. Bainbridge | 7c75cac | 2020-02-19 08:53:46 -0800 | [diff] [blame] | 244 | w.WriteHeader(http.StatusInternalServerError) |
| 245 | return |
| 246 | } |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 247 | comma = ", " |
| 248 | } |
David K. Bainbridge | 7c75cac | 2020-02-19 08:53:46 -0800 | [diff] [blame] | 249 | if _, err := w.Write([]byte("}")); err != nil { |
Neha Sharma | 94f16a9 | 2020-06-26 04:17:55 +0000 | [diff] [blame] | 250 | logger.Errorw(ctx, "write-response", log.Fields{"error": err}) |
David K. Bainbridge | 7c75cac | 2020-02-19 08:53:46 -0800 | [diff] [blame] | 251 | w.WriteHeader(http.StatusInternalServerError) |
| 252 | return |
| 253 | } |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 254 | w.WriteHeader(http.StatusOK) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 255 | } |
| 256 | |
| 257 | // ListenAndServe implements 3 HTTP endpoints on the given port for healthz, readz, and detailz. Returns only on error |
Neha Sharma | 94f16a9 | 2020-06-26 04:17:55 +0000 | [diff] [blame] | 258 | func (p *Probe) ListenAndServe(ctx context.Context, address string) { |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 259 | mux := http.NewServeMux() |
| 260 | |
| 261 | // Returns the result of the readyFunc calculation |
| 262 | mux.HandleFunc("/readz", p.readzFunc) |
| 263 | |
| 264 | // Returns the result of the healthFunc calculation |
| 265 | mux.HandleFunc("/healthz", p.healthzFunc) |
| 266 | |
| 267 | // Returns the details of the services and their status as JSON |
| 268 | mux.HandleFunc("/detailz", p.detailzFunc) |
| 269 | s := &http.Server{ |
| 270 | Addr: address, |
| 271 | Handler: mux, |
| 272 | } |
Neha Sharma | 94f16a9 | 2020-06-26 04:17:55 +0000 | [diff] [blame] | 273 | logger.Fatal(ctx, s.ListenAndServe()) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 274 | } |
| 275 | |
Scott Baker | 104b67d | 2019-10-29 15:56:27 -0700 | [diff] [blame] | 276 | func (p *Probe) IsReady() bool { |
| 277 | return p.isReady |
| 278 | } |
| 279 | |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 280 | // defaultReadyFunc if all services are running then ready, else not |
| 281 | func defaultReadyFunc(services map[string]ServiceStatus) bool { |
| 282 | if len(services) == 0 { |
| 283 | return false |
| 284 | } |
| 285 | for _, status := range services { |
| 286 | if status != ServiceStatusRunning { |
| 287 | return false |
| 288 | } |
| 289 | } |
| 290 | return true |
| 291 | } |
| 292 | |
| 293 | // defaultHealthFunc if no service is stopped or failed, then healthy, else not. |
| 294 | // service is start as unknown, so they are considered healthy |
| 295 | func defaultHealthFunc(services map[string]ServiceStatus) bool { |
| 296 | if len(services) == 0 { |
| 297 | return false |
| 298 | } |
| 299 | for _, status := range services { |
| 300 | if status == ServiceStatusStopped || status == ServiceStatusFailed { |
| 301 | return false |
| 302 | } |
| 303 | } |
| 304 | return true |
| 305 | } |