2016-11-01 19:45:37 +00:00
|
|
|
// Copyright 2016 Circonus, Inc. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
2016-07-22 16:33:10 +00:00
|
|
|
// Package circonusgometrics provides instrumentation for your applications in the form
|
|
|
|
// of counters, gauges and histograms and allows you to publish them to
|
|
|
|
// Circonus
|
|
|
|
//
|
|
|
|
// Counters
|
|
|
|
//
|
|
|
|
// A counter is a monotonically-increasing, unsigned, 64-bit integer used to
|
|
|
|
// represent the number of times an event has occurred. By tracking the deltas
|
|
|
|
// between measurements of a counter over intervals of time, an aggregation
|
|
|
|
// layer can derive rates, acceleration, etc.
|
|
|
|
//
|
|
|
|
// Gauges
|
|
|
|
//
|
|
|
|
// A gauge returns instantaneous measurements of something using signed, 64-bit
|
|
|
|
// integers. This value does not need to be monotonic.
|
|
|
|
//
|
|
|
|
// Histograms
|
|
|
|
//
|
|
|
|
// A histogram tracks the distribution of a stream of values (e.g. the number of
|
|
|
|
// seconds it takes to handle requests). Circonus can calculate complex
|
|
|
|
// analytics on these.
|
|
|
|
//
|
|
|
|
// Reporting
|
|
|
|
//
|
|
|
|
// A period push to a Circonus httptrap is confgurable.
|
|
|
|
package circonusgometrics
|
|
|
|
|
|
|
|
import (
|
2018-09-10 22:03:17 +00:00
|
|
|
"bufio"
|
|
|
|
"bytes"
|
|
|
|
"fmt"
|
2016-07-22 16:33:10 +00:00
|
|
|
"io/ioutil"
|
|
|
|
"log"
|
|
|
|
"os"
|
2016-11-01 19:45:37 +00:00
|
|
|
"strconv"
|
2018-09-10 22:03:17 +00:00
|
|
|
"strings"
|
2016-07-22 16:33:10 +00:00
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/circonus-labs/circonus-gometrics/api"
|
|
|
|
"github.com/circonus-labs/circonus-gometrics/checkmgr"
|
2018-09-10 22:03:17 +00:00
|
|
|
"github.com/pkg/errors"
|
2016-07-22 16:33:10 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
defaultFlushInterval = "10s" // 10 * time.Second
|
|
|
|
)
|
|
|
|
|
2018-09-10 22:03:17 +00:00
|
|
|
// Metric defines an individual metric
|
|
|
|
type Metric struct {
|
|
|
|
Type string `json:"_type"`
|
|
|
|
Value interface{} `json:"_value"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// Metrics holds host metrics
|
|
|
|
type Metrics map[string]Metric
|
|
|
|
|
2016-07-22 16:33:10 +00:00
|
|
|
// Config options for circonus-gometrics
|
|
|
|
type Config struct {
|
2016-11-01 19:45:37 +00:00
|
|
|
Log *log.Logger
|
|
|
|
Debug bool
|
|
|
|
ResetCounters string // reset/delete counters on flush (default true)
|
|
|
|
ResetGauges string // reset/delete gauges on flush (default true)
|
|
|
|
ResetHistograms string // reset/delete histograms on flush (default true)
|
|
|
|
ResetText string // reset/delete text on flush (default true)
|
2016-07-22 16:33:10 +00:00
|
|
|
|
|
|
|
// API, Check and Broker configuration options
|
|
|
|
CheckManager checkmgr.Config
|
|
|
|
|
2017-01-31 19:42:18 +00:00
|
|
|
// how frequenly to submit metrics to Circonus, default 10 seconds.
|
|
|
|
// Set to 0 to disable automatic flushes and call Flush manually.
|
2016-07-22 16:33:10 +00:00
|
|
|
Interval string
|
|
|
|
}
|
|
|
|
|
2018-09-10 22:03:17 +00:00
|
|
|
type prevMetrics struct {
|
|
|
|
metrics *Metrics
|
|
|
|
metricsmu sync.Mutex
|
|
|
|
ts time.Time
|
|
|
|
}
|
|
|
|
|
2016-07-22 16:33:10 +00:00
|
|
|
// CirconusMetrics state
|
|
|
|
type CirconusMetrics struct {
|
2017-01-31 19:42:18 +00:00
|
|
|
Log *log.Logger
|
|
|
|
Debug bool
|
|
|
|
|
2016-11-01 19:45:37 +00:00
|
|
|
resetCounters bool
|
|
|
|
resetGauges bool
|
|
|
|
resetHistograms bool
|
|
|
|
resetText bool
|
|
|
|
flushInterval time.Duration
|
|
|
|
flushing bool
|
|
|
|
flushmu sync.Mutex
|
2018-09-10 22:03:17 +00:00
|
|
|
packagingmu sync.Mutex
|
2016-11-01 19:45:37 +00:00
|
|
|
check *checkmgr.CheckManager
|
2018-09-10 22:03:17 +00:00
|
|
|
lastMetrics *prevMetrics
|
2016-07-22 16:33:10 +00:00
|
|
|
|
|
|
|
counters map[string]uint64
|
|
|
|
cm sync.Mutex
|
|
|
|
|
|
|
|
counterFuncs map[string]func() uint64
|
|
|
|
cfm sync.Mutex
|
|
|
|
|
2018-09-10 22:03:17 +00:00
|
|
|
gauges map[string]interface{}
|
2016-07-22 16:33:10 +00:00
|
|
|
gm sync.Mutex
|
|
|
|
|
|
|
|
gaugeFuncs map[string]func() int64
|
|
|
|
gfm sync.Mutex
|
|
|
|
|
|
|
|
histograms map[string]*Histogram
|
|
|
|
hm sync.Mutex
|
|
|
|
|
|
|
|
text map[string]string
|
|
|
|
tm sync.Mutex
|
|
|
|
|
|
|
|
textFuncs map[string]func() string
|
|
|
|
tfm sync.Mutex
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewCirconusMetrics returns a CirconusMetrics instance
|
|
|
|
func NewCirconusMetrics(cfg *Config) (*CirconusMetrics, error) {
|
2017-01-31 19:42:18 +00:00
|
|
|
return New(cfg)
|
|
|
|
}
|
|
|
|
|
|
|
|
// New returns a CirconusMetrics instance
|
|
|
|
func New(cfg *Config) (*CirconusMetrics, error) {
|
2016-07-22 16:33:10 +00:00
|
|
|
|
|
|
|
if cfg == nil {
|
2017-01-31 19:42:18 +00:00
|
|
|
return nil, errors.New("invalid configuration (nil)")
|
2016-07-22 16:33:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
cm := &CirconusMetrics{
|
|
|
|
counters: make(map[string]uint64),
|
|
|
|
counterFuncs: make(map[string]func() uint64),
|
2018-09-10 22:03:17 +00:00
|
|
|
gauges: make(map[string]interface{}),
|
2016-07-22 16:33:10 +00:00
|
|
|
gaugeFuncs: make(map[string]func() int64),
|
|
|
|
histograms: make(map[string]*Histogram),
|
|
|
|
text: make(map[string]string),
|
|
|
|
textFuncs: make(map[string]func() string),
|
2018-09-10 22:03:17 +00:00
|
|
|
lastMetrics: &prevMetrics{},
|
2016-07-22 16:33:10 +00:00
|
|
|
}
|
|
|
|
|
2017-01-31 19:42:18 +00:00
|
|
|
// Logging
|
|
|
|
{
|
|
|
|
cm.Debug = cfg.Debug
|
|
|
|
cm.Log = cfg.Log
|
2016-11-01 19:45:37 +00:00
|
|
|
|
2017-01-31 19:42:18 +00:00
|
|
|
if cm.Debug && cm.Log == nil {
|
|
|
|
cm.Log = log.New(os.Stderr, "", log.LstdFlags)
|
|
|
|
}
|
|
|
|
if cm.Log == nil {
|
|
|
|
cm.Log = log.New(ioutil.Discard, "", log.LstdFlags)
|
|
|
|
}
|
2016-07-22 16:33:10 +00:00
|
|
|
}
|
|
|
|
|
2017-01-31 19:42:18 +00:00
|
|
|
// Flush Interval
|
|
|
|
{
|
|
|
|
fi := defaultFlushInterval
|
|
|
|
if cfg.Interval != "" {
|
|
|
|
fi = cfg.Interval
|
|
|
|
}
|
2016-07-22 16:33:10 +00:00
|
|
|
|
2017-01-31 19:42:18 +00:00
|
|
|
dur, err := time.ParseDuration(fi)
|
|
|
|
if err != nil {
|
2018-09-10 22:03:17 +00:00
|
|
|
return nil, errors.Wrap(err, "parsing flush interval")
|
2017-01-31 19:42:18 +00:00
|
|
|
}
|
|
|
|
cm.flushInterval = dur
|
2016-07-22 16:33:10 +00:00
|
|
|
}
|
|
|
|
|
2017-01-31 19:42:18 +00:00
|
|
|
// metric resets
|
2016-11-01 19:45:37 +00:00
|
|
|
|
|
|
|
cm.resetCounters = true
|
|
|
|
if cfg.ResetCounters != "" {
|
2017-01-31 19:42:18 +00:00
|
|
|
setting, err := strconv.ParseBool(cfg.ResetCounters)
|
|
|
|
if err != nil {
|
2018-09-10 22:03:17 +00:00
|
|
|
return nil, errors.Wrap(err, "parsing reset counters")
|
2016-11-01 19:45:37 +00:00
|
|
|
}
|
2017-01-31 19:42:18 +00:00
|
|
|
cm.resetCounters = setting
|
2016-11-01 19:45:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
cm.resetGauges = true
|
|
|
|
if cfg.ResetGauges != "" {
|
2017-01-31 19:42:18 +00:00
|
|
|
setting, err := strconv.ParseBool(cfg.ResetGauges)
|
|
|
|
if err != nil {
|
2018-09-10 22:03:17 +00:00
|
|
|
return nil, errors.Wrap(err, "parsing reset gauges")
|
2016-11-01 19:45:37 +00:00
|
|
|
}
|
2017-01-31 19:42:18 +00:00
|
|
|
cm.resetGauges = setting
|
2016-11-01 19:45:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
cm.resetHistograms = true
|
|
|
|
if cfg.ResetHistograms != "" {
|
2017-01-31 19:42:18 +00:00
|
|
|
setting, err := strconv.ParseBool(cfg.ResetHistograms)
|
|
|
|
if err != nil {
|
2018-09-10 22:03:17 +00:00
|
|
|
return nil, errors.Wrap(err, "parsing reset histograms")
|
2016-11-01 19:45:37 +00:00
|
|
|
}
|
2017-01-31 19:42:18 +00:00
|
|
|
cm.resetHistograms = setting
|
2016-11-01 19:45:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
cm.resetText = true
|
|
|
|
if cfg.ResetText != "" {
|
2017-01-31 19:42:18 +00:00
|
|
|
setting, err := strconv.ParseBool(cfg.ResetText)
|
|
|
|
if err != nil {
|
2018-09-10 22:03:17 +00:00
|
|
|
return nil, errors.Wrap(err, "parsing reset text")
|
2016-11-01 19:45:37 +00:00
|
|
|
}
|
2017-01-31 19:42:18 +00:00
|
|
|
cm.resetText = setting
|
2016-11-01 19:45:37 +00:00
|
|
|
}
|
|
|
|
|
2017-01-31 19:42:18 +00:00
|
|
|
// check manager
|
|
|
|
{
|
|
|
|
cfg.CheckManager.Debug = cm.Debug
|
|
|
|
cfg.CheckManager.Log = cm.Log
|
2016-07-22 16:33:10 +00:00
|
|
|
|
2017-01-31 19:42:18 +00:00
|
|
|
check, err := checkmgr.New(&cfg.CheckManager)
|
|
|
|
if err != nil {
|
2018-09-10 22:03:17 +00:00
|
|
|
return nil, errors.Wrap(err, "creating new check manager")
|
2017-01-31 19:42:18 +00:00
|
|
|
}
|
|
|
|
cm.check = check
|
2016-07-22 16:33:10 +00:00
|
|
|
}
|
|
|
|
|
2017-01-31 19:42:18 +00:00
|
|
|
// start background initialization
|
|
|
|
cm.check.Initialize()
|
|
|
|
|
|
|
|
// if automatic flush is enabled, start it.
|
2018-09-10 22:03:17 +00:00
|
|
|
// NOTE: submit will jettison metrics until initialization has completed.
|
2017-01-31 19:42:18 +00:00
|
|
|
if cm.flushInterval > time.Duration(0) {
|
|
|
|
go func() {
|
2018-09-10 22:03:17 +00:00
|
|
|
for range time.NewTicker(cm.flushInterval).C {
|
2017-01-31 19:42:18 +00:00
|
|
|
cm.Flush()
|
|
|
|
}
|
|
|
|
}()
|
2016-07-22 16:33:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return cm, nil
|
|
|
|
}
|
|
|
|
|
2017-01-31 19:42:18 +00:00
|
|
|
// Start deprecated NOP, automatic flush is started in New if flush interval > 0.
|
2016-07-22 16:33:10 +00:00
|
|
|
func (m *CirconusMetrics) Start() {
|
2018-09-10 22:03:17 +00:00
|
|
|
// nop
|
2017-01-31 19:42:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Ready returns true or false indicating if the check is ready to accept metrics
|
|
|
|
func (m *CirconusMetrics) Ready() bool {
|
|
|
|
return m.check.IsReady()
|
2016-07-22 16:33:10 +00:00
|
|
|
}
|
|
|
|
|
2018-09-10 22:03:17 +00:00
|
|
|
func (m *CirconusMetrics) packageMetrics() (map[string]*api.CheckBundleMetric, Metrics) {
|
|
|
|
|
|
|
|
m.packagingmu.Lock()
|
|
|
|
defer m.packagingmu.Unlock()
|
2016-07-22 16:33:10 +00:00
|
|
|
|
|
|
|
if m.Debug {
|
2018-09-10 22:03:17 +00:00
|
|
|
m.Log.Println("[DEBUG] Packaging metrics")
|
2016-07-22 16:33:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
counters, gauges, histograms, text := m.snapshot()
|
2018-09-10 22:03:17 +00:00
|
|
|
newMetrics := make(map[string]*api.CheckBundleMetric)
|
|
|
|
output := make(Metrics, len(counters)+len(gauges)+len(histograms)+len(text))
|
2016-07-22 16:33:10 +00:00
|
|
|
for name, value := range counters {
|
|
|
|
send := m.check.IsMetricActive(name)
|
|
|
|
if !send && m.check.ActivateMetric(name) {
|
|
|
|
send = true
|
|
|
|
newMetrics[name] = &api.CheckBundleMetric{
|
|
|
|
Name: name,
|
|
|
|
Type: "numeric",
|
|
|
|
Status: "active",
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if send {
|
2018-09-10 22:03:17 +00:00
|
|
|
output[name] = Metric{Type: "L", Value: value}
|
2016-07-22 16:33:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for name, value := range gauges {
|
|
|
|
send := m.check.IsMetricActive(name)
|
|
|
|
if !send && m.check.ActivateMetric(name) {
|
|
|
|
send = true
|
|
|
|
newMetrics[name] = &api.CheckBundleMetric{
|
|
|
|
Name: name,
|
|
|
|
Type: "numeric",
|
|
|
|
Status: "active",
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if send {
|
2018-09-10 22:03:17 +00:00
|
|
|
output[name] = Metric{Type: m.getGaugeType(value), Value: value}
|
2016-07-22 16:33:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for name, value := range histograms {
|
|
|
|
send := m.check.IsMetricActive(name)
|
|
|
|
if !send && m.check.ActivateMetric(name) {
|
|
|
|
send = true
|
|
|
|
newMetrics[name] = &api.CheckBundleMetric{
|
|
|
|
Name: name,
|
|
|
|
Type: "histogram",
|
|
|
|
Status: "active",
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if send {
|
2018-09-10 22:03:17 +00:00
|
|
|
output[name] = Metric{Type: "n", Value: value.DecStrings()}
|
2016-07-22 16:33:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for name, value := range text {
|
|
|
|
send := m.check.IsMetricActive(name)
|
|
|
|
if !send && m.check.ActivateMetric(name) {
|
|
|
|
send = true
|
|
|
|
newMetrics[name] = &api.CheckBundleMetric{
|
|
|
|
Name: name,
|
|
|
|
Type: "text",
|
|
|
|
Status: "active",
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if send {
|
2018-09-10 22:03:17 +00:00
|
|
|
output[name] = Metric{Type: "s", Value: value}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
m.lastMetrics.metricsmu.Lock()
|
|
|
|
defer m.lastMetrics.metricsmu.Unlock()
|
|
|
|
m.lastMetrics.metrics = &output
|
|
|
|
m.lastMetrics.ts = time.Now()
|
|
|
|
|
|
|
|
return newMetrics, output
|
|
|
|
}
|
|
|
|
|
|
|
|
// PromOutput returns lines of metrics in prom format
|
|
|
|
func (m *CirconusMetrics) PromOutput() (*bytes.Buffer, error) {
|
|
|
|
m.lastMetrics.metricsmu.Lock()
|
|
|
|
defer m.lastMetrics.metricsmu.Unlock()
|
|
|
|
|
|
|
|
if m.lastMetrics.metrics == nil {
|
|
|
|
return nil, errors.New("no metrics available")
|
|
|
|
}
|
|
|
|
|
|
|
|
var b bytes.Buffer
|
|
|
|
w := bufio.NewWriter(&b)
|
|
|
|
|
|
|
|
ts := m.lastMetrics.ts.UnixNano() / int64(time.Millisecond)
|
|
|
|
|
|
|
|
for name, metric := range *m.lastMetrics.metrics {
|
|
|
|
switch metric.Type {
|
|
|
|
case "n":
|
|
|
|
if strings.HasPrefix(fmt.Sprintf("%v", metric.Value), "[H[") {
|
|
|
|
continue // circonus histogram != prom "histogram" (aka percentile)
|
2016-07-22 16:33:10 +00:00
|
|
|
}
|
2018-09-10 22:03:17 +00:00
|
|
|
case "s":
|
|
|
|
continue // text metrics unsupported
|
2016-07-22 16:33:10 +00:00
|
|
|
}
|
2018-09-10 22:03:17 +00:00
|
|
|
fmt.Fprintf(w, "%s %v %d\n", name, metric.Value, ts)
|
|
|
|
}
|
|
|
|
|
|
|
|
err := w.Flush()
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "flushing metric buffer")
|
|
|
|
}
|
|
|
|
|
|
|
|
return &b, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// FlushMetrics flushes current metrics to a structure and returns it (does NOT send to Circonus)
|
|
|
|
func (m *CirconusMetrics) FlushMetrics() *Metrics {
|
|
|
|
m.flushmu.Lock()
|
|
|
|
if m.flushing {
|
|
|
|
m.flushmu.Unlock()
|
|
|
|
return &Metrics{}
|
|
|
|
}
|
|
|
|
|
|
|
|
m.flushing = true
|
|
|
|
m.flushmu.Unlock()
|
|
|
|
|
|
|
|
_, output := m.packageMetrics()
|
|
|
|
|
|
|
|
m.flushmu.Lock()
|
|
|
|
m.flushing = false
|
|
|
|
m.flushmu.Unlock()
|
|
|
|
|
|
|
|
return &output
|
|
|
|
}
|
|
|
|
|
|
|
|
// Flush metrics kicks off the process of sending metrics to Circonus
|
|
|
|
func (m *CirconusMetrics) Flush() {
|
|
|
|
m.flushmu.Lock()
|
|
|
|
if m.flushing {
|
|
|
|
m.flushmu.Unlock()
|
|
|
|
return
|
2016-07-22 16:33:10 +00:00
|
|
|
}
|
|
|
|
|
2018-09-10 22:03:17 +00:00
|
|
|
m.flushing = true
|
|
|
|
m.flushmu.Unlock()
|
|
|
|
|
|
|
|
newMetrics, output := m.packageMetrics()
|
|
|
|
|
2016-11-01 19:45:37 +00:00
|
|
|
if len(output) > 0 {
|
|
|
|
m.submit(output, newMetrics)
|
|
|
|
} else {
|
|
|
|
if m.Debug {
|
|
|
|
m.Log.Println("[DEBUG] No metrics to send, skipping")
|
|
|
|
}
|
|
|
|
}
|
2016-07-22 16:33:10 +00:00
|
|
|
|
|
|
|
m.flushmu.Lock()
|
|
|
|
m.flushing = false
|
|
|
|
m.flushmu.Unlock()
|
|
|
|
}
|