Skip to content

Commit c808463

Browse files
committed
Use non-zero rate_interval to enable rate logic
1 parent ab1fa87 commit c808463

File tree

3 files changed

+313
-19
lines changed

3 files changed

+313
-19
lines changed

plugins/outputs/datadog/datadog.go

+5-6
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ type Datadog struct {
2929
Timeout config.Duration `toml:"timeout"`
3030
URL string `toml:"url"`
3131
Compression string `toml:"compression"`
32-
ShouldRateCounts bool `toml:"should_rate_counts"`
3332
RateInterval int64 `toml:"rate_interval"`
3433
Log telegraf.Logger `toml:"-"`
3534

@@ -85,7 +84,8 @@ func (d *Datadog) convertToDatadogMetric(metrics []telegraf.Metric) ([]*Metric,
8584
if dogMs, err := buildMetrics(m); err == nil {
8685
metricTags := buildTags(m.TagList())
8786
host, _ := m.GetTag("host")
88-
metricType, _ := m.GetTag("metric_type")
87+
// Retrieve the metric_type tag created by inputs.statsd
88+
statsDMetricType, _ := m.GetTag("metric_type")
8989

9090
if len(dogMs) == 0 {
9191
continue
@@ -105,7 +105,7 @@ func (d *Datadog) convertToDatadogMetric(metrics []telegraf.Metric) ([]*Metric,
105105
interval = 1
106106
switch m.Type() {
107107
case telegraf.Counter, telegraf.Untyped:
108-
if d.ShouldRateCounts && isRateable(metricType, fieldName) {
108+
if d.RateInterval > 0 && isRateable(statsDMetricType, fieldName) {
109109
interval = d.RateInterval
110110
dogM[1] = dogM[1] / float64(interval)
111111
tname = "rate"
@@ -238,8 +238,8 @@ func verifyValue(v interface{}) bool {
238238
return true
239239
}
240240

241-
func isRateable(metricType string, fieldName string) bool {
242-
switch metricType {
241+
func isRateable(statsDMetricType string, fieldName string) bool {
242+
switch statsDMetricType {
243243
case
244244
"counter":
245245
return true
@@ -280,7 +280,6 @@ func init() {
280280
return &Datadog{
281281
URL: datadogAPI,
282282
Compression: "none",
283-
RateInterval: 10,
284283
}
285284
})
286285
}

plugins/outputs/datadog/datadog_test.go

+302-6
Original file line numberDiff line numberDiff line change
@@ -306,11 +306,10 @@ func TestInfIsSkipped(t *testing.T) {
306306
require.NoError(t, err)
307307
}
308308

309-
func TestShouldRateCount(t *testing.T) {
309+
func TestNonZeroRateIntervalConvertsRatesToCount(t *testing.T) {
310310
d := &Datadog{
311-
Apikey: "123456",
312-
ShouldRateCounts: true,
313-
RateInterval: 10,
311+
Apikey: "123456",
312+
RateInterval: 10,
314313
}
315314

316315
var tests = []struct {
@@ -322,7 +321,7 @@ func TestShouldRateCount(t *testing.T) {
322321
"convert counter metrics to rate",
323322
[]telegraf.Metric{
324323
testutil.MustMetric(
325-
"count_metric_converted_to_rate",
324+
"count_metric",
326325
map[string]string{
327326
"metric_type": "counter",
328327
},
@@ -335,7 +334,7 @@ func TestShouldRateCount(t *testing.T) {
335334
},
336335
[]*Metric{
337336
{
338-
Metric: "count_metric_converted_to_rate",
337+
Metric: "count_metric",
339338
Points: [1]Point{
340339
{
341340
float64(time.Date(2009, time.November, 10, 23, 0, 0, 0, time.UTC).Unix()),
@@ -604,3 +603,300 @@ func TestShouldRateCount(t *testing.T) {
604603
})
605604
}
606605
}
606+
607+
func TestZeroRateIntervalConvertsRatesToCount(t *testing.T) {
608+
d := &Datadog{
609+
Apikey: "123456",
610+
}
611+
612+
var tests = []struct {
613+
name string
614+
metricsIn []telegraf.Metric
615+
metricsOut []*Metric
616+
}{
617+
{
618+
"does not convert counter metrics to rate",
619+
[]telegraf.Metric{
620+
testutil.MustMetric(
621+
"count_metric",
622+
map[string]string{
623+
"metric_type": "counter",
624+
},
625+
map[string]interface{}{
626+
"value": 100,
627+
},
628+
time.Date(2009, time.November, 10, 23, 0, 0, 0, time.UTC),
629+
telegraf.Counter,
630+
),
631+
},
632+
[]*Metric{
633+
{
634+
Metric: "count_metric",
635+
Points: [1]Point{
636+
{
637+
float64(time.Date(2009, time.November, 10, 23, 0, 0, 0, time.UTC).Unix()),
638+
100,
639+
},
640+
},
641+
Type: "count",
642+
Tags: []string{
643+
"metric_type:counter",
644+
},
645+
Interval: 1,
646+
},
647+
},
648+
},
649+
{
650+
"does not convert count value in timing metrics to rate",
651+
[]telegraf.Metric{
652+
testutil.MustMetric(
653+
"timing_metric",
654+
map[string]string{
655+
"metric_type": "timing",
656+
},
657+
map[string]interface{}{
658+
"count": 1,
659+
"lower": float64(10),
660+
"mean": float64(10),
661+
"median": float64(10),
662+
"stddev": float64(0),
663+
"sum": float64(10),
664+
"upper": float64(10),
665+
},
666+
time.Date(2009, time.November, 10, 23, 0, 0, 0, time.UTC),
667+
telegraf.Untyped,
668+
),
669+
},
670+
[]*Metric{
671+
{
672+
Metric: "timing_metric.count",
673+
Points: [1]Point{
674+
{
675+
float64(time.Date(2009, time.November, 10, 23, 0, 0, 0, time.UTC).Unix()),
676+
1,
677+
},
678+
},
679+
Type: "",
680+
Tags: []string{
681+
"metric_type:timing",
682+
},
683+
Interval: 1,
684+
},
685+
{
686+
Metric: "timing_metric.lower",
687+
Points: [1]Point{
688+
{
689+
float64(time.Date(2009, time.November, 10, 23, 0, 0, 0, time.UTC).Unix()),
690+
float64(10),
691+
},
692+
},
693+
Type: "",
694+
Tags: []string{
695+
"metric_type:timing",
696+
},
697+
Interval: 1,
698+
},
699+
{
700+
Metric: "timing_metric.mean",
701+
Points: [1]Point{
702+
{
703+
float64(time.Date(2009, time.November, 10, 23, 0, 0, 0, time.UTC).Unix()),
704+
float64(10),
705+
},
706+
},
707+
Type: "",
708+
Tags: []string{
709+
"metric_type:timing",
710+
},
711+
Interval: 1,
712+
},
713+
{
714+
Metric: "timing_metric.median",
715+
Points: [1]Point{
716+
{
717+
float64(time.Date(2009, time.November, 10, 23, 0, 0, 0, time.UTC).Unix()),
718+
float64(10),
719+
},
720+
},
721+
Type: "",
722+
Tags: []string{
723+
"metric_type:timing",
724+
},
725+
Interval: 1,
726+
},
727+
{
728+
Metric: "timing_metric.stddev",
729+
Points: [1]Point{
730+
{
731+
float64(time.Date(2009, time.November, 10, 23, 0, 0, 0, time.UTC).Unix()),
732+
float64(0),
733+
},
734+
},
735+
Type: "",
736+
Tags: []string{
737+
"metric_type:timing",
738+
},
739+
Interval: 1,
740+
},
741+
{
742+
Metric: "timing_metric.sum",
743+
Points: [1]Point{
744+
{
745+
float64(time.Date(2009, time.November, 10, 23, 0, 0, 0, time.UTC).Unix()),
746+
float64(10),
747+
},
748+
},
749+
Type: "",
750+
Tags: []string{
751+
"metric_type:timing",
752+
},
753+
Interval: 1,
754+
},
755+
{
756+
Metric: "timing_metric.upper",
757+
Points: [1]Point{
758+
{
759+
float64(time.Date(2009, time.November, 10, 23, 0, 0, 0, time.UTC).Unix()),
760+
float64(10),
761+
},
762+
},
763+
Type: "",
764+
Tags: []string{
765+
"metric_type:timing",
766+
},
767+
Interval: 1,
768+
},
769+
},
770+
},
771+
{
772+
"does not convert count value in histogram metrics to rate",
773+
[]telegraf.Metric{
774+
testutil.MustMetric(
775+
"histogram_metric",
776+
map[string]string{
777+
"metric_type": "histogram",
778+
},
779+
map[string]interface{}{
780+
"count": 1,
781+
"lower": float64(10),
782+
"mean": float64(10),
783+
"median": float64(10),
784+
"stddev": float64(0),
785+
"sum": float64(10),
786+
"upper": float64(10),
787+
},
788+
time.Date(2009, time.November, 10, 23, 0, 0, 0, time.UTC),
789+
telegraf.Untyped,
790+
),
791+
},
792+
[]*Metric{
793+
{
794+
Metric: "histogram_metric.count",
795+
Points: [1]Point{
796+
{
797+
float64(time.Date(2009, time.November, 10, 23, 0, 0, 0, time.UTC).Unix()),
798+
1,
799+
},
800+
},
801+
Type: "",
802+
Tags: []string{
803+
"metric_type:histogram",
804+
},
805+
Interval: 1,
806+
},
807+
{
808+
Metric: "histogram_metric.lower",
809+
Points: [1]Point{
810+
{
811+
float64(time.Date(2009, time.November, 10, 23, 0, 0, 0, time.UTC).Unix()),
812+
float64(10),
813+
},
814+
},
815+
Type: "",
816+
Tags: []string{
817+
"metric_type:histogram",
818+
},
819+
Interval: 1,
820+
},
821+
{
822+
Metric: "histogram_metric.mean",
823+
Points: [1]Point{
824+
{
825+
float64(time.Date(2009, time.November, 10, 23, 0, 0, 0, time.UTC).Unix()),
826+
float64(10),
827+
},
828+
},
829+
Type: "",
830+
Tags: []string{
831+
"metric_type:histogram",
832+
},
833+
Interval: 1,
834+
},
835+
{
836+
Metric: "histogram_metric.median",
837+
Points: [1]Point{
838+
{
839+
float64(time.Date(2009, time.November, 10, 23, 0, 0, 0, time.UTC).Unix()),
840+
float64(10),
841+
},
842+
},
843+
Type: "",
844+
Tags: []string{
845+
"metric_type:histogram",
846+
},
847+
Interval: 1,
848+
},
849+
{
850+
Metric: "histogram_metric.stddev",
851+
Points: [1]Point{
852+
{
853+
float64(time.Date(2009, time.November, 10, 23, 0, 0, 0, time.UTC).Unix()),
854+
float64(0),
855+
},
856+
},
857+
Type: "",
858+
Tags: []string{
859+
"metric_type:histogram",
860+
},
861+
Interval: 1,
862+
},
863+
{
864+
Metric: "histogram_metric.sum",
865+
Points: [1]Point{
866+
{
867+
float64(time.Date(2009, time.November, 10, 23, 0, 0, 0, time.UTC).Unix()),
868+
float64(10),
869+
},
870+
},
871+
Type: "",
872+
Tags: []string{
873+
"metric_type:histogram",
874+
},
875+
Interval: 1,
876+
},
877+
{
878+
Metric: "histogram_metric.upper",
879+
Points: [1]Point{
880+
{
881+
float64(time.Date(2009, time.November, 10, 23, 0, 0, 0, time.UTC).Unix()),
882+
float64(10),
883+
},
884+
},
885+
Type: "",
886+
Tags: []string{
887+
"metric_type:histogram",
888+
},
889+
Interval: 1,
890+
},
891+
},
892+
},
893+
}
894+
895+
for _, tt := range tests {
896+
t.Run(tt.name, func(t *testing.T) {
897+
actualMetricsOut, actualLen := d.convertToDatadogMetric(tt.metricsIn)
898+
require.Len(t, actualMetricsOut, actualLen)
899+
require.ElementsMatch(t, tt.metricsOut, actualMetricsOut)
900+
})
901+
}
902+
}

plugins/outputs/datadog/sample.conf

+6-7
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,9 @@
1919
## Supports: "zlib", "none"
2020
# compression = "none"
2121

22-
## Convert counts to rates
23-
## Use this to be able to submit metrics from telegraf alongside Datadog agent
24-
# should_rate_counts = true
25-
26-
## When should_rate_counts is enabled, this overrides the
27-
## default (10s) rate interval used to divide count metrics by
28-
# rate_interval = 20
22+
## When non-zero, converts count metrics submitted by inputs.statsd
23+
## into rate, while dividing the metric value by this number.
24+
## Note that in order for metrics to be submitted simultaenously alongside
25+
## a Datadog agent, rate_interval has to match the interval used by the
26+
## agent - which defaults to 10
27+
# rate_interval = 0

0 commit comments

Comments
 (0)