Skip to content

Commit

Permalink
fix(serializers.splunkmetric): put event metric tag behind option
Browse files Browse the repository at this point in the history
  • Loading branch information
powersj committed Oct 14, 2022
1 parent ab293e8 commit 5ddac0c
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 17 deletions.
9 changes: 6 additions & 3 deletions plugins/serializers/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,9 @@ type Config struct {
// Enable Splunk MultiMetric output (Splunk 8.0+)
SplunkmetricMultiMetric bool `toml:"splunkmetric_multi_metric"`

// Enable Splunk Event "metric" tag
SplunkmetricEventTag bool `toml:"splunkmetric_event_tag"`

// Point tags to use as the source name for Wavefront (if none found, host will be used).
WavefrontSourceOverride []string `toml:"wavefront_source_override"`

Expand Down Expand Up @@ -149,7 +152,7 @@ func NewSerializer(config *Config) (Serializer, error) {
case "json":
serializer, err = NewJSONSerializer(config.TimestampUnits, config.TimestampFormat, config.Transformation)
case "splunkmetric":
serializer, err = NewSplunkmetricSerializer(config.HecRouting, config.SplunkmetricMultiMetric)
serializer, err = NewSplunkmetricSerializer(config.HecRouting, config.SplunkmetricMultiMetric, config.SplunkmetricEventTag)
case "nowmetric":
serializer, err = NewNowSerializer()
case "carbon2":
Expand Down Expand Up @@ -225,8 +228,8 @@ func NewCarbon2Serializer(carbon2format string, carbon2SanitizeReplaceChar strin
return carbon2.NewSerializer(carbon2format, carbon2SanitizeReplaceChar)
}

func NewSplunkmetricSerializer(splunkmetricHecRouting bool, splunkmetricMultimetric bool) (Serializer, error) {
return splunkmetric.NewSerializer(splunkmetricHecRouting, splunkmetricMultimetric)
func NewSplunkmetricSerializer(splunkmetricHecRouting bool, splunkmetricMultimetric bool, splunkmetricEventTag bool) (Serializer, error) {
return splunkmetric.NewSerializer(splunkmetricHecRouting, splunkmetricMultimetric, splunkmetricEventTag)
}

func NewNowSerializer() (Serializer, error) {
Expand Down
12 changes: 9 additions & 3 deletions plugins/serializers/splunkmetric/splunkmetric.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
type serializer struct {
HecRouting bool
SplunkmetricMultiMetric bool
EventTag bool
}

type CommonTags struct {
Expand All @@ -30,11 +31,12 @@ type HECTimeSeries struct {
}

// NewSerializer Setup our new serializer
func NewSerializer(splunkmetricHecRouting bool, splunkmetricMultimetric bool) (*serializer, error) {
func NewSerializer(splunkmetricHecRouting bool, splunkmetricMultimetric bool, splunkmetricEventTag bool) (*serializer, error) {
/* Define output params */
s := &serializer{
HecRouting: splunkmetricHecRouting,
SplunkmetricMultiMetric: splunkmetricMultimetric,
EventTag: splunkmetricEventTag,
}
return s, nil
}
Expand Down Expand Up @@ -66,7 +68,9 @@ func (s *serializer) createMulti(metric telegraf.Metric, dataGroup HECTimeSeries
var metricJSON []byte

// Set the event data from the commonTags above.
dataGroup.Event = "metric"
if s.EventTag {
dataGroup.Event = "metric"
}
dataGroup.Time = commonTags.Time
dataGroup.Host = commonTags.Host
dataGroup.Index = commonTags.Index
Expand Down Expand Up @@ -123,7 +127,9 @@ func (s *serializer) createSingle(metric telegraf.Metric, dataGroup HECTimeSerie
dataGroup.Time = commonTags.Time

// Apply the common tags from above to every record.
dataGroup.Event = "metric"
if s.EventTag {
dataGroup.Event = "metric"
}
dataGroup.Host = commonTags.Host
dataGroup.Index = commonTags.Index
dataGroup.Source = commonTags.Source
Expand Down
22 changes: 11 additions & 11 deletions plugins/serializers/splunkmetric/splunkmetric_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ func TestSerializeMetricFloat(t *testing.T) {
}
m := metric.New("cpu", tags, fields, now)

s, _ := NewSerializer(false, false)
s, _ := NewSerializer(false, false, false)
var buf []byte
buf, err := s.Serialize(m)
require.NoError(t, err)
Expand All @@ -40,7 +40,7 @@ func TestSerializeMetricFloatHec(t *testing.T) {
}
m := metric.New("cpu", tags, fields, now)

s, _ := NewSerializer(true, false)
s, _ := NewSerializer(true, false, true)
var buf []byte
buf, err := s.Serialize(m)
require.NoError(t, err)
Expand All @@ -58,7 +58,7 @@ func TestSerializeMetricInt(t *testing.T) {
}
m := metric.New("cpu", tags, fields, now)

s, _ := NewSerializer(false, false)
s, _ := NewSerializer(false, false, false)
var buf []byte
buf, err := s.Serialize(m)
require.NoError(t, err)
Expand All @@ -77,7 +77,7 @@ func TestSerializeMetricIntHec(t *testing.T) {
}
m := metric.New("cpu", tags, fields, now)

s, _ := NewSerializer(true, false)
s, _ := NewSerializer(true, false, true)
var buf []byte
buf, err := s.Serialize(m)
require.NoError(t, err)
Expand All @@ -96,7 +96,7 @@ func TestSerializeMetricBool(t *testing.T) {
}
m := metric.New("docker", tags, fields, now)

s, _ := NewSerializer(false, false)
s, _ := NewSerializer(false, false, false)
var buf []byte
buf, err := s.Serialize(m)
require.NoError(t, err)
Expand All @@ -115,7 +115,7 @@ func TestSerializeMetricBoolHec(t *testing.T) {
}
m := metric.New("docker", tags, fields, now)

s, _ := NewSerializer(true, false)
s, _ := NewSerializer(true, false, true)
var buf []byte
buf, err := s.Serialize(m)
require.NoError(t, err)
Expand All @@ -135,7 +135,7 @@ func TestSerializeMetricString(t *testing.T) {
}
m := metric.New("cpu", tags, fields, now)

s, _ := NewSerializer(false, false)
s, _ := NewSerializer(false, false, false)
var buf []byte
buf, err := s.Serialize(m)
require.NoError(t, err)
Expand Down Expand Up @@ -165,7 +165,7 @@ func TestSerializeBatch(t *testing.T) {
)

metrics := []telegraf.Metric{m, n}
s, _ := NewSerializer(false, false)
s, _ := NewSerializer(false, false, false)
buf, err := s.SerializeBatch(metrics)
require.NoError(t, err)

Expand All @@ -185,7 +185,7 @@ func TestSerializeMulti(t *testing.T) {
)

metrics := []telegraf.Metric{m}
s, _ := NewSerializer(false, true)
s, _ := NewSerializer(false, true, false)
buf, err := s.SerializeBatch(metrics)
require.NoError(t, err)

Expand All @@ -211,7 +211,7 @@ func TestSerializeBatchHec(t *testing.T) {
time.Unix(0, 0),
)
metrics := []telegraf.Metric{m, n}
s, _ := NewSerializer(true, false)
s, _ := NewSerializer(true, false, true)
buf, err := s.SerializeBatch(metrics)
require.NoError(t, err)

Expand All @@ -231,7 +231,7 @@ func TestSerializeMultiHec(t *testing.T) {
)

metrics := []telegraf.Metric{m}
s, _ := NewSerializer(true, true)
s, _ := NewSerializer(true, true, true)
buf, err := s.SerializeBatch(metrics)
require.NoError(t, err)

Expand Down

0 comments on commit 5ddac0c

Please sign in to comment.