forked from prometheus-community/elasticsearch_exporter
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor cluster info collector (prometheus-community#536)
* Refactor cluster info collector Adds new cluster info collector. This will eventually replace the cluster info collector in pkg/clusterinfo. The current limitation in fully replacing that package is that it provides downstream collectors with the cluster info to apply the cluster name label. I plan to find a better solution for this when I refactor the next collector. Updates the Collector interactions to more closely match node_exporter and postgres_exporter style of registering collectors. Signed-off-by: Joe Adams <[email protected]> * Fix ci Signed-off-by: Joe Adams <[email protected]>
- Loading branch information
Showing
3 changed files
with
245 additions
and
5 deletions.
There are no files selected for viewing
This file contains 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,109 @@ | ||
// Copyright 2022 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 ( | ||
"context" | ||
"encoding/json" | ||
"io/ioutil" | ||
"net/http" | ||
"net/url" | ||
|
||
"github.com/blang/semver" | ||
"github.com/go-kit/log" | ||
"github.com/prometheus/client_golang/prometheus" | ||
) | ||
|
||
func init() { | ||
registerCollector("cluster-info", defaultEnabled, NewClusterInfo) | ||
} | ||
|
||
type ClusterInfoCollector struct { | ||
logger log.Logger | ||
u *url.URL | ||
hc *http.Client | ||
} | ||
|
||
func NewClusterInfo(logger log.Logger, u *url.URL, hc *http.Client) (Collector, error) { | ||
return &ClusterInfoCollector{ | ||
logger: logger, | ||
u: u, | ||
hc: hc, | ||
}, nil | ||
} | ||
|
||
var clusterInfoDesc = map[string]*prometheus.Desc{ | ||
"version": prometheus.NewDesc( | ||
prometheus.BuildFQName(namespace, "", "version"), | ||
"Elasticsearch version information.", | ||
[]string{ | ||
"cluster", | ||
"cluster_uuid", | ||
"build_date", | ||
"build_hash", | ||
"version", | ||
"lucene_version", | ||
}, | ||
nil, | ||
), | ||
} | ||
|
||
// ClusterInfoResponse is the cluster info retrievable from the / endpoint | ||
type ClusterInfoResponse struct { | ||
Name string `json:"name"` | ||
ClusterName string `json:"cluster_name"` | ||
ClusterUUID string `json:"cluster_uuid"` | ||
Version VersionInfo `json:"version"` | ||
Tagline string `json:"tagline"` | ||
} | ||
|
||
// VersionInfo is the version info retrievable from the / endpoint, embedded in ClusterInfoResponse | ||
type VersionInfo struct { | ||
Number semver.Version `json:"number"` | ||
BuildHash string `json:"build_hash"` | ||
BuildDate string `json:"build_date"` | ||
BuildSnapshot bool `json:"build_snapshot"` | ||
LuceneVersion semver.Version `json:"lucene_version"` | ||
} | ||
|
||
func (c *ClusterInfoCollector) Update(ctx context.Context, ch chan<- prometheus.Metric) error { | ||
resp, err := c.hc.Get(c.u.String()) | ||
if err != nil { | ||
return err | ||
} | ||
defer resp.Body.Close() | ||
b, err := ioutil.ReadAll(resp.Body) | ||
if err != nil { | ||
return err | ||
} | ||
var info ClusterInfoResponse | ||
err = json.Unmarshal(b, &info) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
ch <- prometheus.MustNewConstMetric( | ||
clusterInfoDesc["version"], | ||
prometheus.GaugeValue, | ||
1, | ||
info.ClusterName, | ||
info.ClusterUUID, | ||
info.Version.BuildDate, | ||
info.Version.BuildHash, | ||
info.Version.Number.String(), | ||
info.Version.LuceneVersion.String(), | ||
) | ||
|
||
return nil | ||
} |
This file contains 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
This file contains 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