forked from AliyunContainerService/log-pilot
-
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 AliyunContainerService#6 from jzwlqx/refactor
refactor and add regexp log format support
- Loading branch information
Showing
5 changed files
with
199 additions
and
42 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,62 @@ | ||
package pilot | ||
|
||
import "fmt" | ||
|
||
type FormatConverter func(info *LogInfoNode) (map[string]string, error) | ||
|
||
var converters = make(map[string]FormatConverter) | ||
|
||
func Register(format string, converter FormatConverter) { | ||
converters[format] = converter | ||
} | ||
|
||
func Convert(info *LogInfoNode) (map[string]string, error) { | ||
converter := converters[info.value] | ||
if converter == nil { | ||
return nil, fmt.Errorf("unsupported log format: %s", info.value) | ||
} | ||
return converter(info) | ||
} | ||
|
||
type SimpleConverter struct { | ||
properties map[string]bool | ||
} | ||
|
||
func init() { | ||
|
||
simpleConverter := func(properties []string) FormatConverter { | ||
return func(info *LogInfoNode) (map[string]string, error) { | ||
validProperties := make(map[string]bool) | ||
for _, property := range properties { | ||
validProperties[property] = true | ||
} | ||
ret := make(map[string]string) | ||
for k, v := range info.children { | ||
if _, ok := validProperties[k]; !ok { | ||
return nil, fmt.Errorf("%s is not a valid properties for format %s", k, info.value) | ||
} | ||
ret[k] = v.value | ||
} | ||
return ret, nil | ||
} | ||
} | ||
|
||
Register("none", simpleConverter([]string{})) | ||
Register("csv", simpleConverter([]string{"time_key", "time_format", "keys"})) | ||
Register("json", simpleConverter([]string{"time_key", "time_format"})) | ||
Register("regexp", simpleConverter([]string{"time_key", "time_format", })) | ||
Register("apache2", simpleConverter([]string{})) | ||
Register("apache_error", simpleConverter([]string{})) | ||
Register("nginx", simpleConverter([]string{})) | ||
Register("regexp", func(info *LogInfoNode) (map[string]string, error) { | ||
ret, err := simpleConverter([]string{"pattern", "time_format"})(info) | ||
if err != nil { | ||
return ret, err | ||
} | ||
if ret["pattern"] == "" { | ||
return nil, fmt.Errorf("regex pattern can not be emtpy") | ||
} | ||
return ret, nil | ||
}) | ||
} | ||
|
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