Skip to content
Merged
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
18 changes: 15 additions & 3 deletions stats.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,22 @@ import (
)

// StatMetric is one measured value within a Stat, optionally carrying
// Prometheus-style labels.
// Prometheus-style labels. A counter, gauge or untyped metric sets Value; a
// histogram sets SampleSum, SampleCount and Buckets instead. The server sends
// every number as a string.
type StatMetric struct {
Value string `json:"value"`
Labels map[string]string `json:"labels,omitempty"`
Value string `json:"value,omitempty"`
Labels map[string]string `json:"labels,omitempty"`
SampleSum string `json:"sample_sum,omitempty"`
SampleCount string `json:"sample_count,omitempty"`
Buckets []StatBucket `json:"bucket,omitempty"`
}

// StatBucket is one bucket of a histogram StatMetric: how many samples were
// at most UpperBound ("+Inf" for the last bucket).
type StatBucket struct {
CumulativeCount string `json:"cumulative_count"`
UpperBound string `json:"upper_bound"`
}

// Stat is one metric family reported by the /_stats endpoint.
Expand Down
24 changes: 24 additions & 0 deletions stats_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ package cinc

import (
"context"
"encoding/json"
"net/http"
"net/http/httptest"
"reflect"
"testing"
)

Expand Down Expand Up @@ -86,3 +88,25 @@ func TestStats_Unauthorized(t *testing.T) {
t.Fatal("expected 401")
}
}

// A histogram carries sample_sum, sample_count and buckets instead of a value
// (chef_wm_prometheus_json_format:emit_metric), all as strings.
func TestStats_Histogram(t *testing.T) {
var stats []Stat
body := `[{"name":"chef_index_batch_queue_latency","type":"HISTOGRAM","help":"h","metrics":[
{"labels":{"stage":"flush"},"sample_sum":"12.5","sample_count":"3",
"bucket":[{"cumulative_count":"1","upper_bound":"0.5"},{"cumulative_count":"3","upper_bound":"+Inf"}]}]}]`
if err := json.Unmarshal([]byte(body), &stats); err != nil {
t.Fatal(err)
}
m := stats[0].Metrics[0]
want := StatMetric{
Labels: map[string]string{"stage": "flush"},
SampleSum: "12.5",
SampleCount: "3",
Buckets: []StatBucket{{CumulativeCount: "1", UpperBound: "0.5"}, {CumulativeCount: "3", UpperBound: "+Inf"}},
}
if !reflect.DeepEqual(m, want) {
t.Errorf("metric = %+v\nwant %+v", m, want)
}
}