From 94d8cc2f015edc8f5c2702b4402a4339f5340de0 Mon Sep 17 00:00:00 2001 From: Henry Hu Date: Wed, 3 Feb 2016 17:04:00 +0800 Subject: [PATCH] Add parser for Socket Line protocol... --- internal/encoding/parser.go | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/internal/encoding/parser.go b/internal/encoding/parser.go index 5e52a0aa4..dc73bfe68 100644 --- a/internal/encoding/parser.go +++ b/internal/encoding/parser.go @@ -55,3 +55,36 @@ func (p *Parser) Parse(dataFormat string, out []byte, acc telegraf.Accumulator) return err } + +func (p *Parser) ParseSocketLines(dataFormat string, buf []byte) ([]telegraf.Metric, error) { + var err error + var metrics []telegraf.Metric + + switch dataFormat { + case "", "graphite": + metrics, err = p.graphiteParser.ParseMetrics(buf) + case "influx": + metrics, err = telegraf.ParseMetrics(buf) + default: + err = fmt.Errorf("Unsupported data format: %s. Must be either influx or graphite ", dataFormat) + } + + if err != nil { + return nil, err + } + return metrics, nil +} + +func (p *Parser) ParseSocketLine(dataFormat, line string) (telegraf.Metric, error) { + metrics, err := p.ParseSocketLines(dataFormat, []byte(line+"\n")) + + if err != nil { + return nil, err + } + + if len(metrics) < 1 { + return nil, fmt.Errorf("Can not parse the line: %s, for data format: %s ", line, dataFormat) + } + + return metrics[0], nil +}