|
| 1 | +// |
| 2 | +// DISCLAIMER |
| 3 | +// |
| 4 | +// Copyright 2016-2021 ArangoDB GmbH, Cologne, Germany |
| 5 | +// |
| 6 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | +// you may not use this file except in compliance with the License. |
| 8 | +// You may obtain a copy of the License at |
| 9 | +// |
| 10 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | +// |
| 12 | +// Unless required by applicable law or agreed to in writing, software |
| 13 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | +// See the License for the specific language governing permissions and |
| 16 | +// limitations under the License. |
| 17 | +// |
| 18 | +// Copyright holder is ArangoDB GmbH, Cologne, Germany |
| 19 | +// |
| 20 | +// Author Jakub Wierzbowski |
| 21 | +// |
| 22 | + |
| 23 | +package exporter |
| 24 | + |
| 25 | +import ( |
| 26 | + "context" |
| 27 | + "encoding/json" |
| 28 | + "errors" |
| 29 | + "fmt" |
| 30 | + "io/ioutil" |
| 31 | + "net/url" |
| 32 | + "os" |
| 33 | + "path" |
| 34 | + "strings" |
| 35 | + "sync/atomic" |
| 36 | + "time" |
| 37 | + |
| 38 | + "github.com/arangodb/go-driver" |
| 39 | + "github.com/arangodb/kube-arangodb/pkg/util/k8sutil" |
| 40 | + |
| 41 | + "github.com/rs/zerolog/log" |
| 42 | +) |
| 43 | + |
| 44 | +const ( |
| 45 | + monitorMetricTemplate = "arangodb_member_health{role=\"%s\",id=\"%s\"} %d \n" |
| 46 | + successRefreshInterval = time.Second * 120 |
| 47 | + failRefreshInterval = time.Second * 15 |
| 48 | +) |
| 49 | + |
| 50 | +var currentMembersStatus atomic.Value |
| 51 | + |
| 52 | +func NewMonitor(arangodbEndpoint string, auth Authentication, sslVerify bool, timeout time.Duration) *monitor { |
| 53 | + uri, err := setPath(arangodbEndpoint, k8sutil.ArangoExporterClusterHealthEndpoint) |
| 54 | + if err != nil { |
| 55 | + log.Error().Err(err).Msgf("Fatal") |
| 56 | + os.Exit(1) |
| 57 | + } |
| 58 | + |
| 59 | + return &monitor{ |
| 60 | + factory: newHttpClientFactory(arangodbEndpoint, auth, sslVerify, timeout), |
| 61 | + healthURI: uri, |
| 62 | + } |
| 63 | +} |
| 64 | + |
| 65 | +type monitor struct { |
| 66 | + factory httpClientFactory |
| 67 | + healthURI *url.URL |
| 68 | +} |
| 69 | + |
| 70 | +// UpdateMonitorStatus load monitor metrics for current cluster into currentMembersStatus |
| 71 | +func (m monitor) UpdateMonitorStatus(ctx context.Context) { |
| 72 | + for { |
| 73 | + sleep := successRefreshInterval |
| 74 | + |
| 75 | + health, err := m.GetClusterHealth() |
| 76 | + if err != nil { |
| 77 | + log.Error().Err(err).Msg("GetClusterHealth error") |
| 78 | + sleep = failRefreshInterval |
| 79 | + } else { |
| 80 | + var output strings.Builder |
| 81 | + for key, value := range health.Health { |
| 82 | + entry, err := m.GetMemberStatus(key, value) |
| 83 | + if err != nil { |
| 84 | + log.Error().Err(err).Msg("GetMemberStatus error") |
| 85 | + sleep = failRefreshInterval |
| 86 | + } |
| 87 | + output.WriteString(entry) |
| 88 | + } |
| 89 | + currentMembersStatus.Store(output.String()) |
| 90 | + } |
| 91 | + |
| 92 | + select { |
| 93 | + case <-ctx.Done(): |
| 94 | + return |
| 95 | + case <-time.After(sleep): |
| 96 | + continue |
| 97 | + } |
| 98 | + } |
| 99 | +} |
| 100 | + |
| 101 | +// GetClusterHealth returns current ArangoDeployment cluster health status |
| 102 | +func (m monitor) GetClusterHealth() (*driver.ClusterHealth, error) { |
| 103 | + c, req, err := m.factory() |
| 104 | + if err != nil { |
| 105 | + return nil, err |
| 106 | + } |
| 107 | + req.URL = m.healthURI |
| 108 | + resp, err := c.Do(req) |
| 109 | + if err != nil { |
| 110 | + return nil, err |
| 111 | + } |
| 112 | + |
| 113 | + defer resp.Body.Close() |
| 114 | + body, err := ioutil.ReadAll(resp.Body) |
| 115 | + if err != nil { |
| 116 | + return nil, err |
| 117 | + } |
| 118 | + |
| 119 | + var result driver.ClusterHealth |
| 120 | + if err := json.Unmarshal(body, &result); err != nil { |
| 121 | + return nil, err |
| 122 | + } |
| 123 | + |
| 124 | + return &result, err |
| 125 | +} |
| 126 | + |
| 127 | +// GetMemberStatus returns Prometheus monitor metric for specific member |
| 128 | +func (m monitor) GetMemberStatus(id driver.ServerID, member driver.ServerHealth) (string, error) { |
| 129 | + result := fmt.Sprintf(monitorMetricTemplate, member.Role, id, 0) |
| 130 | + |
| 131 | + c, req, err := m.factory() |
| 132 | + if err != nil { |
| 133 | + return result, err |
| 134 | + } |
| 135 | + |
| 136 | + req.URL, err = setPath(member.Endpoint, k8sutil.ArangoExporterStatusEndpoint) |
| 137 | + if err != nil { |
| 138 | + return result, err |
| 139 | + } |
| 140 | + |
| 141 | + resp, err := c.Do(req) |
| 142 | + if err != nil { |
| 143 | + return result, err |
| 144 | + } |
| 145 | + |
| 146 | + if resp.StatusCode != 200 { |
| 147 | + defer resp.Body.Close() |
| 148 | + body, err := ioutil.ReadAll(resp.Body) |
| 149 | + if err != nil { |
| 150 | + return result, err |
| 151 | + } |
| 152 | + return result, errors.New(string(body)) |
| 153 | + } |
| 154 | + return fmt.Sprintf(monitorMetricTemplate, member.Role, id, 1), nil |
| 155 | +} |
| 156 | + |
| 157 | +func setPath(uri, uriPath string) (*url.URL, error) { |
| 158 | + u, err := url.Parse(uri) |
| 159 | + if err != nil { |
| 160 | + return u, err |
| 161 | + } |
| 162 | + u.Path = path.Join(uriPath) |
| 163 | + u.Scheme = "https" |
| 164 | + return u, nil |
| 165 | +} |
0 commit comments