Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add option to set Counter to be monotonic #4154

Merged
merged 4 commits into from
Jul 20, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
update tests
  • Loading branch information
axonlmai committed Jul 12, 2021
commit 927c1c9ea0cd30ce8183ec504004b8b5f92b2cc5
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ func TestBuildCounterMetric(t *testing.T) {
labelKeys: []string{"mykey"},
labelValues: []string{"myvalue"},
}
metric := buildCounterMetric(parsedMetric, timeNow)
isMonotonicCounter := false
metric := buildCounterMetric(parsedMetric, isMonotonicCounter, timeNow)
expectedMetrics := pdata.NewInstrumentationLibraryMetrics()
expectedMetric := expectedMetrics.Metrics().AppendEmpty()
expectedMetric.SetName("testCounter")
Expand Down
69 changes: 58 additions & 11 deletions receiver/statsdreceiver/protocol/statsd_parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -651,9 +651,9 @@ func TestStatsDParser_Aggregate(t *testing.T) {
expectedGauges: map[statsDMetricdescription]pdata.InstrumentationLibraryMetrics{},
expectedCounters: map[statsDMetricdescription]pdata.InstrumentationLibraryMetrics{
testDescription("statsdTestMetric1", "c",
[]string{"mykey"}, []string{"myvalue"}): buildCounterMetric(testStatsDMetric("statsdTestMetric1", "", 7000, 0, false, "c", 0, []string{"mykey"}, []string{"myvalue"}), time.Unix(711, 0)),
[]string{"mykey"}, []string{"myvalue"}): buildCounterMetric(testStatsDMetric("statsdTestMetric1", "", 7000, 0, false, "c", 0, []string{"mykey"}, []string{"myvalue"}), false, time.Unix(711, 0)),
testDescription("statsdTestMetric2", "c",
[]string{"mykey"}, []string{"myvalue"}): buildCounterMetric(testStatsDMetric("statsdTestMetric2", "", 50, 0, false, "c", 0, []string{"mykey"}, []string{"myvalue"}), time.Unix(711, 0)),
[]string{"mykey"}, []string{"myvalue"}): buildCounterMetric(testStatsDMetric("statsdTestMetric2", "", 50, 0, false, "c", 0, []string{"mykey"}, []string{"myvalue"}), false, time.Unix(711, 0)),
},
expectedTimer: []pdata.InstrumentationLibraryMetrics{},
},
Expand All @@ -676,9 +676,9 @@ func TestStatsDParser_Aggregate(t *testing.T) {
},
expectedCounters: map[statsDMetricdescription]pdata.InstrumentationLibraryMetrics{
testDescription("statsdTestMetric1", "c",
[]string{"mykey"}, []string{"myvalue"}): buildCounterMetric(testStatsDMetric("statsdTestMetric1", "", 7000, 0, false, "c", 0, []string{"mykey"}, []string{"myvalue"}), time.Unix(711, 0)),
[]string{"mykey"}, []string{"myvalue"}): buildCounterMetric(testStatsDMetric("statsdTestMetric1", "", 7000, 0, false, "c", 0, []string{"mykey"}, []string{"myvalue"}), false, time.Unix(711, 0)),
testDescription("statsdTestMetric2", "c",
[]string{"mykey"}, []string{"myvalue"}): buildCounterMetric(testStatsDMetric("statsdTestMetric2", "", 50, 0, false, "c", 0, []string{"mykey"}, []string{"myvalue"}), time.Unix(711, 0)),
[]string{"mykey"}, []string{"myvalue"}): buildCounterMetric(testStatsDMetric("statsdTestMetric2", "", 50, 0, false, "c", 0, []string{"mykey"}, []string{"myvalue"}), false, time.Unix(711, 0)),
},
expectedTimer: []pdata.InstrumentationLibraryMetrics{},
},
Expand All @@ -704,9 +704,9 @@ func TestStatsDParser_Aggregate(t *testing.T) {
},
expectedCounters: map[statsDMetricdescription]pdata.InstrumentationLibraryMetrics{
testDescription("statsdTestMetric1", "c",
[]string{"mykey"}, []string{"myvalue"}): buildCounterMetric(testStatsDMetric("statsdTestMetric1", "", 215, 0, false, "c", 0, []string{"mykey"}, []string{"myvalue"}), time.Unix(711, 0)),
[]string{"mykey"}, []string{"myvalue"}): buildCounterMetric(testStatsDMetric("statsdTestMetric1", "", 215, 0, false, "c", 0, []string{"mykey"}, []string{"myvalue"}), false, time.Unix(711, 0)),
testDescription("statsdTestMetric2", "c",
[]string{"mykey"}, []string{"myvalue"}): buildCounterMetric(testStatsDMetric("statsdTestMetric2", "", 75, 0, false, "c", 0, []string{"mykey"}, []string{"myvalue"}), time.Unix(711, 0)),
[]string{"mykey"}, []string{"myvalue"}): buildCounterMetric(testStatsDMetric("statsdTestMetric2", "", 75, 0, false, "c", 0, []string{"mykey"}, []string{"myvalue"}), false, time.Unix(711, 0)),
},
expectedTimer: []pdata.InstrumentationLibraryMetrics{},
},
Expand All @@ -732,7 +732,7 @@ func TestStatsDParser_Aggregate(t *testing.T) {
t.Run(tt.name, func(t *testing.T) {
var err error
p := &StatsDParser{}
p.Initialize(false, []TimerHistogramMapping{{StatsdType: "timer", ObserverType: "gauge"}, {StatsdType: "histogram", ObserverType: "gauge"}})
p.Initialize(false, false, []TimerHistogramMapping{{StatsdType: "timer", ObserverType: "gauge"}, {StatsdType: "histogram", ObserverType: "gauge"}})
for _, line := range tt.input {
err = p.Aggregate(line)
}
Expand Down Expand Up @@ -790,17 +790,64 @@ func TestStatsDParser_AggregateWithMetricType(t *testing.T) {
expectedGauges: map[statsDMetricdescription]pdata.InstrumentationLibraryMetrics{},
expectedCounters: map[statsDMetricdescription]pdata.InstrumentationLibraryMetrics{
testDescription("statsdTestMetric1", "c",
[]string{"mykey", "metric_type"}, []string{"myvalue", "counter"}): buildCounterMetric(testStatsDMetric("statsdTestMetric1", "", 7000, 0, false, "c", 0, []string{"mykey", "metric_type"}, []string{"myvalue", "counter"}), time.Unix(711, 0)),
[]string{"mykey", "metric_type"}, []string{"myvalue", "counter"}): buildCounterMetric(testStatsDMetric("statsdTestMetric1", "", 7000, 0, false, "c", 0, []string{"mykey", "metric_type"}, []string{"myvalue", "counter"}), false, time.Unix(711, 0)),
testDescription("statsdTestMetric2", "c",
[]string{"mykey", "metric_type"}, []string{"myvalue", "counter"}): buildCounterMetric(testStatsDMetric("statsdTestMetric2", "", 50, 0, false, "c", 0, []string{"mykey", "metric_type"}, []string{"myvalue", "counter"}), time.Unix(711, 0)),
[]string{"mykey", "metric_type"}, []string{"myvalue", "counter"}): buildCounterMetric(testStatsDMetric("statsdTestMetric2", "", 50, 0, false, "c", 0, []string{"mykey", "metric_type"}, []string{"myvalue", "counter"}), false, time.Unix(711, 0)),
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
var err error
p := &StatsDParser{}
p.Initialize(true, []TimerHistogramMapping{{StatsdType: "timer", ObserverType: "gauge"}, {StatsdType: "histogram", ObserverType: "gauge"}})
p.Initialize(true, false, []TimerHistogramMapping{{StatsdType: "timer", ObserverType: "gauge"}, {StatsdType: "histogram", ObserverType: "gauge"}})
for _, line := range tt.input {
err = p.Aggregate(line)
}
if tt.err != nil {
assert.Equal(t, tt.err, err)
} else {
assert.Equal(t, tt.expectedGauges, p.gauges)
assert.Equal(t, tt.expectedCounters, p.counters)
}
})
}
}

