Skip to content

Commit

Permalink
First stab at integrating raw statsd from #335
Browse files Browse the repository at this point in the history
  • Loading branch information
magiconair committed Mar 24, 2018
1 parent 806c4f0 commit 20fa958
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 0 deletions.
9 changes: 9 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
"github.com/fabiolb/fabio/metrics4"
"github.com/fabiolb/fabio/metrics4/flat"
"github.com/fabiolb/fabio/metrics4/label"
"github.com/fabiolb/fabio/metrics4/statsdraw"
"github.com/fabiolb/fabio/noroute"
"github.com/fabiolb/fabio/proxy"
"github.com/fabiolb/fabio/proxy/tcp"
Expand Down Expand Up @@ -317,6 +318,14 @@ func initMetrics(cfg *config.Config) metrics4.Provider {
p = append(p, &flat.Provider{})
case "label":
p = append(p, &label.Provider{})
case "statsd_raw":
// prefix := cfg.Metrics.Prefix // prefix is a template and needs to be expanded
prefix := ""
pp, err := statsdraw.NewProvider(prefix, cfg.Metrics.StatsDAddr, cfg.Metrics.Interval)
if err != nil {
exit.Fatalf("[FATAL] Cannot initialize statsd metrics: %s", err)
}
p = append(p, pp)
default:
log.Printf("[WARN] Skipping unknown metrics provider %q", x)
continue
Expand Down
73 changes: 73 additions & 0 deletions metrics4/statsdraw/statsd.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package statsdraw

import (
"time"

"github.com/alexcesaro/statsd"
"github.com/fabiolb/fabio/metrics4"
"github.com/fabiolb/fabio/metrics4/names"
)

type Provider struct {
c *statsd.Client
}

func NewProvider(prefix, addr string, interval time.Duration) (*Provider, error) {
opts := []statsd.Option{
statsd.Address(addr),
statsd.FlushPeriod(interval),
}
if prefix != "" {
opts = append(opts, statsd.Prefix(prefix))
}

c, err := statsd.New(opts...)
if err != nil {
return nil, err
}
return &Provider{c}, nil
}

func (p *Provider) NewCounter(name string, labels ...string) metrics4.Counter {
return &Counter{c: p.c, name: name, labels: labels}
}

func (p *Provider) NewGauge(name string, labels ...string) metrics4.Gauge {
return &Gauge{c: p.c, name: name, labels: labels}
}

func (p *Provider) NewTimer(name string, labels ...string) metrics4.Timer {
return &Timer{c: p.c, name: name, labels: labels}
}

func (p *Provider) Unregister(interface{}) {}

type Counter struct {
c *statsd.Client
name string
labels []string
}

func (v *Counter) Count(n int) {
v.c.Count(names.Flatten(v.name, v.labels, names.DotSeparator), n)
}

type Gauge struct {
c *statsd.Client
name string
labels []string
}

func (v *Gauge) Update(n int) {
v.c.Gauge(names.Flatten(v.name, v.labels, names.DotSeparator), n)
}

type Timer struct {
c *statsd.Client
name string
labels []string
}

func (v *Timer) Update(d time.Duration) {
v.c.Timing(names.Flatten(v.name, v.labels, names.DotSeparator), d)
}

0 comments on commit 20fa958

Please sign in to comment.