-
Notifications
You must be signed in to change notification settings - Fork 619
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
Changes from all commits
025eec1
7aea55f
186b936
b92a69e
abc9eb6
553ff13
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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" | ||
|
||
// 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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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))) | ||
} |
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) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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() | ||
|
||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
There was a problem hiding this comment.
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 usingfmt.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.