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"
|
2020-03-04 18:13:44 +00:00
|
|
|
"io"
|
2019-02-26 18:48:41 +00:00
|
|
|
"strings"
|
2018-04-02 19:52:23 +00:00
|
|
|
"sync"
|
2020-03-04 18:13:44 +00:00
|
|
|
"time"
|
2016-02-06 00:36:35 +00:00
|
|
|
|
|
|
|
"github.com/influxdata/telegraf"
|
|
|
|
)
|
|
|
|
|
2018-03-28 00:30:51 +00:00
|
|
|
const (
|
|
|
|
maxErrorBufferSize = 1024
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
ErrNoMetric = errors.New("no metric in line")
|
|
|
|
)
|
|
|
|
|
2020-03-04 18:13:44 +00:00
|
|
|
type TimeFunc func() time.Time
|
|
|
|
|
|
|
|
// ParseError indicates a error in the parsing of the text.
|
2018-03-28 00:30:51 +00:00
|
|
|
type ParseError struct {
|
2019-02-26 18:48:41 +00:00
|
|
|
Offset int
|
|
|
|
LineOffset int
|
|
|
|
LineNumber int
|
|
|
|
Column int
|
|
|
|
msg string
|
|
|
|
buf string
|
2018-03-28 00:30:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (e *ParseError) Error() string {
|
2019-02-26 18:48:41 +00:00
|
|
|
buffer := e.buf[e.LineOffset:]
|
|
|
|
eol := strings.IndexAny(buffer, "\r\n")
|
|
|
|
if eol >= 0 {
|
|
|
|
buffer = buffer[:eol]
|
|
|
|
}
|
2018-03-28 00:30:51 +00:00
|
|
|
if len(buffer) > maxErrorBufferSize {
|
|
|
|
buffer = buffer[:maxErrorBufferSize] + "..."
|
|
|
|
}
|
2019-02-26 18:48:41 +00:00
|
|
|
return fmt.Sprintf("metric parse error: %s at %d:%d: %q", e.msg, e.LineNumber, e.Column, buffer)
|
2018-03-28 00:30:51 +00:00
|
|
|
}
|
|
|
|
|
2020-03-04 18:13:44 +00:00
|
|
|
// Parser is an InfluxDB Line Protocol parser that implements the
|
|
|
|
// parsers.Parser interface.
|
2018-03-28 00:30:51 +00:00
|
|
|
type Parser struct {
|
2016-02-06 00:36:35 +00:00
|
|
|
DefaultTags map[string]string
|
2018-03-28 00:30:51 +00:00
|
|
|
|
2018-04-02 19:52:23 +00:00
|
|
|
sync.Mutex
|
2018-03-28 00:30:51 +00:00
|
|
|
*machine
|
|
|
|
handler *MetricHandler
|
2016-02-06 00:36:35 +00:00
|
|
|
}
|
|
|
|
|
2018-05-14 18:00:03 +00:00
|
|
|
// NewParser returns a Parser than accepts line protocol
|
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
|
|
|
}
|
|
|
|
|
2018-05-14 18:00:03 +00:00
|
|
|
// NewSeriesParser returns a Parser than accepts a measurement and tagset
|
|
|
|
func NewSeriesParser(handler *MetricHandler) *Parser {
|
|
|
|
return &Parser{
|
|
|
|
machine: NewSeriesMachine(handler),
|
|
|
|
handler: handler,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-04 18:13:44 +00:00
|
|
|
func (h *Parser) SetTimeFunc(f TimeFunc) {
|
|
|
|
h.handler.SetTimeFunc(f)
|
|
|
|
}
|
|
|
|
|
2018-03-28 00:30:51 +00:00
|
|
|
func (p *Parser) Parse(input []byte) ([]telegraf.Metric, error) {
|
2018-04-02 19:52:23 +00:00
|
|
|
p.Lock()
|
|
|
|
defer p.Unlock()
|
2018-03-28 00:30:51 +00:00
|
|
|
metrics := make([]telegraf.Metric, 0)
|
|
|
|
p.machine.SetData(input)
|
|
|
|
|
2019-02-26 18:48:41 +00:00
|
|
|
for {
|
|
|
|
err := p.machine.Next()
|
|
|
|
if err == EOF {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
2018-03-28 00:30:51 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, &ParseError{
|
2019-02-26 18:48:41 +00:00
|
|
|
Offset: p.machine.Position(),
|
|
|
|
LineOffset: p.machine.LineOffset(),
|
|
|
|
LineNumber: p.machine.LineNumber(),
|
|
|
|
Column: p.machine.Column(),
|
|
|
|
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
|
|
|
|
}
|
2019-02-26 18:48:41 +00:00
|
|
|
|
|
|
|
if metric == nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2018-03-28 00:30:51 +00:00
|
|
|
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) {
|
2019-02-26 18:48:41 +00:00
|
|
|
metrics, err := p.Parse([]byte(line))
|
2016-02-06 00:36:35 +00:00
|
|
|
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 {
|
2020-03-04 18:13:44 +00:00
|
|
|
p.applyDefaultTagsSingle(m)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Parser) applyDefaultTagsSingle(metric telegraf.Metric) {
|
|
|
|
for k, v := range p.DefaultTags {
|
|
|
|
if !metric.HasTag(k) {
|
|
|
|
metric.AddTag(k, v)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// StreamParser is an InfluxDB Line Protocol parser. It is not safe for
|
|
|
|
// concurrent use in multiple goroutines.
|
|
|
|
type StreamParser struct {
|
|
|
|
machine *streamMachine
|
|
|
|
handler *MetricHandler
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewStreamParser(r io.Reader) *StreamParser {
|
|
|
|
handler := NewMetricHandler()
|
|
|
|
return &StreamParser{
|
|
|
|
machine: NewStreamMachine(r, handler),
|
|
|
|
handler: handler,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetTimeFunc changes the function used to determine the time of metrics
|
|
|
|
// without a timestamp. The default TimeFunc is time.Now. Useful mostly for
|
|
|
|
// testing, or perhaps if you want all metrics to have the same timestamp.
|
|
|
|
func (h *StreamParser) SetTimeFunc(f TimeFunc) {
|
|
|
|
h.handler.SetTimeFunc(f)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *StreamParser) SetTimePrecision(u time.Duration) {
|
|
|
|
h.handler.SetTimePrecision(u)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Next parses the next item from the stream. You can repeat calls to this
|
2020-04-28 21:42:37 +00:00
|
|
|
// function if it returns ParseError to get the next metric or error.
|
2020-03-04 18:13:44 +00:00
|
|
|
func (p *StreamParser) Next() (telegraf.Metric, error) {
|
|
|
|
err := p.machine.Next()
|
|
|
|
if err == EOF {
|
2020-04-28 21:42:37 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if e, ok := err.(*readErr); ok {
|
|
|
|
return nil, e.Err
|
2020-03-04 18:13:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, &ParseError{
|
|
|
|
Offset: p.machine.Position(),
|
|
|
|
LineOffset: p.machine.LineOffset(),
|
|
|
|
LineNumber: p.machine.LineNumber(),
|
|
|
|
Column: p.machine.Column(),
|
|
|
|
msg: err.Error(),
|
|
|
|
buf: p.machine.LineText(),
|
2018-03-28 00:30:51 +00:00
|
|
|
}
|
|
|
|
}
|
2020-03-04 18:13:44 +00:00
|
|
|
|
|
|
|
metric, err := p.handler.Metric()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return metric, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Position returns the current byte offset into the data.
|
|
|
|
func (p *StreamParser) Position() int {
|
|
|
|
return p.machine.Position()
|
|
|
|
}
|
|
|
|
|
|
|
|
// LineOffset returns the byte offset of the current line.
|
|
|
|
func (p *StreamParser) LineOffset() int {
|
|
|
|
return p.machine.LineOffset()
|
|
|
|
}
|
|
|
|
|
|
|
|
// LineNumber returns the current line number. Lines are counted based on the
|
|
|
|
// regular expression `\r?\n`.
|
|
|
|
func (p *StreamParser) LineNumber() int {
|
|
|
|
return p.machine.LineNumber()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Column returns the current column.
|
|
|
|
func (p *StreamParser) Column() int {
|
|
|
|
return p.machine.Column()
|
|
|
|
}
|
|
|
|
|
|
|
|
// LineText returns the text of the current line that has been parsed so far.
|
|
|
|
func (p *StreamParser) LineText() string {
|
|
|
|
return p.machine.LineText()
|
2018-03-28 00:30:51 +00:00
|
|
|
}
|