diff --git a/pkg/aggregator/sender.go b/pkg/aggregator/sender.go index 0a4a78741913..3ab754cb38dc 100644 --- a/pkg/aggregator/sender.go +++ b/pkg/aggregator/sender.go @@ -28,6 +28,7 @@ type Sender interface { Rate(metric string, value float64, hostname string, tags []string) Count(metric string, value float64, hostname string, tags []string) MonotonicCount(metric string, value float64, hostname string, tags []string) + Counter(metric string, value float64, hostname string, tags []string) Histogram(metric string, value float64, hostname string, tags []string) Historate(metric string, value float64, hostname string, tags []string) ServiceCheck(checkName string, status metrics.ServiceCheckStatus, hostname string, tags []string, message string) @@ -197,6 +198,13 @@ func (s *checkSender) MonotonicCount(metric string, value float64, hostname stri s.sendMetricSample(metric, value, hostname, tags, metrics.MonotonicCountType) } +// Counter is DEPRECATED and only implemented to preserve backward compatibility with python checks. Prefer using either: +// * `Gauge` if you're counting states +// * `Count` if you're counting events +func (s *checkSender) Counter(metric string, value float64, hostname string, tags []string) { + s.sendMetricSample(metric, value, hostname, tags, metrics.CounterType) +} + // Histogram should be used to track the statistical distribution of a set of values during a check run // Should be called multiple times on the same (metric, hostname, tags) so that a distribution can be computed func (s *checkSender) Histogram(metric string, value float64, hostname string, tags []string) { diff --git a/pkg/aggregator/sender_test.go b/pkg/aggregator/sender_test.go index f1841fa1739f..83bcd177317f 100644 --- a/pkg/aggregator/sender_test.go +++ b/pkg/aggregator/sender_test.go @@ -127,6 +127,7 @@ func TestCheckSenderInterface(t *testing.T) { checkSender.Rate("my.rate_metric", 2.0, "my-hostname", []string{"foo", "bar"}) checkSender.Count("my.count_metric", 123.0, "my-hostname", []string{"foo", "bar"}) checkSender.MonotonicCount("my.monotonic_count_metric", 12.0, "my-hostname", []string{"foo", "bar"}) + checkSender.Counter("my.counter_metric", 1.0, "my-hostname", []string{"foo", "bar"}) checkSender.Histogram("my.histo_metric", 3.0, "my-hostname", []string{"foo", "bar"}) checkSender.Commit() checkSender.ServiceCheck("my_service.can_connect", metrics.ServiceCheckOK, "my-hostname", []string{"foo", "bar"}, "message") @@ -163,6 +164,11 @@ func TestCheckSenderInterface(t *testing.T) { assert.Equal(t, metrics.MonotonicCountType, monotonicCountSenderSample.metricSample.Mtype) assert.Equal(t, false, monotonicCountSenderSample.commit) + CounterSenderSample := <-senderMetricSampleChan + assert.EqualValues(t, checkID1, CounterSenderSample.id) + assert.Equal(t, metrics.CounterType, CounterSenderSample.metricSample.Mtype) + assert.Equal(t, false, CounterSenderSample.commit) + histoSenderSample := <-senderMetricSampleChan assert.EqualValues(t, checkID1, histoSenderSample.id) assert.Equal(t, metrics.HistogramType, histoSenderSample.metricSample.Mtype) diff --git a/pkg/collector/corechecks/network/common_test.go b/pkg/collector/corechecks/network/common_test.go index 5fff73f9213d..73e37d969e05 100644 --- a/pkg/collector/corechecks/network/common_test.go +++ b/pkg/collector/corechecks/network/common_test.go @@ -37,6 +37,11 @@ func (m *MockSender) MonotonicCount(metric string, value float64, hostname strin m.Called(metric, value, hostname, tags) } +//Counter adds a counter type to the mock calls. +func (m *MockSender) Counter(metric string, value float64, hostname string, tags []string) { + m.Called(metric, value, hostname, tags) +} + //Histogram adds a histogram type to the mock calls. func (m *MockSender) Histogram(metric string, value float64, hostname string, tags []string) { m.Called(metric, value, hostname, tags) diff --git a/pkg/collector/corechecks/system/common_test.go b/pkg/collector/corechecks/system/common_test.go index 66d37163a1ca..cad8ded7c603 100644 --- a/pkg/collector/corechecks/system/common_test.go +++ b/pkg/collector/corechecks/system/common_test.go @@ -37,6 +37,11 @@ func (m *MockSender) MonotonicCount(metric string, value float64, hostname strin m.Called(metric, value, hostname, tags) } +//Counter adds a counter type to the mock calls. +func (m *MockSender) Counter(metric string, value float64, hostname string, tags []string) { + m.Called(metric, value, hostname, tags) +} + //Histogram adds a histogram type to the mock calls. func (m *MockSender) Histogram(metric string, value float64, hostname string, tags []string) { m.Called(metric, value, hostname, tags) diff --git a/pkg/collector/dist/checks/__init__.py b/pkg/collector/dist/checks/__init__.py index e92678c50db3..8d3e76cb2822 100644 --- a/pkg/collector/dist/checks/__init__.py +++ b/pkg/collector/dist/checks/__init__.py @@ -86,8 +86,8 @@ def __init__(self, *args, **kwargs): self._deprecations = { 'increment': [ False, - "DEPRECATION NOTICE: `AgentCheck.increment`/`AgentCheck.decrement` are deprecated, sending these " + - "metrics with `AgentCheck.count` and a '_count' suffix instead", + "DEPRECATION NOTICE: `AgentCheck.increment`/`AgentCheck.decrement` are deprecated, please use " + + "`AgentCheck.gauge` or `AgentCheck.count` instead, with a different metric name", ], 'device_name': [ False, @@ -132,11 +132,11 @@ def historate(self, name, value, tags=None, hostname=None, device_name=None): def increment(self, name, value=1, tags=None, hostname=None, device_name=None): self._log_deprecation("increment") - self.count(name + "_count", value, tags=tags, hostname=hostname, device_name=device_name) + self._submit_metric(aggregator.COUNTER, name, value, tags=tags, hostname=hostname, device_name=device_name) def decrement(self, name, value=-1, tags=None, hostname=None, device_name=None): self._log_deprecation("increment") - self.count(name + "_count", value, tags=tags, hostname=hostname, device_name=device_name) + self._submit_metric(aggregator.COUNTER, name, value, tags=tags, hostname=hostname, device_name=device_name) def _log_deprecation(self, deprecation_key): """ diff --git a/pkg/collector/py/api.c b/pkg/collector/py/api.c index 5a575db3ca79..c5af31b5641e 100644 --- a/pkg/collector/py/api.c +++ b/pkg/collector/py/api.c @@ -10,6 +10,7 @@ char* MetricTypeNames[] = { "RATE", "COUNT", "MONOTONIC_COUNT", + "COUNTER", "HISTOGRAM", "HISTORATE" }; diff --git a/pkg/collector/py/api.go b/pkg/collector/py/api.go index b0d4f808ad68..b6162c61d993 100644 --- a/pkg/collector/py/api.go +++ b/pkg/collector/py/api.go @@ -55,6 +55,8 @@ func SubmitMetric(check *C.PyObject, checkID *C.char, mt C.MetricType, name *C.c sender.Count(_name, _value, _hostname, _tags) case C.MONOTONIC_COUNT: sender.MonotonicCount(_name, _value, _hostname, _tags) + case C.COUNTER: + sender.Counter(_name, _value, _hostname, _tags) case C.HISTOGRAM: sender.Histogram(_name, _value, _hostname, _tags) case C.HISTORATE: diff --git a/pkg/collector/py/api.h b/pkg/collector/py/api.h index 88f8c7f1c9f4..a92f902fa350 100644 --- a/pkg/collector/py/api.h +++ b/pkg/collector/py/api.h @@ -9,6 +9,7 @@ typedef enum { RATE, COUNT, MONOTONIC_COUNT, + COUNTER, HISTOGRAM, HISTORATE, MT_LAST = HISTORATE diff --git a/pkg/collector/py/check_test.go b/pkg/collector/py/check_test.go index 6ce6541b8d5a..d40739c2087e 100644 --- a/pkg/collector/py/check_test.go +++ b/pkg/collector/py/check_test.go @@ -143,6 +143,8 @@ func TestAggregatorLink(t *testing.T) { []string{"foo", "bar"}, "a message").Return().Times(1) mockSender.On("Gauge", "testmetric", mock.AnythingOfType("float64"), "", []string(nil)).Return().Times(1) mockSender.On("Gauge", "testmetricstringvalue", mock.AnythingOfType("float64"), "", []string(nil)).Return().Times(1) + mockSender.On("Counter", "test.increment", 1., "", []string{"foo", "bar"}).Return().Times(1) + mockSender.On("Counter", "test.decrement", -1., "", []string{"foo", "bar", "baz"}).Return().Times(1) mockSender.On("Event", mock.AnythingOfType("metrics.Event")).Return().Times(1) mockSender.On("Commit").Return().Times(1) @@ -166,6 +168,8 @@ func TestAggregatorLinkTwoRuns(t *testing.T) { []string{"foo", "bar"}, "a message").Return().Times(2) mockSender.On("Gauge", "testmetric", mock.AnythingOfType("float64"), "", []string(nil)).Return().Times(2) mockSender.On("Gauge", "testmetricstringvalue", mock.AnythingOfType("float64"), "", []string(nil)).Return().Times(2) + mockSender.On("Counter", "test.increment", 1., "", []string{"foo", "bar"}).Return().Times(2) + mockSender.On("Counter", "test.decrement", -1., "", []string{"foo", "bar", "baz"}).Return().Times(2) mockSender.On("Event", mock.AnythingOfType("metrics.Event")).Return().Times(2) mockSender.On("Commit").Return().Times(2) diff --git a/pkg/collector/py/common_test.go b/pkg/collector/py/common_test.go index 1a17397d9a47..c18bb7906404 100644 --- a/pkg/collector/py/common_test.go +++ b/pkg/collector/py/common_test.go @@ -37,6 +37,11 @@ func (m *MockSender) MonotonicCount(metric string, value float64, hostname strin m.Called(metric, value, hostname, tags) } +//Counter adds a counter type to the mock calls. +func (m *MockSender) Counter(metric string, value float64, hostname string, tags []string) { + m.Called(metric, value, hostname, tags) +} + //Histogram adds a histogram type to the mock calls. func (m *MockSender) Histogram(metric string, value float64, hostname string, tags []string) { m.Called(metric, value, hostname, tags) diff --git a/pkg/collector/py/tests/testaggregator.py b/pkg/collector/py/tests/testaggregator.py index a3e6abd66a72..2a4f47a9ae77 100644 --- a/pkg/collector/py/tests/testaggregator.py +++ b/pkg/collector/py/tests/testaggregator.py @@ -28,6 +28,9 @@ def check(self, instance): else: raise Exception("Expected gauge to raise ValueError") + self.increment("test.increment", tags=['foo', 'bar']) + self.decrement("test.decrement", tags=['foo', 'bar', 'baz']) + self.event({ "event_type": "new.event", "msg_title": "new test event",