Skip to content

Commit 6c24229

Browse files
authored
Revert "Add cluster label to shard metric (prometheus-community#639)"
This reverts commit b1136b2.
1 parent 8e31f72 commit 6c24229

File tree

2 files changed

+24
-67
lines changed

2 files changed

+24
-67
lines changed

collector/shards.go

Lines changed: 23 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,11 @@
1010
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1111
// See the License for the specific language governing permissions and
1212
// limitations under the License.
13-
1413
package collector
1514

1615
import (
1716
"encoding/json"
1817
"fmt"
19-
"github.com/prometheus-community/elasticsearch_exporter/pkg/clusterinfo"
2018
"net/http"
2119
"net/url"
2220
"path"
@@ -26,104 +24,66 @@ import (
2624
"github.com/prometheus/client_golang/prometheus"
2725
)
2826

27+
var (
28+
defaultNodeShardLabels = []string{"node"}
29+
30+
defaultNodeShardLabelValues = func(node string) []string {
31+
return []string{
32+
node,
33+
}
34+
}
35+
)
36+
2937
// ShardResponse has shard's node and index info
3038
type ShardResponse struct {
3139
Index string `json:"index"`
3240
Shard string `json:"shard"`
33-
State string `json:"state"`
3441
Node string `json:"node"`
3542
}
3643

3744
// Shards information struct
3845
type Shards struct {
39-
logger log.Logger
40-
client *http.Client
41-
url *url.URL
42-
clusterInfoCh chan *clusterinfo.Response
43-
lastClusterInfo *clusterinfo.Response
46+
logger log.Logger
47+
client *http.Client
48+
url *url.URL
4449

4550
nodeShardMetrics []*nodeShardMetric
4651
jsonParseFailures prometheus.Counter
4752
}
4853

49-
// ClusterLabelUpdates returns a pointer to a channel to receive cluster info updates. It implements the
50-
// (not exported) clusterinfo.consumer interface
51-
func (s *Shards) ClusterLabelUpdates() *chan *clusterinfo.Response {
52-
return &s.clusterInfoCh
53-
}
54-
55-
// String implements the stringer interface. It is part of the clusterinfo.consumer interface
56-
func (s *Shards) String() string {
57-
return namespace + "shards"
58-
}
59-
6054
type nodeShardMetric struct {
6155
Type prometheus.ValueType
6256
Desc *prometheus.Desc
6357
Value func(shards float64) float64
64-
Labels labels
58+
Labels func(node string) []string
6559
}
6660

6761
// NewShards defines Shards Prometheus metrics
6862
func NewShards(logger log.Logger, client *http.Client, url *url.URL) *Shards {
69-
70-
nodeLabels := labels{
71-
keys: func(...string) []string {
72-
return []string{"node", "cluster"}
73-
},
74-
values: func(lastClusterinfo *clusterinfo.Response, s ...string) []string {
75-
if lastClusterinfo != nil {
76-
return append(s, lastClusterinfo.ClusterName)
77-
}
78-
// this shouldn't happen, as the clusterinfo Retriever has a blocking
79-
// Run method. It blocks until the first clusterinfo call has succeeded
80-
return append(s, "unknown_cluster")
81-
},
82-
}
83-
84-
shards := &Shards{
63+
return &Shards{
8564
logger: logger,
8665
client: client,
8766
url: url,
8867

89-
clusterInfoCh: make(chan *clusterinfo.Response),
90-
lastClusterInfo: &clusterinfo.Response{
91-
ClusterName: "unknown_cluster",
92-
},
93-
9468
nodeShardMetrics: []*nodeShardMetric{
9569
{
9670
Type: prometheus.GaugeValue,
9771
Desc: prometheus.NewDesc(
9872
prometheus.BuildFQName(namespace, "node_shards", "total"),
9973
"Total shards per node",
100-
nodeLabels.keys(), nil,
74+
defaultNodeShardLabels, nil,
10175
),
10276
Value: func(shards float64) float64 {
10377
return shards
10478
},
105-
Labels: nodeLabels,
79+
Labels: defaultNodeShardLabelValues,
10680
}},
10781

10882
jsonParseFailures: prometheus.NewCounter(prometheus.CounterOpts{
10983
Name: prometheus.BuildFQName(namespace, "node_shards", "json_parse_failures"),
11084
Help: "Number of errors while parsing JSON.",
11185
}),
11286
}
113-
114-
// start go routine to fetch clusterinfo updates and save them to lastClusterinfo
115-
go func() {
116-
level.Debug(logger).Log("msg", "starting cluster info receive loop")
117-
for ci := range shards.clusterInfoCh {
118-
if ci != nil {
119-
level.Debug(logger).Log("msg", "received cluster info update", "cluster", ci.ClusterName)
120-
shards.lastClusterInfo = ci
121-
}
122-
}
123-
level.Debug(logger).Log("msg", "exiting cluster info receive loop")
124-
}()
125-
126-
return shards
12787
}
12888

12989
// Describe Shards
@@ -177,7 +137,7 @@ func (s *Shards) fetchAndDecodeShards() ([]ShardResponse, error) {
177137
return sfr, err
178138
}
179139

180-
// Collect number of shards on each node
140+
// Collect number of shards on each nodes
181141
func (s *Shards) Collect(ch chan<- prometheus.Metric) {
182142

183143
defer func() {
@@ -196,8 +156,10 @@ func (s *Shards) Collect(ch chan<- prometheus.Metric) {
196156
nodeShards := make(map[string]float64)
197157

198158
for _, shard := range sr {
199-
if shard.State == "STARTED" {
200-
nodeShards[shard.Node]++
159+
if val, ok := nodeShards[shard.Node]; ok {
160+
nodeShards[shard.Node] = val + 1
161+
} else {
162+
nodeShards[shard.Node] = 1
201163
}
202164
}
203165

@@ -207,7 +169,7 @@ func (s *Shards) Collect(ch chan<- prometheus.Metric) {
207169
metric.Desc,
208170
metric.Type,
209171
metric.Value(shards),
210-
metric.Labels.values(s.lastClusterInfo, node)...,
172+
metric.Labels(node)...,
211173
)
212174
}
213175
}

main.go

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -199,18 +199,13 @@ func main() {
199199
prometheus.MustRegister(collector.NewNodes(logger, httpClient, esURL, *esAllNodes, *esNode))
200200

201201
if *esExportIndices || *esExportShards {
202-
sC := collector.NewShards(logger, httpClient, esURL)
203-
prometheus.MustRegister(sC)
202+
prometheus.MustRegister(collector.NewShards(logger, httpClient, esURL))
204203
iC := collector.NewIndices(logger, httpClient, esURL, *esExportShards, *esExportIndexAliases)
205204
prometheus.MustRegister(iC)
206205
if registerErr := clusterInfoRetriever.RegisterConsumer(iC); registerErr != nil {
207206
level.Error(logger).Log("msg", "failed to register indices collector in cluster info")
208207
os.Exit(1)
209208
}
210-
if registerErr := clusterInfoRetriever.RegisterConsumer(sC); registerErr != nil {
211-
level.Error(logger).Log("msg", "failed to register shards collector in cluster info")
212-
os.Exit(1)
213-
}
214209
}
215210

216211
if *esExportSLM {

0 commit comments

Comments
 (0)