Commit 1919913c authored by James Hartig's avatar James Hartig Committed by GitHub

plugin/metrics: Added New func (#1309)

If external plugins wanted to extend metrics there was no way since
zoneNames couldn't be initialized. Now plugins can call New to get an
instance of Metrics that they can extend.
parent 2e2e5e1b
...@@ -36,6 +36,11 @@ type Metrics struct { ...@@ -36,6 +36,11 @@ type Metrics struct {
zoneMu sync.RWMutex zoneMu sync.RWMutex
} }
// New returns a new instance of Metrics with the given address
func New(addr string) *Metrics {
return &Metrics{Addr: addr, zoneMap: make(map[string]bool)}
}
// AddZone adds zone z to m. // AddZone adds zone z to m.
func (m *Metrics) AddZone(z string) { func (m *Metrics) AddZone(z string) {
m.zoneMu.Lock() m.zoneMu.Lock()
......
...@@ -13,7 +13,7 @@ import ( ...@@ -13,7 +13,7 @@ import (
) )
func TestMetrics(t *testing.T) { func TestMetrics(t *testing.T) {
met := &Metrics{Addr: "localhost:0", zoneMap: make(map[string]bool)} met := New("localhost:0")
if err := met.OnStartup(); err != nil { if err := met.OnStartup(); err != nil {
t.Fatalf("Failed to start metrics handler: %s", err) t.Fatalf("Failed to start metrics handler: %s", err)
} }
......
...@@ -42,10 +42,7 @@ func setup(c *caddy.Controller) error { ...@@ -42,10 +42,7 @@ func setup(c *caddy.Controller) error {
} }
func prometheusParse(c *caddy.Controller) (*Metrics, error) { func prometheusParse(c *caddy.Controller) (*Metrics, error) {
var ( var met = New(defaultAddr)
met = &Metrics{Addr: addr, zoneMap: make(map[string]bool)}
err error
)
defer func() { defer func() {
uniqAddr.SetAddress(met.Addr) uniqAddr.SetAddress(met.Addr)
...@@ -73,7 +70,7 @@ func prometheusParse(c *caddy.Controller) (*Metrics, error) { ...@@ -73,7 +70,7 @@ func prometheusParse(c *caddy.Controller) (*Metrics, error) {
return met, c.ArgErr() return met, c.ArgErr()
} }
} }
return met, err return met, nil
} }
var uniqAddr addrs var uniqAddr addrs
...@@ -91,8 +88,8 @@ func (a *addrs) SetAddress(addr string) { ...@@ -91,8 +88,8 @@ func (a *addrs) SetAddress(addr string) {
a.a[addr] = todo a.a[addr] = todo
} }
// Addr is the address the where the metrics are exported by default. // defaultAddr is the address the where the metrics are exported by default.
const addr = "localhost:9153" const defaultAddr = "localhost:9153"
const ( const (
todo = 1 todo = 1
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment