classify.go 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. package classify
  2. import (
  3. "strings"
  4. "context"
  5. "go-common/app/service/ops/log-agent/event"
  6. "go-common/app/service/ops/log-agent/processor"
  7. "go-common/app/service/ops/log-agent/pkg/common"
  8. )
  9. type Classify struct {
  10. c *Config
  11. }
  12. func init() {
  13. err := processor.Register("classify", Process)
  14. if err != nil {
  15. panic(err)
  16. }
  17. }
  18. func Process(ctx context.Context, config interface{}, input <-chan *event.ProcessorEvent) (output chan *event.ProcessorEvent, err error) {
  19. classify := new(Classify)
  20. if c, ok := config.(*Config); !ok {
  21. panic("Error config for Classify Processor")
  22. } else {
  23. if err = c.ConfigValidate(); err != nil {
  24. return nil, err
  25. }
  26. classify.c = c
  27. }
  28. output = make(chan *event.ProcessorEvent)
  29. go func() {
  30. for {
  31. select {
  32. case e := <-input:
  33. // only do classify for ops-log
  34. if e.Destination == "lancer-ops-log" {
  35. if common.CriticalLog(e.Level) || e.Priority == "high" {
  36. e.LogId = classify.getLogIdByLevel("important")
  37. } else {
  38. e.LogId = classify.getLogIdByAppId(e.AppId)
  39. }
  40. }
  41. output <- e
  42. case <-ctx.Done():
  43. return
  44. }
  45. }
  46. }()
  47. return output, nil
  48. }
  49. // getLogLevel get logId level by appId
  50. func (c *Classify) getLogIdByAppId(appId []byte) (logId string) {
  51. // get logId by setting
  52. if logLevel, ok := c.c.LogLevelMapConfig[string(appId)]; ok {
  53. return c.getLogIdByLevel(logLevel)
  54. }
  55. // appId format error, logId 1
  56. if len(strings.Split(string(appId), ".")) < 3 {
  57. return c.getLogIdByLevel("low") // low level
  58. }
  59. // set logLevel to 2 by default
  60. return c.getLogIdByLevel("normal") // normal level
  61. }
  62. // getLogIdByLevel return logid by level
  63. func (c *Classify) getLogIdByLevel(level string) (logId string) {
  64. if logId, ok := c.c.LogIdMapConfig[level]; ok {
  65. return logId
  66. } else {
  67. // return 000161 by default
  68. return "000161"
  69. }
  70. }