Create & Init Project...

This commit is contained in:
2019-04-22 18:49:16 +08:00
commit fc4fa37393
25440 changed files with 4054998 additions and 0 deletions

View File

@@ -0,0 +1,39 @@
package(default_visibility = ["//visibility:public"])
load(
"@io_bazel_rules_go//go:def.bzl",
"go_library",
)
go_library(
name = "go_default_library",
srcs = [
"classify.go",
"config.go",
],
importpath = "go-common/app/service/ops/log-agent/processor/classify",
tags = ["automanaged"],
visibility = ["//visibility:public"],
deps = [
"//app/service/ops/log-agent/conf/configcenter:go_default_library",
"//app/service/ops/log-agent/event:go_default_library",
"//app/service/ops/log-agent/pkg/common:go_default_library",
"//app/service/ops/log-agent/processor:go_default_library",
"//library/log:go_default_library",
"//vendor/github.com/BurntSushi/toml:go_default_library",
],
)
filegroup(
name = "package-srcs",
srcs = glob(["**"]),
tags = ["automanaged"],
visibility = ["//visibility:private"],
)
filegroup(
name = "all-srcs",
srcs = [":package-srcs"],
tags = ["automanaged"],
visibility = ["//visibility:public"],
)

View File

@@ -0,0 +1,80 @@
package classify
import (
"strings"
"context"
"go-common/app/service/ops/log-agent/event"
"go-common/app/service/ops/log-agent/processor"
"go-common/app/service/ops/log-agent/pkg/common"
)
type Classify struct {
c *Config
}
func init() {
err := processor.Register("classify", Process)
if err != nil {
panic(err)
}
}
func Process(ctx context.Context, config interface{}, input <-chan *event.ProcessorEvent) (output chan *event.ProcessorEvent, err error) {
classify := new(Classify)
if c, ok := config.(*Config); !ok {
panic("Error config for Classify Processor")
} else {
if err = c.ConfigValidate(); err != nil {
return nil, err
}
classify.c = c
}
output = make(chan *event.ProcessorEvent)
go func() {
for {
select {
case e := <-input:
// only do classify for ops-log
if e.Destination == "lancer-ops-log" {
if common.CriticalLog(e.Level) || e.Priority == "high" {
e.LogId = classify.getLogIdByLevel("important")
} else {
e.LogId = classify.getLogIdByAppId(e.AppId)
}
}
output <- e
case <-ctx.Done():
return
}
}
}()
return output, nil
}
// getLogLevel get logId level by appId
func (c *Classify) getLogIdByAppId(appId []byte) (logId string) {
// get logId by setting
if logLevel, ok := c.c.LogLevelMapConfig[string(appId)]; ok {
return c.getLogIdByLevel(logLevel)
}
// appId format error, logId 1
if len(strings.Split(string(appId), ".")) < 3 {
return c.getLogIdByLevel("low") // low level
}
// set logLevel to 2 by default
return c.getLogIdByLevel("normal") // normal level
}
// getLogIdByLevel return logid by level
func (c *Classify) getLogIdByLevel(level string) (logId string) {
if logId, ok := c.c.LogIdMapConfig[level]; ok {
return logId
} else {
// return 000161 by default
return "000161"
}
}

View File

@@ -0,0 +1,101 @@
package classify
import (
"fmt"
"errors"
"time"
"go-common/app/service/ops/log-agent/conf/configcenter"
"go-common/library/log"
"github.com/BurntSushi/toml"
)
const (
logLevelMap = "logLevelMap.toml"
logIdMap = "logIdMap.toml"
)
type Config struct {
Local bool `toml:"local"`
LogLevelMapConfig map[string]string `toml:"logLevelMapConfig"`
LogIdMapConfig map[string]string `toml:"logIdMapConfig"`
PriorityBlackList map[string]string `toml:"priorityBlackList"`
}
func (c *Config) ConfigValidate() (error) {
if c == nil {
return fmt.Errorf("Error can't be nil")
}
if c.LogLevelMapConfig == nil {
c.LogLevelMapConfig = make(map[string]string)
}
if c.LogIdMapConfig == nil {
return fmt.Errorf("LogIdMapConfig of classify can't be nil")
}
if c.PriorityBlackList == nil {
c.PriorityBlackList = make(map[string]string)
}
return nil
}
func DecodeConfig(md toml.MetaData, primValue toml.Primitive) (c interface{}, err error) {
config := new(Config)
if err = md.PrimitiveDecode(primValue, config); err != nil {
return nil, err
}
// read config from config center
if !config.Local {
if err = config.readConfig(); err != nil {
return nil, err
}
// watch update and reload config
go func() {
currentVersion := configcenter.Version
for {
if currentVersion != configcenter.Version {
log.Info("classify config reload")
if err := config.readConfig(); err != nil {
log.Error("classify config reload error (%v)", err)
}
currentVersion = configcenter.Version
}
time.Sleep(time.Second)
}
}()
}
return config, nil
}
func (c *Config) readConfig() (err error) {
var (
ok bool
value string
tmplogLevelMap map[string]string
tmplogIdMap map[string]string
)
// logLevel config
if value, ok = configcenter.Client.Value(logLevelMap); !ok {
return errors.New("failed to get logLevelMap.toml")
}
if _, err = toml.Decode(value, &tmplogLevelMap); err != nil {
return err
}
c.LogLevelMapConfig = tmplogLevelMap
// logIdMap config
if value, ok = configcenter.Client.Value(logIdMap); !ok {
return errors.New("failed to get logIdMap.toml")
}
if _, err = toml.Decode(value, &tmplogIdMap); err != nil {
return err
}
c.LogIdMapConfig = tmplogIdMap
return nil
}