| 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 |
package circonusgometrics |
| 31 |
|
| 32 |
import ( |
| 33 |
"bufio" |
| 34 |
"bytes" |
| 35 |
"fmt" |
| 36 |
"io/ioutil" |
| 37 |
"log" |
| 38 |
"os" |
| 39 |
"strconv" |
| 40 |
"strings" |
| 41 |
"sync" |
| 42 |
"time" |
| 43 |
|
| 44 |
"github.com/circonus-labs/circonus-gometrics/api" |
| 45 |
"github.com/circonus-labs/circonus-gometrics/checkmgr" |
| 46 |
"github.com/pkg/errors" |
| 47 |
) |
| 48 |
|
| 49 |
const ( |
| 50 |
defaultFlushInterval = "10s" |
| 51 |
) |
| 52 |
|
| 53 |
|
| 54 |
type Metric struct { |
| 55 |
Type string `json:"_type"` |
| 56 |
Value interface{} `json:"_value"` |
| 57 |
} |
| 58 |
|
| 59 |
|
| 60 |
type Metrics map[string]Metric |
| 61 |
|
| 62 |
|
| 63 |
type Config struct { |
| 64 |
Log *log.Logger |
| 65 |
Debug bool |
| 66 |
ResetCounters string |
| 67 |
ResetGauges string |
| 68 |
ResetHistograms string |
| 69 |
ResetText string |
| 70 |
|
| 71 |
|
| 72 |
CheckManager checkmgr.Config |
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
Interval string |
| 77 |
} |
| 78 |
|
| 79 |
type prevMetrics struct { |
| 80 |
metrics *Metrics |
| 81 |
metricsmu sync.Mutex |
| 82 |
ts time.Time |
| 83 |
} |
| 84 |
|
| 85 |
|
| 86 |
type CirconusMetrics struct { |
| 87 |
Log *log.Logger |
| 88 |
Debug bool |
| 89 |
|
| 90 |
resetCounters bool |
| 91 |
resetGauges bool |
| 92 |
resetHistograms bool |
| 93 |
resetText bool |
| 94 |
flushInterval time.Duration |
| 95 |
flushing bool |
| 96 |
flushmu sync.Mutex |
| 97 |
packagingmu sync.Mutex |
| 98 |
check *checkmgr.CheckManager |
| 99 |
lastMetrics *prevMetrics |
| 100 |
|
| 101 |
counters map[string]uint64 |
| 102 |
cm sync.Mutex |
| 103 |
|
| 104 |
counterFuncs map[string]func() uint64 |
| 105 |
cfm sync.Mutex |
| 106 |
|
| 107 |
gauges map[string]interface{} |
| 108 |
gm sync.Mutex |
| 109 |
|
| 110 |
gaugeFuncs map[string]func() int64 |
| 111 |
gfm sync.Mutex |
| 112 |
|
| 113 |
histograms map[string]*Histogram |
| 114 |
hm sync.Mutex |
| 115 |
|
| 116 |
text map[string]string |
| 117 |
tm sync.Mutex |
| 118 |
|
| 119 |
textFuncs map[string]func() string |
| 120 |
tfm sync.Mutex |
| 121 |
} |
| 122 |
|
| 123 |
|
| 124 |
func NewCirconusMetrics(cfg *Config) (*CirconusMetrics, error) { |
| 125 |
return New(cfg) |
| 126 |
} |
| 127 |
|
| 128 |
|
| 129 |
func New(cfg *Config) (*CirconusMetrics, error) { |
| 130 |
|
| 131 |
if cfg == nil { |
| 132 |
return nil, errors.New("invalid configuration (nil)") |
| 133 |
} |
| 134 |
|
| 135 |
cm := &CirconusMetrics{ |
| 136 |
counters: make(map[string]uint64), |
| 137 |
counterFuncs: make(map[string]func() uint64), |
| 138 |
gauges: make(map[string]interface{}), |
| 139 |
gaugeFuncs: make(map[string]func() int64), |
| 140 |
histograms: make(map[string]*Histogram), |
| 141 |
text: make(map[string]string), |
| 142 |
textFuncs: make(map[string]func() string), |
| 143 |
lastMetrics: &prevMetrics{}, |
| 144 |
} |
| 145 |
|
| 146 |
|
| 147 |
{ |
| 148 |
cm.Debug = cfg.Debug |
| 149 |
cm.Log = cfg.Log |
| 150 |
|
| 151 |
if cm.Debug && cm.Log == nil { |
| 152 |
cm.Log = log.New(os.Stderr, "", log.LstdFlags) |
| 153 |
} |
| 154 |
if cm.Log == nil { |
| 155 |
cm.Log = log.New(ioutil.Discard, "", log.LstdFlags) |
| 156 |
} |
| 157 |
} |
| 158 |
|
| 159 |
|
| 160 |
{ |
| 161 |
fi := defaultFlushInterval |
| 162 |
if cfg.Interval != "" { |
| 163 |
fi = cfg.Interval |
| 164 |
} |
| 165 |
|
| 166 |
dur, err := time.ParseDuration(fi) |
| 167 |
if err != nil { |
| 168 |
return nil, errors.Wrap(err, "parsing flush interval") |
| 169 |
} |
| 170 |
cm.flushInterval = dur |
| 171 |
} |
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
cm.resetCounters = true |
| 176 |
if cfg.ResetCounters != "" { |
| 177 |
setting, err := strconv.ParseBool(cfg.ResetCounters) |
| 178 |
if err != nil { |
| 179 |
return nil, errors.Wrap(err, "parsing reset counters") |
| 180 |
} |
| 181 |
cm.resetCounters = setting |
| 182 |
} |
| 183 |
|
| 184 |
cm.resetGauges = true |
| 185 |
if cfg.ResetGauges != "" { |
| 186 |
setting, err := strconv.ParseBool(cfg.ResetGauges) |
| 187 |
if err != nil { |
| 188 |
return nil, errors.Wrap(err, "parsing reset gauges") |
| 189 |
} |
| 190 |
cm.resetGauges = setting |
| 191 |
} |
| 192 |
|
| 193 |
cm.resetHistograms = true |
| 194 |
if cfg.ResetHistograms != "" { |
| 195 |
setting, err := strconv.ParseBool(cfg.ResetHistograms) |
| 196 |
if err != nil { |
| 197 |
return nil, errors.Wrap(err, "parsing reset histograms") |
| 198 |
} |
| 199 |
cm.resetHistograms = setting |
| 200 |
} |
| 201 |
|
| 202 |
cm.resetText = true |
| 203 |
if cfg.ResetText != "" { |
| 204 |
setting, err := strconv.ParseBool(cfg.ResetText) |
| 205 |
if err != nil { |
| 206 |
return nil, errors.Wrap(err, "parsing reset text") |
| 207 |
} |
| 208 |
cm.resetText = setting |
| 209 |
} |
| 210 |
|
| 211 |
|
| 212 |
{ |
| 213 |
cfg.CheckManager.Debug = cm.Debug |
| 214 |
cfg.CheckManager.Log = cm.Log |
| 215 |
|
| 216 |
check, err := checkmgr.New(&cfg.CheckManager) |
| 217 |
if err != nil { |
| 218 |
return nil, errors.Wrap(err, "creating new check manager") |
| 219 |
} |
| 220 |
cm.check = check |
| 221 |
} |
| 222 |
|
| 223 |
|
| 224 |
cm.check.Initialize() |
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
if cm.flushInterval > time.Duration(0) { |
| 229 |
go func() { |
| 230 |
for range time.NewTicker(cm.flushInterval).C { |
| 231 |
cm.Flush() |
| 232 |
} |
| 233 |
}() |
| 234 |
} |
| 235 |
|
| 236 |
return cm, nil |
| 237 |
} |
| 238 |
|
| 239 |
|
| 240 |
func (m *CirconusMetrics) Start() { |
| 241 |
|
| 242 |
} |
| 243 |
|
| 244 |
|
| 245 |
func (m *CirconusMetrics) Ready() bool { |
| 246 |
return m.check.IsReady() |
| 247 |
} |
| 248 |
|
| 249 |
func (m *CirconusMetrics) packageMetrics() (map[string]*api.CheckBundleMetric, Metrics) { |
| 250 |
|
| 251 |
m.packagingmu.Lock() |
| 252 |
defer m.packagingmu.Unlock() |
| 253 |
|
| 254 |
if m.Debug { |
| 255 |
m.Log.Println("[DEBUG] Packaging metrics") |
| 256 |
} |
| 257 |
|
| 258 |
counters, gauges, histograms, text := m.snapshot() |
| 259 |
newMetrics := make(map[string]*api.CheckBundleMetric) |
| 260 |
output := make(Metrics, len(counters)+len(gauges)+len(histograms)+len(text)) |
| 261 |
for name, value := range counters { |
| 262 |
send := m.check.IsMetricActive(name) |
| 263 |
if !send && m.check.ActivateMetric(name) { |
| 264 |
send = true |
| 265 |
newMetrics[name] = &api.CheckBundleMetric{ |
| 266 |
Name: name, |
| 267 |
Type: "numeric", |
| 268 |
Status: "active", |
| 269 |
} |
| 270 |
} |
| 271 |
if send { |
| 272 |
output[name] = Metric{Type: "L", Value: value} |
| 273 |
} |
| 274 |
} |
| 275 |
|
| 276 |
for name, value := range gauges { |
| 277 |
send := m.check.IsMetricActive(name) |
| 278 |
if !send && m.check.ActivateMetric(name) { |
| 279 |
send = true |
| 280 |
newMetrics[name] = &api.CheckBundleMetric{ |
| 281 |
Name: name, |
| 282 |
Type: "numeric", |
| 283 |
Status: "active", |
| 284 |
} |
| 285 |
} |
| 286 |
if send { |
| 287 |
output[name] = Metric{Type: m.getGaugeType(value), Value: value} |
| 288 |
} |
| 289 |
} |
| 290 |
|
| 291 |
for name, value := range histograms { |
| 292 |
send := m.check.IsMetricActive(name) |
| 293 |
if !send && m.check.ActivateMetric(name) { |
| 294 |
send = true |
| 295 |
newMetrics[name] = &api.CheckBundleMetric{ |
| 296 |
Name: name, |
| 297 |
Type: "histogram", |
| 298 |
Status: "active", |
| 299 |
} |
| 300 |
} |
| 301 |
if send { |
| 302 |
output[name] = Metric{Type: "n", Value: value.DecStrings()} |
| 303 |
} |
| 304 |
} |
| 305 |
|
| 306 |
for name, value := range text { |
| 307 |
send := m.check.IsMetricActive(name) |
| 308 |
if !send && m.check.ActivateMetric(name) { |
| 309 |
send = true |
| 310 |
newMetrics[name] = &api.CheckBundleMetric{ |
| 311 |
Name: name, |
| 312 |
Type: "text", |
| 313 |
Status: "active", |
| 314 |
} |
| 315 |
} |
| 316 |
if send { |
| 317 |
output[name] = Metric{Type: "s", Value: value} |
| 318 |
} |
| 319 |
} |
| 320 |
|
| 321 |
m.lastMetrics.metricsmu.Lock() |
| 322 |
defer m.lastMetrics.metricsmu.Unlock() |
| 323 |
m.lastMetrics.metrics = &output |
| 324 |
m.lastMetrics.ts = time.Now() |
| 325 |
|
| 326 |
return newMetrics, output |
| 327 |
} |
| 328 |
|
| 329 |
|
| 330 |
func (m *CirconusMetrics) PromOutput() (*bytes.Buffer, error) { |
| 331 |
m.lastMetrics.metricsmu.Lock() |
| 332 |
defer m.lastMetrics.metricsmu.Unlock() |
| 333 |
|
| 334 |
if m.lastMetrics.metrics == nil { |
| 335 |
return nil, errors.New("no metrics available") |
| 336 |
} |
| 337 |
|
| 338 |
var b bytes.Buffer |
| 339 |
w := bufio.NewWriter(&b) |
| 340 |
|
| 341 |
ts := m.lastMetrics.ts.UnixNano() / int64(time.Millisecond) |
| 342 |
|
| 343 |
for name, metric := range *m.lastMetrics.metrics { |
| 344 |
switch metric.Type { |
| 345 |
case "n": |
| 346 |
if strings.HasPrefix(fmt.Sprintf("%v", metric.Value), "[H[") { |
| 347 |
continue |
| 348 |
} |
| 349 |
case "s": |
| 350 |
continue |
| 351 |
} |
| 352 |
fmt.Fprintf(w, "%s %v %d\n", name, metric.Value, ts) |
| 353 |
} |
| 354 |
|
| 355 |
err := w.Flush() |
| 356 |
if err != nil { |
| 357 |
return nil, errors.Wrap(err, "flushing metric buffer") |
| 358 |
} |
| 359 |
|
| 360 |
return &b, err |
| 361 |
} |
| 362 |
|
| 363 |
|
| 364 |
func (m *CirconusMetrics) FlushMetrics() *Metrics { |
| 365 |
m.flushmu.Lock() |
| 366 |
if m.flushing { |
| 367 |
m.flushmu.Unlock() |
| 368 |
return &Metrics{} |
| 369 |
} |
| 370 |
|
| 371 |
m.flushing = true |
| 372 |
m.flushmu.Unlock() |
| 373 |
|
| 374 |
_, output := m.packageMetrics() |
| 375 |
|
| 376 |
m.flushmu.Lock() |
| 377 |
m.flushing = false |
| 378 |
m.flushmu.Unlock() |
| 379 |
|
| 380 |
return &output |
| 381 |
} |
| 382 |
|
| 383 |
|
| 384 |
func (m *CirconusMetrics) Flush() { |
| 385 |
m.flushmu.Lock() |
| 386 |
if m.flushing { |
| 387 |
m.flushmu.Unlock() |
| 388 |
return |
| 389 |
} |
| 390 |
|
| 391 |
m.flushing = true |
| 392 |
m.flushmu.Unlock() |
| 393 |
|
| 394 |
newMetrics, output := m.packageMetrics() |
| 395 |
|
| 396 |
if len(output) > 0 { |
| 397 |
m.submit(output, newMetrics) |
| 398 |
} else { |
| 399 |
if m.Debug { |
| 400 |
m.Log.Println("[DEBUG] No metrics to send, skipping") |
| 401 |
} |
| 402 |
} |
| 403 |
|
| 404 |
m.flushmu.Lock() |
| 405 |
m.flushing = false |
| 406 |
m.flushmu.Unlock() |
| 407 |
} |