statsd used with GaugeDelta for +1 and -1 results in negative value instead of 0

I am using "github.com/cactus/go-statsd-client/statsd" library to post stats

  1. I create a client with NewBufferedClient
  2. I initialize metric with client.Gauge("metric_name", 0, 1)
  3. In one call of the function I run client.GaugeDelta("metric_name", 1, 1) and than after some logic client.GaugeDelta("metric_name, -1, 1). This can be called multiple times at many workers.

As I understand it should end up with metric_name at AWS CloudWatch agent set up at 0, but it actually creates a negative values metric and an error that it cannot be negative.

  1. Why does it happen?
  2. How to mitigate it?

$ go.mod

...
github.com/cactus/go-statsd-client v0.0.0-00010101000000-000000000000
...

$ content.go

import (
    ...
    "github.com/cactus/go-statsd-client/statsd"
    ...
)

func init() {
    var err error
    cwclient, err = statsd.NewBufferedClient(host, namespace, time.Second, 0)
    if err != nil {
        log.Fatal(err)
    }
    cwclient.Gauge("metric", 0, 1)
}

type Content struct {
    ...
}

func NewContent() *Content {
    ...
}

func (self *Content) Work() {
    self.cwclient.GaugeDelta("metric", 1, 1)
    err := self.work()
    if err != nil {
        // Do something
    }
    self.cwclient.GaugeDelta("metric", -1, 1)
}

func (self *Content) work() (err error) {
    ...
}

Upvotes: 1

Views: 27

Answers (0)

Related Questions