-
Notifications
You must be signed in to change notification settings - Fork 5.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Value parser, for parsing a single value into a metric
closes #849
- Loading branch information
Showing
5 changed files
with
355 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
package value | ||
|
||
import ( | ||
"bytes" | ||
"fmt" | ||
"strconv" | ||
"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) { | ||
// separate out any fields in the buffer, ignore anything but the first. | ||
values := bytes.Fields(buf) | ||
if len(values) < 1 { | ||
return []telegraf.Metric{}, nil | ||
} | ||
valueStr := string(values[0]) | ||
|
||
var value interface{} | ||
var err error | ||
switch v.DataType { | ||
case "", "int", "integer": | ||
value, err = strconv.Atoi(valueStr) | ||
case "float", "long": | ||
value, err = strconv.ParseFloat(valueStr, 64) | ||
case "str", "string": | ||
value = valueStr | ||
case "bool", "boolean": | ||
value, err = strconv.ParseBool(valueStr) | ||
} | ||
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 | ||
} |
Oops, something went wrong.