Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue #151: Add support for Circonus metrics #150

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 10 additions & 5 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,11 +67,16 @@ type Runtime struct {
}

type Metrics struct {
Target string
Prefix string
Interval time.Duration
GraphiteAddr string
StatsDAddr string
Target string
Prefix string
Interval time.Duration
GraphiteAddr string
StatsDAddr string
CirconusAPIKey string
CirconusAPIApp string
CirconusAPIURL string
CirconusCheckID string
CirconusBrokerID string
}

type Registry struct {
Expand Down
5 changes: 3 additions & 2 deletions config/default.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,9 @@ var Default = &Config{
Color: "light-green",
},
Metrics: Metrics{
Prefix: "default",
Interval: 30 * time.Second,
Prefix: "default",
Interval: 30 * time.Second,
CirconusAPIApp: "fabio",
},
CertSources: map[string]CertSource{},
}
5 changes: 5 additions & 0 deletions config/load.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,11 @@ func load(p *properties.Properties) (cfg *Config, err error) {
f.DurationVar(&cfg.Metrics.Interval, "metrics.interval", Default.Metrics.Interval, "metrics reporting interval")
f.StringVar(&cfg.Metrics.GraphiteAddr, "metrics.graphite.addr", Default.Metrics.GraphiteAddr, "graphite server address")
f.StringVar(&cfg.Metrics.StatsDAddr, "metrics.statsd.addr", Default.Metrics.StatsDAddr, "statsd server address")
f.StringVar(&cfg.Metrics.CirconusAPIKey, "metrics.circonus.apikey", Default.Metrics.CirconusAPIKey, "Circonus API token key")
f.StringVar(&cfg.Metrics.CirconusAPIApp, "metrics.circonus.apiapp", Default.Metrics.CirconusAPIApp, "Circonus API token app")
f.StringVar(&cfg.Metrics.CirconusAPIURL, "metrics.circonus.apiurl", Default.Metrics.CirconusAPIURL, "Circonus API URL")
f.StringVar(&cfg.Metrics.CirconusBrokerID, "metrics.circonus.brokerid", Default.Metrics.CirconusBrokerID, "Circonus Broker ID")
f.StringVar(&cfg.Metrics.CirconusCheckID, "metrics.circonus.checkid", Default.Metrics.CirconusCheckID, "Circonus Check ID")
f.StringVar(&cfg.Registry.Backend, "registry.backend", Default.Registry.Backend, "registry backend")
f.StringVar(&cfg.Registry.File.Path, "registry.file.path", Default.Registry.File.Path, "path to file based routing table")
f.StringVar(&cfg.Registry.Static.Routes, "registry.static.routes", Default.Registry.Static.Routes, "static routes")
Expand Down
43 changes: 43 additions & 0 deletions fabio.properties
Original file line number Diff line number Diff line change
Expand Up @@ -462,6 +462,7 @@
# stdout: report metrics to stdout
# graphite: report metrics to Graphite on ${metrics.graphite.addr}
# statsd: report metrics to StatsD on ${metrics.statsd.addr}
# circonus: report metrics to Circonus (http://circonus.com/)
#
# The default is
#
Expand Down Expand Up @@ -505,6 +506,48 @@
# metrics.statsd.addr =


# metrics.circonus.apiapp configures the API token app to use when
# submitting metrics to Circonus. See: https://login.circonus.com/user/tokens
# This is optional when ${metrics.target} is set to "circonus".
#
# The default is
#
# metrics.circonus.apiapp = fabio


# metrics.circonus.apiurl configures the API URL to use when
# submitting metrics to Circonus. https://api.circonus.com/v2/
# will be used if no specific URL is provided.
# This is optional when ${metrics.target} is set to "circonus".
#
#
# The default is
#
# metrics.circonus.apiurl =


# metrics.circonus.brokerid configures a specific broker to use when
# creating a check for submitting metrics to Circonus.
# This is optional when ${metrics.target} is set to "circonus".
# Optional for public brokers, required for Inside brokers.
# Only applicable if a check is being created.
#
# The default is
#
# metrics.circonus.brokerid =


# metrics.circonus.checkid configures a specific check to use when
# submitting metrics to Circonus.
# This is optional when ${metrics.target} is set to "circonus".
# An attempt will be made to search for a previously created check,
# if no applicable check is found, one will be created.
#
# The default is
#
# metrics.circonus.checkid =


# runtime.gogc configures GOGC (the GC target percentage).
#
# Setting runtime.gogc is equivalent to setting the GOGC
Expand Down
120 changes: 120 additions & 0 deletions metrics/circonus.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
package metrics

import (
"errors"
"fmt"
"log"
"os"
"sync"
"time"

cgm "github.com/circonus-labs/circonus-gometrics"
)

type cgmRegistry struct {
metrics *cgm.CirconusMetrics
prefix string
}

type cgmTimer struct {
metrics *cgm.CirconusMetrics
name string
}

var (
circonus *cgmRegistry
once sync.Once
)

const serviceName = "fabio"
Copy link
Contributor

@magiconair magiconair Aug 30, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't this the value from metrics.circonus.apiapp? If not, then either make it configurable or inline it below instead of using fmt.Sprintf

Update: nvm, missed that this is the default name. I'd still prefer this to come from the defaults or fabio should not start if this is a config error.


// circonusRegistry returns a provider that reports to Circonus.
func circonusRegistry(prefix string,
circKey string,
circApp string,
circURL string,
circBrokerID string,
circCheckID string,
interval time.Duration) (Registry, error) {

var initError error

once.Do(func() {

if circKey == "" {
initError = errors.New("metrics: Circonus API token key")
return
}

if circApp == "" {
circApp = serviceName
}

host, err := os.Hostname()
if err != nil {
initError = fmt.Errorf("metrics: unable to initialize Circonus %s", err)
return
}

cfg := &cgm.Config{}

cfg.CheckManager.API.TokenKey = circKey
cfg.CheckManager.API.TokenApp = circApp
cfg.CheckManager.API.URL = circURL
cfg.CheckManager.Check.ID = circCheckID
cfg.CheckManager.Broker.ID = circBrokerID
cfg.Interval = fmt.Sprintf("%.0fs", interval.Seconds())
cfg.CheckManager.Check.InstanceID = host
cfg.CheckManager.Check.DisplayName = fmt.Sprintf("%s /%s", host, serviceName)
cfg.CheckManager.Check.SearchTag = fmt.Sprintf("service:%s", serviceName)

metrics, err := cgm.NewCirconusMetrics(cfg)
if err != nil {
initError = fmt.Errorf("metrics: unable to initialize Circonus %s", err)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't you need to return here or can you start the metrics even though you have an error? This would be different than for the other metrics libs.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

doh! yeah, good catch, thanks.

return
}

circonus = &cgmRegistry{metrics, prefix}

metrics.Start()

log.Print("[INFO] Sending metrics to Circonus")

})

return circonus, initError
}

// Names is not supported by Circonus.
func (m *cgmRegistry) Names() []string { return nil }

// Unregister is implicitly supported by Circonus,
// stop submitting the metric and it stops being sent to Circonus.
func (m *cgmRegistry) Unregister(name string) {}

// UnregisterAll is implicitly supported by Circonus,
// stop submitting metrics and they will no longer be sent to Circonus.
func (m *cgmRegistry) UnregisterAll() {}

// GetTimer returns a timer for the given metric name.
func (m *cgmRegistry) GetTimer(name string) Timer {
metricName := fmt.Sprintf("%s`%s", m.prefix, name)
return &cgmTimer{m.metrics, metricName}
}

// Percentile is not supported by Circonus.
func (t cgmTimer) Percentile(nth float64) float64 {
return 0
}

// Rate1 is not supported by Circonus.
func (t cgmTimer) Rate1() float64 {
return 0
}

// UpdateSince adds delta between start and current time as
// a sample to a histogram. The histogram is created if it
// does not already exist.
func (t cgmTimer) UpdateSince(start time.Time) {
t.metrics.Timing(t.name, float64(time.Since(start)))
}
81 changes: 81 additions & 0 deletions metrics/circonus_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
package metrics

import (
"os"
"testing"
"time"
)

func TestProvider(t *testing.T) {
t.Log("Testing registry provider interface")

p := &cgmRegistry{}

t.Log("\tNames()")
names := p.Names()
if names != nil {
t.Errorf("Expected nil got '%+v'", names)
}

t.Log("\tUnregister()")
p.Unregister("foo")

t.Log("\tUnregisterAll()")
p.UnregisterAll()

t.Log("\tGetTimer()")
timer := p.GetTimer("foo")
if timer == nil {
t.Error("Expected a timer, got nil")
}
}

func TestTimer(t *testing.T) {
t.Log("Testing timer interface")

timer := &cgmTimer{}

t.Log("\tPercentile()")
pct := timer.Percentile(99.9)
if pct != 0 {
t.Errorf("Expected 0 got '%+v'", pct)
}

t.Log("\tRate1()")
rate := timer.Rate1()
if rate != 0 {
t.Errorf("Expected 0 got '%+v'", rate)
}
}

func TestAll(t *testing.T) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What exactly is this testing? How do you know that this test has passed?

start := time.Now()

if os.Getenv("CIRCONUS_API_TOKEN") == "" {
t.Skip("skipping test; $CIRCONUS_API_TOKEN not set")
}

t.Log("Testing cgm functionality -- this *will* create/use a check")

apiKey := os.Getenv("CIRCONUS_API_TOKEN")
apiApp := os.Getenv("CIRCONUS_API_APP")
apiURL := os.Getenv("CIRCONUS_API_URL")
brokerID := os.Getenv("CIRCONUS_BROKER_ID")
checkID := os.Getenv("CIRCONUS_CHECK_ID")

interval, err := time.ParseDuration("60s")
if err != nil {
t.Fatalf("Unable to parse interval %+v", err)
}

circ, err := circonusRegistry("test", apiKey, apiApp, apiURL, brokerID, checkID, interval)
if err != nil {
t.Fatalf("Unable to initialize Circonus +%v", err)
}

timer := circ.GetTimer("foo")
timer.UpdateSince(start)

circonus.metrics.Flush()

}
9 changes: 9 additions & 0 deletions metrics/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,15 @@ func NewRegistry(cfg config.Metrics) (r Registry, err error) {
log.Printf("[INFO] Sending metrics to StatsD on %s as %q", cfg.StatsDAddr, prefix)
return gmStatsDRegistry(prefix, cfg.StatsDAddr, cfg.Interval)

case "circonus":
return circonusRegistry(prefix,
cfg.CirconusAPIKey,
cfg.CirconusAPIApp,
cfg.CirconusAPIURL,
cfg.CirconusBrokerID,
cfg.CirconusCheckID,
cfg.Interval)

default:
exit.Fatal("[FATAL] Invalid metrics target ", cfg.Target)
}
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading