Skip to content

Commit c85fb58

Browse files
authored
Dynatrace Output Plugin: Fixed behaviour when state map is cleared (#8251)
1 parent 7c2c2c5 commit c85fb58

2 files changed

Lines changed: 18 additions & 8 deletions

File tree

plugins/outputs/dynatrace/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Dynatrace Output Plugin
22

3-
This plugin is sending telegraf metrics to [Dynatrace](www.dynatrace.com). It has two operational modes.
3+
This plugin is sending telegraf metrics to [Dynatrace](https://www.dynatrace.com). It has two operational modes.
44

55
Telegraf minimum version: Telegraf 1.16
66
Plugin minimum tested version: 1.16

plugins/outputs/dynatrace/dynatrace.go

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -195,17 +195,26 @@ func (d *Dynatrace) Write(metrics []telegraf.Metric) error {
195195
// write metric id,tags and value
196196
switch metric.Type() {
197197
case telegraf.Counter:
198+
var delta float64 = 0
199+
200+
// Check if LastValue exists
198201
if lastvalue, ok := counts[metricID+tagb.String()]; ok {
199-
// only send a counter if a lastvalue is found in the map
200-
// if last value is found we can calc and send the delta value
201-
if v, err := strconv.ParseFloat(lastvalue, 32); err == nil {
202-
if v2, err := strconv.ParseFloat(value, 32); err == nil {
203-
fmt.Fprintf(&buf, "%s%s count,delta=%f\n", metricID, tagb.String(), v2-v)
204-
}
202+
// Convert Strings to Floats
203+
floatLastValue, err := strconv.ParseFloat(lastvalue, 32)
204+
if err != nil {
205+
d.Log.Debugf("Could not parse last value: %s", lastvalue)
206+
}
207+
floatCurrentValue, err := strconv.ParseFloat(value, 32)
208+
if err != nil {
209+
d.Log.Debugf("Could not parse current value: %s", value)
210+
}
211+
if floatCurrentValue > floatLastValue {
212+
delta = floatCurrentValue - floatLastValue
213+
fmt.Fprintf(&buf, "%s%s count,delta=%f\n", metricID, tagb.String(), delta)
205214
}
206215
}
207-
// put the current value into the map as last value
208216
counts[metricID+tagb.String()] = value
217+
209218
default:
210219
fmt.Fprintf(&buf, "%s%s %v\n", metricID, tagb.String(), value)
211220
}
@@ -214,6 +223,7 @@ func (d *Dynatrace) Write(metrics []telegraf.Metric) error {
214223
}
215224
sent++
216225
// in typical interval of 10s, we will clean the counter state once in 24h which is 8640 iterations
226+
217227
if sent%8640 == 0 {
218228
counts = make(map[string]string)
219229
}

0 commit comments

Comments
 (0)