jsonLog.go 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. package jsonLog
  2. import (
  3. "time"
  4. "strconv"
  5. "context"
  6. "go-common/app/service/ops/log-agent/event"
  7. "go-common/app/service/ops/log-agent/processor"
  8. "go-common/app/service/ops/log-agent/pkg/common"
  9. "go-common/app/service/ops/log-agent/pkg/flowmonitor"
  10. )
  11. const (
  12. _appIdKey = `"app_id":`
  13. _levelKey = `"level":`
  14. _logTime = `"time":`
  15. )
  16. var (
  17. local, _ = time.LoadLocation("Local")
  18. )
  19. type JsonLog struct {
  20. c *Config
  21. }
  22. func init() {
  23. err := processor.Register("jsonLog", Process)
  24. if err != nil {
  25. panic(err)
  26. }
  27. }
  28. func Process(ctx context.Context, config interface{}, input <-chan *event.ProcessorEvent) (output chan *event.ProcessorEvent, err error) {
  29. jsonLog := new(JsonLog)
  30. if c, ok := config.(*Config); !ok {
  31. panic("Error config for jsonLog Processor")
  32. } else {
  33. if err = c.ConfigValidate(); err != nil {
  34. return nil, err
  35. }
  36. jsonLog.c = c
  37. }
  38. output = make(chan *event.ProcessorEvent)
  39. var (
  40. t time.Time
  41. )
  42. go func() {
  43. for {
  44. select {
  45. case e := <-input:
  46. // only do jsonLog for ops-log
  47. if e.Destination != "lancer-ops-log" {
  48. output <- e
  49. continue
  50. }
  51. if e.Length == 0 {
  52. event.PutEvent(e)
  53. continue
  54. }
  55. // seek app_id
  56. if appId, err := common.SeekValue([]byte(_appIdKey), e.Bytes()); err == nil {
  57. e.AppId = appId
  58. }
  59. // priority
  60. if priority, err := common.GetPriority(e.Bytes()); err == nil {
  61. e.Priority = string(priority)
  62. }
  63. // seek time
  64. if timeValue, err := common.SeekValue([]byte(_logTime), e.Bytes()); err == nil {
  65. if len(timeValue) >= 19 {
  66. // parse time
  67. if t, err = time.Parse(time.RFC3339Nano, string(timeValue)); err != nil {
  68. if t, err = time.ParseInLocation("2006-01-02T15:04:05", string(timeValue), local); err != nil {
  69. if t, err = time.ParseInLocation("2006-01-02T15:04:05", string(timeValue[0:19]), local); err != nil {
  70. }
  71. }
  72. }
  73. e.Time = t
  74. }
  75. }
  76. // TimeRangeKey for flow monitor
  77. if !e.Time.IsZero() {
  78. e.TimeRangeKey = strconv.FormatInt(e.Time.Unix()/100*100, 10)
  79. }
  80. // seek level
  81. if level, err := common.SeekValue([]byte(_levelKey), e.Bytes()); err == nil {
  82. e.Level = level
  83. }
  84. flowmonitor.Fm.AddEvent(e, "log-agent.processor.jsonLog", "OK", "received")
  85. output <- e
  86. case <-ctx.Done():
  87. return
  88. }
  89. }
  90. }()
  91. return output, nil
  92. }