|
| 1 | +// Copyright 2023 The Prometheus Authors |
| 2 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 3 | +// you may not use this file except in compliance with the License. |
| 4 | +// You may obtain a copy of the License at |
| 5 | +// |
| 6 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 7 | +// |
| 8 | +// Unless required by applicable law or agreed to in writing, software |
| 9 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 10 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 | +// See the License for the specific language governing permissions and |
| 12 | +// limitations under the License. |
| 13 | + |
| 14 | +package collector |
| 15 | + |
| 16 | +import ( |
| 17 | + "encoding/json" |
| 18 | + "fmt" |
| 19 | + "io" |
| 20 | + "net/http" |
| 21 | + "net/url" |
| 22 | + "path" |
| 23 | + |
| 24 | + "github.com/go-kit/log" |
| 25 | + "github.com/go-kit/log/level" |
| 26 | + "github.com/prometheus/client_golang/prometheus" |
| 27 | +) |
| 28 | + |
| 29 | +type clusterLicenseMetric struct { |
| 30 | + Type prometheus.ValueType |
| 31 | + Desc *prometheus.Desc |
| 32 | + Value func(clusterLicenseStats clusterLicenseResponse) float64 |
| 33 | + Labels func(clusterLicenseStats clusterLicenseResponse) []string |
| 34 | +} |
| 35 | + |
| 36 | +var ( |
| 37 | + defaultClusterLicenseLabels = []string{"cluster_license_type"} |
| 38 | + defaultClusterLicenseValues = func(clusterLicense clusterLicenseResponse) []string { |
| 39 | + return []string{clusterLicense.License.Type} |
| 40 | + } |
| 41 | +) |
| 42 | + |
| 43 | +// License Information Struct |
| 44 | +type ClusterLicense struct { |
| 45 | + logger log.Logger |
| 46 | + client *http.Client |
| 47 | + url *url.URL |
| 48 | + |
| 49 | + clusterLicenseMetrics []*clusterLicenseMetric |
| 50 | +} |
| 51 | + |
| 52 | +// NewClusterLicense defines ClusterLicense Prometheus metrics |
| 53 | +func NewClusterLicense(logger log.Logger, client *http.Client, url *url.URL) *ClusterLicense { |
| 54 | + return &ClusterLicense{ |
| 55 | + logger: logger, |
| 56 | + client: client, |
| 57 | + url: url, |
| 58 | + |
| 59 | + clusterLicenseMetrics: []*clusterLicenseMetric{ |
| 60 | + { |
| 61 | + Type: prometheus.GaugeValue, |
| 62 | + Desc: prometheus.NewDesc( |
| 63 | + prometheus.BuildFQName(namespace, "cluster_license", "max_nodes"), |
| 64 | + "The max amount of nodes allowed by the license", |
| 65 | + defaultClusterLicenseLabels, nil, |
| 66 | + ), |
| 67 | + Value: func(clusterLicenseStats clusterLicenseResponse) float64 { |
| 68 | + return float64(clusterLicenseStats.License.MaxNodes) |
| 69 | + }, |
| 70 | + Labels: defaultClusterLicenseValues, |
| 71 | + }, |
| 72 | + { |
| 73 | + Type: prometheus.GaugeValue, |
| 74 | + Desc: prometheus.NewDesc( |
| 75 | + prometheus.BuildFQName(namespace, "cluster_license", "issue_date_in_millis"), |
| 76 | + "License issue date in milliseconds", |
| 77 | + defaultClusterLicenseLabels, nil, |
| 78 | + ), |
| 79 | + Value: func(clusterLicenseStats clusterLicenseResponse) float64 { |
| 80 | + return float64(clusterLicenseStats.License.IssueDateInMillis) |
| 81 | + }, |
| 82 | + Labels: defaultClusterLicenseValues, |
| 83 | + }, |
| 84 | + { |
| 85 | + Type: prometheus.GaugeValue, |
| 86 | + Desc: prometheus.NewDesc( |
| 87 | + prometheus.BuildFQName(namespace, "cluster_license", "expiry_date_in_millis"), |
| 88 | + "License expiry date in milliseconds", |
| 89 | + defaultClusterLicenseLabels, nil, |
| 90 | + ), |
| 91 | + Value: func(clusterLicenseStats clusterLicenseResponse) float64 { |
| 92 | + return float64(clusterLicenseStats.License.ExpiryDateInMillis) |
| 93 | + }, |
| 94 | + Labels: defaultClusterLicenseValues, |
| 95 | + }, |
| 96 | + { |
| 97 | + Type: prometheus.GaugeValue, |
| 98 | + Desc: prometheus.NewDesc( |
| 99 | + prometheus.BuildFQName(namespace, "cluster_license", "start_date_in_millis"), |
| 100 | + "License start date in milliseconds", |
| 101 | + defaultClusterLicenseLabels, nil, |
| 102 | + ), |
| 103 | + Value: func(clusterLicenseStats clusterLicenseResponse) float64 { |
| 104 | + return float64(clusterLicenseStats.License.StartDateInMillis) |
| 105 | + }, |
| 106 | + Labels: defaultClusterLicenseValues, |
| 107 | + }, |
| 108 | + }, |
| 109 | + } |
| 110 | +} |
| 111 | + |
| 112 | +// Describe adds License metrics descriptions |
| 113 | +func (cl *ClusterLicense) Describe(ch chan<- *prometheus.Desc) { |
| 114 | + for _, metric := range cl.clusterLicenseMetrics { |
| 115 | + ch <- metric.Desc |
| 116 | + } |
| 117 | +} |
| 118 | + |
| 119 | +func (cl *ClusterLicense) fetchAndDecodeClusterLicense() (clusterLicenseResponse, error) { |
| 120 | + var clr clusterLicenseResponse |
| 121 | + |
| 122 | + u := *cl.url |
| 123 | + u.Path = path.Join(u.Path, "/_license") |
| 124 | + res, err := cl.client.Get(u.String()) |
| 125 | + if err != nil { |
| 126 | + return clr, fmt.Errorf("failed to get license stats from %s://%s:%s%s: %s", |
| 127 | + u.Scheme, u.Hostname(), u.Port(), u.Path, err) |
| 128 | + } |
| 129 | + |
| 130 | + defer func() { |
| 131 | + err = res.Body.Close() |
| 132 | + if err != nil { |
| 133 | + level.Warn(cl.logger).Log( |
| 134 | + "msg", "failed to close http.Client", |
| 135 | + "err", err, |
| 136 | + ) |
| 137 | + } |
| 138 | + }() |
| 139 | + |
| 140 | + if res.StatusCode != http.StatusOK { |
| 141 | + return clr, fmt.Errorf("HTTP Request failed with code %d", res.StatusCode) |
| 142 | + } |
| 143 | + |
| 144 | + bts, err := io.ReadAll(res.Body) |
| 145 | + if err != nil { |
| 146 | + return clr, err |
| 147 | + } |
| 148 | + |
| 149 | + if err := json.Unmarshal(bts, &clr); err != nil { |
| 150 | + return clr, err |
| 151 | + } |
| 152 | + |
| 153 | + return clr, nil |
| 154 | +} |
| 155 | + |
| 156 | +// Collect gets ClusterLicense metric values |
| 157 | +func (cl *ClusterLicense) Collect(ch chan<- prometheus.Metric) { |
| 158 | + |
| 159 | + clusterLicenseResp, err := cl.fetchAndDecodeClusterLicense() |
| 160 | + if err != nil { |
| 161 | + level.Warn(cl.logger).Log( |
| 162 | + "msg", "failed to fetch and decode license stats", |
| 163 | + "err", err, |
| 164 | + ) |
| 165 | + return |
| 166 | + } |
| 167 | + |
| 168 | + for _, metric := range cl.clusterLicenseMetrics { |
| 169 | + ch <- prometheus.MustNewConstMetric( |
| 170 | + metric.Desc, |
| 171 | + metric.Type, |
| 172 | + metric.Value(clusterLicenseResp), |
| 173 | + metric.Labels(clusterLicenseResp)..., |
| 174 | + ) |
| 175 | + } |
| 176 | +} |
0 commit comments