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

metrics: refactor metrics to deal only with events #334

Closed
wants to merge 1 commit 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
4 changes: 2 additions & 2 deletions admin/api/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,8 @@ func (h *RoutesHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
Weight: tg.Weight,
Tags: tg.Tags,
Cmd: "route add",
Rate1: tg.Timer.Rate1(),
Pct99: tg.Timer.Percentile(0.99),
// Rate1: tg.Timer.Rate1(),
// Pct99: tg.Timer.Percentile(0.99),
}
routes = append(routes, ar)
}
Expand Down
26 changes: 10 additions & 16 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,6 @@ func newHTTPProxy(cfg *config.Config) http.Handler {

pick := route.Picker[cfg.Proxy.Strategy]
match := route.Matcher[cfg.Proxy.Matcher]
notFound := metrics.DefaultRegistry.GetCounter("notfound")
log.Printf("[INFO] Using routing strategy %q", cfg.Proxy.Strategy)
log.Printf("[INFO] Using route matching %q", cfg.Proxy.Matcher)

Expand All @@ -166,24 +165,22 @@ func newHTTPProxy(cfg *config.Config) http.Handler {
Lookup: func(r *http.Request) *route.Target {
t := route.GetTable().Lookup(r, r.Header.Get("trace"), pick, match)
if t == nil {
notFound.Inc(1)
metrics.IncDefault("notfound", 1)
log.Print("[WARN] No route for ", r.Host, r.URL)
}
return t
},
Requests: metrics.DefaultRegistry.GetTimer("requests"),
Noroute: metrics.DefaultRegistry.GetCounter("notfound"),
Requests: "requests",
Logger: l,
}
}

func lookupHostFn(cfg *config.Config) func(string) *route.Target {
pick := route.Picker[cfg.Proxy.Strategy]
notFound := metrics.DefaultRegistry.GetCounter("notfound")
return func(host string) *route.Target {
t := route.GetTable().LookupHost(host, pick)
if t == nil {
notFound.Inc(1)
metrics.IncDefault("notfound", 1)
log.Print("[WARN] No route for ", host)
}
return t
Expand Down Expand Up @@ -254,9 +251,9 @@ func startServers(cfg *config.Config) {
h := &tcp.Proxy{
DialTimeout: cfg.Proxy.DialTimeout,
Lookup: lookupHostFn(cfg),
Conn: metrics.DefaultRegistry.GetCounter("tcp.conn"),
ConnFail: metrics.DefaultRegistry.GetCounter("tcp.connfail"),
Noroute: metrics.DefaultRegistry.GetCounter("tcp.noroute"),
Conn: "tcp.conn",
ConnFail: "tcp.connfail",
Noroute: "tcp.noroute",
}
if err := proxy.ListenAndServeTCP(l, h, tlscfg); err != nil {
exit.Fatal("[FATAL] ", err)
Expand All @@ -267,9 +264,9 @@ func startServers(cfg *config.Config) {
h := &tcp.SNIProxy{
DialTimeout: cfg.Proxy.DialTimeout,
Lookup: lookupHostFn(cfg),
Conn: metrics.DefaultRegistry.GetCounter("tcp_sni.conn"),
ConnFail: metrics.DefaultRegistry.GetCounter("tcp_sni.connfail"),
Noroute: metrics.DefaultRegistry.GetCounter("tcp_sni.noroute"),
Conn: "tcp_sni.conn",
ConnFail: "tcp_sni.connfail",
Noroute: "tcp_sni.noroute",
}
if err := proxy.ListenAndServeTCP(l, h, tlscfg); err != nil {
exit.Fatal("[FATAL] ", err)
Expand All @@ -288,10 +285,7 @@ func initMetrics(cfg *config.Config) {
}

var err error
if metrics.DefaultRegistry, err = metrics.NewRegistry(cfg.Metrics); err != nil {
exit.Fatal("[FATAL] ", err)
}
if route.ServiceRegistry, err = metrics.NewRegistry(cfg.Metrics); err != nil {
if metrics.M, err = metrics.NewRegistry(cfg.Metrics); err != nil {
exit.Fatal("[FATAL] ", err)
}
}
Expand Down
128 changes: 0 additions & 128 deletions metrics/circonus.go

This file was deleted.

79 changes: 79 additions & 0 deletions metrics/circonus/registry.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
package circonus

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

cgm "github.com/circonus-labs/circonus-gometrics"
"github.com/fabiolb/fabio/config"
)

const serviceName = "fabio"

// NewRegistry returns a provider that reports to Circonus.
func NewRegistry(prefix string, circ config.Circonus, interval time.Duration) (*registry, error) {

if circ.APIKey == "" {
return nil, errors.New("metrics: Circonus API token key")
}

if circ.APIApp == "" {
circ.APIApp = serviceName
}

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

cfg := &cgm.Config{}
cfg.CheckManager.API.TokenKey = circ.APIKey
cfg.CheckManager.API.TokenApp = circ.APIApp
cfg.CheckManager.API.URL = circ.APIURL
cfg.CheckManager.Check.ID = circ.CheckID
cfg.CheckManager.Broker.ID = circ.BrokerID
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 {
return nil, fmt.Errorf("metrics: unable to initialize Circonus %s", err)
}
metrics.Start()

log.Print("[INFO] Sending metrics to Circonus")
return &registry{metrics, prefix}, nil
}

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

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

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

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

func (m *registry) Gauge(_, name string, n float64) {
m.metrics.Gauge(m.prefix+"`"+name, n)
}

func (m *registry) Inc(_, name string, n int64) {
m.metrics.Add(m.prefix+"`"+name, uint64(n))
}

func (m *registry) Time(_, name string, d time.Duration) {
m.metrics.Timing(m.prefix+"`"+name, float64(d))
}
46 changes: 9 additions & 37 deletions metrics/circonus_test.go → metrics/circonus/registry_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package metrics
package circonus

import (
"os"
Expand All @@ -11,43 +11,19 @@ import (
func TestRegistry(t *testing.T) {
t.Log("Testing registry interface")

p := &cgmRegistry{}
p := &registry{}

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

t.Log("\tUnregister()")
p.Unregister("foo")
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)
}
p.UnregisterAll("")
}

func TestAll(t *testing.T) {
Expand All @@ -72,16 +48,12 @@ func TestAll(t *testing.T) {
t.Fatalf("Unable to parse interval %+v", err)
}

circ, err := circonusRegistry("test", cfg, interval)
r, err := NewRegistry("test", cfg, interval)
if err != nil {
t.Fatalf("Unable to initialize Circonus +%v", err)
}

counter := circ.GetCounter("fooCounter")
counter.Inc(3)

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

circonus.metrics.Flush()
r.Inc("", "fooCounter", 3)
r.Time("", "fooTimer", time.Since(start))
r.metrics.Flush()
}
Loading