Skip to content

Commit

Permalink
add 'namespace' to allow parallel tests
Browse files Browse the repository at this point in the history
add failing test case which shows old metrics are not cleared

code cleanup

code cleanup

assure new gagues are exported properly

recreate gauges on each scrape to clear stale values

include tests for stale metrics of all types

clear containers as well
  • Loading branch information
matt-deboer authored and justinclayton committed Jan 11, 2017
1 parent a864312 commit a435c71
Show file tree
Hide file tree
Showing 5 changed files with 267 additions and 80 deletions.
20 changes: 12 additions & 8 deletions containers.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,14 @@ const (
)

type CounterContainer struct {
counters map[string]*prometheus.CounterVec
counters map[string]*prometheus.CounterVec
namespace string
}

func NewCounterContainer() *CounterContainer {
func NewCounterContainer(namespace string) *CounterContainer {
return &CounterContainer{
counters: make(map[string]*prometheus.CounterVec),
counters: make(map[string]*prometheus.CounterVec),
namespace: namespace,
}
}

Expand All @@ -33,7 +35,7 @@ func (c *CounterContainer) Fetch(name, help string, labels ...string) (*promethe

if !exists {
counter = prometheus.NewCounterVec(prometheus.CounterOpts{
Namespace: namespace,
Namespace: c.namespace,
Name: name,
Help: help,
}, labels)
Expand All @@ -45,12 +47,14 @@ func (c *CounterContainer) Fetch(name, help string, labels ...string) (*promethe
}

type GaugeContainer struct {
gauges map[string]*prometheus.GaugeVec
gauges map[string]*prometheus.GaugeVec
namespace string
}

func NewGaugeContainer() *GaugeContainer {
func NewGaugeContainer(namespace string) *GaugeContainer {
return &GaugeContainer{
gauges: make(map[string]*prometheus.GaugeVec),
gauges: make(map[string]*prometheus.GaugeVec),
namespace: namespace,
}
}

Expand All @@ -60,7 +64,7 @@ func (c *GaugeContainer) Fetch(name, help string, labels ...string) (*prometheus

if !exists {
gauge = prometheus.NewGaugeVec(prometheus.GaugeOpts{
Namespace: namespace,
Namespace: c.namespace,
Name: name,
Help: help,
}, labels)
Expand Down
4 changes: 2 additions & 2 deletions containers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func Test_container_key(t *testing.T) {
}

func Test_container_fetch_counter(t *testing.T) {
container := NewCounterContainer()
container := NewCounterContainer("marathon")
_, new := container.Fetch("foo", "")

if !new {
Expand All @@ -52,7 +52,7 @@ func Test_container_fetch_counter(t *testing.T) {
}

func Test_container_fetch_gauge(t *testing.T) {
container := NewGaugeContainer()
container := NewGaugeContainer("marathon")
_, new := container.Fetch("foo", "")

if !new {
Expand Down
11 changes: 7 additions & 4 deletions exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
"github.com/prometheus/common/log"
)

const namespace = "marathon"
const defaultNamespace = "marathon"

type Exporter struct {
scraper Scraper
Expand Down Expand Up @@ -71,6 +71,9 @@ func (e *Exporter) scrape(ch chan<- prometheus.Metric) {
}
}(time.Now())

// Rebuild gauges & coutners to avoid stale values
e.Gauges = NewGaugeContainer(e.Gauges.namespace)
e.Counters = NewCounterContainer(e.Counters.namespace)
if err = e.exportApps(ch); err != nil {
return
}
Expand Down Expand Up @@ -422,11 +425,11 @@ func (e *Exporter) scrapeTimer(key string, json *gabs.Container) (bool, error) {
return new, nil
}

func NewExporter(s Scraper) *Exporter {
func NewExporter(s Scraper, namespace string) *Exporter {
return &Exporter{
scraper: s,
Counters: NewCounterContainer(),
Gauges: NewGaugeContainer(),
Counters: NewCounterContainer(namespace),
Gauges: NewGaugeContainer(namespace),
duration: prometheus.NewGauge(prometheus.GaugeOpts{
Namespace: namespace,
Subsystem: "exporter",
Expand Down
Loading

0 comments on commit a435c71

Please sign in to comment.