Skip to content
Snippets Groups Projects
  • Pieter Slabbert's avatar
    196509cc
    Trim null characters in Value data format (#2049) · 196509cc
    Pieter Slabbert authored
    * Trim null characters in Value data format
    
    Some producers (such as the paho embedded c mqtt client) add a null
    character "\x00" to the end of a message.  The Value parser would fail on
    any message from such a producer.
    
    * Trim whitespace and null in all Value data formats
    
    * No unnecessary reassignments in Value data format parser
    
    * Update change log for Value data format fix
    196509cc
    History
    Trim null characters in Value data format (#2049)
    Pieter Slabbert authored
    * Trim null characters in Value data format
    
    Some producers (such as the paho embedded c mqtt client) add a null
    character "\x00" to the end of a message.  The Value parser would fail on
    any message from such a producer.
    
    * Trim whitespace and null in all Value data formats
    
    * No unnecessary reassignments in Value data format parser
    
    * Update change log for Value data format fix
parser.go 1.50 KiB
package value

import (
	"bytes"
	"fmt"
	"strconv"
	"strings"
	"time"

	"github.com/influxdata/telegraf"
)

type ValueParser struct {
	MetricName  string
	DataType    string
	DefaultTags map[string]string
}

func (v *ValueParser) Parse(buf []byte) ([]telegraf.Metric, error) {
	vStr := string(bytes.TrimSpace(bytes.Trim(buf, "\x00")))

	// unless it's a string, separate out any fields in the buffer,
	// ignore anything but the last.
	if v.DataType != "string" {
		values := strings.Fields(vStr)
		if len(values) < 1 {
			return []telegraf.Metric{}, nil
		}
		vStr = string(values[len(values)-1])
	}

	var value interface{}
	var err error
	switch v.DataType {
	case "", "int", "integer":
		value, err = strconv.Atoi(vStr)
	case "float", "long":
		value, err = strconv.ParseFloat(vStr, 64)
	case "str", "string":
		value = vStr
	case "bool", "boolean":
		value, err = strconv.ParseBool(vStr)
	}
	if err != nil {
		return nil, err
	}

	fields := map[string]interface{}{"value": value}
	metric, err := telegraf.NewMetric(v.MetricName, v.DefaultTags,
		fields, time.Now().UTC())
	if err != nil {
		return nil, err
	}

	return []telegraf.Metric{metric}, nil
}

func (v *ValueParser) ParseLine(line string) (telegraf.Metric, error) {
	metrics, err := v.Parse([]byte(line))

	if err != nil {
		return nil, err
	}

	if len(metrics) < 1 {
		return nil, fmt.Errorf("Can not parse the line: %s, for data format: value", line)
	}

	return metrics[0], nil
}

func (v *ValueParser) SetDefaultTags(tags map[string]string) {
	v.DefaultTags = tags
}