1
1
package main
2
2
3
3
import (
4
+ "errors"
4
5
"fmt"
5
6
"regexp"
6
7
"strings"
@@ -14,70 +15,72 @@ const indexLimit = 500
14
15
15
16
// populateMetrics wrapper to call each of the individual populate functions
16
17
func populateMetrics (i * integration.Integration , client Client , env string ) {
17
- err := populateNodesMetrics (i , client )
18
+ clusterName , err := populateClusterMetrics (i , client , env )
18
19
if err != nil {
19
- log .Error ("There was an error populating metrics for nodes : %v" , err )
20
+ log .Error ("There was an error populating metrics for clusters : %v" , err )
20
21
}
21
22
22
- err = populateClusterMetrics (i , client , env )
23
+ err = populateNodesMetrics (i , client , clusterName )
23
24
if err != nil {
24
- log .Error ("There was an error populating metrics for clusters : %v" , err )
25
+ log .Error ("There was an error populating metrics for nodes : %v" , err )
25
26
}
26
27
27
28
// we want to use the response from common to populate some index-specific stats.
28
- commonResponse , err := populateCommonMetrics (i , client )
29
+ commonResponse , err := populateCommonMetrics (i , client , clusterName )
29
30
if err != nil {
30
31
log .Error ("There was an error populating metrics for common metrics: %v" , err )
31
32
}
32
33
33
34
if args .CollectIndices {
34
- err = populateIndicesMetrics (i , client , commonResponse )
35
+ err = populateIndicesMetrics (i , client , commonResponse , clusterName )
35
36
if err != nil {
36
37
log .Error ("There was an error populating metrics for indices: %v" , err )
37
38
}
38
39
}
39
40
}
40
41
41
- func populateNodesMetrics (i * integration.Integration , client Client ) error {
42
+ func populateNodesMetrics (i * integration.Integration , client Client , clusterName * string ) error {
42
43
log .Info ("Collecting node metrics" )
43
44
nodeResponse := new (NodeResponse )
44
45
err := client .Request (nodeStatsEndpoint , & nodeResponse )
45
46
if err != nil {
46
47
return err
47
48
}
48
49
49
- setNodesMetricsResponse (i , nodeResponse )
50
+ setNodesMetricsResponse (i , nodeResponse , clusterName )
50
51
return nil
51
52
}
52
53
53
54
// setNodesMetricsResponse calls setMetricsResponse for each node in the response
54
- func setNodesMetricsResponse (integration * integration.Integration , resp * NodeResponse ) {
55
+ func setNodesMetricsResponse (integration * integration.Integration , resp * NodeResponse , clusterName * string ) {
55
56
for node := range resp .Nodes {
56
- err := setMetricsResponse (integration , resp .Nodes [node ], node , "node" )
57
+ err := setMetricsResponse (integration , resp .Nodes [node ], * resp . Nodes [ node ]. Host , "node" , clusterName )
57
58
if err != nil {
58
59
log .Error ("There was an error setting metrics for node metrics on %s: %v" , node , err )
59
60
}
60
61
}
61
62
}
62
63
63
- func populateClusterMetrics (i * integration.Integration , client Client , env string ) error {
64
+ func populateClusterMetrics (i * integration.Integration , client Client , env string ) ( * string , error ) {
64
65
log .Info ("Collecting cluster metrics." )
65
66
clusterResponse := new (ClusterResponse )
66
67
err := client .Request (clusterEndpoint , & clusterResponse )
67
68
if err != nil {
68
- return err
69
+ return nil , err
69
70
}
70
71
71
72
if clusterResponse .Name == nil {
72
- return fmt . Errorf ("cannot set metric response, missing cluster name" )
73
+ return nil , errors . New ("cannot set metric response, missing cluster name" )
73
74
}
75
+
74
76
if env != "" {
75
77
* clusterResponse .Name = * clusterResponse .Name + ":" + env
76
78
}
77
- return setMetricsResponse (i , clusterResponse , * clusterResponse .Name , "cluster" )
79
+
80
+ return clusterResponse .Name , setMetricsResponse (i , clusterResponse , * clusterResponse .Name , "cluster" , nil )
78
81
}
79
82
80
- func populateCommonMetrics (i * integration.Integration , client Client ) (* CommonMetrics , error ) {
83
+ func populateCommonMetrics (i * integration.Integration , client Client , clusterName * string ) (* CommonMetrics , error ) {
81
84
log .Info ("Collecting common metrics." )
82
85
commonResponse := new (CommonMetrics )
83
86
err := client .Request (commonStatsEndpoint , & commonResponse )
@@ -86,13 +89,13 @@ func populateCommonMetrics(i *integration.Integration, client Client) (*CommonMe
86
89
}
87
90
88
91
if args .CollectPrimaries {
89
- err = setMetricsResponse (i , commonResponse .All , "commonMetrics" , "common" )
92
+ err = setMetricsResponse (i , commonResponse .All , "commonMetrics" , "common" , clusterName )
90
93
}
91
94
92
95
return commonResponse , err
93
96
}
94
97
95
- func populateIndicesMetrics (i * integration.Integration , client Client , commonStats * CommonMetrics ) error {
98
+ func populateIndicesMetrics (i * integration.Integration , client Client , commonStats * CommonMetrics , clusterName * string ) error {
96
99
log .Info ("Collecting indices metrics" )
97
100
indicesStats := make ([]* IndexStats , 0 )
98
101
err := client .Request (indicesStatsEndpoint , & indicesStats )
@@ -105,7 +108,7 @@ func populateIndicesMetrics(i *integration.Integration, client Client, commonSta
105
108
return err
106
109
}
107
110
108
- setIndicesStatsMetricsResponse (i , indicesStats , commonStats , indexRegex )
111
+ setIndicesStatsMetricsResponse (i , indicesStats , commonStats , clusterName , indexRegex )
109
112
return nil
110
113
}
111
114
@@ -119,7 +122,7 @@ func buildRegex() (indexRegex *regexp.Regexp, err error) {
119
122
return indexRegex , nil
120
123
}
121
124
122
- func setIndicesStatsMetricsResponse (integration * integration.Integration , indexResponse []* IndexStats , commonResponse * CommonMetrics , indexRegex * regexp.Regexp ) {
125
+ func setIndicesStatsMetricsResponse (integration * integration.Integration , indexResponse []* IndexStats , commonResponse * CommonMetrics , clusterName * string , indexRegex * regexp.Regexp ) {
123
126
type indexStatsObject struct {
124
127
name string
125
128
stats * IndexStats
@@ -161,7 +164,7 @@ func setIndicesStatsMetricsResponse(integration *integration.Integration, indexR
161
164
}
162
165
163
166
for _ , index := range indicesToCollect {
164
- if err := setMetricsResponse (integration , index .stats , index .name , "index" ); err != nil {
167
+ if err := setMetricsResponse (integration , index .stats , index .name , "index" , clusterName ); err != nil {
165
168
log .Error ("There was an error setting metrics for indices metrics: %v" , err )
166
169
}
167
170
}
@@ -177,16 +180,23 @@ func getIndexFromCommon(indexName string, indexList map[string]*Index) (*Index,
177
180
178
181
// setMetricsResponse creates an entity and a metric set for the
179
182
// type of response and calls MarshalMetrics using that response
180
- func setMetricsResponse (integration * integration.Integration , resp interface {}, name string , namespace string ) error {
183
+ func setMetricsResponse (integration * integration.Integration , resp interface {}, name string , namespace string , clusterName * string ) error {
181
184
entity , err := integration .Entity (name , namespace )
182
185
if err != nil {
183
186
return err
184
187
}
185
188
186
- metricSet := entity .NewMetricSet (getSampleName (namespace ),
187
- metric.Attribute {Key : "displayName" , Value : entity .Metadata .Name },
188
- metric.Attribute {Key : "entityName" , Value : entity .Metadata .Namespace + ":" + entity .Metadata .Name },
189
- )
189
+ idAttributes := []metric.Attribute {
190
+ {Key : "displayName" , Value : entity .Metadata .Name },
191
+ {Key : "entityName" , Value : entity .Metadata .Namespace + ":" + entity .Metadata .Name },
192
+ }
193
+
194
+ // If clusterName is non empty apply it
195
+ if clusterName != nil {
196
+ idAttributes = append (idAttributes , metric.Attribute {Key : "clusterName" , Value : * clusterName })
197
+ }
198
+
199
+ metricSet := entity .NewMetricSet (getSampleName (namespace ), idAttributes ... )
190
200
191
201
return metricSet .MarshalMetrics (resp )
192
202
}
0 commit comments