Skip non-numerical values in graphite format (#3179)
This commit is contained in:
		
							parent
							
								
									ef8876b70b
								
							
						
					
					
						commit
						3806424aab
					
				|  | @ -96,6 +96,8 @@ tars.cpu-total.us-east-1.cpu.usage_user 0.89 1455320690 | |||
| tars.cpu-total.us-east-1.cpu.usage_idle 98.09 1455320690 | ||||
| ``` | ||||
| 
 | ||||
| Fields with non-numeric values will be skipped. | ||||
| 
 | ||||
| ### Graphite Configuration: | ||||
| 
 | ||||
| ```toml | ||||
|  |  | |||
|  | @ -32,13 +32,16 @@ func (s *GraphiteSerializer) Serialize(metric telegraf.Metric) ([]byte, error) { | |||
| 	} | ||||
| 
 | ||||
| 	for fieldName, value := range metric.Fields() { | ||||
| 		// Convert value to string
 | ||||
| 		valueS := fmt.Sprintf("%#v", value) | ||||
| 		point := []byte(fmt.Sprintf("%s %s %d\n", | ||||
| 		switch value.(type) { | ||||
| 		case string: | ||||
| 			continue | ||||
| 		} | ||||
| 		metricString := fmt.Sprintf("%s %#v %d\n", | ||||
| 			// insert "field" section of template
 | ||||
| 			sanitizedChars.Replace(InsertField(bucket, fieldName)), | ||||
| 			sanitizedChars.Replace(valueS), | ||||
| 			timestamp)) | ||||
| 			value, | ||||
| 			timestamp) | ||||
| 		point := []byte(metricString) | ||||
| 		out = append(out, point...) | ||||
| 	} | ||||
| 	return out, nil | ||||
|  |  | |||
|  | @ -165,6 +165,28 @@ func TestSerializeValueField2(t *testing.T) { | |||
| 	assert.Equal(t, expS, mS) | ||||
| } | ||||
| 
 | ||||
| func TestSerializeValueString(t *testing.T) { | ||||
| 	now := time.Now() | ||||
| 	tags := map[string]string{ | ||||
| 		"host":       "localhost", | ||||
| 		"cpu":        "cpu0", | ||||
| 		"datacenter": "us-west-2", | ||||
| 	} | ||||
| 	fields := map[string]interface{}{ | ||||
| 		"value": "asdasd", | ||||
| 	} | ||||
| 	m, err := metric.New("cpu", tags, fields, now) | ||||
| 	assert.NoError(t, err) | ||||
| 
 | ||||
| 	s := GraphiteSerializer{ | ||||
| 		Template: "host.field.tags.measurement", | ||||
| 	} | ||||
| 	buf, _ := s.Serialize(m) | ||||
| 	mS := strings.Split(strings.TrimSpace(string(buf)), "\n") | ||||
| 	assert.NoError(t, err) | ||||
| 	assert.Equal(t, "", mS[0]) | ||||
| } | ||||
| 
 | ||||
| // test that fields with spaces get fixed.
 | ||||
| func TestSerializeFieldWithSpaces(t *testing.T) { | ||||
| 	now := time.Now() | ||||
|  |  | |||
		Loading…
	
		Reference in New Issue