-
Notifications
You must be signed in to change notification settings - Fork 802
feat: add cluster license collector #833
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
kekkker
wants to merge
7
commits into
prometheus-community:master
Choose a base branch
from
kekkker:feat/add-cluster-license
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
617e127
feat: add cluster license collector
kekkker 0c74085
Remove cluster_license_response file
kekkker 728e447
Remove esExportLicense flag
kekkker b4149e1
Refactor cluster_license, use the collector interface
kekkker e283dcc
Convert millis to seconds
kekkker 365715f
Add versions to the tests
kekkker 7e48099
Make flag match others, update README
kekkker File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,176 @@ | ||
// Copyright 2023 The Prometheus Authors | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package collector | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"io" | ||
"net/http" | ||
"net/url" | ||
"path" | ||
|
||
"github.com/go-kit/log" | ||
"github.com/go-kit/log/level" | ||
"github.com/prometheus/client_golang/prometheus" | ||
) | ||
|
||
type clusterLicenseMetric struct { | ||
Type prometheus.ValueType | ||
Desc *prometheus.Desc | ||
Value func(clusterLicenseStats clusterLicenseResponse) float64 | ||
Labels func(clusterLicenseStats clusterLicenseResponse) []string | ||
} | ||
|
||
var ( | ||
defaultClusterLicenseLabels = []string{"cluster_license_type"} | ||
defaultClusterLicenseValues = func(clusterLicense clusterLicenseResponse) []string { | ||
return []string{clusterLicense.License.Type} | ||
} | ||
) | ||
|
||
// License Information Struct | ||
type ClusterLicense struct { | ||
logger log.Logger | ||
client *http.Client | ||
url *url.URL | ||
|
||
clusterLicenseMetrics []*clusterLicenseMetric | ||
} | ||
|
||
// NewClusterLicense defines ClusterLicense Prometheus metrics | ||
func NewClusterLicense(logger log.Logger, client *http.Client, url *url.URL) *ClusterLicense { | ||
return &ClusterLicense{ | ||
logger: logger, | ||
client: client, | ||
url: url, | ||
|
||
clusterLicenseMetrics: []*clusterLicenseMetric{ | ||
{ | ||
Type: prometheus.GaugeValue, | ||
Desc: prometheus.NewDesc( | ||
prometheus.BuildFQName(namespace, "cluster_license", "max_nodes"), | ||
"The max amount of nodes allowed by the license", | ||
defaultClusterLicenseLabels, nil, | ||
), | ||
Value: func(clusterLicenseStats clusterLicenseResponse) float64 { | ||
return float64(clusterLicenseStats.License.MaxNodes) | ||
}, | ||
Labels: defaultClusterLicenseValues, | ||
}, | ||
{ | ||
Type: prometheus.GaugeValue, | ||
Desc: prometheus.NewDesc( | ||
prometheus.BuildFQName(namespace, "cluster_license", "issue_date_in_millis"), | ||
kekkker marked this conversation as resolved.
Show resolved
Hide resolved
|
||
"License issue date in milliseconds", | ||
defaultClusterLicenseLabels, nil, | ||
), | ||
Value: func(clusterLicenseStats clusterLicenseResponse) float64 { | ||
return float64(clusterLicenseStats.License.IssueDateInMillis) | ||
}, | ||
Labels: defaultClusterLicenseValues, | ||
}, | ||
{ | ||
Type: prometheus.GaugeValue, | ||
Desc: prometheus.NewDesc( | ||
prometheus.BuildFQName(namespace, "cluster_license", "expiry_date_in_millis"), | ||
"License expiry date in milliseconds", | ||
defaultClusterLicenseLabels, nil, | ||
), | ||
Value: func(clusterLicenseStats clusterLicenseResponse) float64 { | ||
return float64(clusterLicenseStats.License.ExpiryDateInMillis) | ||
}, | ||
Labels: defaultClusterLicenseValues, | ||
}, | ||
{ | ||
Type: prometheus.GaugeValue, | ||
Desc: prometheus.NewDesc( | ||
prometheus.BuildFQName(namespace, "cluster_license", "start_date_in_millis"), | ||
"License start date in milliseconds", | ||
defaultClusterLicenseLabels, nil, | ||
), | ||
Value: func(clusterLicenseStats clusterLicenseResponse) float64 { | ||
return float64(clusterLicenseStats.License.StartDateInMillis) | ||
}, | ||
Labels: defaultClusterLicenseValues, | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
// Describe adds License metrics descriptions | ||
func (cl *ClusterLicense) Describe(ch chan<- *prometheus.Desc) { | ||
for _, metric := range cl.clusterLicenseMetrics { | ||
ch <- metric.Desc | ||
} | ||
} | ||
|
||
func (cl *ClusterLicense) fetchAndDecodeClusterLicense() (clusterLicenseResponse, error) { | ||
var clr clusterLicenseResponse | ||
|
||
u := *cl.url | ||
u.Path = path.Join(u.Path, "/_license") | ||
res, err := cl.client.Get(u.String()) | ||
if err != nil { | ||
return clr, fmt.Errorf("failed to get license stats from %s://%s:%s%s: %s", | ||
u.Scheme, u.Hostname(), u.Port(), u.Path, err) | ||
} | ||
|
||
defer func() { | ||
err = res.Body.Close() | ||
if err != nil { | ||
level.Warn(cl.logger).Log( | ||
"msg", "failed to close http.Client", | ||
"err", err, | ||
) | ||
} | ||
}() | ||
|
||
if res.StatusCode != http.StatusOK { | ||
return clr, fmt.Errorf("HTTP Request failed with code %d", res.StatusCode) | ||
} | ||
|
||
bts, err := io.ReadAll(res.Body) | ||
if err != nil { | ||
return clr, err | ||
} | ||
|
||
if err := json.Unmarshal(bts, &clr); err != nil { | ||
return clr, err | ||
} | ||
|
||
return clr, nil | ||
} | ||
|
||
// Collect gets ClusterLicense metric values | ||
func (cl *ClusterLicense) Collect(ch chan<- prometheus.Metric) { | ||
|
||
clusterLicenseResp, err := cl.fetchAndDecodeClusterLicense() | ||
if err != nil { | ||
level.Warn(cl.logger).Log( | ||
"msg", "failed to fetch and decode license stats", | ||
"err", err, | ||
) | ||
return | ||
} | ||
|
||
for _, metric := range cl.clusterLicenseMetrics { | ||
ch <- prometheus.MustNewConstMetric( | ||
metric.Desc, | ||
metric.Type, | ||
metric.Value(clusterLicenseResp), | ||
metric.Labels(clusterLicenseResp)..., | ||
) | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
// Copyright 2023 The Prometheus Authors | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
kekkker marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
package collector | ||
|
||
import "time" | ||
|
||
type clusterLicenseResponse struct { | ||
License struct { | ||
Status string `json:"status"` | ||
UID string `json:"uid"` | ||
Type string `json:"type"` | ||
IssueDate time.Time `json:"issue_date"` | ||
IssueDateInMillis int64 `json:"issue_date_in_millis"` | ||
ExpiryDate time.Time `json:"expiry_date"` | ||
ExpiryDateInMillis int64 `json:"expiry_date_in_millis"` | ||
MaxNodes int `json:"max_nodes"` | ||
IssuedTo string `json:"issued_to"` | ||
Issuer string `json:"issuer"` | ||
StartDateInMillis int64 `json:"start_date_in_millis"` | ||
} `json:"license"` | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
// Copyright 2023 The Prometheus Authors | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package collector | ||
|
||
import ( | ||
"io" | ||
"net/http" | ||
"net/http/httptest" | ||
"net/url" | ||
"os" | ||
"strings" | ||
"testing" | ||
|
||
"github.com/go-kit/log" | ||
"github.com/prometheus/client_golang/prometheus/testutil" | ||
) | ||
|
||
func TestIndicesHealth(t *testing.T) { | ||
// Testcases created using: | ||
// docker run -d -p 9200:9200 elasticsearch:VERSION | ||
// curl http://localhost:9200/_license | ||
tests := []struct { | ||
name string | ||
file string | ||
want string | ||
}{ | ||
{ | ||
name: "basic", | ||
file: "../fixtures/clusterlicense/basic.json", | ||
kekkker marked this conversation as resolved.
Show resolved
Hide resolved
|
||
want: ` | ||
# HELP elasticsearch_cluster_license_expiry_date_in_millis License expiry date in milliseconds | ||
# TYPE elasticsearch_cluster_license_expiry_date_in_millis gauge | ||
elasticsearch_cluster_license_expiry_date_in_millis{cluster_license_type="basic"} 0 | ||
# HELP elasticsearch_cluster_license_issue_date_in_millis License issue date in milliseconds | ||
# TYPE elasticsearch_cluster_license_issue_date_in_millis gauge | ||
elasticsearch_cluster_license_issue_date_in_millis{cluster_license_type="basic"} 1.702196247064e+12 | ||
# HELP elasticsearch_cluster_license_max_nodes The max amount of nodes allowed by the license | ||
# TYPE elasticsearch_cluster_license_max_nodes gauge | ||
elasticsearch_cluster_license_max_nodes{cluster_license_type="basic"} 1000 | ||
# HELP elasticsearch_cluster_license_start_date_in_millis License start date in milliseconds | ||
# TYPE elasticsearch_cluster_license_start_date_in_millis gauge | ||
elasticsearch_cluster_license_start_date_in_millis{cluster_license_type="basic"} -1 | ||
`, | ||
}, | ||
{ | ||
name: "platinum", | ||
file: "../fixtures/clusterlicense/platinum.json", | ||
want: ` | ||
# HELP elasticsearch_cluster_license_expiry_date_in_millis License expiry date in milliseconds | ||
# TYPE elasticsearch_cluster_license_expiry_date_in_millis gauge | ||
elasticsearch_cluster_license_expiry_date_in_millis{cluster_license_type="platinum"} 1.714521599999e+12 | ||
# HELP elasticsearch_cluster_license_issue_date_in_millis License issue date in milliseconds | ||
# TYPE elasticsearch_cluster_license_issue_date_in_millis gauge | ||
elasticsearch_cluster_license_issue_date_in_millis{cluster_license_type="platinum"} 1.6192224e+12 | ||
# HELP elasticsearch_cluster_license_max_nodes The max amount of nodes allowed by the license | ||
# TYPE elasticsearch_cluster_license_max_nodes gauge | ||
elasticsearch_cluster_license_max_nodes{cluster_license_type="platinum"} 10 | ||
# HELP elasticsearch_cluster_license_start_date_in_millis License start date in milliseconds | ||
# TYPE elasticsearch_cluster_license_start_date_in_millis gauge | ||
elasticsearch_cluster_license_start_date_in_millis{cluster_license_type="platinum"} 1.6192224e+12 | ||
`, | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
f, err := os.Open(tt.file) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
defer f.Close() | ||
|
||
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
io.Copy(w, f) | ||
})) | ||
|
||
defer ts.Close() | ||
|
||
u, err := url.Parse(ts.URL) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
c := NewClusterLicense(log.NewNopLogger(), http.DefaultClient, u) | ||
|
||
if err := testutil.CollectAndCompare(c, strings.NewReader(tt.want)); err != nil { | ||
t.Fatal(err) | ||
} | ||
}) | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
{ | ||
"license": { | ||
"status": "active", | ||
"uid": "redacted", | ||
"type": "basic", | ||
"issue_date": "2023-12-10T08:17:27.064Z", | ||
"issue_date_in_millis": 1702196247064, | ||
"max_nodes": 1000, | ||
"max_resource_units": null, | ||
"issued_to": "redacted", | ||
"issuer": "elasticsearch", | ||
"start_date_in_millis": -1 | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
{ | ||
"license": { | ||
"status": "active", | ||
"uid": "redacted", | ||
"type": "platinum", | ||
"issue_date": "2021-04-24T00:00:00.000Z", | ||
"issue_date_in_millis": 1619222400000, | ||
"expiry_date": "2024-04-30T23:59:59.999Z", | ||
"expiry_date_in_millis": 1714521599999, | ||
"max_nodes": 10, | ||
"issued_to": "redacted", | ||
"issuer": "API", | ||
"start_date_in_millis": 1619222400000 | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.