Skip to content

Commit

Permalink
fix(serializers.splunkmetric): omit event metric tag with option
Browse files Browse the repository at this point in the history
  • Loading branch information
powersj committed Oct 27, 2022
1 parent 7466834 commit 4e43a8e
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 19 deletions.
3 changes: 2 additions & 1 deletion config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -1179,6 +1179,7 @@ func (c *Config) buildSerializer(tbl *ast.Table) (serializers.Serializer, error)

c.getFieldBool(tbl, "splunkmetric_hec_routing", &sc.HecRouting)
c.getFieldBool(tbl, "splunkmetric_multimetric", &sc.SplunkmetricMultiMetric)
c.getFieldBool(tbl, "splunkmetric_omit_event_tag", &sc.SplunkmetricOmitEventTag)

c.getFieldStringSlice(tbl, "wavefront_source_override", &sc.WavefrontSourceOverride)
c.getFieldBool(tbl, "wavefront_use_strict", &sc.WavefrontUseStrict)
Expand Down Expand Up @@ -1257,7 +1258,7 @@ func (c *Config) missingTomlField(_ reflect.Type, key string) error {
"json_timestamp_format", "json_timestamp_units", "json_transformation",
"prometheus_export_timestamp", "prometheus_sort_metrics", "prometheus_string_as_label",
"prometheus_compact_encoding",
"splunkmetric_hec_routing", "splunkmetric_multimetric",
"splunkmetric_hec_routing", "splunkmetric_multimetric", "splunkmetric_omit_event_tag",
"wavefront_disable_prefix_conversion", "wavefront_source_override", "wavefront_use_strict":
default:
c.unusedFieldsMutex.Lock()
Expand Down
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"`

// Omit the Splunk Event "metric" tag
SplunkmetricOmitEventTag bool `toml:"splunkmetric_omit_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.SplunkmetricOmitEventTag)
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, splunkmetricOmitEventTag bool) (Serializer, error) {
return splunkmetric.NewSerializer(splunkmetricHecRouting, splunkmetricMultimetric, splunkmetricOmitEventTag)
}

func NewNowSerializer() (Serializer, error) {
Expand Down
14 changes: 10 additions & 4 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
OmitEventTag bool
}

type CommonTags struct {
Expand All @@ -22,19 +23,20 @@ type CommonTags struct {

type HECTimeSeries struct {
Time float64 `json:"time"`
Event string `json:"event"`
Event string `json:"event,omitempty"`
Host string `json:"host,omitempty"`
Index string `json:"index,omitempty"`
Source string `json:"source,omitempty"`
Fields map[string]interface{} `json:"fields"`
}

// NewSerializer Setup our new serializer
func NewSerializer(splunkmetricHecRouting bool, splunkmetricMultimetric bool) (*serializer, error) {
func NewSerializer(splunkmetricHecRouting bool, splunkmetricMultimetric bool, splunkmetricOmitEventTag bool) (*serializer, error) {
/* Define output params */
s := &serializer{
HecRouting: splunkmetricHecRouting,
SplunkmetricMultiMetric: splunkmetricMultimetric,
OmitEventTag: splunkmetricOmitEventTag,
}
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.OmitEventTag {
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.OmitEventTag {
dataGroup.Event = "metric"
}
dataGroup.Host = commonTags.Host
dataGroup.Index = commonTags.Index
dataGroup.Source = commonTags.Source
Expand Down
42 changes: 31 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, false)
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, false)
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, false)
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, false)
buf, err := s.SerializeBatch(metrics)
require.NoError(t, err)

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

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

expS := `{"time":0,"event":"metric","fields":{"metric_name:cpu.system":8,"metric_name:cpu.usage":42}}`
require.Equal(t, expS, string(buf))
}

func TestSerializeOmitEvent(t *testing.T) {
m := metric.New(
"cpu",
map[string]string{},
map[string]interface{}{
"usage": 42.0,
"system": 8.0,
},
time.Unix(0, 0),
)

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

expS := `{"time":0,"fields":{"metric_name:cpu.system":8,"metric_name:cpu.usage":42}}`
require.Equal(t, expS, string(buf))
}

0 comments on commit 4e43a8e

Please sign in to comment.