func TestStatsDParser_AggregateWithIsMonotonicCounter(t *testing.T) {
timeNowFunc = func() time.Time {
return time.Unix(711, 0)
}

tests := []struct {
name string
input []string
expectedGauges map[statsDMetricdescription]pdata.InstrumentationLibraryMetrics
expectedCounters map[statsDMetricdescription]pdata.InstrumentationLibraryMetrics
err error
}{
{
name: "counter with increment and sample rate",
input: []string{
"statsdTestMetric1:3000|c|#mykey:myvalue",
"statsdTestMetric1:4000|c|#mykey:myvalue",
"statsdTestMetric2:20|c|@0.8|#mykey:myvalue",
"statsdTestMetric2:20|c|@0.8|#mykey:myvalue",
},
expectedGauges: map[statsDMetricdescription]pdata.InstrumentationLibraryMetrics{},
expectedCounters: map[statsDMetricdescription]pdata.InstrumentationLibraryMetrics{
testDescription("statsdTestMetric1", "c",
[]string{"mykey", "metric_type"}, []string{"myvalue", "counter"}): buildCounterMetric(testStatsDMetric("statsdTestMetric1", "", 7000, 0, false, "c", 0, []string{"mykey", "metric_type"}, []string{"myvalue", "counter"}), true, time.Unix(711, 0)),
testDescription("statsdTestMetric2", "c",
[]string{"mykey", "metric_type"}, []string{"myvalue", "counter"}): buildCounterMetric(testStatsDMetric("statsdTestMetric2", "", 50, 0, false, "c", 0, []string{"mykey", "metric_type"}, []string{"myvalue", "counter"}), true, time.Unix(711, 0)),
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
var err error
p := &StatsDParser{}
p.Initialize(false, true, []TimerHistogramMapping{{StatsdType: "timer", ObserverType: "gauge"}, {StatsdType: "histogram", ObserverType: "gauge"}})
for _, line := range tt.input {
err = p.Aggregate(line)
}
Expand Down Expand Up @@ -891,7 +938,7 @@ func TestStatsDParser_AggregateTimmerWithSummary(t *testing.T) {
t.Run(tt.name, func(t *testing.T) {
var err error
p := &StatsDParser{}
p.Initialize(false, []TimerHistogramMapping{{StatsdType: "timer", ObserverType: "summary"}, {StatsdType: "histogram", ObserverType: "summary"}})
p.Initialize(false, false, []TimerHistogramMapping{{StatsdType: "timer", ObserverType: "summary"}, {StatsdType: "histogram", ObserverType: "summary"}})
for _, line := range tt.input {
err = p.Aggregate(line)
}
Expand Down