aboutsummaryrefslogtreecommitdiff
path: root/plugin/metrics
diff options
context:
space:
mode:
authorGravatar Miek Gieben <miek@miek.nl> 2018-04-25 11:45:09 +0100
committerGravatar GitHub <noreply@github.com> 2018-04-25 11:45:09 +0100
commit5e6114b797b56d4b0f910d0c11a5dc85b37bfd52 (patch)
treedd489f9c9918d0dbad6719411d19b133bb5040a6 /plugin/metrics
parentce084012df435a98cac5511a294a0c45a1a3abf9 (diff)
downloadcoredns-5e6114b797b56d4b0f910d0c11a5dc85b37bfd52.tar.gz
coredns-5e6114b797b56d4b0f910d0c11a5dc85b37bfd52.tar.zst
coredns-5e6114b797b56d4b0f910d0c11a5dc85b37bfd52.zip
plugin/pkg/uniq: add (#1733)
Spin this out the metrics package so we can use it in the health one of well to fix some reload bugs.
Diffstat (limited to 'plugin/metrics')
-rw-r--r--plugin/metrics/addr.go52
-rw-r--r--plugin/metrics/addr_test.go17
-rw-r--r--plugin/metrics/metrics.go2
-rw-r--r--plugin/metrics/setup.go12
4 files changed, 8 insertions, 75 deletions
diff --git a/plugin/metrics/addr.go b/plugin/metrics/addr.go
deleted file mode 100644
index fe8e5e5fe..000000000
--- a/plugin/metrics/addr.go
+++ /dev/null
@@ -1,52 +0,0 @@
-package metrics
-
-// addrs keeps track on which addrs we listen, so we only start one listener, is
-// prometheus is used in multiple Server Blocks.
-type addrs struct {
- a map[string]value
-}
-
-type value struct {
- state int
- f func() error
-}
-
-var uniqAddr addrs
-
-func newAddress() addrs {
- return addrs{a: make(map[string]value)}
-}
-
-func (a addrs) setAddress(addr string, f func() error) {
- if a.a[addr].state == done {
- return
- }
- a.a[addr] = value{todo, f}
-}
-
-// setAddressTodo sets addr to 'todo' again.
-func (a addrs) setAddressTodo(addr string) {
- v, ok := a.a[addr]
- if !ok {
- return
- }
- v.state = todo
- a.a[addr] = v
-}
-
-// forEachTodo iterates for a and executes f for each element that is 'todo' and sets it to 'done'.
-func (a addrs) forEachTodo() error {
- for k, v := range a.a {
- if v.state == todo {
- v.f()
- }
- v.state = done
- a.a[k] = v
- }
- return nil
-}
-
-const (
- todo = 1
- done = 2
-)
diff --git a/plugin/metrics/addr_test.go b/plugin/metrics/addr_test.go
deleted file mode 100644
index d7a08656b..000000000
--- a/plugin/metrics/addr_test.go
+++ /dev/null
@@ -1,17 +0,0 @@
-package metrics
-
-import "testing"
-
-func TestForEachTodo(t *testing.T) {
- a, i := newAddress(), 0
- a.setAddress("test", func() error { i++; return nil })
-
- a.forEachTodo()
- if i != 1 {
- t.Errorf("Failed to executed f for %s", "test")
- }
- a.forEachTodo()
- if i != 1 {
- t.Errorf("Executed f twice instead of once")
- }
-}
diff --git a/plugin/metrics/metrics.go b/plugin/metrics/metrics.go
index e183d33d5..5816288a4 100644
--- a/plugin/metrics/metrics.go
+++ b/plugin/metrics/metrics.go
@@ -106,7 +106,7 @@ func (m *Metrics) OnRestart() error {
return nil
}
- uniqAddr.setAddressTodo(m.Addr)
+ uniqAddr.SetTodo(m.Addr)
m.ln.Close()
m.lnSetup = false
diff --git a/plugin/metrics/setup.go b/plugin/metrics/setup.go
index 52d5775c1..c00f44a83 100644
--- a/plugin/metrics/setup.go
+++ b/plugin/metrics/setup.go
@@ -8,19 +8,21 @@ import (
"github.com/coredns/coredns/coremain"
"github.com/coredns/coredns/plugin"
clog "github.com/coredns/coredns/plugin/pkg/log"
+ "github.com/coredns/coredns/plugin/pkg/uniq"
"github.com/mholt/caddy"
)
-var log = clog.NewWithPlugin("prometheus")
+var (
+ log = clog.NewWithPlugin("prometheus")
+ uniqAddr = uniq.New()
+)
func init() {
caddy.RegisterPlugin("prometheus", caddy.Plugin{
ServerType: "dns",
Action: setup,
})
-
- uniqAddr = newAddress()
}
func setup(c *caddy.Controller) error {
@@ -36,7 +38,7 @@ func setup(c *caddy.Controller) error {
c.OncePerServerBlock(func() error {
c.OnStartup(func() error {
- return uniqAddr.forEachTodo()
+ return uniqAddr.ForEach()
})
return nil
})
@@ -54,7 +56,7 @@ func prometheusParse(c *caddy.Controller) (*Metrics, error) {
var met = New(defaultAddr)
defer func() {
- uniqAddr.setAddress(met.Addr, met.OnStartup)
+ uniqAddr.Set(met.Addr, met.OnStartup)
}()
i := 0