opslog_test.go 3.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. package opslog
  2. import (
  3. "bufio"
  4. "context"
  5. "fmt"
  6. "net/http"
  7. "net/http/httptest"
  8. "os"
  9. "strconv"
  10. "strings"
  11. "testing"
  12. "time"
  13. )
  14. func TestOpsLog(t *testing.T) {
  15. testSessionID := "c860e25e5360fc08888a3aaf8c7a0bec"
  16. testResponse := `{"responses":[{"took":4,"timed_out":false,"_shards":{"total":8,"successful":8,"failed":0},"hits":{"total":2,"max_score":null,"hits":[{"_index":"billions-main.web-svr.web-interface-@2018.09.10-uat-1","_type":"logs","_id":"AWXBhTtRe-NhC44S955A","_version":1,"_score":null,"_source":{"@timestamp":"2018-09-10T03:27:34.42933Z","app_id":"main.web-svr.web-interface","args":"","env":"uat","error":"","instance_id":"web-interface-32096-758958f64f-k4gnc","ip":"172.22.35.133:9000","level":"INFO","level_value":1,"path":"/passport.service.identify.v1.Identify/GetCookieInfo","ret":0,"source":"go-common/library/net/rpc/warden.logging:195","stack":"\u003cnil\u003e","traceid":"2406767965117552819","ts":0.001041696,"user":"","zone":"sh001"},"fields":{"@timestamp":[1536550054429]},"highlight":{"traceid":["@kibana-highlighted-field@2406767965117552819@/kibana-highlighted-field@"]},"sort":[1536550054429]},{"_index":"billions-main.web-svr.web-interface-@2018.09.10-uat-1","_type":"logs","_id":"AWXBhTfFS1y0J6vacgAH","_version":1,"_score":null,"_source":{"@timestamp":"2018-09-10T03:27:34.429376Z","app_id":"main.web-svr.web-interface","env":"uat","err":"-101","instance_id":"web-interface-32096-758958f64f-k4gnc","ip":"10.23.50.21","level":"ERROR","level_value":3,"method":"GET","mid":null,"msg":"账号未登录","params":"","path":"/x/web-interface/nav","ret":-101,"source":"go-common/library/net/http/blademaster.Logger.func1:46","stack":"-101","traceid":"2406767965117552819","ts":0.001167299,"user":"no_user","zone":"sh001"},"fields":{"@timestamp":[1536550054429]},"highlight":{"traceid":["@kibana-highlighted-field@2406767965117552819@/kibana-highlighted-field@"]},"sort":[1536550054429]}]},"aggregations":{"2":{"buckets":[{"key_as_string":"2018-09-10T09:00:00.000+08:00","key":1536541200000,"doc_count":2}]}},"status":200}]}`
  17. svr := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  18. session, err := r.Cookie(_ajsSessioID)
  19. if err != nil || session.Value != testSessionID {
  20. w.WriteHeader(http.StatusBadRequest)
  21. fmt.Fprintf(w, "invalid session id: %s", session.Value)
  22. return
  23. }
  24. bufReader := bufio.NewReader(r.Body)
  25. first, err := bufReader.ReadString('\n')
  26. if err != nil {
  27. w.WriteHeader(http.StatusInternalServerError)
  28. return
  29. }
  30. if !strings.Contains(first, "billions-main.web-svr.web-interface*") {
  31. w.WriteHeader(http.StatusBadRequest)
  32. fmt.Fprintf(w, "invalid familys: %s", first)
  33. return
  34. }
  35. w.Write([]byte(testResponse))
  36. }))
  37. defer svr.Close()
  38. client := New(svr.URL, nil)
  39. end := time.Now().Unix()
  40. start := end - 3600
  41. familys := []string{"main.web-svr.web-interface"}
  42. records, err := client.Query(context.Background(), familys, 8111326167741382285, testSessionID, start, end)
  43. if err != nil {
  44. t.Fatal(err)
  45. }
  46. for _, record := range records {
  47. t.Logf("record: %v", record)
  48. }
  49. }
  50. func TestOpsLogReal(t *testing.T) {
  51. sessionID := os.Getenv("TEST_SESSION_ID")
  52. if sessionID == "" {
  53. t.Skipf("miss sessionID skip test")
  54. }
  55. traceID, _ := strconv.ParseUint("7b91b9a72f87c13", 16, 64)
  56. client := New("http://uat-ops-log.bilibili.co/elasticsearch/_msearch", nil)
  57. records, err := client.Query(context.Background(), []string{"main.community.tag"}, traceID, sessionID, 1545296000, 1545296286)
  58. if err != nil {
  59. t.Fatal(err)
  60. }
  61. for _, record := range records {
  62. t.Logf("record: %v", record)
  63. }
  64. }