summaryrefslogtreecommitdiff
path: root/internal/mapr/logformat/parser.go
diff options
context:
space:
mode:
Diffstat (limited to 'internal/mapr/logformat/parser.go')
-rw-r--r--internal/mapr/logformat/parser.go61
1 files changed, 12 insertions, 49 deletions
diff --git a/internal/mapr/logformat/parser.go b/internal/mapr/logformat/parser.go
index b980e3d..2d9634d 100644
--- a/internal/mapr/logformat/parser.go
+++ b/internal/mapr/logformat/parser.go
@@ -2,9 +2,6 @@ package logformat
import (
"errors"
- "fmt"
- "reflect"
- "strings"
"time"
"github.com/mimecast/dtail/internal/config"
@@ -15,61 +12,27 @@ import (
var ErrIgnoreFields error = errors.New("Ignore this field set")
// Parser is used to parse the mapreduce information from the server log files.
-type Parser struct {
- hostname string
- makeFieldsFunc reflect.Value
- makeFieldsReceiver reflect.Value
- timeZoneName string
- timeZoneOffset string
+type Parser interface {
+ // MakeFields creates a field map from an input log line.
+ MakeFields(string) (map[string]string, error)
}
// NewParser returns a new log parser.
-func NewParser(logFormatName string, query *mapr.Query) (*Parser, error) {
+func NewParser(logFormatName string, query *mapr.Query) (Parser, error) {
hostname, err := config.Hostname()
if err != nil {
return nil, err
}
now := time.Now()
- zone, offset := now.Zone()
+ timeZoneName, timeZoneOffset := now.Zone()
- p := Parser{
- hostname: hostname,
- timeZoneName: zone,
- timeZoneOffset: fmt.Sprintf("%d", offset),
- }
-
- err = p.reflectLogFormat(logFormatName)
- if err != nil {
- return nil, err
- }
- return &p, nil
-}
-
-// The aim of this is that everyone can plug in their own mapr log format
-// parsing method to DTail. Just add a method MakeFieldsMODULENAME to type
-// Parser. Whereas MODULENAME must be a upeprcase string.
-func (p *Parser) reflectLogFormat(logFormatName string) error {
- methodName := fmt.Sprintf("MakeFields%s", strings.ToUpper(logFormatName))
- rt := reflect.TypeOf(p)
- method, ok := rt.MethodByName(methodName)
- if !ok {
- return errors.New("No such mapr log format module: " + methodName)
- }
-
- p.makeFieldsFunc = method.Func
- p.makeFieldsReceiver = reflect.ValueOf(p)
- return nil
-}
+ switch logFormatName {
+ case "generic":
+ return newGenericParser(hostname, timeZoneName, timeZoneOffset)
+ case "generickv":
+ return newGenericKVParser(hostname, timeZoneName, timeZoneOffset)
+ default:
+ return newDefaultParser(hostname, timeZoneName, timeZoneOffset)
-// MakeFields is for returning the fields from a given log line.
-func (p *Parser) MakeFields(maprLine string) (fields map[string]string, err error) {
- inputValues := []reflect.Value{p.makeFieldsReceiver, reflect.ValueOf(maprLine)}
- returnValues := p.makeFieldsFunc.Call(inputValues)
- errInterface := returnValues[1].Interface()
- if errInterface == nil {
- fields, err = returnValues[0].Interface().(map[string]string), nil
- return
}
- fields, err = returnValues[0].Interface().(map[string]string), errInterface.(error)
- return
}