@@ -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