-
Notifications
You must be signed in to change notification settings - Fork 35
/
scrape.go
208 lines (178 loc) · 5.81 KB
/
scrape.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
package collector
import (
"context"
"fmt"
"net/http"
"net/url"
"strings"
"github.com/cprobe/cprobe/lib/logger"
"github.com/cprobe/cprobe/plugins/elasticsearch/pkg/roundtripper"
"github.com/cprobe/cprobe/types"
"github.com/pkg/errors"
"github.com/prometheus/client_golang/prometheus"
)
func (c *Config) Scrape(ctx context.Context, _target string, ss *types.Samples) error {
if !strings.HasPrefix(_target, "http") {
_target = "http://" + _target
}
target, err := url.Parse(_target)
if err != nil {
return errors.WithMessagef(err, "invalid target: %s", _target)
}
if c.Username != "" && c.Password != "" {
target.User = url.UserPassword(c.Username, c.Password)
}
// returns nil if not provided and falls back to simple TCP.
tlsConfig := createTLSConfig(c.TLSCa, c.TLSClientCert, c.TLSClientPrivateKey, c.TLSSkipVerify)
var httpTransport http.RoundTripper
httpTransport = &http.Transport{
TLSClientConfig: tlsConfig,
Proxy: http.ProxyFromEnvironment,
}
if c.ApiKey != "" {
httpTransport = &transportWithAPIKey{
underlyingTransport: httpTransport,
apiKey: c.ApiKey,
}
}
httpClient := &http.Client{
Timeout: c.Timeout,
Transport: httpTransport,
}
if c.AwsRegion != "" {
httpClient.Transport, err = roundtripper.NewAWSSigningTransport(httpTransport, c.AwsRegion, c.AwsRoleArn)
if err != nil {
return errors.WithMessage(err, "failed to create AWS transport")
}
}
defer httpClient.CloseIdleConnections()
var clusterName string
// 下面的几个 gather 方法是魔改了 exporter,不过指标名称尽量保持一致
// 这个改动比原来更清晰,原来的 exporter 考虑了把 cluster 信息周期性更新,引入了复杂度,实在没必要
// cprobe 这个场景只需要每次采集的一开始获取一次 cluster 信息即可
if clusterName, err = c.gatherClusterInfo(ctx, target, httpClient, ss); err != nil {
return errors.WithMessage(err, "failed to gather cluster info")
}
if err = c.gatherClusterHealth(ctx, target, httpClient, ss, clusterName); err != nil {
return errors.WithMessage(err, "failed to gather cluster health")
}
if err = c.gatherClusterSettings(ctx, target, httpClient, ss); err != nil {
logger.Errorf("failed to gather cluster settings: %s", err)
}
if err = c.gatherSnapshots(ctx, target, httpClient, ss); err != nil {
logger.Errorf("failed to gather snapshots: %s", err)
}
// 换成另一个写法,尽量不改动原本的 exporter 的逻辑,传递 channel 收集数据,主要是这部分指标太多了,改起来费劲
nodes := NewNodes(httpClient, target, c.GatherNode == "*", c.GatherNode)
nodesCh := make(chan prometheus.Metric)
go func() {
nodes.Collect(nodesCh)
close(nodesCh)
}()
for m := range nodesCh {
if err := ss.AddPromMetric(m); err != nil {
logger.Warnf("failed to transform nodes metric: %s", err)
}
}
if c.GatherIndices || c.GatherIndicesShards {
indices := NewIndices(httpClient, target, c.GatherIndicesShards, c.GatherIndicesUseAlias, clusterName)
indicesCh := make(chan prometheus.Metric)
go func() {
indices.Collect(indicesCh)
close(indicesCh)
}()
for m := range indicesCh {
if err := ss.AddPromMetric(m); err != nil {
logger.Warnf("failed to transform indices metric: %s", err)
}
}
if err = c.gatherShardsTotal(ctx, target, httpClient, ss, clusterName); err != nil {
logger.Errorf("failed to gather shards total: %s", err)
}
}
if c.GatherSlm {
slm := NewSLM(httpClient, target)
slmCh := make(chan prometheus.Metric)
go func() {
slm.Collect(slmCh)
close(slmCh)
}()
for m := range slmCh {
if err := ss.AddPromMetric(m); err != nil {
logger.Warnf("failed to transform slm metric: %s", err)
}
}
}
if c.GatherDataStream {
dataStream := NewDataStream(httpClient, target)
dataStreamCh := make(chan prometheus.Metric)
go func() {
dataStream.Collect(dataStreamCh)
close(dataStreamCh)
}()
for m := range dataStreamCh {
if err := ss.AddPromMetric(m); err != nil {
logger.Warnf("failed to transform data stream metric: %s", err)
}
}
}
if c.GatherIndicesSettings {
indicesSettings := NewIndicesSettings(httpClient, target)
indicesSettingsCh := make(chan prometheus.Metric)
go func() {
indicesSettings.Collect(indicesSettingsCh)
close(indicesSettingsCh)
}()
for m := range indicesSettingsCh {
if err := ss.AddPromMetric(m); err != nil {
logger.Warnf("failed to transform indices settings metric: %s", err)
}
}
}
if c.GatherIndicesMappings {
indicesMappings := NewIndicesMappings(httpClient, target)
indicesMappingsCh := make(chan prometheus.Metric)
go func() {
indicesMappings.Collect(indicesMappingsCh)
close(indicesMappingsCh)
}()
for m := range indicesMappingsCh {
if err := ss.AddPromMetric(m); err != nil {
logger.Warnf("failed to transform indices mappings metric: %s", err)
}
}
}
if c.GatherIlm {
ilmStatus := NewIlmStatus(httpClient, target)
ilmStatusCh := make(chan prometheus.Metric)
go func() {
ilmStatus.Collect(ilmStatusCh)
close(ilmStatusCh)
}()
for m := range ilmStatusCh {
if err := ss.AddPromMetric(m); err != nil {
logger.Warnf("failed to transform ilm status metric: %s", err)
}
}
ilmIndicies := NewIlmIndicies(httpClient, target)
ilmIndiciesCh := make(chan prometheus.Metric)
go func() {
ilmIndicies.Collect(ilmIndiciesCh)
close(ilmIndiciesCh)
}()
for m := range ilmIndiciesCh {
if err := ss.AddPromMetric(m); err != nil {
logger.Warnf("failed to transform ilm indicies metric: %s", err)
}
}
}
return nil
}
type transportWithAPIKey struct {
underlyingTransport http.RoundTripper
apiKey string
}
func (t *transportWithAPIKey) RoundTrip(req *http.Request) (*http.Response, error) {
req.Header.Add("Authorization", fmt.Sprintf("ApiKey %s", t.apiKey))
return t.underlyingTransport.RoundTrip(req)
}