14
14
package collector
15
15
16
16
import (
17
+ "context"
17
18
"encoding/json"
18
19
"fmt"
19
20
"io"
@@ -27,13 +28,6 @@ import (
27
28
"github.com/prometheus/client_golang/prometheus"
28
29
)
29
30
30
- type clusterLicenseMetric struct {
31
- Type prometheus.ValueType
32
- Desc * prometheus.Desc
33
- Value func (clusterLicenseStats clusterLicenseResponse ) float64
34
- Labels func (clusterLicenseStats clusterLicenseResponse ) []string
35
- }
36
-
37
31
type clusterLicenseResponse struct {
38
32
License struct {
39
33
Status string `json:"status"`
@@ -51,143 +45,115 @@ type clusterLicenseResponse struct {
51
45
}
52
46
53
47
var (
54
- defaultClusterLicenseLabels = []string {"cluster_license_type " }
55
- defaultClusterLicenseValues = func (clusterLicense clusterLicenseResponse ) []string {
56
- return []string {clusterLicense .License .Type }
48
+ defaultClusterLicenseLabels = []string {"issued_to" , "issuer" , "type" , "status " }
49
+ defaultClusterLicenseLabelsValues = func (clusterLicense clusterLicenseResponse ) []string {
50
+ return []string {clusterLicense .License .IssuedTo , clusterLicense . License . Issuer , clusterLicense . License . Type , clusterLicense . License . Status }
57
51
}
58
52
)
59
53
54
+ var (
55
+ licenseMaxNodes = prometheus .NewDesc (
56
+ prometheus .BuildFQName (namespace , "cluster_license" , "max_nodes" ),
57
+ "The max amount of nodes allowed by the license" ,
58
+ defaultClusterLicenseLabels , nil ,
59
+ )
60
+ licenseIssueDate = prometheus .NewDesc (
61
+ prometheus .BuildFQName (namespace , "cluster_license" , "issue_date_in_millis" ),
62
+ "License issue date in milliseconds" ,
63
+ defaultClusterLicenseLabels , nil ,
64
+ )
65
+ licenseExpiryDate = prometheus .NewDesc (
66
+ prometheus .BuildFQName (namespace , "cluster_license" , "expiry_date_in_millis" ),
67
+ "License expiry date in milliseconds" ,
68
+ defaultClusterLicenseLabels , nil ,
69
+ )
70
+ licenseStartDate = prometheus .NewDesc (
71
+ prometheus .BuildFQName (namespace , "cluster_license" , "start_date_in_millis" ),
72
+ "License start date in milliseconds" ,
73
+ defaultClusterLicenseLabels , nil ,
74
+ )
75
+ )
76
+
77
+ func init () {
78
+ registerCollector ("cluster_license" , defaultDisabled , NewClusterLicense )
79
+ }
80
+
60
81
// License Information Struct
61
82
type ClusterLicense struct {
62
83
logger log.Logger
63
- client * http.Client
64
- url * url.URL
65
-
66
- clusterLicenseMetrics []* clusterLicenseMetric
84
+ hc * http.Client
85
+ u * url.URL
67
86
}
68
87
69
- // NewClusterLicense defines ClusterLicense Prometheus metrics
70
- func NewClusterLicense (logger log.Logger , client * http.Client , url * url.URL ) * ClusterLicense {
88
+ func NewClusterLicense (logger log.Logger , u * url.URL , hc * http.Client ) (Collector , error ) {
71
89
return & ClusterLicense {
72
90
logger : logger ,
73
- client : client ,
74
- url : url ,
75
-
76
- clusterLicenseMetrics : []* clusterLicenseMetric {
77
- {
78
- Type : prometheus .GaugeValue ,
79
- Desc : prometheus .NewDesc (
80
- prometheus .BuildFQName (namespace , "cluster_license" , "max_nodes" ),
81
- "The max amount of nodes allowed by the license" ,
82
- defaultClusterLicenseLabels , nil ,
83
- ),
84
- Value : func (clusterLicenseStats clusterLicenseResponse ) float64 {
85
- return float64 (clusterLicenseStats .License .MaxNodes )
86
- },
87
- Labels : defaultClusterLicenseValues ,
88
- },
89
- {
90
- Type : prometheus .GaugeValue ,
91
- Desc : prometheus .NewDesc (
92
- prometheus .BuildFQName (namespace , "cluster_license" , "issue_date_in_millis" ),
93
- "License issue date in milliseconds" ,
94
- defaultClusterLicenseLabels , nil ,
95
- ),
96
- Value : func (clusterLicenseStats clusterLicenseResponse ) float64 {
97
- return float64 (clusterLicenseStats .License .IssueDateInMillis )
98
- },
99
- Labels : defaultClusterLicenseValues ,
100
- },
101
- {
102
- Type : prometheus .GaugeValue ,
103
- Desc : prometheus .NewDesc (
104
- prometheus .BuildFQName (namespace , "cluster_license" , "expiry_date_in_millis" ),
105
- "License expiry date in milliseconds" ,
106
- defaultClusterLicenseLabels , nil ,
107
- ),
108
- Value : func (clusterLicenseStats clusterLicenseResponse ) float64 {
109
- return float64 (clusterLicenseStats .License .ExpiryDateInMillis )
110
- },
111
- Labels : defaultClusterLicenseValues ,
112
- },
113
- {
114
- Type : prometheus .GaugeValue ,
115
- Desc : prometheus .NewDesc (
116
- prometheus .BuildFQName (namespace , "cluster_license" , "start_date_in_millis" ),
117
- "License start date in milliseconds" ,
118
- defaultClusterLicenseLabels , nil ,
119
- ),
120
- Value : func (clusterLicenseStats clusterLicenseResponse ) float64 {
121
- return float64 (clusterLicenseStats .License .StartDateInMillis )
122
- },
123
- Labels : defaultClusterLicenseValues ,
124
- },
125
- },
126
- }
127
- }
128
-
129
- // Describe adds License metrics descriptions
130
- func (cl * ClusterLicense ) Describe (ch chan <- * prometheus.Desc ) {
131
- for _ , metric := range cl .clusterLicenseMetrics {
132
- ch <- metric .Desc
133
- }
91
+ u : u ,
92
+ hc : hc ,
93
+ }, nil
134
94
}
135
95
136
- func (cl * ClusterLicense ) fetchAndDecodeClusterLicense () ( clusterLicenseResponse , error ) {
96
+ func (c * ClusterLicense ) Update ( ctx context. Context , ch chan <- prometheus. Metric ) error {
137
97
var clr clusterLicenseResponse
138
98
139
- u := * cl . url
99
+ u := * c . u
140
100
u .Path = path .Join (u .Path , "/_license" )
141
- res , err := cl .client .Get (u .String ())
101
+ res , err := c .hc .Get (u .String ())
102
+
142
103
if err != nil {
143
- return clr , fmt .Errorf ("failed to get license stats from %s://%s:%s%s: %s" ,
144
- u .Scheme , u .Hostname (), u .Port (), u .Path , err )
104
+ return err
145
105
}
146
106
147
107
defer func () {
148
108
err = res .Body .Close ()
149
109
if err != nil {
150
- level .Warn (cl .logger ).Log (
110
+ level .Warn (c .logger ).Log (
151
111
"msg" , "failed to close http.Client" ,
152
112
"err" , err ,
153
113
)
154
114
}
155
115
}()
156
116
157
117
if res .StatusCode != http .StatusOK {
158
- return clr , fmt .Errorf ("HTTP Request failed with code %d" , res .StatusCode )
118
+ return fmt .Errorf ("HTTP Request failed with code %d" , res .StatusCode )
159
119
}
160
120
161
121
bts , err := io .ReadAll (res .Body )
162
122
if err != nil {
163
- return clr , err
123
+ return err
164
124
}
165
125
166
126
if err := json .Unmarshal (bts , & clr ); err != nil {
167
- return clr , err
168
- }
169
-
170
- return clr , nil
171
- }
172
-
173
- // Collect gets ClusterLicense metric values
174
- func (cl * ClusterLicense ) Collect (ch chan <- prometheus.Metric ) {
175
-
176
- clusterLicenseResp , err := cl .fetchAndDecodeClusterLicense ()
177
- if err != nil {
178
- level .Warn (cl .logger ).Log (
179
- "msg" , "failed to fetch and decode license stats" ,
180
- "err" , err ,
181
- )
182
- return
127
+ return err
183
128
}
184
129
185
- for _ , metric := range cl .clusterLicenseMetrics {
186
- ch <- prometheus .MustNewConstMetric (
187
- metric .Desc ,
188
- metric .Type ,
189
- metric .Value (clusterLicenseResp ),
190
- metric .Labels (clusterLicenseResp )... ,
191
- )
192
- }
130
+ ch <- prometheus .MustNewConstMetric (
131
+ licenseMaxNodes ,
132
+ prometheus .GaugeValue ,
133
+ float64 (clr .License .MaxNodes ),
134
+ defaultClusterLicenseLabelsValues (clr )... ,
135
+ )
136
+
137
+ ch <- prometheus .MustNewConstMetric (
138
+ licenseIssueDate ,
139
+ prometheus .GaugeValue ,
140
+ float64 (clr .License .IssueDateInMillis ),
141
+ defaultClusterLicenseLabelsValues (clr )... ,
142
+ )
143
+
144
+ ch <- prometheus .MustNewConstMetric (
145
+ licenseExpiryDate ,
146
+ prometheus .GaugeValue ,
147
+ float64 (clr .License .ExpiryDateInMillis ),
148
+ defaultClusterLicenseLabelsValues (clr )... ,
149
+ )
150
+
151
+ ch <- prometheus .MustNewConstMetric (
152
+ licenseStartDate ,
153
+ prometheus .GaugeValue ,
154
+ float64 (clr .License .StartDateInMillis ),
155
+ defaultClusterLicenseLabelsValues (clr )... ,
156
+ )
157
+
158
+ return nil
193
159
}
0 commit comments