log_sink.go 861 B

1234567891011121314151617181920212223242526272829303132333435363738
  1. package sinks
  2. import (
  3. "context"
  4. "engine/common"
  5. "engine/xstream/collectors"
  6. "fmt"
  7. "sync"
  8. "time"
  9. )
  10. // log action, no properties now
  11. // example: {"log":{}}
  12. func NewLogSink(name string, ruleId string) *collectors.FuncCollector {
  13. return collectors.Func(name, func(ctx context.Context, data interface{}) error {
  14. log := common.GetLogger(ctx)
  15. log.Printf("sink result for rule %s: %s", ruleId, data)
  16. return nil
  17. })
  18. }
  19. type QueryResult struct {
  20. Results []string
  21. LastFetch time.Time
  22. Mux sync.Mutex
  23. }
  24. var QR = &QueryResult{LastFetch:time.Now()}
  25. func NewLogSinkToMemory(name string, ruleId string) *collectors.FuncCollector {
  26. QR.Results = make([]string, 10)
  27. return collectors.Func(name, func(ctx context.Context, data interface{}) error {
  28. QR.Mux.Lock()
  29. QR.Results = append(QR.Results, fmt.Sprintf("%s", data))
  30. QR.Mux.Unlock()
  31. return nil
  32. })
  33. }