Skip to content

Commit

Permalink
Merge pull request #1 from prometheus/art
Browse files Browse the repository at this point in the history
Last touches (readability and consistency)
  • Loading branch information
ArthurSens committed Sep 20, 2023
2 parents 226eb8d + 447c5da commit 5151a52
Show file tree
Hide file tree
Showing 13 changed files with 281 additions and 205 deletions.
9 changes: 4 additions & 5 deletions prometheus/counter.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,8 @@ type counter struct {
labelPairs []*dto.LabelPair
exemplar atomic.Value // Containing nil or a *dto.Exemplar.

now func() time.Time // For testing, all constructors put time.Now() here.
// now is for testing purposes, by default it's time.Now.
now func() time.Time
}

func (c *counter) Desc() *Desc {
Expand Down Expand Up @@ -165,9 +166,7 @@ func (c *counter) Write(out *dto.Metric) error {
exemplar = e.(*dto.Exemplar)
}
val := c.get()

ct := c.createdTs.AsTime()
return populateMetric(CounterValue, val, c.labelPairs, exemplar, out, &ct)
return populateMetric(CounterValue, val, c.labelPairs, exemplar, out, c.createdTs)
}

func (c *counter) updateExemplar(v float64, l Labels) {
Expand Down Expand Up @@ -215,7 +214,7 @@ func (v2) NewCounterVec(opts CounterVecOpts) *CounterVec {
if len(lvs) != len(desc.variableLabels.names) {
panic(makeInconsistentCardinalityError(desc.fqName, desc.variableLabels.names, lvs))
}
result := &counter{desc: desc, labelPairs: MakeLabelPairs(desc, lvs), now: time.Now}
result := &counter{desc: desc, labelPairs: MakeLabelPairs(desc, lvs), now: opts.now}
result.init(result) // Init self-collection.
result.createdTs = timestamppb.New(opts.now())
return result
Expand Down
107 changes: 70 additions & 37 deletions prometheus/counter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,12 @@ import (

func TestCounterAdd(t *testing.T) {
now := time.Now()
nowFn := func() time.Time { return now }

counter := NewCounter(CounterOpts{
Name: "test",
Help: "test help",
ConstLabels: Labels{"a": "1", "b": "2"},
now: nowFn,
now: func() time.Time { return now },
}).(*counter)
counter.Inc()
if expected, got := 0.0, math.Float64frombits(counter.valBits); expected != got {
Expand Down Expand Up @@ -71,7 +71,7 @@ func TestCounterAdd(t *testing.T) {
},
Counter: &dto.Counter{
Value: proto.Float64(67.42),
CreatedTimestamp: timestamppb.New(nowFn()),
CreatedTimestamp: timestamppb.New(now),
},
}
if !proto.Equal(expected, m) {
Expand Down Expand Up @@ -146,11 +146,11 @@ func expectPanic(t *testing.T, op func(), errorMsg string) {

func TestCounterAddInf(t *testing.T) {
now := time.Now()
nowFn := func() time.Time { return now }

counter := NewCounter(CounterOpts{
Name: "test",
Help: "test help",
now: nowFn,
now: func() time.Time { return now },
}).(*counter)

counter.Inc()
Expand Down Expand Up @@ -183,7 +183,7 @@ func TestCounterAddInf(t *testing.T) {
expected := &dto.Metric{
Counter: &dto.Counter{
Value: proto.Float64(math.Inf(1)),
CreatedTimestamp: timestamppb.New(nowFn()),
CreatedTimestamp: timestamppb.New(now),
},
}

Expand All @@ -194,11 +194,11 @@ func TestCounterAddInf(t *testing.T) {

func TestCounterAddLarge(t *testing.T) {
now := time.Now()
nowFn := func() time.Time { return now }

counter := NewCounter(CounterOpts{
Name: "test",
Help: "test help",
now: nowFn,
now: func() time.Time { return now },
}).(*counter)

// large overflows the underlying type and should therefore be stored in valBits.
Expand All @@ -217,7 +217,7 @@ func TestCounterAddLarge(t *testing.T) {
expected := &dto.Metric{
Counter: &dto.Counter{
Value: proto.Float64(large),
CreatedTimestamp: timestamppb.New(nowFn()),
CreatedTimestamp: timestamppb.New(now),
},
}

Expand All @@ -228,12 +228,13 @@ func TestCounterAddLarge(t *testing.T) {

func TestCounterAddSmall(t *testing.T) {
now := time.Now()
nowFn := func() time.Time { return now }

counter := NewCounter(CounterOpts{
Name: "test",
Help: "test help",
now: nowFn,
now: func() time.Time { return now },
}).(*counter)

small := 0.000000000001
counter.Add(small)
if expected, got := small, math.Float64frombits(counter.valBits); expected != got {
Expand All @@ -249,7 +250,7 @@ func TestCounterAddSmall(t *testing.T) {
expected := &dto.Metric{
Counter: &dto.Counter{
Value: proto.Float64(small),
CreatedTimestamp: timestamppb.New(nowFn()),
CreatedTimestamp: timestamppb.New(now),
},
}

Expand All @@ -264,8 +265,8 @@ func TestCounterExemplar(t *testing.T) {
counter := NewCounter(CounterOpts{
Name: "test",
Help: "test help",
now: func() time.Time { return now },
}).(*counter)
counter.now = func() time.Time { return now }

ts := timestamppb.New(now)
if err := ts.CheckValid(); err != nil {
Expand Down Expand Up @@ -317,39 +318,71 @@ func TestCounterExemplar(t *testing.T) {
}
}

func TestCounterCreatedTimestamp(t *testing.T) {
func TestCounterVecCreatedTimestampWithDeletes(t *testing.T) {
now := time.Now()
counter := NewCounter(CounterOpts{

counterVec := NewCounterVec(CounterOpts{
Name: "test",
Help: "test help",
now: func() time.Time { return now },
})
}, []string{"label"})

var metric dto.Metric
if err := counter.Write(&metric); err != nil {
t.Fatal(err)
}
// First use of "With" should populate CT.
counterVec.WithLabelValues("1")
expected := map[string]time.Time{"1": now}

if metric.Counter.CreatedTimestamp.AsTime().Unix() != now.Unix() {
t.Errorf("expected created timestamp %d, got %d", now.Unix(), metric.Counter.CreatedTimestamp.AsTime().Unix())
}
now = now.Add(1 * time.Hour)
expectCTsForMetricVecValues(t, counterVec.MetricVec, dto.MetricType_COUNTER, expected)

// Two more labels at different times.
counterVec.WithLabelValues("2")
expected["2"] = now

now = now.Add(1 * time.Hour)

counterVec.WithLabelValues("3")
expected["3"] = now

now = now.Add(1 * time.Hour)
expectCTsForMetricVecValues(t, counterVec.MetricVec, dto.MetricType_COUNTER, expected)

// Recreate metric instance should reset created timestamp to now.
counterVec.DeleteLabelValues("1")
counterVec.WithLabelValues("1")
expected["1"] = now

now = now.Add(1 * time.Hour)
expectCTsForMetricVecValues(t, counterVec.MetricVec, dto.MetricType_COUNTER, expected)
}

func TestCounterVecCreatedTimestamp(t *testing.T) {
now := time.Now()
counterVec := NewCounterVec(CounterOpts{
Name: "test",
Help: "test help",
now: func() time.Time { return now },
}, []string{"label"})
counter := counterVec.WithLabelValues("value")
func expectCTsForMetricVecValues(t testing.TB, vec *MetricVec, typ dto.MetricType, ctsPerLabelValue map[string]time.Time) {
t.Helper()

var metric dto.Metric
if err := counter.Write(&metric); err != nil {
t.Fatal(err)
}
for val, ct := range ctsPerLabelValue {
var metric dto.Metric
m, err := vec.GetMetricWithLabelValues(val)
if err != nil {
t.Fatal(err)
}

if metric.Counter.CreatedTimestamp.AsTime().Unix() != now.Unix() {
t.Errorf("expected created timestamp %d, got %d", now.Unix(), metric.Counter.CreatedTimestamp.AsTime().Unix())
if err := m.Write(&metric); err != nil {
t.Fatal(err)
}

var gotTs time.Time
switch typ {
case dto.MetricType_COUNTER:
gotTs = metric.Counter.CreatedTimestamp.AsTime()
case dto.MetricType_HISTOGRAM:
gotTs = metric.Histogram.CreatedTimestamp.AsTime()
case dto.MetricType_SUMMARY:
gotTs = metric.Summary.CreatedTimestamp.AsTime()
default:
t.Fatalf("unknown metric type %v", typ)
}

if !gotTs.Equal(ct) {
t.Errorf("expected created timestamp for %s with label value %q: %s, got %s", typ, val, ct, gotTs)
}
}
}
4 changes: 3 additions & 1 deletion prometheus/example_metricvec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
package prometheus_test

import (
"fmt"

"google.golang.org/protobuf/proto"

dto "github.com/prometheus/client_model/go"
Expand Down Expand Up @@ -124,7 +126,7 @@ func ExampleMetricVec() {
if err != nil || len(metricFamilies) != 1 {
panic("unexpected behavior of custom test registry")
}
printlnNormalized(metricFamilies[0])
fmt.Println(toNormalizedJSON(metricFamilies[0]))

// Output:
// {"name":"library_version_info","help":"Versions of the libraries used in this binary.","type":"GAUGE","metric":[{"label":[{"name":"library","value":"k8s.io/client-go"},{"name":"version","value":"0.18.8"}],"gauge":{"value":1}},{"label":[{"name":"library","value":"prometheus/client_golang"},{"name":"version","value":"1.7.1"}],"gauge":{"value":1}}]}
Expand Down
44 changes: 26 additions & 18 deletions prometheus/examples_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,22 @@ func ExampleCounterVec() {
httpReqs.DeleteLabelValues("200", "GET")
// Same thing with the more verbose Labels syntax.
httpReqs.Delete(prometheus.Labels{"method": "GET", "code": "200"})

// Just for demonstration, let's check the state of the counter vector
// by registering it with a custom registry and then let it collect the
// metrics.
reg := prometheus.NewRegistry()
reg.MustRegister(httpReqs)

metricFamilies, err := reg.Gather()
if err != nil || len(metricFamilies) != 1 {
panic("unexpected behavior of custom test registry")
}

fmt.Println(toNormalizedJSON(sanitizeMetricFamily(metricFamilies[0])))

// Output:
// {"name":"http_requests_total","help":"How many HTTP requests processed, partitioned by status code and HTTP method.","type":"COUNTER","metric":[{"label":[{"name":"code","value":"404"},{"name":"method","value":"POST"}],"counter":{"value":42,"createdTimestamp":"1970-01-01T00:00:10Z"}}]}
}

func ExampleRegister() {
Expand Down Expand Up @@ -319,13 +335,11 @@ func ExampleSummary() {
// internally).
metric := &dto.Metric{}
temps.Write(metric)
// We remove CreatedTimestamp just to make sure the assert below works.
metric.Summary.CreatedTimestamp = nil

printlnNormalized(metric)
fmt.Println(toNormalizedJSON(sanitizeMetric(metric)))

// Output:
// {"summary":{"sampleCount":"1000","sampleSum":29969.50000000001,"quantile":[{"quantile":0.5,"value":31.1},{"quantile":0.9,"value":41.3},{"quantile":0.99,"value":41.9}]}}
// {"summary":{"sampleCount":"1000","sampleSum":29969.50000000001,"quantile":[{"quantile":0.5,"value":31.1},{"quantile":0.9,"value":41.3},{"quantile":0.99,"value":41.9}],"createdTimestamp":"1970-01-01T00:00:10Z"}}
}

func ExampleSummaryVec() {
Expand Down Expand Up @@ -357,15 +371,11 @@ func ExampleSummaryVec() {
if err != nil || len(metricFamilies) != 1 {
panic("unexpected behavior of custom test registry")
}
// We remove CreatedTimestamp just to make sure the assert below works.
for _, m := range metricFamilies[0].Metric {
m.Summary.CreatedTimestamp = nil
}

printlnNormalized(metricFamilies[0])
fmt.Println(toNormalizedJSON(sanitizeMetricFamily(metricFamilies[0])))

// Output:
// {"name":"pond_temperature_celsius","help":"The temperature of the frog pond.","type":"SUMMARY","metric":[{"label":[{"name":"species","value":"leiopelma-hochstetteri"}],"summary":{"sampleCount":"0","sampleSum":0,"quantile":[{"quantile":0.5,"value":"NaN"},{"quantile":0.9,"value":"NaN"},{"quantile":0.99,"value":"NaN"}]}},{"label":[{"name":"species","value":"lithobates-catesbeianus"}],"summary":{"sampleCount":"1000","sampleSum":31956.100000000017,"quantile":[{"quantile":0.5,"value":32.4},{"quantile":0.9,"value":41.4},{"quantile":0.99,"value":41.9}]}},{"label":[{"name":"species","value":"litoria-caerulea"}],"summary":{"sampleCount":"1000","sampleSum":29969.50000000001,"quantile":[{"quantile":0.5,"value":31.1},{"quantile":0.9,"value":41.3},{"quantile":0.99,"value":41.9}]}}]}
// {"name":"pond_temperature_celsius","help":"The temperature of the frog pond.","type":"SUMMARY","metric":[{"label":[{"name":"species","value":"leiopelma-hochstetteri"}],"summary":{"sampleCount":"0","sampleSum":0,"quantile":[{"quantile":0.5,"value":"NaN"},{"quantile":0.9,"value":"NaN"},{"quantile":0.99,"value":"NaN"}],"createdTimestamp":"1970-01-01T00:00:10Z"}},{"label":[{"name":"species","value":"lithobates-catesbeianus"}],"summary":{"sampleCount":"1000","sampleSum":31956.100000000017,"quantile":[{"quantile":0.5,"value":32.4},{"quantile":0.9,"value":41.4},{"quantile":0.99,"value":41.9}],"createdTimestamp":"1970-01-01T00:00:10Z"}},{"label":[{"name":"species","value":"litoria-caerulea"}],"summary":{"sampleCount":"1000","sampleSum":29969.50000000001,"quantile":[{"quantile":0.5,"value":31.1},{"quantile":0.9,"value":41.3},{"quantile":0.99,"value":41.9}],"createdTimestamp":"1970-01-01T00:00:10Z"}}]}
}

func ExampleNewConstSummary() {
Expand All @@ -389,7 +399,7 @@ func ExampleNewConstSummary() {
// internally).
metric := &dto.Metric{}
s.Write(metric)
printlnNormalized(metric)
fmt.Println(toNormalizedJSON(metric))

// Output:
// {"label":[{"name":"code","value":"200"},{"name":"method","value":"get"},{"name":"owner","value":"example"}],"summary":{"sampleCount":"4711","sampleSum":403.34,"quantile":[{"quantile":0.5,"value":42.3},{"quantile":0.9,"value":323.3}]}}
Expand All @@ -412,13 +422,11 @@ func ExampleHistogram() {
// internally).
metric := &dto.Metric{}
temps.Write(metric)
// We remove CreatedTimestamp just to make sure the assert below works.
metric.Histogram.CreatedTimestamp = nil

printlnNormalized(metric)
fmt.Println(toNormalizedJSON(sanitizeMetric(metric)))

// Output:
// {"histogram":{"sampleCount":"1000","sampleSum":29969.50000000001,"bucket":[{"cumulativeCount":"192","upperBound":20},{"cumulativeCount":"366","upperBound":25},{"cumulativeCount":"501","upperBound":30},{"cumulativeCount":"638","upperBound":35},{"cumulativeCount":"816","upperBound":40}]}}
// {"histogram":{"sampleCount":"1000","sampleSum":29969.50000000001,"bucket":[{"cumulativeCount":"192","upperBound":20},{"cumulativeCount":"366","upperBound":25},{"cumulativeCount":"501","upperBound":30},{"cumulativeCount":"638","upperBound":35},{"cumulativeCount":"816","upperBound":40}],"createdTimestamp":"1970-01-01T00:00:10Z"}}
}

func ExampleNewConstHistogram() {
Expand All @@ -442,7 +450,7 @@ func ExampleNewConstHistogram() {
// internally).
metric := &dto.Metric{}
h.Write(metric)
printlnNormalized(metric)
fmt.Println(toNormalizedJSON(metric))

// Output:
// {"label":[{"name":"code","value":"200"},{"name":"method","value":"get"},{"name":"owner","value":"example"}],"histogram":{"sampleCount":"4711","sampleSum":403.34,"bucket":[{"cumulativeCount":"121","upperBound":25},{"cumulativeCount":"2403","upperBound":50},{"cumulativeCount":"3221","upperBound":100},{"cumulativeCount":"4233","upperBound":200}]}}
Expand Down Expand Up @@ -480,7 +488,7 @@ func ExampleNewConstHistogram_WithExemplar() {
// internally).
metric := &dto.Metric{}
h.Write(metric)
printlnNormalized(metric)
fmt.Println(toNormalizedJSON(metric))

// Output:
// {"label":[{"name":"code","value":"200"},{"name":"method","value":"get"},{"name":"owner","value":"example"}],"histogram":{"sampleCount":"4711","sampleSum":403.34,"bucket":[{"cumulativeCount":"121","upperBound":25,"exemplar":{"label":[{"name":"testName","value":"testVal"}],"value":24,"timestamp":"2006-01-02T15:04:05Z"}},{"cumulativeCount":"2403","upperBound":50,"exemplar":{"label":[{"name":"testName","value":"testVal"}],"value":42,"timestamp":"2006-01-02T15:04:05Z"}},{"cumulativeCount":"3221","upperBound":100,"exemplar":{"label":[{"name":"testName","value":"testVal"}],"value":89,"timestamp":"2006-01-02T15:04:05Z"}},{"cumulativeCount":"4233","upperBound":200,"exemplar":{"label":[{"name":"testName","value":"testVal"}],"value":157,"timestamp":"2006-01-02T15:04:05Z"}}]}}
Expand Down Expand Up @@ -642,7 +650,7 @@ func ExampleNewMetricWithTimestamp() {
// internally).
metric := &dto.Metric{}
s.Write(metric)
printlnNormalized(metric)
fmt.Println(toNormalizedJSON(metric))

// Output:
// {"gauge":{"value":298.15},"timestampMs":"1257894000012"}
Expand Down
2 changes: 1 addition & 1 deletion prometheus/expvar_collector_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ func ExampleNewExpvarCollector() {
if !strings.Contains(m.Desc().String(), "expvar_memstats") {
metric.Reset()
m.Write(&metric)
metricStrings = append(metricStrings, protoToNormalizedJSON(&metric))
metricStrings = append(metricStrings, toNormalizedJSON(&metric))
}
}
sort.Strings(metricStrings)
Expand Down
Loading

0 comments on commit 5151a52

Please sign in to comment.