Fix carbon2 serializer not falling through to field separate when carbon2_format field is unset (#8201)
This commit is contained in:
parent
88698a68d9
commit
cc089e6eb6
|
|
@ -245,20 +245,6 @@ func TestContentType(t *testing.T) {
|
||||||
},
|
},
|
||||||
expectedBody: []byte("metric=cpu field=value 42 0\n"),
|
expectedBody: []byte("metric=cpu field=value 42 0\n"),
|
||||||
},
|
},
|
||||||
{
|
|
||||||
name: "carbon2 (data format unset) is supported and falls back to include field in metric name",
|
|
||||||
plugin: func() *SumoLogic {
|
|
||||||
s := Default()
|
|
||||||
s.headers = map[string]string{
|
|
||||||
contentTypeHeader: carbon2ContentType,
|
|
||||||
}
|
|
||||||
sr, err := carbon2.NewSerializer(string(carbon2.Carbon2FormatFieldEmpty))
|
|
||||||
require.NoError(t, err)
|
|
||||||
s.SetSerializer(sr)
|
|
||||||
return s
|
|
||||||
},
|
|
||||||
expectedBody: []byte("metric=cpu_value 42 0\n"),
|
|
||||||
},
|
|
||||||
{
|
{
|
||||||
name: "carbon2 (data format = metric includes field) is supported",
|
name: "carbon2 (data format = metric includes field) is supported",
|
||||||
plugin: func() *SumoLogic {
|
plugin: func() *SumoLogic {
|
||||||
|
|
|
||||||
|
|
@ -12,13 +12,13 @@ import (
|
||||||
type format string
|
type format string
|
||||||
|
|
||||||
const (
|
const (
|
||||||
Carbon2FormatFieldEmpty = format("")
|
carbon2FormatFieldEmpty = format("")
|
||||||
Carbon2FormatFieldSeparate = format("field_separate")
|
Carbon2FormatFieldSeparate = format("field_separate")
|
||||||
Carbon2FormatMetricIncludesField = format("metric_includes_field")
|
Carbon2FormatMetricIncludesField = format("metric_includes_field")
|
||||||
)
|
)
|
||||||
|
|
||||||
var formats = map[format]struct{}{
|
var formats = map[format]struct{}{
|
||||||
Carbon2FormatFieldEmpty: {},
|
carbon2FormatFieldEmpty: {},
|
||||||
Carbon2FormatFieldSeparate: {},
|
Carbon2FormatFieldSeparate: {},
|
||||||
Carbon2FormatMetricIncludesField: {},
|
Carbon2FormatMetricIncludesField: {},
|
||||||
}
|
}
|
||||||
|
|
@ -29,10 +29,16 @@ type Serializer struct {
|
||||||
|
|
||||||
func NewSerializer(metricsFormat string) (*Serializer, error) {
|
func NewSerializer(metricsFormat string) (*Serializer, error) {
|
||||||
var f = format(metricsFormat)
|
var f = format(metricsFormat)
|
||||||
|
|
||||||
if _, ok := formats[f]; !ok {
|
if _, ok := formats[f]; !ok {
|
||||||
return nil, fmt.Errorf("unknown carbon2 format: %s", f)
|
return nil, fmt.Errorf("unknown carbon2 format: %s", f)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// When unset, default to field separate.
|
||||||
|
if f == carbon2FormatFieldEmpty {
|
||||||
|
f = Carbon2FormatFieldSeparate
|
||||||
|
}
|
||||||
|
|
||||||
return &Serializer{
|
return &Serializer{
|
||||||
metricsFormat: f,
|
metricsFormat: f,
|
||||||
}, nil
|
}, nil
|
||||||
|
|
@ -60,8 +66,6 @@ func (s *Serializer) createObject(metric telegraf.Metric) []byte {
|
||||||
}
|
}
|
||||||
|
|
||||||
switch metricsFormat {
|
switch metricsFormat {
|
||||||
// Field separate is the default when no format specified.
|
|
||||||
case Carbon2FormatFieldEmpty:
|
|
||||||
case Carbon2FormatFieldSeparate:
|
case Carbon2FormatFieldSeparate:
|
||||||
m.WriteString(serializeMetricFieldSeparate(
|
m.WriteString(serializeMetricFieldSeparate(
|
||||||
metric.Name(), fieldName,
|
metric.Name(), fieldName,
|
||||||
|
|
@ -101,7 +105,7 @@ func (s *Serializer) getMetricsFormat() format {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Serializer) IsMetricsFormatUnset() bool {
|
func (s *Serializer) IsMetricsFormatUnset() bool {
|
||||||
return s.metricsFormat == Carbon2FormatFieldEmpty
|
return s.metricsFormat == carbon2FormatFieldEmpty
|
||||||
}
|
}
|
||||||
|
|
||||||
func serializeMetricFieldSeparate(name, fieldName string) string {
|
func serializeMetricFieldSeparate(name, fieldName string) string {
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue