-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4 from kayac/feature/add-parser-flag-for-cloudfro…
…nt-log add CloudFront Standard Log Parser
- Loading branch information
Showing
13 changed files
with
478 additions
and
27 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package router | ||
|
||
import ( | ||
"encoding/json" | ||
"log" | ||
) | ||
|
||
// LF represents LineFeed \n | ||
var LF = []byte("\n") | ||
|
||
type noneEncoder struct { | ||
body buffer | ||
} | ||
|
||
func newNoneEncoder(body buffer) encoder { | ||
return &noneEncoder{ | ||
body: body, | ||
} | ||
} | ||
|
||
func (e *noneEncoder) Encode(_ record, recordBytes []byte) error { | ||
if _, err := e.body.Write(recordBytes); err != nil { | ||
return err | ||
} | ||
_, err := e.body.Write(LF) | ||
return err | ||
} | ||
|
||
func (e *noneEncoder) Buffer() buffer { | ||
return e.body | ||
} | ||
|
||
type jsonEncoder struct { | ||
body buffer | ||
} | ||
|
||
func newJSONEncoder(body buffer) encoder { | ||
return &jsonEncoder{ | ||
body: body, | ||
} | ||
} | ||
|
||
func (e *jsonEncoder) Encode(rec record, _ []byte) error { | ||
|
||
bytes, err := json.Marshal(rec) | ||
if err != nil { | ||
log.Println("[warn] failed to generate json record", err) | ||
return err | ||
} | ||
if _, err := e.body.Write(bytes); err != nil { | ||
return err | ||
} | ||
_, err = e.body.Write(LF) | ||
return err | ||
} | ||
|
||
func (e *jsonEncoder) Buffer() buffer { | ||
return e.body | ||
} |
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,78 @@ | ||
package router | ||
|
||
import ( | ||
"fmt" | ||
"strings" | ||
|
||
"github.com/pkg/errors" | ||
) | ||
|
||
const ( | ||
defaultCloudFrontNumColumns = 33 | ||
) | ||
|
||
//recordParser predefined errors | ||
var ( | ||
SkipLine = errors.New("Please skip this line.") | ||
) | ||
|
||
type recordParserFunc func([]byte, *record) error | ||
|
||
func (p recordParserFunc) Parse(bs []byte, r *record) error { | ||
return p(bs, r) | ||
} | ||
|
||
type cloudfrontParser struct { | ||
version string | ||
fields []string | ||
} | ||
|
||
func (p *cloudfrontParser) Parse(bs []byte, r *record) error { | ||
str := string(bs) | ||
rec := make(record, defaultCloudFrontNumColumns) | ||
*r = rec | ||
if str[0] == '#' { | ||
part := strings.SplitN(str[1:], ":", 2) | ||
if len(part) != 2 { | ||
return SkipLine | ||
} | ||
key := strings.TrimSpace(part[0]) | ||
value := strings.TrimSpace(part[1]) | ||
switch key { | ||
case "Version": | ||
p.version = value | ||
case "Fields": | ||
rawFields := strings.Split(value, " ") | ||
//convert to snake case | ||
fields := make([]string, 0, len(rawFields)) | ||
replaceTargets := []string{"(", ")", "-"} | ||
for _, rawField := range rawFields { | ||
field := rawField | ||
for _, target := range replaceTargets { | ||
field = strings.ReplaceAll(field, target, "_") | ||
} | ||
field = strings.ToLower(field) | ||
field = strings.TrimRight(field, "_") | ||
fields = append(fields, field) | ||
} | ||
p.fields = fields | ||
} | ||
return SkipLine | ||
} | ||
values := strings.Split(str, "\t") | ||
if len(values) > len(p.fields) { | ||
return fmt.Errorf("this row has more values than fields, num of values = %d, num of feilds = %d", len(values), len(p.fields)) | ||
} | ||
var dateValue, timeValue string | ||
for i, field := range p.fields { | ||
rec[field] = values[i] | ||
if field == "date" { | ||
dateValue = values[i] | ||
} | ||
if field == "time" { | ||
timeValue = values[i] | ||
} | ||
} | ||
rec["datetime"] = dateValue + "T" + timeValue + "Z" | ||
return nil | ||
} |
Oops, something went wrong.