#8328 Fixed a bug with the state map in Dynatrace Plugin (#8329)

This commit is contained in:
Thomas Schuetz 2020-10-29 16:04:11 +01:00 committed by GitHub
parent 5f5e87b596
commit a49e37a2a6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 17 additions and 15 deletions

View File

@ -28,16 +28,15 @@ var (
maxMetricKeyLen = 250
)
var counts map[string]string
var sent = 0
// Dynatrace Configuration for the Dynatrace output plugin
type Dynatrace struct {
URL string `toml:"url"`
APIToken string `toml:"api_token"`
Prefix string `toml:"prefix"`
Log telegraf.Logger `toml:"-"`
Timeout internal.Duration `toml:"timeout"`
URL string `toml:"url"`
APIToken string `toml:"api_token"`
Prefix string `toml:"prefix"`
Log telegraf.Logger `toml:"-"`
Timeout internal.Duration `toml:"timeout"`
State map[string]string
SendCounter int
tls.ClientConfig
@ -117,6 +116,8 @@ func (d *Dynatrace) normalize(s string, max int) (string, error) {
normalizedString = normalizedString[:len(normalizedString)-1]
}
normalizedString = strings.ReplaceAll(normalizedString, "..", "_")
if len(normalizedString) == 0 {
return "", fmt.Errorf("error normalizing the string: %s", s)
}
@ -198,7 +199,7 @@ func (d *Dynatrace) Write(metrics []telegraf.Metric) error {
var delta float64 = 0
// Check if LastValue exists
if lastvalue, ok := counts[metricID+tagb.String()]; ok {
if lastvalue, ok := d.State[metricID+tagb.String()]; ok {
// Convert Strings to Floats
floatLastValue, err := strconv.ParseFloat(lastvalue, 32)
if err != nil {
@ -213,7 +214,7 @@ func (d *Dynatrace) Write(metrics []telegraf.Metric) error {
fmt.Fprintf(&buf, "%s%s count,delta=%f\n", metricID, tagb.String(), delta)
}
}
counts[metricID+tagb.String()] = value
d.State[metricID+tagb.String()] = value
default:
fmt.Fprintf(&buf, "%s%s %v\n", metricID, tagb.String(), value)
@ -221,11 +222,11 @@ func (d *Dynatrace) Write(metrics []telegraf.Metric) error {
}
}
}
sent++
d.SendCounter++
// 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)
if d.SendCounter%8640 == 0 {
d.State = make(map[string]string)
}
return d.send(buf.Bytes())
}
@ -269,7 +270,7 @@ func (d *Dynatrace) send(msg []byte) error {
}
func (d *Dynatrace) Init() error {
counts = make(map[string]string)
d.State = make(map[string]string)
if len(d.URL) == 0 {
d.Log.Infof("Dynatrace URL is empty, defaulting to OneAgent metrics interface")
d.URL = oneAgentMetricsUrl
@ -297,7 +298,8 @@ func (d *Dynatrace) Init() error {
func init() {
outputs.Add("dynatrace", func() telegraf.Output {
return &Dynatrace{
Timeout: internal.Duration{Duration: time.Second * 5},
Timeout: internal.Duration{Duration: time.Second * 5},
SendCounter: 0,
}
})
}