Skip to content

Commit

Permalink
Merge pull request #26 from colinmcintosh/cm/issue-23
Browse files Browse the repository at this point in the history
Fixes #23. Prometheus negative counter value bug
  • Loading branch information
colinmcintosh authored May 4, 2021
2 parents 7700d86 + 65b6264 commit 03322ae
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 6 deletions.
2 changes: 1 addition & 1 deletion gateway/exporters/prometheus/prometheus.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ func (e *PrometheusExporter) Export(leaf *ctree.Leaf) {
switch m := metric.(type) {
case prom.Counter:
delta, exists := e.deltaCalc.Calc(metricHash, value)
if exists {
if exists && delta >= 0 {
m.Add(delta)
}
case prom.Gauge:
Expand Down
52 changes: 47 additions & 5 deletions gateway/exporters/prometheus/prometheus_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,14 @@
// See the License for the specific language governing permissions and
// limitations under the License.

package prometheus_test
package prometheus

import (
"github.com/openconfig/gnmi-gateway/gateway/configuration"
"github.com/openconfig/gnmi/cache"
"github.com/openconfig/gnmi/ctree"
pb "github.com/openconfig/gnmi/proto/gnmi"
"github.com/prometheus/client_golang/prometheus/promauto"
"math/rand"
"strconv"
"testing"
Expand All @@ -24,10 +29,9 @@ import (
"github.com/stretchr/testify/assert"

"github.com/openconfig/gnmi-gateway/gateway/exporters"
"github.com/openconfig/gnmi-gateway/gateway/exporters/prometheus"
)

var _ exporters.Exporter = new(prometheus.PrometheusExporter)
var _ exporters.Exporter = new(PrometheusExporter)

func makeExampleLabels(seed int) prom.Labels {
rand.Seed(int64(seed))
Expand All @@ -45,8 +49,46 @@ func TestMapHash(t *testing.T) {

testLabels := makeExampleLabels(2906) // randomly selected consistent seed

firstHash := prometheus.NewStringMapHash("test_metric", testLabels)
firstHash := NewStringMapHash("test_metric", testLabels)
for i := 0; i < 100; i++ {
assertions.Equal(firstHash, prometheus.NewStringMapHash("test_metric", testLabels), "All hashes of the testLabels should be the same.")
assertions.Equal(firstHash, NewStringMapHash("test_metric", testLabels), "All hashes of the testLabels should be the same.")
}
}

func TestPrometheusExporter_Export(t *testing.T) {
n := &pb.Notification{
Prefix: &pb.Path{Target: "a", Origin: "b"},
Update: []*pb.Update{
{
Path: &pb.Path{
Elem: []*pb.PathElem{{Name: "c"}},
},
Val: &pb.TypedValue{Value: &pb.TypedValue_IntVal{IntVal: -1}},
},
},
}

metricName, labels := UpdateToMetricNameAndLabels(n.GetPrefix(), n.Update[0])
metricHash := NewStringMapHash(metricName, labels)

// Prime the delta calculator
calc := NewDeltaCalculator()
calc.Calc(metricHash, 20)

e := &PrometheusExporter{
config: &configuration.GatewayConfig{},
cache: cache.New(nil),
deltaCalc: calc,
metrics: map[Hash]prom.Metric{
metricHash: promauto.NewCounter(prom.CounterOpts{
Name: metricName,
ConstLabels: labels,
}),
},
typeLookup: nil,
}
assert.NotPanics(t, func() {
e.Export(ctree.DetachedLeaf(n))
})

}

0 comments on commit 03322ae

Please sign in to comment.