/
esstats.go
261 lines (214 loc) · 6.6 KB
/
esstats.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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
// Copyright 2019 The Serviced 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 isvcs
import (
"github.com/Sirupsen/logrus"
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"net/http"
"strconv"
"sync"
"time"
)
var esStore = NewElasticSearchStatsStore()
type ElasticSearchStats struct {
Address string
gc_young_count int
gc_young_time float64
gc_old_count int
gc_old_time float64
threads int
}
type ElasticSearchStatsCache struct {
stats map[string]ElasticSearchStats
mux sync.RWMutex
}
func NewElasticSearchStatsCache() *ElasticSearchStatsCache {
return &ElasticSearchStatsCache{
stats: make(map[string]ElasticSearchStats),
}
}
func (c *ElasticSearchStatsCache) ReadAll() []ElasticSearchStats {
c.mux.RLock()
defer c.mux.RUnlock()
stats := []ElasticSearchStats{}
for _, s := range c.stats {
stats = append(stats, s)
}
return stats
}
func (c *ElasticSearchStatsCache) Read(address string) (ElasticSearchStats, error) {
c.mux.RLock()
defer c.mux.RUnlock()
if s, ok := c.stats[address]; ok {
return s, nil
}
return ElasticSearchStats{}, errors.New(fmt.Sprintf("Unable to find %s in cache", address))
}
func (c *ElasticSearchStatsCache) Write(stats ElasticSearchStats) {
c.mux.Lock()
defer c.mux.Unlock()
c.stats[stats.Address] = stats
}
type ElasticSearchStatsStore interface {
ReadAll() []ElasticSearchStats
Read(address string) (ElasticSearchStats, error)
Write(stats ElasticSearchStats)
WriteAll(stats []ElasticSearchStats)
}
type elasticSearchStatsStore struct {
cache *ElasticSearchStatsCache
}
func NewElasticSearchStatsStore() ElasticSearchStatsStore {
return &elasticSearchStatsStore{NewElasticSearchStatsCache()}
}
func (ss *elasticSearchStatsStore) ReadAll() []ElasticSearchStats {
return ss.cache.ReadAll()
}
func (ss *elasticSearchStatsStore) Read(address string) (ElasticSearchStats, error) {
return ss.cache.Read(address)
}
func (ss *elasticSearchStatsStore) Write(stats ElasticSearchStats) {
ss.cache.Write(stats)
writeESToOpenTSDB([]ElasticSearchStats{stats})
}
func (ss *elasticSearchStatsStore) WriteAll(stats []ElasticSearchStats) {
for _, s := range stats {
ss.cache.Write(s)
}
writeESToOpenTSDB(stats)
}
func newElasticSearchMetric(name string, value string, timestamp int64, address string) metric {
tags := map[string]string{
"isvc": "true",
}
if address == "http://127.0.0.1:9200" {
tags["controlplane_service_id"] = elasticsearch_serviced.ID
} else if address == "http://127.0.0.1:9100" {
tags["controlplane_service_id"] = elasticsearch_logstash.ID
}
return metric{name, value, timestamp, tags}
}
func writeESToOpenTSDB(stats []ElasticSearchStats) {
t := time.Now()
metrics := []metric{}
for _, s := range stats {
threadsMetric := newElasticSearchMetric(
"isvcs.jvm.threads.count",
strconv.Itoa(s.threads),
t.Unix(),
s.Address,
)
metrics = append(metrics, threadsMetric)
gcOldTimeMetric := newElasticSearchMetric(
"isvcs.jvm.gc.old.collection_time",
fmt.Sprintf("%.2f", s.gc_old_time),
t.Unix(),
s.Address,
)
metrics = append(metrics, gcOldTimeMetric)
gcOldCountMetric := newElasticSearchMetric(
"isvcs.jvm.gc.old.collection_count",
strconv.Itoa(s.gc_old_count),
t.Unix(),
s.Address,
)
metrics = append(metrics, gcOldCountMetric)
gcYoungTimeMetric := newElasticSearchMetric(
"isvcs.jvm.gc.young.collection_time",
fmt.Sprintf("%.2f", s.gc_young_time),
t.Unix(),
s.Address,
)
metrics = append(metrics, gcYoungTimeMetric)
gcYoungCountMetric := newElasticSearchMetric(
"isvcs.jvm.gc.young.collection_count",
strconv.Itoa(s.gc_young_count),
t.Unix(),
s.Address,
)
metrics = append(metrics, gcYoungCountMetric)
}
err := postDataToOpenTSDB(metrics)
if err != nil {
log.WithFields(logrus.Fields{
"numberOfMetrics": len(metrics),
}).WithError(err).Warn("Unable to write ElasticSearch metrics to OpenTSDB")
}
}
func queryElasticSearchStats(address string) ElasticSearchStats {
logger := log.WithField("elasticsearch_address", address)
stats := ElasticSearchStats{Address: address}
resp, err := http.Get(address + "/_nodes/stats/jvm")
if resp != nil && resp.Body != nil {
defer resp.Body.Close()
}
if err != nil {
logger.WithError(err).Warn("Unable to get ElasticSearch stats")
return stats
}
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
logger.WithError(err).Warn("Unable to read ElasticSearch stats")
return stats
}
var result map[string]interface{}
json.Unmarshal([]byte(body), &result)
nodes := result["nodes"].(map[string]interface{})
for _, value := range nodes {
jvm_metrics := value.(map[string]interface{})["jvm"].(map[string]interface{})
thread_count := jvm_metrics["threads"].(map[string]interface{})["count"].(float64)
stats.threads += int(thread_count)
gc_collectors := jvm_metrics["gc"].(map[string]interface{})["collectors"].(map[string]interface{})
for key, value := range gc_collectors {
gc_metrics := value.(map[string]interface{})
if key == "young" || key == "ParNew" {
stats.gc_young_count += int(gc_metrics["collection_count"].(float64))
stats.gc_young_time += gc_metrics["collection_time_in_millis"].(float64) / 1000
} else if key == "old" || key == "ConcurrentMarkSweep" {
stats.gc_old_count += int(gc_metrics["collection_count"].(float64))
stats.gc_old_time += gc_metrics["collection_time_in_millis"].(float64) / 1000
}
}
}
return stats
}
// GetElasticSearchCustomStats retrieves ElasticSearch specific stats form the ElasticSearch servers.
// This should be run as a separate go routine.
func GetElasticSearchCustomStats(halt <-chan struct{}) error {
timeout := 30 * time.Second
timer := time.NewTimer(timeout)
es_addresses := [2]string{"http://127.0.0.1:9200", "http://127.0.0.1:9100"}
for {
select {
case <-timer.C:
stats := []ElasticSearchStats{}
for key := range es_addresses {
stats = append(stats, queryElasticSearchStats(es_addresses[key]))
}
esStore.WriteAll(stats)
case <-halt:
log.Info("Stopped getting custom stats for ElasticSearch")
return nil
}
if !timer.Stop() {
select {
case <-timer.C:
default:
}
}
timer.Reset(timeout)
}
}