|
| 1 | +// Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one |
| 2 | +// or more contributor license agreements. Licensed under the Elastic License; |
| 3 | +// you may not use this file except in compliance with the Elastic License. |
| 4 | + |
| 5 | +package monitoring |
| 6 | + |
| 7 | +import ( |
| 8 | + "fmt" |
| 9 | + "net/http" |
| 10 | + "time" |
| 11 | + |
| 12 | + "github.com/elastic/elastic-agent-client/v7/pkg/client" |
| 13 | +) |
| 14 | + |
| 15 | +const formValueKey = "failon" |
| 16 | + |
| 17 | +type LivenessFailConfig struct { |
| 18 | + Degraded bool `yaml:"degraded" config:"degraded"` |
| 19 | + Failed bool `yaml:"failed" config:"failed"` |
| 20 | + Heartbeat bool `yaml:"heartbeat" config:"heartbeat"` |
| 21 | +} |
| 22 | + |
| 23 | +// process the form values we get via HTTP |
| 24 | +func handleFormValues(req *http.Request) (LivenessFailConfig, error) { |
| 25 | + err := req.ParseForm() |
| 26 | + if err != nil { |
| 27 | + return LivenessFailConfig{}, fmt.Errorf("Error parsing form: %w", err) |
| 28 | + } |
| 29 | + |
| 30 | + defaultUserCfg := LivenessFailConfig{Degraded: false, Failed: false, Heartbeat: true} |
| 31 | + |
| 32 | + for formKey := range req.Form { |
| 33 | + if formKey != formValueKey { |
| 34 | + return defaultUserCfg, fmt.Errorf("got invalid HTTP form key: '%s'", formKey) |
| 35 | + } |
| 36 | + } |
| 37 | + |
| 38 | + userConfig := req.Form.Get(formValueKey) |
| 39 | + switch userConfig { |
| 40 | + case "failed": |
| 41 | + return LivenessFailConfig{Degraded: false, Failed: true, Heartbeat: true}, nil |
| 42 | + case "degraded": |
| 43 | + return LivenessFailConfig{Failed: true, Degraded: true, Heartbeat: true}, nil |
| 44 | + case "heartbeat", "": |
| 45 | + return defaultUserCfg, nil |
| 46 | + default: |
| 47 | + return defaultUserCfg, fmt.Errorf("got unexpected value for `%s` attribute: %s", formValueKey, userConfig) |
| 48 | + } |
| 49 | +} |
| 50 | + |
| 51 | +func livenessHandler(coord CoordinatorState) func(http.ResponseWriter, *http.Request) error { |
| 52 | + return func(w http.ResponseWriter, r *http.Request) error { |
| 53 | + w.Header().Set("Content-Type", "application/json; charset=utf-8") |
| 54 | + |
| 55 | + state := coord.State() |
| 56 | + isUp := coord.CoordinatorActive(time.Second * 10) |
| 57 | + // the coordinator check is always on, so if that fails, always return false |
| 58 | + if !isUp { |
| 59 | + w.WriteHeader(http.StatusServiceUnavailable) |
| 60 | + return nil |
| 61 | + } |
| 62 | + |
| 63 | + failConfig, err := handleFormValues(r) |
| 64 | + if err != nil { |
| 65 | + return fmt.Errorf("error handling form values: %w", err) |
| 66 | + } |
| 67 | + |
| 68 | + // if user has requested `coordinator` mode, just revert to that, skip everything else |
| 69 | + if !failConfig.Degraded && !failConfig.Failed && failConfig.Heartbeat { |
| 70 | + if !isUp { |
| 71 | + w.WriteHeader(http.StatusServiceUnavailable) |
| 72 | + return nil |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + unhealthyComponent := false |
| 77 | + for _, comp := range state.Components { |
| 78 | + if (failConfig.Failed && comp.State.State == client.UnitStateFailed) || (failConfig.Degraded && comp.State.State == client.UnitStateDegraded) { |
| 79 | + unhealthyComponent = true |
| 80 | + } |
| 81 | + } |
| 82 | + // bias towards the coordinator check, since it can be otherwise harder to diagnose |
| 83 | + if unhealthyComponent { |
| 84 | + w.WriteHeader(http.StatusInternalServerError) |
| 85 | + } |
| 86 | + return nil |
| 87 | + } |
| 88 | +} |
0 commit comments