2016-02-06 00:36:35 +00:00
|
|
|
package influx
|
|
|
|
|
|
|
|
import (
|
2018-03-28 00:30:51 +00:00
|
|
|
"errors"
|
2016-02-06 00:36:35 +00:00
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"github.com/influxdata/telegraf"
|
|
|
|
)
|
|
|
|
|
2018-03-28 00:30:51 +00:00
|
|
|
const (
|
|
|
|
maxErrorBufferSize = 1024
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
ErrNoMetric = errors.New("no metric in line")
|
|
|
|
)
|
|
|
|
|
|
|
|
type Handler interface {
|
|
|
|
SetMeasurement(name []byte)
|
|
|
|
AddTag(key []byte, value []byte)
|
|
|
|
AddInt(key []byte, value []byte)
|
|
|
|
AddFloat(key []byte, value []byte)
|
|
|
|
AddString(key []byte, value []byte)
|
|
|
|
AddBool(key []byte, value []byte)
|
|
|
|
SetTimestamp(tm []byte)
|
|
|
|
Reset()
|
|
|
|
}
|
|
|
|
|
|
|
|
type ParseError struct {
|
|
|
|
Offset int
|
|
|
|
msg string
|
|
|
|
buf string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *ParseError) Error() string {
|
|
|
|
buffer := e.buf
|
|
|
|
if len(buffer) > maxErrorBufferSize {
|
|
|
|
buffer = buffer[:maxErrorBufferSize] + "..."
|
|
|
|
}
|
|
|
|
return fmt.Sprintf("metric parse error: %s at offset %d: %q", e.msg, e.Offset, buffer)
|
|
|
|
}
|
|
|
|
|
|
|
|
type Parser struct {
|
2016-02-06 00:36:35 +00:00
|
|
|
DefaultTags map[string]string
|
2018-03-28 00:30:51 +00:00
|
|
|
|
|
|
|
*machine
|
|
|
|
handler *MetricHandler
|
2016-02-06 00:36:35 +00:00
|
|
|
}
|
|
|
|
|
2018-03-28 00:30:51 +00:00
|
|
|
func NewParser(handler *MetricHandler) *Parser {
|
|
|
|
return &Parser{
|
|
|
|
machine: NewMachine(handler),
|
|
|
|
handler: handler,
|
2017-01-23 21:50:52 +00:00
|
|
|
}
|
2018-03-28 00:30:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Parser) Parse(input []byte) ([]telegraf.Metric, error) {
|
|
|
|
metrics := make([]telegraf.Metric, 0)
|
|
|
|
p.machine.SetData(input)
|
|
|
|
|
|
|
|
for p.machine.ParseLine() {
|
|
|
|
err := p.machine.Err()
|
|
|
|
if err != nil {
|
|
|
|
return nil, &ParseError{
|
|
|
|
Offset: p.machine.Position(),
|
|
|
|
msg: err.Error(),
|
|
|
|
buf: string(input),
|
2016-02-06 00:36:35 +00:00
|
|
|
}
|
|
|
|
}
|
2018-03-28 00:30:51 +00:00
|
|
|
|
|
|
|
metric, err := p.handler.Metric()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
p.handler.Reset()
|
|
|
|
metrics = append(metrics, metric)
|
2016-02-06 00:36:35 +00:00
|
|
|
}
|
|
|
|
|
2018-03-28 00:30:51 +00:00
|
|
|
p.applyDefaultTags(metrics)
|
|
|
|
return metrics, nil
|
2016-10-18 11:22:23 +00:00
|
|
|
}
|
|
|
|
|
2018-03-28 00:30:51 +00:00
|
|
|
func (p *Parser) ParseLine(line string) (telegraf.Metric, error) {
|
2016-02-06 00:36:35 +00:00
|
|
|
metrics, err := p.Parse([]byte(line + "\n"))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(metrics) < 1 {
|
2018-03-28 00:30:51 +00:00
|
|
|
return nil, ErrNoMetric
|
2016-02-06 00:36:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return metrics[0], nil
|
|
|
|
}
|
2016-02-09 22:03:46 +00:00
|
|
|
|
2018-03-28 00:30:51 +00:00
|
|
|
func (p *Parser) SetDefaultTags(tags map[string]string) {
|
2016-02-09 22:03:46 +00:00
|
|
|
p.DefaultTags = tags
|
|
|
|
}
|
2018-03-28 00:30:51 +00:00
|
|
|
|
|
|
|
func (p *Parser) applyDefaultTags(metrics []telegraf.Metric) {
|
|
|
|
if len(p.DefaultTags) == 0 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, m := range metrics {
|
|
|
|
for k, v := range p.DefaultTags {
|
|
|
|
if !m.HasTag(k) {
|
|
|
|
m.AddTag(k, v)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|