end_to_end_test.go 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. // Copyright 2018, 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 ocgrpc_test
  16. import (
  17. "context"
  18. "io"
  19. "reflect"
  20. "testing"
  21. "go.opencensus.io/internal/testpb"
  22. "go.opencensus.io/plugin/ocgrpc"
  23. "go.opencensus.io/stats/view"
  24. "go.opencensus.io/tag"
  25. )
  26. var keyAccountId, _ = tag.NewKey("account_id")
  27. func TestEndToEnd_Single(t *testing.T) {
  28. view.Register(ocgrpc.DefaultClientViews...)
  29. defer view.Unregister(ocgrpc.DefaultClientViews...)
  30. view.Register(ocgrpc.DefaultServerViews...)
  31. defer view.Unregister(ocgrpc.DefaultServerViews...)
  32. extraViews := []*view.View{
  33. ocgrpc.ServerReceivedMessagesPerRPCView,
  34. ocgrpc.ClientReceivedMessagesPerRPCView,
  35. ocgrpc.ServerSentMessagesPerRPCView,
  36. ocgrpc.ClientSentMessagesPerRPCView,
  37. }
  38. view.Register(extraViews...)
  39. defer view.Unregister(extraViews...)
  40. client, done := testpb.NewTestClient(t)
  41. defer done()
  42. ctx := context.Background()
  43. ctx, _ = tag.New(ctx, tag.Insert(keyAccountId, "abc123"))
  44. var (
  45. clientMethodTag = tag.Tag{Key: ocgrpc.KeyClientMethod, Value: "testpb.Foo/Single"}
  46. serverMethodTag = tag.Tag{Key: ocgrpc.KeyServerMethod, Value: "testpb.Foo/Single"}
  47. clientStatusOKTag = tag.Tag{Key: ocgrpc.KeyClientStatus, Value: "OK"}
  48. serverStatusOKTag = tag.Tag{Key: ocgrpc.KeyServerStatus, Value: "OK"}
  49. serverStatusUnknownTag = tag.Tag{Key: ocgrpc.KeyClientStatus, Value: "UNKNOWN"}
  50. clientStatusUnknownTag = tag.Tag{Key: ocgrpc.KeyServerStatus, Value: "UNKNOWN"}
  51. )
  52. _, err := client.Single(ctx, &testpb.FooRequest{})
  53. if err != nil {
  54. t.Fatal(err)
  55. }
  56. checkCount(t, ocgrpc.ClientCompletedRPCsView, 1, clientMethodTag, clientStatusOKTag)
  57. checkCount(t, ocgrpc.ServerCompletedRPCsView, 1, serverMethodTag, serverStatusOKTag)
  58. _, _ = client.Single(ctx, &testpb.FooRequest{Fail: true})
  59. checkCount(t, ocgrpc.ClientCompletedRPCsView, 1, clientMethodTag, serverStatusUnknownTag)
  60. checkCount(t, ocgrpc.ServerCompletedRPCsView, 1, serverMethodTag, clientStatusUnknownTag)
  61. tcs := []struct {
  62. v *view.View
  63. tags []tag.Tag
  64. mean float64
  65. }{
  66. {ocgrpc.ClientSentMessagesPerRPCView, []tag.Tag{clientMethodTag}, 1.0},
  67. {ocgrpc.ServerReceivedMessagesPerRPCView, []tag.Tag{serverMethodTag}, 1.0},
  68. {ocgrpc.ClientReceivedMessagesPerRPCView, []tag.Tag{clientMethodTag}, 0.5},
  69. {ocgrpc.ServerSentMessagesPerRPCView, []tag.Tag{serverMethodTag}, 0.5},
  70. {ocgrpc.ClientSentBytesPerRPCView, []tag.Tag{clientMethodTag}, 1.0},
  71. {ocgrpc.ServerReceivedBytesPerRPCView, []tag.Tag{serverMethodTag}, 1.0},
  72. {ocgrpc.ClientReceivedBytesPerRPCView, []tag.Tag{clientMethodTag}, 0.0},
  73. {ocgrpc.ServerSentBytesPerRPCView, []tag.Tag{serverMethodTag}, 0.0},
  74. }
  75. for _, tt := range tcs {
  76. t.Run("view="+tt.v.Name, func(t *testing.T) {
  77. dist := getDistribution(t, tt.v, tt.tags...)
  78. if got, want := dist.Count, int64(2); got != want {
  79. t.Errorf("Count = %d; want %d", got, want)
  80. }
  81. if got, want := dist.Mean, tt.mean; got != want {
  82. t.Errorf("Mean = %v; want %v", got, want)
  83. }
  84. })
  85. }
  86. }
  87. func TestEndToEnd_Stream(t *testing.T) {
  88. view.Register(ocgrpc.DefaultClientViews...)
  89. defer view.Unregister(ocgrpc.DefaultClientViews...)
  90. view.Register(ocgrpc.DefaultServerViews...)
  91. defer view.Unregister(ocgrpc.DefaultServerViews...)
  92. extraViews := []*view.View{
  93. ocgrpc.ServerReceivedMessagesPerRPCView,
  94. ocgrpc.ClientReceivedMessagesPerRPCView,
  95. ocgrpc.ServerSentMessagesPerRPCView,
  96. ocgrpc.ClientSentMessagesPerRPCView,
  97. }
  98. view.Register(extraViews...)
  99. defer view.Unregister(extraViews...)
  100. client, done := testpb.NewTestClient(t)
  101. defer done()
  102. ctx := context.Background()
  103. ctx, _ = tag.New(ctx, tag.Insert(keyAccountId, "abc123"))
  104. var (
  105. clientMethodTag = tag.Tag{Key: ocgrpc.KeyClientMethod, Value: "testpb.Foo/Multiple"}
  106. serverMethodTag = tag.Tag{Key: ocgrpc.KeyServerMethod, Value: "testpb.Foo/Multiple"}
  107. clientStatusOKTag = tag.Tag{Key: ocgrpc.KeyClientStatus, Value: "OK"}
  108. serverStatusOKTag = tag.Tag{Key: ocgrpc.KeyServerStatus, Value: "OK"}
  109. )
  110. const msgCount = 3
  111. stream, err := client.Multiple(ctx)
  112. if err != nil {
  113. t.Fatal(err)
  114. }
  115. for i := 0; i < msgCount; i++ {
  116. stream.Send(&testpb.FooRequest{})
  117. _, err := stream.Recv()
  118. if err != nil {
  119. t.Fatal(err)
  120. }
  121. }
  122. if err := stream.CloseSend(); err != nil {
  123. t.Fatal(err)
  124. }
  125. if _, err = stream.Recv(); err != io.EOF {
  126. t.Fatal(err)
  127. }
  128. checkCount(t, ocgrpc.ClientCompletedRPCsView, 1, clientMethodTag, clientStatusOKTag)
  129. checkCount(t, ocgrpc.ServerCompletedRPCsView, 1, serverMethodTag, serverStatusOKTag)
  130. tcs := []struct {
  131. v *view.View
  132. tag tag.Tag
  133. }{
  134. {ocgrpc.ClientSentMessagesPerRPCView, clientMethodTag},
  135. {ocgrpc.ServerReceivedMessagesPerRPCView, serverMethodTag},
  136. {ocgrpc.ServerSentMessagesPerRPCView, serverMethodTag},
  137. {ocgrpc.ClientReceivedMessagesPerRPCView, clientMethodTag},
  138. }
  139. for _, tt := range tcs {
  140. serverSent := getDistribution(t, tt.v, tt.tag)
  141. if got, want := serverSent.Mean, float64(msgCount); got != want {
  142. t.Errorf("%q.Count = %v; want %v", ocgrpc.ServerSentMessagesPerRPCView.Name, got, want)
  143. }
  144. }
  145. }
  146. func checkCount(t *testing.T, v *view.View, want int64, tags ...tag.Tag) {
  147. if got, ok := getCount(t, v, tags...); ok && got != want {
  148. t.Errorf("View[name=%q].Row[tags=%v].Data = %d; want %d", v.Name, tags, got, want)
  149. }
  150. }
  151. func getCount(t *testing.T, v *view.View, tags ...tag.Tag) (int64, bool) {
  152. if len(tags) != len(v.TagKeys) {
  153. t.Errorf("Invalid tag specification, want %#v tags got %#v", v.TagKeys, tags)
  154. return 0, false
  155. }
  156. for i := range v.TagKeys {
  157. if tags[i].Key != v.TagKeys[i] {
  158. t.Errorf("Invalid tag specification, want %#v tags got %#v", v.TagKeys, tags)
  159. return 0, false
  160. }
  161. }
  162. rows, err := view.RetrieveData(v.Name)
  163. if err != nil {
  164. t.Fatal(err)
  165. }
  166. var foundRow *view.Row
  167. for _, row := range rows {
  168. if reflect.DeepEqual(row.Tags, tags) {
  169. foundRow = row
  170. break
  171. }
  172. }
  173. if foundRow == nil {
  174. var gotTags [][]tag.Tag
  175. for _, row := range rows {
  176. gotTags = append(gotTags, row.Tags)
  177. }
  178. t.Errorf("Failed to find row with keys %v among:\n%v", tags, gotTags)
  179. return 0, false
  180. }
  181. return foundRow.Data.(*view.CountData).Value, true
  182. }
  183. func getDistribution(t *testing.T, v *view.View, tags ...tag.Tag) *view.DistributionData {
  184. if len(tags) != len(v.TagKeys) {
  185. t.Fatalf("Invalid tag specification, want %#v tags got %#v", v.TagKeys, tags)
  186. return nil
  187. }
  188. for i := range v.TagKeys {
  189. if tags[i].Key != v.TagKeys[i] {
  190. t.Fatalf("Invalid tag specification, want %#v tags got %#v", v.TagKeys, tags)
  191. return nil
  192. }
  193. }
  194. rows, err := view.RetrieveData(v.Name)
  195. if err != nil {
  196. t.Fatal(err)
  197. }
  198. var foundRow *view.Row
  199. for _, row := range rows {
  200. if reflect.DeepEqual(row.Tags, tags) {
  201. foundRow = row
  202. break
  203. }
  204. }
  205. if foundRow == nil {
  206. var gotTags [][]tag.Tag
  207. for _, row := range rows {
  208. gotTags = append(gotTags, row.Tags)
  209. }
  210. t.Fatalf("Failed to find row with keys %v among:\n%v", tags, gotTags)
  211. return nil
  212. }
  213. return foundRow.Data.(*view.DistributionData)
  214. }