zpages_test.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. // Copyright 2017, OpenCensus Authors
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. //
  15. package zpages
  16. import (
  17. "bytes"
  18. "reflect"
  19. "testing"
  20. "time"
  21. "fmt"
  22. "net/http"
  23. "net/http/httptest"
  24. "go.opencensus.io/trace"
  25. )
  26. var (
  27. tid = trace.TraceID{1, 2, 3, 4, 5, 6, 7, 8, 1, 2, 4, 8, 16, 32, 64, 128}
  28. sid = trace.SpanID{1, 2, 4, 8, 16, 32, 64, 128}
  29. sid2 = trace.SpanID{0, 3, 5, 9, 17, 33, 65, 129}
  30. )
  31. func TestTraceRows(t *testing.T) {
  32. now := time.Now()
  33. later := now.Add(2 * time.Second)
  34. data := traceDataFromSpans("foo", []*trace.SpanData{{
  35. SpanContext: trace.SpanContext{TraceID: tid, SpanID: sid},
  36. ParentSpanID: sid2,
  37. Name: "foo",
  38. StartTime: now,
  39. EndTime: later,
  40. Attributes: map[string]interface{}{"stringkey": "stringvalue", "intkey": 42, "boolkey": true},
  41. Annotations: []trace.Annotation{
  42. {Time: now.Add(time.Millisecond), Message: "hello, world", Attributes: map[string]interface{}{"foo": "bar"}},
  43. {Time: now.Add(1500 * time.Millisecond), Message: "hello, world"},
  44. },
  45. MessageEvents: []trace.MessageEvent{
  46. {Time: now, EventType: 2, MessageID: 0x3, UncompressedByteSize: 0x190, CompressedByteSize: 0x12c},
  47. {Time: later, EventType: 1, MessageID: 0x1, UncompressedByteSize: 0xc8, CompressedByteSize: 0x64},
  48. },
  49. Status: trace.Status{
  50. Code: 1,
  51. Message: "d'oh!",
  52. },
  53. }})
  54. fakeTime := "2006/01/02-15:04:05.123456"
  55. for i := range data.Rows {
  56. data.Rows[i].Fields[0] = fakeTime
  57. }
  58. if want := (traceData{
  59. Name: "foo",
  60. Num: 1,
  61. Rows: []traceRow{
  62. {Fields: [3]string{fakeTime, " 2.000000", ""}, SpanContext: trace.SpanContext{TraceID: tid, SpanID: sid}, ParentSpanID: sid2},
  63. {Fields: [3]string{fakeTime, "", `Status{canonicalCode=CANCELLED, description="d'oh!"}`}},
  64. {Fields: [3]string{fakeTime, "", `Attributes:{boolkey=true, intkey=42, stringkey="stringvalue"}`}},
  65. {Fields: [3]string{fakeTime, " . 0", "received message [400 bytes, 300 compressed bytes]"}},
  66. {Fields: [3]string{fakeTime, " . 1000", `hello, world Attributes:{foo="bar"}`}},
  67. {Fields: [3]string{fakeTime, " 1.499000", "hello, world"}},
  68. {Fields: [3]string{fakeTime, " .500000", "sent message [200 bytes, 100 compressed bytes]"}}}}); !reflect.DeepEqual(data, want) {
  69. t.Errorf("traceRows: got %v want %v\n", data, want)
  70. }
  71. var buf bytes.Buffer
  72. writeTextTraces(&buf, data)
  73. if want := `When Elapsed(s) Type
  74. 2006/01/02-15:04:05.123456 2.000000 trace_id: 01020304050607080102040810204080 span_id: 0102040810204080 parent_span_id: 0003050911214181
  75. 2006/01/02-15:04:05.123456 Status{canonicalCode=CANCELLED, description="d'oh!"}
  76. 2006/01/02-15:04:05.123456 Attributes:{boolkey=true, intkey=42, stringkey="stringvalue"}
  77. 2006/01/02-15:04:05.123456 . 0 received message [400 bytes, 300 compressed bytes]
  78. 2006/01/02-15:04:05.123456 . 1000 hello, world Attributes:{foo="bar"}
  79. 2006/01/02-15:04:05.123456 1.499000 hello, world
  80. 2006/01/02-15:04:05.123456 .500000 sent message [200 bytes, 100 compressed bytes]
  81. `; buf.String() != want {
  82. t.Errorf("writeTextTraces: got %q want %q\n", buf.String(), want)
  83. }
  84. }
  85. func TestGetZPages(t *testing.T) {
  86. mux := http.NewServeMux()
  87. Handle(mux, "/debug")
  88. server := httptest.NewServer(mux)
  89. defer server.Close()
  90. tests := []string{"/debug/rpcz", "/debug/tracez"}
  91. for _, tt := range tests {
  92. t.Run(fmt.Sprintf("GET %s", tt), func(t *testing.T) {
  93. res, err := http.Get(server.URL + tt)
  94. if err != nil {
  95. t.Error(err)
  96. return
  97. }
  98. if got, want := res.StatusCode, http.StatusOK; got != want {
  99. t.Errorf("res.StatusCode = %d; want %d", got, want)
  100. }
  101. })
  102. }
  103. }
  104. func TestGetZPages_default(t *testing.T) {
  105. server := httptest.NewServer(Handler)
  106. defer server.Close()
  107. tests := []string{"/rpcz", "/tracez"}
  108. for _, tt := range tests {
  109. t.Run(fmt.Sprintf("GET %s", tt), func(t *testing.T) {
  110. res, err := http.Get(server.URL + tt)
  111. if err != nil {
  112. t.Error(err)
  113. return
  114. }
  115. if got, want := res.StatusCode, http.StatusOK; got != want {
  116. t.Errorf("res.StatusCode = %d; want %d", got, want)
  117. }
  118. })
  119. }
  120. }