Skip to content

Commit

Permalink
Dynatrace Output Plugin: Fixed behaviour when state map is cleared (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
thschue authored Oct 16, 2020
1 parent 7c2c2c5 commit c85fb58
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 8 deletions.
2 changes: 1 addition & 1 deletion plugins/outputs/dynatrace/README.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
24 changes: 17 additions & 7 deletions plugins/outputs/dynatrace/dynatrace.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand All @@ -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)
}
Expand Down

0 comments on commit c85fb58

Please sign in to comment.