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,36 @@
package(default_visibility = ["//visibility:public"])
load(
"@io_bazel_rules_go//go:def.bzl",
"go_library",
)
go_library(
name = "go_default_library",
srcs = [
"config.go",
"fileLog.go",
],
importpath = "go-common/app/service/ops/log-agent/processor/fileLog",
tags = ["automanaged"],
visibility = ["//visibility:public"],
deps = [
"//app/service/ops/log-agent/event:go_default_library",
"//app/service/ops/log-agent/processor: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,24 @@
package fileLog
import (
"fmt"
"github.com/BurntSushi/toml"
)
type Config struct {
}
func (c *Config) ConfigValidate() (error) {
if c == nil {
return fmt.Errorf("Error can't be nil")
}
return nil
}
func DecodeConfig(md toml.MetaData, primValue toml.Primitive) (c interface{}, err error) {
c = new(Config)
if err = md.PrimitiveDecode(primValue, c); err != nil {
return nil, err
}
return c, nil
}

View File

@@ -0,0 +1,94 @@
package fileLog
import (
"time"
"context"
"encoding/json"
"os"
"go-common/app/service/ops/log-agent/event"
"go-common/app/service/ops/log-agent/processor"
)
const (
_logIdLen = 6
_logLancerHeaderLen = 19
_appIdKey = `"app_id":`
_levelKey = `"level":`
_logTime = `"time":`
)
var (
local, _ = time.LoadLocation("Local")
hostname, _ = os.Hostname()
)
type FileLog struct {
c *Config
}
func init() {
err := processor.Register("fileLog", Process)
if err != nil {
panic(err)
}
}
func Process(ctx context.Context, config interface{}, input <-chan *event.ProcessorEvent) (output chan *event.ProcessorEvent, err error) {
fileLog := new(FileLog)
if c, ok := config.(*Config); !ok {
panic("Error config for jsonLog Processor")
} else {
if err = c.ConfigValidate(); err != nil {
return nil, err
}
fileLog.c = c
}
output = make(chan *event.ProcessorEvent)
go func() {
for {
select {
case e := <-input:
// only do jsonLog for ops-log
if e.Destination != "lancer-ops-log" {
output <- e
continue
}
// format message
message := make(map[string]interface{})
if len(e.ParsedFields) != 0 {
for k, v := range e.ParsedFields {
message[k] = v
}
}
message["log"] = e.String()
e.Fields["hostname"] = hostname
if len(e.Tags) != 0 {
e.Fields["tag"] = e.Tags
}
if len(e.Fields) != 0 {
message["fields"] = e.Fields
}
message["app_id"] = string(e.AppId)
message["time"] = e.Time.UTC().Format(time.RFC3339Nano)
if body, err := json.Marshal(message); err == nil {
e.Write(body)
output <- e
}
case <-ctx.Done():
return
}
}
}()
return output, nil
}