main.go 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. // m5-tail-test connects to the ingestd live-tail endpoint and
  2. // prints every alert frame it receives until --duration
  3. // elapses. This is the smoke driver used in scripts/m5_smoke.sh
  4. // to assert that the tail hub actually fans out accepted alerts.
  5. package main
  6. import (
  7. "flag"
  8. "fmt"
  9. "net/http"
  10. "os"
  11. "sync/atomic"
  12. "time"
  13. "github.com/gorilla/websocket"
  14. )
  15. func main() {
  16. target := flag.String("target", "ws://localhost:8800/v1/tail/ws", "tail ws URL")
  17. token := flag.String("token", "tail-dev-token-please-change-in-prod", "tail token")
  18. companyID := flag.String("company", "", "company filter (empty=all)")
  19. duration := flag.Duration("duration", 8*time.Second, "max listen time")
  20. flag.Parse()
  21. url := *target
  22. if *companyID != "" {
  23. url += "?company_id=" + *companyID + "&token=" + *token
  24. } else {
  25. url += "?token=" + *token
  26. }
  27. dialer := *websocket.DefaultDialer
  28. dialer.HandshakeTimeout = 5 * time.Second
  29. hdr := http.Header{}
  30. hdr.Set("X-BA-Tail-Token", *token)
  31. conn, resp, err := dialer.Dial(url, hdr)
  32. if err != nil {
  33. fmt.Println("dial err:", err, "resp:", resp)
  34. os.Exit(1)
  35. }
  36. defer conn.Close()
  37. fmt.Println("TAIL CONNECTED", url)
  38. var frames atomic.Int64
  39. deadline := time.Now().Add(*duration)
  40. // Set a single, long read deadline. We don't poll. We block
  41. // on the first frame; if it never arrives we let the
  42. // overall duration kill us.
  43. _ = conn.SetReadDeadline(deadline)
  44. for {
  45. _, msg, err := conn.ReadMessage()
  46. if err != nil {
  47. fmt.Println("read err:", err)
  48. break
  49. }
  50. frames.Add(1)
  51. fmt.Println("FRAME:", string(msg))
  52. // Move the deadline forward after each successful
  53. // read so a busy tail keeps the test alive.
  54. _ = conn.SetReadDeadline(time.Now().Add(*duration))
  55. }
  56. fmt.Printf("done: %d frames\n", frames.Load())
  57. }