Skip to content

Commit

Permalink
Remove -Inf buckets from go collector histograms (#1049)
Browse files Browse the repository at this point in the history
* Remove -Inf buckets from go collector histograms

Signed-off-by: Kemal Akkoyun <[email protected]>

* Update prometheus/collectors/go_collector_latest_test.go

Co-authored-by: Bartlomiej Plotka <[email protected]>
Signed-off-by: Kemal Akkoyun <[email protected]>

* Simplify

Signed-off-by: Kemal Akkoyun <[email protected]>

Co-authored-by: Bartlomiej Plotka <[email protected]>
  • Loading branch information
kakkoyun and bwplotka committed May 13, 2022
1 parent 049d0fe commit 5fe1d33
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 5 deletions.
39 changes: 39 additions & 0 deletions prometheus/collectors/go_collector_latest_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// Copyright 2021 The Prometheus Authors
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

//go:build go1.17
// +build go1.17

package collectors

import (
"encoding/json"
"testing"

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

func TestGoCollectorMarshalling(t *testing.T) {
reg := prometheus.NewRegistry()
reg.MustRegister(NewGoCollector(
WithGoCollections(GoRuntimeMemStatsCollection | GoRuntimeMetricsCollection),
))
result, err := reg.Gather()
if err != nil {
t.Fatal(err)
}

if _, err := json.Marshal(result); err != nil {
t.Errorf("json marshalling shoud not fail, %v", err)
}
}
14 changes: 11 additions & 3 deletions prometheus/go_collector_latest.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,9 @@ import (

//nolint:staticcheck // Ignore SA1019. Need to keep deprecated package for compatibility.
"github.com/golang/protobuf/proto"
"github.com/prometheus/client_golang/prometheus/internal"
dto "github.com/prometheus/client_model/go"

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

const (
Expand Down Expand Up @@ -429,6 +430,11 @@ type batchHistogram struct {
// buckets must always be from the runtime/metrics package, following
// the same conventions.
func newBatchHistogram(desc *Desc, buckets []float64, hasSum bool) *batchHistogram {
// We need to remove -Inf values. runtime/metrics keeps them around.
// But -Inf bucket should not be allowed for prometheus histograms.
if buckets[0] == math.Inf(-1) {
buckets = buckets[1:]
}
h := &batchHistogram{
desc: desc,
buckets: buckets,
Expand Down Expand Up @@ -487,8 +493,10 @@ func (h *batchHistogram) Write(out *dto.Metric) error {
for i, count := range h.counts {
totalCount += count
if !h.hasSum {
// N.B. This computed sum is an underestimate.
sum += h.buckets[i] * float64(count)
if count != 0 {
// N.B. This computed sum is an underestimate.
sum += h.buckets[i] * float64(count)
}
}

// Skip the +Inf bucket, but only for the bucket list.
Expand Down
2 changes: 1 addition & 1 deletion prometheus/go_collector_metrics_go117_test.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion prometheus/go_collector_metrics_go118_test.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 5fe1d33

Please sign in to comment.