diff --git a/plugins/outputs/dynatrace/README.md b/plugins/outputs/dynatrace/README.md index 78a7ee4990067..ea4b42777752d 100644 --- a/plugins/outputs/dynatrace/README.md +++ b/plugins/outputs/dynatrace/README.md @@ -1,6 +1,6 @@ # Dynatrace Output Plugin -This plugin is sending telegraf metrics to [Dynatrace](www.dynatrace.com). It has two operational modes. +This plugin is sending telegraf metrics to [Dynatrace](https://www.dynatrace.com). It has two operational modes. Telegraf minimum version: Telegraf 1.16 Plugin minimum tested version: 1.16 diff --git a/plugins/outputs/dynatrace/dynatrace.go b/plugins/outputs/dynatrace/dynatrace.go index 8c8fa984d82a8..596366ae8470f 100644 --- a/plugins/outputs/dynatrace/dynatrace.go +++ b/plugins/outputs/dynatrace/dynatrace.go @@ -195,17 +195,26 @@ func (d *Dynatrace) Write(metrics []telegraf.Metric) error { // write metric id,tags and value switch metric.Type() { case telegraf.Counter: + var delta float64 = 0 + + // Check if LastValue exists if lastvalue, ok := counts[metricID+tagb.String()]; ok { - // only send a counter if a lastvalue is found in the map - // if last value is found we can calc and send the delta value - if v, err := strconv.ParseFloat(lastvalue, 32); err == nil { - if v2, err := strconv.ParseFloat(value, 32); err == nil { - fmt.Fprintf(&buf, "%s%s count,delta=%f\n", metricID, tagb.String(), v2-v) - } + // Convert Strings to Floats + floatLastValue, err := strconv.ParseFloat(lastvalue, 32) + if err != nil { + d.Log.Debugf("Could not parse last value: %s", lastvalue) + } + floatCurrentValue, err := strconv.ParseFloat(value, 32) + if err != nil { + d.Log.Debugf("Could not parse current value: %s", value) + } + if floatCurrentValue > floatLastValue { + delta = floatCurrentValue - floatLastValue + fmt.Fprintf(&buf, "%s%s count,delta=%f\n", metricID, tagb.String(), delta) } } - // put the current value into the map as last value counts[metricID+tagb.String()] = value + default: fmt.Fprintf(&buf, "%s%s %v\n", metricID, tagb.String(), value) } @@ -214,6 +223,7 @@ func (d *Dynatrace) Write(metrics []telegraf.Metric) error { } sent++ // in typical interval of 10s, we will clean the counter state once in 24h which is 8640 iterations + if sent%8640 == 0 { counts = make(map[string]string) }