@@ -46,7 +46,7 @@ func TestSerializeMetricFloat(t *testing.T) {
4646
4747 for _ , tc := range testcases {
4848 t .Run (string (tc .format ), func (t * testing.T ) {
49- s , err := NewSerializer (string (tc .format ))
49+ s , err := NewSerializer (string (tc .format ), DefaultSanitizeReplaceChar )
5050 require .NoError (t , err )
5151
5252 buf , err := s .Serialize (m )
@@ -84,7 +84,7 @@ func TestSerializeMetricWithEmptyStringTag(t *testing.T) {
8484
8585 for _ , tc := range testcases {
8686 t .Run (string (tc .format ), func (t * testing.T ) {
87- s , err := NewSerializer (string (tc .format ))
87+ s , err := NewSerializer (string (tc .format ), DefaultSanitizeReplaceChar )
8888 require .NoError (t , err )
8989
9090 buf , err := s .Serialize (m )
@@ -122,7 +122,7 @@ func TestSerializeWithSpaces(t *testing.T) {
122122
123123 for _ , tc := range testcases {
124124 t .Run (string (tc .format ), func (t * testing.T ) {
125- s , err := NewSerializer (string (tc .format ))
125+ s , err := NewSerializer (string (tc .format ), DefaultSanitizeReplaceChar )
126126 require .NoError (t , err )
127127
128128 buf , err := s .Serialize (m )
@@ -160,7 +160,7 @@ func TestSerializeMetricInt(t *testing.T) {
160160
161161 for _ , tc := range testcases {
162162 t .Run (string (tc .format ), func (t * testing.T ) {
163- s , err := NewSerializer (string (tc .format ))
163+ s , err := NewSerializer (string (tc .format ), DefaultSanitizeReplaceChar )
164164 require .NoError (t , err )
165165
166166 buf , err := s .Serialize (m )
@@ -198,7 +198,7 @@ func TestSerializeMetricString(t *testing.T) {
198198
199199 for _ , tc := range testcases {
200200 t .Run (string (tc .format ), func (t * testing.T ) {
201- s , err := NewSerializer (string (tc .format ))
201+ s , err := NewSerializer (string (tc .format ), DefaultSanitizeReplaceChar )
202202 require .NoError (t , err )
203203
204204 buf , err := s .Serialize (m )
@@ -255,7 +255,7 @@ func TestSerializeMetricBool(t *testing.T) {
255255
256256 for _ , tc := range testcases {
257257 t .Run (string (tc .format ), func (t * testing.T ) {
258- s , err := NewSerializer (string (tc .format ))
258+ s , err := NewSerializer (string (tc .format ), DefaultSanitizeReplaceChar )
259259 require .NoError (t , err )
260260
261261 buf , err := s .Serialize (tc .metric )
@@ -300,7 +300,7 @@ metric=cpu_value 42 0
300300
301301 for _ , tc := range testcases {
302302 t .Run (string (tc .format ), func (t * testing.T ) {
303- s , err := NewSerializer (string (tc .format ))
303+ s , err := NewSerializer (string (tc .format ), DefaultSanitizeReplaceChar )
304304 require .NoError (t , err )
305305
306306 buf , err := s .SerializeBatch (metrics )
@@ -310,3 +310,113 @@ metric=cpu_value 42 0
310310 })
311311 }
312312}
313+
314+ func TestSerializeMetricIsProperlySanitized (t * testing.T ) {
315+ now := time .Now ()
316+
317+ testcases := []struct {
318+ metricFunc func () (telegraf.Metric , error )
319+ format format
320+ expected string
321+ replaceChar string
322+ expectedErr bool
323+ }{
324+ {
325+ metricFunc : func () (telegraf.Metric , error ) {
326+ fields := map [string ]interface {}{
327+ "usage_idle" : float64 (91.5 ),
328+ }
329+ return metric .New ("cpu=1" , nil , fields , now )
330+ },
331+ format : Carbon2FormatFieldSeparate ,
332+ expected : fmt .Sprintf ("metric=cpu:1 field=usage_idle 91.5 %d\n " , now .Unix ()),
333+ replaceChar : DefaultSanitizeReplaceChar ,
334+ },
335+ {
336+ metricFunc : func () (telegraf.Metric , error ) {
337+ fields := map [string ]interface {}{
338+ "usage_idle" : float64 (91.5 ),
339+ }
340+ return metric .New ("cpu=1" , nil , fields , now )
341+ },
342+ format : Carbon2FormatFieldSeparate ,
343+ expected : fmt .Sprintf ("metric=cpu_1 field=usage_idle 91.5 %d\n " , now .Unix ()),
344+ replaceChar : "_" ,
345+ },
346+ {
347+ metricFunc : func () (telegraf.Metric , error ) {
348+ fields := map [string ]interface {}{
349+ "usage_idle" : float64 (91.5 ),
350+ }
351+ return metric .New ("cpu=1=tmp$custom" , nil , fields , now )
352+ },
353+ format : Carbon2FormatFieldSeparate ,
354+ expected : fmt .Sprintf ("metric=cpu:1:tmp:custom field=usage_idle 91.5 %d\n " , now .Unix ()),
355+ replaceChar : DefaultSanitizeReplaceChar ,
356+ },
357+ {
358+ metricFunc : func () (telegraf.Metric , error ) {
359+ fields := map [string ]interface {}{
360+ "usage_idle" : float64 (91.5 ),
361+ }
362+ return metric .New ("cpu=1=tmp$custom%namespace" , nil , fields , now )
363+ },
364+ format : Carbon2FormatFieldSeparate ,
365+ expected : fmt .Sprintf ("metric=cpu:1:tmp:custom:namespace field=usage_idle 91.5 %d\n " , now .Unix ()),
366+ replaceChar : DefaultSanitizeReplaceChar ,
367+ },
368+ {
369+ metricFunc : func () (telegraf.Metric , error ) {
370+ fields := map [string ]interface {}{
371+ "usage_idle" : float64 (91.5 ),
372+ }
373+ return metric .New ("cpu=1=tmp$custom%namespace" , nil , fields , now )
374+ },
375+ format : Carbon2FormatMetricIncludesField ,
376+ expected : fmt .Sprintf ("metric=cpu:1:tmp:custom:namespace_usage_idle 91.5 %d\n " , now .Unix ()),
377+ replaceChar : DefaultSanitizeReplaceChar ,
378+ },
379+ {
380+ metricFunc : func () (telegraf.Metric , error ) {
381+ fields := map [string ]interface {}{
382+ "usage_idle" : float64 (91.5 ),
383+ }
384+ return metric .New ("cpu=1=tmp$custom%namespace" , nil , fields , now )
385+ },
386+ format : Carbon2FormatMetricIncludesField ,
387+ expected : fmt .Sprintf ("metric=cpu_1_tmp_custom_namespace_usage_idle 91.5 %d\n " , now .Unix ()),
388+ replaceChar : "_" ,
389+ },
390+ {
391+ metricFunc : func () (telegraf.Metric , error ) {
392+ fields := map [string ]interface {}{
393+ "usage_idle" : float64 (91.5 ),
394+ }
395+ return metric .New ("cpu=1=tmp$custom%namespace" , nil , fields , now )
396+ },
397+ format : Carbon2FormatMetricIncludesField ,
398+ expectedErr : true ,
399+ replaceChar : "___" ,
400+ },
401+ }
402+
403+ for _ , tc := range testcases {
404+ t .Run (string (tc .format ), func (t * testing.T ) {
405+ m , err := tc .metricFunc ()
406+ require .NoError (t , err )
407+
408+ s , err := NewSerializer (string (tc .format ), tc .replaceChar )
409+ if tc .expectedErr {
410+ require .Error (t , err )
411+ return
412+ }
413+
414+ require .NoError (t , err )
415+
416+ buf , err := s .Serialize (m )
417+ require .NoError (t , err )
418+
419+ assert .Equal (t , tc .expected , string (buf ))
420+ })
421+ }
422+ }
0 commit comments