census-instrumentation / opencensus-go

A stats collection and distributed tracing framework
http://opencensus.io
Apache License 2.0
2.05k stars 327 forks source link

Gauge negative values not working #1250

Open Michaelhobo opened 3 years ago

Michaelhobo commented 3 years ago

Please answer these questions before submitting a bug report.

What version of OpenCensus are you using?

0.22.5

What version of Go are you using?

1.14

What did you do?

If possible, provide a recipe for reproducing the error.

I'm writing a wrapper library for opencensus. While writing tests, I noticed that gauges don't seem to handle negative values. You can reproduce this here: https://play.golang.org/p/y2skt697rOc

What did you expect to see?

The Float/Int64Gauge tests that go negative fail. When the gauge stays positive, the tests pass.

What did you see instead?

It seems like any negative value causes the gauge to go to 0.

Here's the test output, in case the playground doesn't compile later:

=== RUN   TestMetrics
=== RUN   TestMetrics/Float64Gauge_negative
    prog.go:120: Registry.Read() (-want +got):
          []*metricdata.Metric{
            &{
                Descriptor: {Name: "bye_gauge", Type: s"TypeGaugeFloat64"},
                Resource:   nil,
                TimeSeries: []*metricdata.TimeSeries{
                    &{
                        LabelValues: {},
                        Points: []metricdata.Point{
                            {
                                ... // 1 ignored field
        -                       Value: float64(-2.8),
        +                       Value: float64(0),
                            },
                        },
                        ... // 1 ignored field
                    },
                },
            },
          }
=== RUN   TestMetrics/Int64Cumulative
=== RUN   TestMetrics/Int64Gauge_add_from_nothing
=== RUN   TestMetrics/Int64Gauge_set_then_add
=== RUN   TestMetrics/Int64Gauge_negative
    prog.go:120: Registry.Read() (-want +got):
          []*metricdata.Metric{
            &{
                Descriptor: {Name: "hello_gauge"},
                Resource:   nil,
                TimeSeries: []*metricdata.TimeSeries{
                    &{
                        LabelValues: {},
                        Points: []metricdata.Point{
                            {
                                ... // 1 ignored field
        -                       Value: int64(-50),
        +                       Value: int64(0),
                            },
                        },
                        ... // 1 ignored field
                    },
                },
            },
          }
=== RUN   TestMetrics/Float64Cumulative
=== RUN   TestMetrics/Float64Gauge
--- FAIL: TestMetrics (0.00s)
    --- FAIL: TestMetrics/Float64Gauge_negative (0.00s)
    --- PASS: TestMetrics/Int64Cumulative (0.00s)
    --- PASS: TestMetrics/Int64Gauge_add_from_nothing (0.00s)
    --- PASS: TestMetrics/Int64Gauge_set_then_add (0.00s)
    --- FAIL: TestMetrics/Int64Gauge_negative (0.00s)
    --- PASS: TestMetrics/Float64Cumulative (0.00s)
    --- PASS: TestMetrics/Float64Gauge (0.00s)
FAIL

3 tests failed.

Additional context

Add any other context about the problem here.

Michaelhobo commented 3 years ago

The gauge test seems to think this is correct behavior: https://github.com/census-instrumentation/opencensus-go/blob/master/metric/gauge_test.go#L270

Why is this the case? If a Gauge's Add function can accept negative values, it seems counterintuitive that negative gauge values always return 0.