You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

337 lines
8.4 KiB

  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 ocgrpc
  16. import (
  17. "testing"
  18. "go.opencensus.io/trace"
  19. "golang.org/x/net/context"
  20. "go.opencensus.io/stats/view"
  21. "go.opencensus.io/tag"
  22. "google.golang.org/grpc/codes"
  23. "google.golang.org/grpc/stats"
  24. "google.golang.org/grpc/status"
  25. )
  26. func TestServerDefaultCollections(t *testing.T) {
  27. k1, _ := tag.NewKey("k1")
  28. k2, _ := tag.NewKey("k2")
  29. type tagPair struct {
  30. k tag.Key
  31. v string
  32. }
  33. type wantData struct {
  34. v func() *view.View
  35. rows []*view.Row
  36. }
  37. type rpc struct {
  38. tags []tagPair
  39. tagInfo *stats.RPCTagInfo
  40. inPayloads []*stats.InPayload
  41. outPayloads []*stats.OutPayload
  42. end *stats.End
  43. }
  44. type testCase struct {
  45. label string
  46. rpcs []*rpc
  47. wants []*wantData
  48. }
  49. tcs := []testCase{
  50. {
  51. "1",
  52. []*rpc{
  53. {
  54. []tagPair{{k1, "v1"}},
  55. &stats.RPCTagInfo{FullMethodName: "/package.service/method"},
  56. []*stats.InPayload{
  57. {Length: 10},
  58. },
  59. []*stats.OutPayload{
  60. {Length: 10},
  61. },
  62. &stats.End{Error: nil},
  63. },
  64. },
  65. []*wantData{
  66. {
  67. func() *view.View { return ServerReceivedMessagesPerRPCView },
  68. []*view.Row{
  69. {
  70. Tags: []tag.Tag{
  71. {Key: KeyServerMethod, Value: "package.service/method"},
  72. },
  73. Data: newDistributionData([]int64{0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, 1, 1, 1, 1, 0),
  74. },
  75. },
  76. },
  77. {
  78. func() *view.View { return ServerSentMessagesPerRPCView },
  79. []*view.Row{
  80. {
  81. Tags: []tag.Tag{
  82. {Key: KeyServerMethod, Value: "package.service/method"},
  83. },
  84. Data: newDistributionData([]int64{0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, 1, 1, 1, 1, 0),
  85. },
  86. },
  87. },
  88. {
  89. func() *view.View { return ServerReceivedBytesPerRPCView },
  90. []*view.Row{
  91. {
  92. Tags: []tag.Tag{
  93. {Key: KeyServerMethod, Value: "package.service/method"},
  94. },
  95. Data: newDistributionData([]int64{1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, 1, 10, 10, 10, 0),
  96. },
  97. },
  98. },
  99. {
  100. func() *view.View { return ServerSentBytesPerRPCView },
  101. []*view.Row{
  102. {
  103. Tags: []tag.Tag{
  104. {Key: KeyServerMethod, Value: "package.service/method"},
  105. },
  106. Data: newDistributionData([]int64{1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, 1, 10, 10, 10, 0),
  107. },
  108. },
  109. },
  110. },
  111. },
  112. {
  113. "2",
  114. []*rpc{
  115. {
  116. []tagPair{{k1, "v1"}},
  117. &stats.RPCTagInfo{FullMethodName: "/package.service/method"},
  118. []*stats.InPayload{
  119. {Length: 10},
  120. },
  121. []*stats.OutPayload{
  122. {Length: 10},
  123. {Length: 10},
  124. {Length: 10},
  125. },
  126. &stats.End{Error: nil},
  127. },
  128. {
  129. []tagPair{{k1, "v11"}},
  130. &stats.RPCTagInfo{FullMethodName: "/package.service/method"},
  131. []*stats.InPayload{
  132. {Length: 10},
  133. {Length: 10},
  134. },
  135. []*stats.OutPayload{
  136. {Length: 10},
  137. {Length: 10},
  138. },
  139. &stats.End{Error: status.Error(codes.Canceled, "canceled")},
  140. },
  141. },
  142. []*wantData{
  143. {
  144. func() *view.View { return ServerReceivedMessagesPerRPCView },
  145. []*view.Row{
  146. {
  147. Tags: []tag.Tag{
  148. {Key: KeyServerMethod, Value: "package.service/method"},
  149. },
  150. Data: newDistributionData([]int64{0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, 2, 1, 2, 1.5, 0.5),
  151. },
  152. },
  153. },
  154. {
  155. func() *view.View { return ServerSentMessagesPerRPCView },
  156. []*view.Row{
  157. {
  158. Tags: []tag.Tag{
  159. {Key: KeyServerMethod, Value: "package.service/method"},
  160. },
  161. Data: newDistributionData([]int64{0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, 2, 2, 3, 2.5, 0.5),
  162. },
  163. },
  164. },
  165. },
  166. },
  167. {
  168. "3",
  169. []*rpc{
  170. {
  171. []tagPair{{k1, "v1"}},
  172. &stats.RPCTagInfo{FullMethodName: "/package.service/method"},
  173. []*stats.InPayload{
  174. {Length: 1},
  175. },
  176. []*stats.OutPayload{
  177. {Length: 1},
  178. {Length: 1024},
  179. {Length: 65536},
  180. },
  181. &stats.End{Error: nil},
  182. },
  183. {
  184. []tagPair{{k1, "v1"}, {k2, "v2"}},
  185. &stats.RPCTagInfo{FullMethodName: "/package.service/method"},
  186. []*stats.InPayload{
  187. {Length: 1024},
  188. },
  189. []*stats.OutPayload{
  190. {Length: 4096},
  191. {Length: 16384},
  192. },
  193. &stats.End{Error: status.Error(codes.Aborted, "aborted")},
  194. },
  195. {
  196. []tagPair{{k1, "v11"}, {k2, "v22"}},
  197. &stats.RPCTagInfo{FullMethodName: "/package.service/method"},
  198. []*stats.InPayload{
  199. {Length: 2048},
  200. {Length: 16384},
  201. },
  202. []*stats.OutPayload{
  203. {Length: 2048},
  204. {Length: 4096},
  205. {Length: 16384},
  206. },
  207. &stats.End{Error: status.Error(codes.Canceled, "canceled")},
  208. },
  209. },
  210. []*wantData{
  211. {
  212. func() *view.View { return ServerReceivedMessagesPerRPCView },
  213. []*view.Row{
  214. {
  215. Tags: []tag.Tag{
  216. {Key: KeyServerMethod, Value: "package.service/method"},
  217. },
  218. Data: newDistributionData([]int64{0, 2, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, 3, 1, 2, 1.333333333, 0.333333333*2),
  219. },
  220. },
  221. },
  222. {
  223. func() *view.View { return ServerSentMessagesPerRPCView },
  224. []*view.Row{
  225. {
  226. Tags: []tag.Tag{
  227. {Key: KeyServerMethod, Value: "package.service/method"},
  228. },
  229. Data: newDistributionData([]int64{0, 0, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, 3, 2, 3, 2.666666666, 0.333333333*2),
  230. },
  231. },
  232. },
  233. {
  234. func() *view.View { return ServerReceivedBytesPerRPCView },
  235. []*view.Row{
  236. {
  237. Tags: []tag.Tag{
  238. {Key: KeyServerMethod, Value: "package.service/method"},
  239. },
  240. Data: newDistributionData([]int64{1, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0}, 3, 1, 18432, 6485.6666667, 2.1459558466666667e+08),
  241. },
  242. },
  243. },
  244. {
  245. func() *view.View { return ServerSentBytesPerRPCView },
  246. []*view.Row{
  247. {
  248. Tags: []tag.Tag{
  249. {Key: KeyServerMethod, Value: "package.service/method"},
  250. },
  251. Data: newDistributionData([]int64{0, 0, 0, 0, 2, 1, 0, 0, 0, 0, 0, 0, 0, 0}, 3, 20480, 66561, 36523, 1.355519318e+09),
  252. },
  253. },
  254. },
  255. },
  256. },
  257. }
  258. views := append(DefaultServerViews[:], ServerReceivedMessagesPerRPCView, ServerSentMessagesPerRPCView)
  259. for _, tc := range tcs {
  260. if err := view.Register(views...); err != nil {
  261. t.Fatal(err)
  262. }
  263. h := &ServerHandler{}
  264. h.StartOptions.Sampler = trace.NeverSample()
  265. for _, rpc := range tc.rpcs {
  266. mods := []tag.Mutator{}
  267. for _, t := range rpc.tags {
  268. mods = append(mods, tag.Upsert(t.k, t.v))
  269. }
  270. ctx, err := tag.New(context.Background(), mods...)
  271. if err != nil {
  272. t.Errorf("%q: NewMap = %v", tc.label, err)
  273. }
  274. encoded := tag.Encode(tag.FromContext(ctx))
  275. ctx = stats.SetTags(context.Background(), encoded)
  276. ctx = h.TagRPC(ctx, rpc.tagInfo)
  277. for _, in := range rpc.inPayloads {
  278. h.HandleRPC(ctx, in)
  279. }
  280. for _, out := range rpc.outPayloads {
  281. h.HandleRPC(ctx, out)
  282. }
  283. h.HandleRPC(ctx, rpc.end)
  284. }
  285. for _, wantData := range tc.wants {
  286. gotRows, err := view.RetrieveData(wantData.v().Name)
  287. if err != nil {
  288. t.Errorf("%q: RetrieveData (%q) = %v", tc.label, wantData.v().Name, err)
  289. continue
  290. }
  291. for _, gotRow := range gotRows {
  292. if !containsRow(wantData.rows, gotRow) {
  293. t.Errorf("%q: unwanted row for view %q: %v", tc.label, wantData.v().Name, gotRow)
  294. break
  295. }
  296. }
  297. for _, wantRow := range wantData.rows {
  298. if !containsRow(gotRows, wantRow) {
  299. t.Errorf("%q: missing row for view %q: %v", tc.label, wantData.v().Name, wantRow)
  300. break
  301. }
  302. }
  303. }
  304. // Unregister views to cleanup.
  305. view.Unregister(views...)
  306. }
  307. }
  308. func newDistributionData(countPerBucket []int64, count int64, min, max, mean, sumOfSquaredDev float64) *view.DistributionData {
  309. return &view.DistributionData{
  310. Count: count,
  311. Min: min,
  312. Max: max,
  313. Mean: mean,
  314. SumOfSquaredDev: sumOfSquaredDev,
  315. CountPerBucket: countPerBucket,
  316. }
  317. }