Fix carbon2 serializer not falling through to field separate when carbon2_format field is unset (#8201)

This commit is contained in:
Patryk Małek 2020-10-07 16:33:09 +02:00 committed by GitHub
parent 88698a68d9
commit cc089e6eb6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 9 additions and 19 deletions

View File

@ -245,20 +245,6 @@ func TestContentType(t *testing.T) {
},
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",
plugin: func() *SumoLogic {

View File

@ -12,13 +12,13 @@ import (
type format string
const (
Carbon2FormatFieldEmpty = format("")
carbon2FormatFieldEmpty = format("")
Carbon2FormatFieldSeparate = format("field_separate")
Carbon2FormatMetricIncludesField = format("metric_includes_field")
)
var formats = map[format]struct{}{
Carbon2FormatFieldEmpty: {},
carbon2FormatFieldEmpty: {},
Carbon2FormatFieldSeparate: {},
Carbon2FormatMetricIncludesField: {},
}
@ -29,10 +29,16 @@ type Serializer struct {
func NewSerializer(metricsFormat string) (*Serializer, error) {
var f = format(metricsFormat)
if _, ok := formats[f]; !ok {
return nil, fmt.Errorf("unknown carbon2 format: %s", f)
}
// When unset, default to field separate.
if f == carbon2FormatFieldEmpty {
f = Carbon2FormatFieldSeparate
}
return &Serializer{
metricsFormat: f,
}, nil
@ -60,8 +66,6 @@ func (s *Serializer) createObject(metric telegraf.Metric) []byte {
}
switch metricsFormat {
// Field separate is the default when no format specified.
case Carbon2FormatFieldEmpty:
case Carbon2FormatFieldSeparate:
m.WriteString(serializeMetricFieldSeparate(
metric.Name(), fieldName,
@ -101,7 +105,7 @@ func (s *Serializer) getMetricsFormat() format {
}
func (s *Serializer) IsMetricsFormatUnset() bool {
return s.metricsFormat == Carbon2FormatFieldEmpty
return s.metricsFormat == carbon2FormatFieldEmpty
}
func serializeMetricFieldSeparate(name, fieldName string) string {