Skip to content
Open
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
61 changes: 61 additions & 0 deletions counter_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package metrics

import (
"testing"

dto "github.com/prometheus/client_model/go"
)

func TestCounter(t *testing.T) {
expected := struct {
Name string
Help string
Type dto.MetricType
Value float64
}{
"test_counter_counter_total",
"count all the things",
dto.MetricType_COUNTER,
5,
}

ns := NewNamespace("test", "counter", nil)

c := ns.NewCounter("counter", "count all the things")
c.Inc(5)

Register(ns)
defer Deregister(ns)

mfs, err := Gather()
if err != nil {
t.Fatal(err)
}

if len(mfs) != 1 {
t.Fatalf("expected one metric family but got %d", len(mfs))
}

family := mfs[0]
if *family.Name != expected.Name {
t.Fatalf("expected name `%s` but got `%s`", expected.Name, *family.Name)
}

if *family.Help != expected.Help {
t.Fatalf("expected help `%s` but got `%s`", expected.Help, *family.Help)
}

if *family.Type != expected.Type {
t.Fatalf("expected type `%d` but got `%d`", expected.Type, *family.Type)
}

if len(family.Metric) != 1 {
t.Fatalf("expected one metric but got %d", len(family.Metric))
}

metric := family.Metric[0]
value := metric.GetCounter().GetValue()
if value != expected.Value {
t.Fatalf("expected counter value %f but got %f", expected.Value, value)
}
}
17 changes: 17 additions & 0 deletions gather.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package metrics

import (
"github.com/prometheus/client_golang/prometheus"
dto "github.com/prometheus/client_model/go"
)

// Gatherer returns the metric gatherer
func Gatherer() prometheus.Gatherer {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, so now that these are exposed, how do you see us using them in the projects? Right now everything that is in this package is being used. There are no unused public APIs so I'm just trying to get an idea of how we are going to use this and the below function in our projects.

@jhorwit2 jhorwit2 Aug 30, 2016

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Gatherer() is useful when we want to include the default registry in debug mode to export more metrics. Prometheus has a concept of Gatherers which is an []Gatherer and merges gatherers in Gather(). This would all you to combine the two registries in debug mode to export them all in /metrics.

The use case I have for Gather() is to allow sending metrics between workers & managers for aggregating container stats. Yes, you could always have the managers hit /metrics; however, the majority of those metrics potentially aren't valuable (containers not in services and daemon metrics). This would allow the worker to filter metrics to only tasks & send those over the wire saving bytes. Then you can aggregate all these in the manager and export them or send them back to a daemon for docker stats

return registry
}

// Gather calls the gatherer to return all the metric families of
// every metric in the registry.
func Gather() ([]*dto.MetricFamily, error) {
return Gatherer().Gather()
}
4 changes: 2 additions & 2 deletions handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ package metrics
import (
"net/http"

"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
)

// Handler returns the global http.Handler that provides the prometheus
// metrics format on GET requests
func Handler() http.Handler {
return prometheus.Handler()
return promhttp.HandlerFor(Gatherer(), promhttp.HandlerOpts{})
}
6 changes: 2 additions & 4 deletions register.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
package metrics

import "github.com/prometheus/client_golang/prometheus"

// Register adds all the metrics in the provided namespace to the global
// metrics registry
func Register(n *Namespace) {
prometheus.MustRegister(n)
registry.MustRegister(n)
}

// Deregister removes all the metrics in the provided namespace from the
// global metrics registry
func Deregister(n *Namespace) {
prometheus.Unregister(n)
registry.Unregister(n)
}
7 changes: 7 additions & 0 deletions registry.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package metrics

import "github.com/prometheus/client_golang/prometheus"

var (
registry = prometheus.NewRegistry()
)