-
Notifications
You must be signed in to change notification settings - Fork 5.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Wavefront parser #4402
Merged
Merged
Wavefront parser #4402
Changes from 2 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
e1c682d
Wavefront parser plugin
puckpuck 54cebbe
fix value parsing and source tag
puckpuck b0c2a66
minor formatting changes
puckpuck ee8ffd6
minor formatting changes
puckpuck bbb3b74
Add Wavefront Parser to docs
puckpuck ef39e4f
Merge branch 'master' into wavefront-parser
puckpuck File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,238 @@ | ||
package wavefront | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"strconv" | ||
"time" | ||
) | ||
|
||
var ( | ||
ErrEOF = errors.New("EOF") | ||
ErrInvalidTimestamp = errors.New("Invalid timestamp") | ||
) | ||
|
||
// Interface for parsing line elements. | ||
type ElementParser interface { | ||
parse(p *PointParser, pt *Point) error | ||
} | ||
|
||
type NameParser struct{} | ||
type ValueParser struct{} | ||
type TimestampParser struct { | ||
optional bool | ||
} | ||
type WhiteSpaceParser struct { | ||
nextOptional bool | ||
} | ||
type TagParser struct{} | ||
type LoopedParser struct { | ||
wrappedParser ElementParser | ||
wsPaser *WhiteSpaceParser | ||
} | ||
type LiteralParser struct { | ||
literal string | ||
} | ||
|
||
func (ep *NameParser) parse(p *PointParser, pt *Point) error { | ||
//Valid characters are: a-z, A-Z, 0-9, hyphen ("-"), underscore ("_"), dot ("."). | ||
// Forward slash ("/") and comma (",") are allowed if metricName is enclosed in double quotes. | ||
name, err := parseLiteral(p) | ||
if err != nil { | ||
return err | ||
} | ||
pt.Name = name | ||
return nil | ||
} | ||
|
||
func (ep *ValueParser) parse(p *PointParser, pt *Point) error { | ||
tok, lit := p.scan() | ||
if tok == EOF { | ||
return fmt.Errorf("found %q, expected number", lit) | ||
} | ||
|
||
p.writeBuf.Reset() | ||
if tok == MINUS_SIGN { | ||
p.writeBuf.WriteString(lit) | ||
tok, lit = p.scan() | ||
} | ||
|
||
for tok != EOF && (tok == LETTER || tok == NUMBER || tok == DOT) { | ||
p.writeBuf.WriteString(lit) | ||
tok, lit = p.scan() | ||
} | ||
p.unscan() | ||
|
||
pt.Value = p.writeBuf.String() | ||
_, err := strconv.ParseFloat(pt.Value, 64) | ||
if err != nil { | ||
return fmt.Errorf("invalid metric value %s", pt.Value) | ||
} | ||
return nil | ||
} | ||
|
||
func (ep *TimestampParser) parse(p *PointParser, pt *Point) error { | ||
tok, lit := p.scan() | ||
if tok == EOF { | ||
if ep.optional { | ||
p.unscanTokens(2) | ||
return setTimestamp(pt, 0, 1) | ||
} | ||
return fmt.Errorf("found %q, expected number", lit) | ||
} | ||
|
||
if tok != NUMBER { | ||
if ep.optional { | ||
p.unscanTokens(2) | ||
return setTimestamp(pt, 0, 1) | ||
} | ||
return ErrInvalidTimestamp | ||
} | ||
|
||
p.writeBuf.Reset() | ||
for tok != EOF && tok == NUMBER { | ||
p.writeBuf.WriteString(lit) | ||
tok, lit = p.scan() | ||
} | ||
p.unscan() | ||
|
||
tsStr := p.writeBuf.String() | ||
ts, err := strconv.ParseInt(tsStr, 10, 64) | ||
if err != nil { | ||
return err | ||
} | ||
return setTimestamp(pt, ts, len(tsStr)) | ||
} | ||
|
||
func setTimestamp(pt *Point, ts int64, numDigits int) error { | ||
|
||
if numDigits == 19 { | ||
// nanoseconds | ||
ts = ts / 1e9 | ||
} else if numDigits == 16 { | ||
// microseconds | ||
ts = ts / 1e6 | ||
} else if numDigits == 13 { | ||
// milliseconds | ||
ts = ts / 1e3 | ||
} else if numDigits != 10 { | ||
// must be in seconds, return error if not 0 | ||
if ts == 0 { | ||
ts = getCurrentTime() | ||
} else { | ||
return ErrInvalidTimestamp | ||
} | ||
} | ||
pt.Timestamp = ts | ||
return nil | ||
} | ||
|
||
func (ep *LoopedParser) parse(p *PointParser, pt *Point) error { | ||
for { | ||
err := ep.wrappedParser.parse(p, pt) | ||
if err != nil { | ||
return err | ||
} | ||
err = ep.wsPaser.parse(p, pt) | ||
if err == ErrEOF { | ||
break | ||
} | ||
} | ||
return nil | ||
} | ||
|
||
func (ep *TagParser) parse(p *PointParser, pt *Point) error { | ||
k, err := parseLiteral(p) | ||
if err != nil { | ||
if k == "" { | ||
return nil | ||
} | ||
return err | ||
} | ||
|
||
next, lit := p.scan() | ||
if next != EQUALS { | ||
return fmt.Errorf("found %q, expected equals", lit) | ||
} | ||
|
||
v, err := parseLiteral(p) | ||
if err != nil { | ||
return err | ||
} | ||
if len(pt.Tags) == 0 { | ||
pt.Tags = make(map[string]string) | ||
} | ||
pt.Tags[k] = v | ||
return nil | ||
} | ||
|
||
func (ep *WhiteSpaceParser) parse(p *PointParser, pt *Point) error { | ||
tok := WS | ||
for tok != EOF && tok == WS { | ||
tok, _ = p.scan() | ||
} | ||
|
||
if tok == EOF { | ||
if !ep.nextOptional { | ||
return ErrEOF | ||
} | ||
return nil | ||
} | ||
p.unscan() | ||
return nil | ||
} | ||
|
||
func (ep *LiteralParser) parse(p *PointParser, pt *Point) error { | ||
l, err := parseLiteral(p) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if l != ep.literal { | ||
return fmt.Errorf("found %s, expected %s", l, ep.literal) | ||
} | ||
return nil | ||
} | ||
|
||
func parseQuotedLiteral(p *PointParser) (string, error) { | ||
p.writeBuf.Reset() | ||
|
||
escaped := false | ||
tok, lit := p.scan() | ||
for tok != EOF && (tok != QUOTES || (tok == QUOTES && escaped)) { | ||
// let everything through | ||
escaped = tok == BACKSLASH | ||
p.writeBuf.WriteString(lit) | ||
tok, lit = p.scan() | ||
} | ||
if tok == EOF { | ||
return "", fmt.Errorf("found %q, expected quotes", lit) | ||
} | ||
return p.writeBuf.String(), nil | ||
} | ||
|
||
func parseLiteral(p *PointParser) (string, error) { | ||
tok, lit := p.scan() | ||
if tok == EOF { | ||
return "", fmt.Errorf("found %q, expected literal", lit) | ||
} | ||
|
||
if tok == QUOTES { | ||
return parseQuotedLiteral(p) | ||
} | ||
|
||
p.writeBuf.Reset() | ||
for tok != EOF && tok > literal_beg && tok < literal_end { | ||
p.writeBuf.WriteString(lit) | ||
tok, lit = p.scan() | ||
} | ||
if tok == QUOTES { | ||
return "", errors.New("found quote inside unquoted literal") | ||
} | ||
p.unscan() | ||
return p.writeBuf.String(), nil | ||
} | ||
|
||
func getCurrentTime() int64 { | ||
return time.Now().UnixNano() / 1e9 | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nitpick: no empty line here