|
| 1 | +package exporters |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "io" |
| 6 | + "net/http" |
| 7 | + "net/http/httptest" |
| 8 | + "os" |
| 9 | + "strings" |
| 10 | + "testing" |
| 11 | + "time" |
| 12 | + |
| 13 | + "github.com/spdeepak/flowtracker" |
| 14 | +) |
| 15 | + |
| 16 | +func NewSankeyServer(tags []string, includeAllTags bool) http.Handler { |
| 17 | + mux := http.NewServeMux() |
| 18 | + mux.HandleFunc("/", Handler) |
| 19 | + mw := flowtracker.NewMiddleware(flowtracker.WithExporter(&SankeyExporter{IncludeTags: tags, IncludeAllTags: includeAllTags})) |
| 20 | + return mw(mux) |
| 21 | +} |
| 22 | + |
| 23 | +func TestSankeyExporter_OK(t *testing.T) { |
| 24 | + // capture stdout |
| 25 | + old := os.Stdout |
| 26 | + r, w, _ := os.Pipe() |
| 27 | + os.Stdout = w |
| 28 | + |
| 29 | + server := NewSankeyServer([]string{}, true) |
| 30 | + |
| 31 | + req := httptest.NewRequest(http.MethodGet, "/", nil) |
| 32 | + rr := httptest.NewRecorder() |
| 33 | + |
| 34 | + server.ServeHTTP(rr, req) |
| 35 | + |
| 36 | + time.Sleep(1 * time.Second) |
| 37 | + |
| 38 | + // restore stdout |
| 39 | + w.Close() |
| 40 | + os.Stdout = old |
| 41 | + |
| 42 | + // read logs |
| 43 | + var buf bytes.Buffer |
| 44 | + _, err := io.Copy(&buf, r) |
| 45 | + if err != nil { |
| 46 | + t.Error("Error should be nil") |
| 47 | + } |
| 48 | + |
| 49 | + logs := buf.String() |
| 50 | + output := strings.Split(logs, "\n") |
| 51 | + if !strings.Contains(output[1], "----- START SANKEY DATA (trace id: trace-") { |
| 52 | + t.Fatalf("expected log not found: %s", output[1]) |
| 53 | + } |
| 54 | + if !strings.Contains(output[2], "GET / [") { |
| 55 | + t.Fatalf("expected log not found: %s", output[2]) |
| 56 | + } |
| 57 | + if !strings.Contains(output[2], "] Process Payment (currency:USD)") { |
| 58 | + t.Fatalf("expected log not found: %s", output[2]) |
| 59 | + } |
| 60 | + if !strings.Contains(output[3], "GET / [") { |
| 61 | + t.Fatalf("expected log not found: %s", output[3]) |
| 62 | + } |
| 63 | + if !strings.Contains(output[3], "] DB: Select User (db.query:SELECT * FROM users...)") { |
| 64 | + t.Fatalf("expected log not found: %s", output[3]) |
| 65 | + } |
| 66 | + if !strings.Contains(output[4], "GET / [") { |
| 67 | + t.Fatalf("expected log not found: %s", output[4]) |
| 68 | + } |
| 69 | + if !strings.Contains(output[4], "] HTTP: Shipping Service") { |
| 70 | + t.Fatalf("expected log not found: %s", output[4]) |
| 71 | + } |
| 72 | + if !strings.Contains(output[5], "HTTP: Shipping Service [") { |
| 73 | + t.Fatalf("expected log not found: %s", output[5]) |
| 74 | + } |
| 75 | + if !strings.Contains(output[5], "] Calculate Weight") { |
| 76 | + t.Fatalf("expected log not found: %s", output[5]) |
| 77 | + } |
| 78 | + if !strings.Contains(output[6], "----- END SANKEY DATA (trace id: trace-") { |
| 79 | + t.Fatalf("expected log not found: %s", output[6]) |
| 80 | + } |
| 81 | +} |
| 82 | + |
| 83 | +func TestSankeyExporter_OK_IncludeTags(t *testing.T) { |
| 84 | + // capture stdout |
| 85 | + old := os.Stdout |
| 86 | + r, w, _ := os.Pipe() |
| 87 | + os.Stdout = w |
| 88 | + |
| 89 | + server := NewSankeyServer([]string{"db.query"}, false) |
| 90 | + |
| 91 | + req := httptest.NewRequest(http.MethodGet, "/", nil) |
| 92 | + rr := httptest.NewRecorder() |
| 93 | + |
| 94 | + server.ServeHTTP(rr, req) |
| 95 | + |
| 96 | + time.Sleep(1 * time.Second) |
| 97 | + |
| 98 | + // restore stdout |
| 99 | + w.Close() |
| 100 | + os.Stdout = old |
| 101 | + |
| 102 | + // read logs |
| 103 | + var buf bytes.Buffer |
| 104 | + _, err := io.Copy(&buf, r) |
| 105 | + if err != nil { |
| 106 | + t.Error("Error should be nil") |
| 107 | + } |
| 108 | + |
| 109 | + logs := buf.String() |
| 110 | + output := strings.Split(logs, "\n") |
| 111 | + if !strings.Contains(output[1], "----- START SANKEY DATA (trace id") { |
| 112 | + t.Fatalf("expected log not found: %s", output[1]) |
| 113 | + } |
| 114 | + if !strings.Contains(output[2], "GET / [") { |
| 115 | + t.Fatalf("expected log not found: %s", output[2]) |
| 116 | + } |
| 117 | + if !strings.Contains(output[2], "] Process Payment") { |
| 118 | + t.Fatalf("expected log not found: %s", output[2]) |
| 119 | + } |
| 120 | + if !strings.Contains(output[3], "GET / [") { |
| 121 | + t.Fatalf("expected log not found: %s", output[3]) |
| 122 | + } |
| 123 | + if !strings.Contains(output[3], "] DB: Select User (db.query:SELECT * FROM users...)") { |
| 124 | + t.Fatalf("expected log not found: %s", output[3]) |
| 125 | + } |
| 126 | + if !strings.Contains(output[4], "GET / [") { |
| 127 | + t.Fatalf("expected log not found: %s", output[4]) |
| 128 | + } |
| 129 | + if !strings.Contains(output[4], "] HTTP: Shipping Service") { |
| 130 | + t.Fatalf("expected log not found: %s", output[4]) |
| 131 | + } |
| 132 | + if !strings.Contains(output[5], "HTTP: Shipping Service [") { |
| 133 | + t.Fatalf("expected log not found: %s", output[5]) |
| 134 | + } |
| 135 | + if !strings.Contains(output[5], "] Calculate Weight") { |
| 136 | + t.Fatalf("expected log not found: %s", output[5]) |
| 137 | + } |
| 138 | + if !strings.Contains(output[6], "----- END SANKEY DATA (trace id: trace-") { |
| 139 | + t.Fatalf("expected log not found: %s", output[6]) |
| 140 | + } |
| 141 | +} |
0 commit comments