Skip to content

Commit

Permalink
Fixed bug with prometheus label names
Browse files Browse the repository at this point in the history
  • Loading branch information
nrwiersma committed Sep 13, 2019
1 parent 6215ec3 commit a9ccfe8
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 5 deletions.
10 changes: 5 additions & 5 deletions prometheus/prometheus.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ func (s *Prometheus) Handler() http.Handler {

// Inc increments a count by the value.
func (s *Prometheus) Inc(name string, value int64, rate float32, tags ...string) {
lblNames, lbls := formatTags(tags)
lblNames, lbls := formatTags(tags, s.fqn)

key := createKey(name, lblNames)
m, ok := s.counters[key]
Expand Down Expand Up @@ -99,7 +99,7 @@ func (s *Prometheus) Dec(name string, value int64, rate float32, tags ...string)

// Gauge measures the value of a metric.
func (s *Prometheus) Gauge(name string, value float64, rate float32, tags ...string) {
lblNames, lbls := formatTags(tags)
lblNames, lbls := formatTags(tags, s.fqn)

key := createKey(name, lblNames)
m, ok := s.gauges[key]
Expand All @@ -125,7 +125,7 @@ func (s *Prometheus) Gauge(name string, value float64, rate float32, tags ...str

// Timing sends the value of a Duration.
func (s *Prometheus) Timing(name string, value time.Duration, rate float32, tags ...string) {
lblNames, lbls := formatTags(tags)
lblNames, lbls := formatTags(tags, s.fqn)

key := createKey(name, lblNames)
m, ok := s.timings[key]
Expand Down Expand Up @@ -161,13 +161,13 @@ func createKey(name string, lblNames []string) string {
}

// formatTags create a prometheus Label map from tags.
func formatTags(t []string) ([]string, prometheus.Labels) {
func formatTags(t []string, fqn *FQN) ([]string, prometheus.Labels) {
t = tags.Deduplicate(tags.Normalize(t))

names := make([]string, 0, len(t)/2)
lbls := make(prometheus.Labels, len(t)/2)
for i := 0; i < len(t); i += 2 {
key := t[i]
key := fqn.Format(t[i])
names = append(names, key)
lbls[key] = t[i+1]
}
Expand Down
14 changes: 14 additions & 0 deletions prometheus/prometheus_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,20 @@ func TestPrometheus_Timing(t *testing.T) {
assert.Contains(t, rr.Body.String(), "test_test_test_count{test=\"test\"} 1")
}

func TestPrometheus_ConvertsLabels(t *testing.T) {
l := &testLogger{}
s := prometheus.New("test.test", l)

s.Inc("test", 2, 1.0, "test-label", "test")

rr := httptest.NewRecorder()
req := httptest.NewRequest("GET", "/metrics", nil)
s.Handler().ServeHTTP(rr, req)

assert.Equal(t, "msg=", l.Render())
assert.Contains(t, rr.Body.String(), "test_test_test{test_label=\"test\"} 2")
}

func TestPrometheus_Close(t *testing.T) {
l := &testLogger{}
s := prometheus.New("test.test", l)
Expand Down

0 comments on commit a9ccfe8

Please sign in to comment.