Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug: testutil package unexpected results for Histograms #498

Closed
philipgough opened this issue Nov 14, 2018 · 1 comment · Fixed by #499
Closed

Bug: testutil package unexpected results for Histograms #498

philipgough opened this issue Nov 14, 2018 · 1 comment · Fixed by #499
Assignees
Labels

Comments

@philipgough
Copy link
Contributor

@beorn7 this follows up from our chat in #497

I'm seeing similar behaviour as reported in #494 with Counters. This time with Histograms. I've written the following failing test

func TestCollectAndCompareHistogram(t *testing.T) {
	inputs := []struct {
		name        string
		c           prometheus.Collector
		metadata    string
		expect      string
		observation float64
	}{
		{
			name: "Testing Histogram Collector",
			c: prometheus.NewHistogram(prometheus.HistogramOpts{
				Name:    "some_histogram",
				Help:    "An example of a histogram",
				Buckets: []float64{1, 2, 3},
			}),
			metadata: `
				# HELP some_histogram An example of a histogram
				# TYPE some_histogram histogram
			`,
			expect: `
				some_histogram{le="1"} 0
				some_histogram{le="2"} 0
				some_histogram{le="3"} 1
        			some_histogram_bucket{le="+Inf"} 1
        			some_histogram_sum 2.5
        			some_histogram_count 1

			`,
			observation: 2.5,
		},
		{
			name: "Testing HistogramVec Collector",
			c: prometheus.NewHistogramVec(prometheus.HistogramOpts{
				Name:    "some_histogram",
				Help:    "An example of a histogram",
				Buckets: []float64{1, 2, 3},
			}, []string{"test"}),

			metadata: `
				# HELP some_histogram An example of a histogram
				# TYPE some_histogram histogram
			`,
			expect: `
				some_histogram{le="1"} 0
				some_histogram{le="2"} 0
				some_histogram{le="3"} 1
        			some_histogram_bucket{le="+Inf"} 1
        			some_histogram_sum 2.5
        			some_histogram_count 1
		
			`,
			observation: 2.5,
		},
	}

	for _, input := range inputs {
		switch collector := input.c.(type) {
		case prometheus.Histogram:
			collector.Observe(input.observation)
		case *prometheus.HistogramVec:
			collector.WithLabelValues("test").Observe(input.observation)
		default:
			t.Fatalf("unsuported collector tested")

		}

		t.Run(input.name, func(t *testing.T) {
			if err := CollectAndCompare(input.c, strings.NewReader(input.metadata+input.expect)); err != nil {
				t.Errorf("unexpected collecting result:\n%s", err)
			}
		})

	}
}

It returns the following output:

FAIL
    --- FAIL: TestCollectAndCompareHistogram/Testing_Histogram_Collector (0.00s)
        testutil_test.go:237: unexpected collecting result:
            
            metric output does not match expectation; want:
            
            # HELP some_histogram An example of a histogram
            # TYPE some_histogram histogram
            some_histogram_bucket{le="1"} 0
            some_histogram_bucket{le="2"} 0
            some_histogram_bucket{le="3"} 1
            some_histogram_bucket{le="+Inf"} 1
            some_histogram_sum 2.5
            some_histogram_count 1
            
            
            got:
            
            # HELP some_histogram An example of a histogram
            # TYPE some_histogram histogram
            some_histogram_bucket{le="1"} 0
            some_histogram_bucket{le="2"} 0
            some_histogram_bucket{le="3"} 1
            some_histogram_bucket{le="+Inf"} 1
            some_histogram_sum 2.5
            some_histogram_count 1

Comparing the want and the got strings:

name:"some_histogram" help:"An example of a histogram" type:HISTOGRAM metric:<histogram:<sample_count:1 sample_sum:2.5 bucket:<cumulative_count:0 upper_bound:1 > bucket:<cumulative_count:0 upper_bound:2 > bucket:<cumulative_count:1 upper_bound:3 > > > 
name:"some_histogram" help:"An example of a histogram" type:HISTOGRAM metric:<histogram:<sample_count:1 sample_sum:2.5 bucket:<cumulative_count:0 upper_bound:1 > bucket:<cumulative_count:0 upper_bound:2 > bucket:<cumulative_count:1 upper_bound:3 > bucket:<cumulative_count:1 upper_bound:inf > > >
@beorn7 beorn7 self-assigned this Nov 14, 2018
@beorn7 beorn7 added the bug label Nov 14, 2018
@beorn7
Copy link
Member

beorn7 commented Nov 14, 2018

I see, that's the +Inf bucket, which is redundant/optional. Hmmm, I think I'll just go one step further and compare the text format directly. Currently, in real-life Prometheus settings, the text format is the one Prometheus uses anyway, so it's most relevant.

Will create another PR at my next convenience.

philipgough added a commit to philipgough/client_golang that referenced this issue Nov 14, 2018
philipgough added a commit to philipgough/client_golang that referenced this issue Nov 14, 2018
@philipgough philipgough mentioned this issue Nov 14, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants