-
Notifications
You must be signed in to change notification settings - Fork 35
/
cluster_info.go
76 lines (62 loc) · 1.77 KB
/
cluster_info.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
package collector
import (
"context"
"encoding/json"
"fmt"
"io"
"net/http"
"net/url"
"github.com/blang/semver/v4"
"github.com/cprobe/cprobe/types"
)
// 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 *Config) gatherClusterInfo(ctx context.Context, u *url.URL, hc *http.Client, ss *types.Samples) (string, error) {
if !c.GatherClusterInfo {
return "", nil
}
resp, err := hc.Get(u.String())
if err != nil {
return "", err
}
if resp.Body == nil {
return "", fmt.Errorf("empty response body")
}
defer resp.Body.Close()
b, err := io.ReadAll(resp.Body)
if err != nil {
return "", err
}
var info ClusterInfoResponse
err = json.Unmarshal(b, &info)
if err != nil {
return "", err
}
fields := map[string]interface{}{
"version": 1.0,
}
tags := map[string]string{
"cluster": info.ClusterName,
"cluster_uuid": info.ClusterUUID,
"build_date": info.Version.BuildDate,
"build_hash": info.Version.BuildHash,
"version": info.Version.Number.String(),
"lucene_version": info.Version.LuceneVersion.String(),
}
ss.AddMetric(namespace, fields, tags)
return info.ClusterName, nil
}