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.
 
 
 

147 lines
5.7 KiB

  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
  16. import (
  17. "strings"
  18. "testing"
  19. "go.opencensus.io/stats"
  20. "go.opencensus.io/stats/view"
  21. )
  22. func TestSpecServerMeasures(t *testing.T) {
  23. spec := `
  24. | Measure name | Unit | Description |
  25. |------------------------------------------|------|-----------------------------------------------------------------------------------------------|
  26. | grpc.io/server/received_messages_per_rpc | 1 | Number of messages received in each RPC. Has value 1 for non-streaming RPCs. |
  27. | grpc.io/server/received_bytes_per_rpc | By | Total bytes received across all messages per RPC. |
  28. | grpc.io/server/sent_messages_per_rpc | 1 | Number of messages sent in each RPC. Has value 1 for non-streaming RPCs. |
  29. | grpc.io/server/sent_bytes_per_rpc | By | Total bytes sent in across all response messages per RPC. |
  30. | grpc.io/server/server_latency | ms | Time between first byte of request received to last byte of response sent, or terminal error. |`
  31. lines := strings.Split(spec, "\n")[3:]
  32. type measureDef struct {
  33. name string
  34. unit string
  35. desc string
  36. }
  37. measureDefs := make([]measureDef, 0, len(lines))
  38. for _, line := range lines {
  39. cols := colSep.Split(line, -1)[1:]
  40. if len(cols) < 3 {
  41. t.Fatalf("Invalid config line %#v", cols)
  42. }
  43. measureDefs = append(measureDefs, measureDef{cols[0], cols[1], cols[2]})
  44. }
  45. gotMeasures := []stats.Measure{
  46. ServerReceivedMessagesPerRPC,
  47. ServerReceivedBytesPerRPC,
  48. ServerSentMessagesPerRPC,
  49. ServerSentBytesPerRPC,
  50. ServerLatency,
  51. }
  52. if got, want := len(gotMeasures), len(measureDefs); got != want {
  53. t.Fatalf("len(gotMeasures) = %d; want %d", got, want)
  54. }
  55. for i, m := range gotMeasures {
  56. defn := measureDefs[i]
  57. if got, want := m.Name(), defn.name; got != want {
  58. t.Errorf("Name = %q; want %q", got, want)
  59. }
  60. if got, want := m.Unit(), defn.unit; got != want {
  61. t.Errorf("%q: Unit = %q; want %q", defn.name, got, want)
  62. }
  63. if got, want := m.Description(), defn.desc; got != want {
  64. t.Errorf("%q: Description = %q; want %q", defn.name, got, want)
  65. }
  66. }
  67. }
  68. func TestSpecServerViews(t *testing.T) {
  69. defaultViewsSpec := `
  70. | View name | Measure suffix | Aggregation | Tags suffix |
  71. |---------------------------------------|------------------------|--------------|------------------------------|
  72. | grpc.io/server/received_bytes_per_rpc | received_bytes_per_rpc | distribution | server_method |
  73. | grpc.io/server/sent_bytes_per_rpc | sent_bytes_per_rpc | distribution | server_method |
  74. | grpc.io/server/server_latency | server_latency | distribution | server_method |
  75. | grpc.io/server/completed_rpcs | server_latency | count | server_method, server_status |`
  76. extraViewsSpec := `
  77. | View name | Measure suffix | Aggregation | Tags suffix |
  78. |------------------------------------------|---------------------------|--------------|---------------|
  79. | grpc.io/server/received_messages_per_rpc | received_messages_per_rpc | distribution | server_method |
  80. | grpc.io/server/sent_messages_per_rpc | sent_messages_per_rpc | distribution | server_method |`
  81. lines := strings.Split(defaultViewsSpec, "\n")[3:]
  82. lines = append(lines, strings.Split(extraViewsSpec, "\n")[3:]...)
  83. type viewDef struct {
  84. name string
  85. measureSuffix string
  86. aggregation string
  87. tags string
  88. }
  89. viewDefs := make([]viewDef, 0, len(lines))
  90. for _, line := range lines {
  91. cols := colSep.Split(line, -1)[1:]
  92. if len(cols) < 4 {
  93. t.Fatalf("Invalid config line %#v", cols)
  94. }
  95. viewDefs = append(viewDefs, viewDef{cols[0], cols[1], cols[2], cols[3]})
  96. }
  97. views := DefaultServerViews
  98. views = append(views, ServerReceivedMessagesPerRPCView, ServerSentMessagesPerRPCView)
  99. if got, want := len(views), len(viewDefs); got != want {
  100. t.Fatalf("len(gotMeasures) = %d; want %d", got, want)
  101. }
  102. for i, v := range views {
  103. defn := viewDefs[i]
  104. if got, want := v.Name, defn.name; got != want {
  105. t.Errorf("Name = %q; want %q", got, want)
  106. }
  107. if got, want := v.Measure.Name(), "grpc.io/server/"+defn.measureSuffix; got != want {
  108. t.Errorf("%q: Measure.Name = %q; want %q", defn.name, got, want)
  109. }
  110. switch v.Aggregation.Type {
  111. case view.AggTypeDistribution:
  112. if got, want := "distribution", defn.aggregation; got != want {
  113. t.Errorf("%q: Description = %q; want %q", defn.name, got, want)
  114. }
  115. case view.AggTypeCount:
  116. if got, want := "count", defn.aggregation; got != want {
  117. t.Errorf("%q: Description = %q; want %q", defn.name, got, want)
  118. }
  119. default:
  120. t.Errorf("Invalid aggregation type")
  121. }
  122. wantTags := strings.Split(defn.tags, ", ")
  123. if got, want := len(v.TagKeys), len(wantTags); got != want {
  124. t.Errorf("len(TagKeys) = %d; want %d", got, want)
  125. }
  126. for j := range wantTags {
  127. if got, want := v.TagKeys[j].Name(), "grpc_"+wantTags[j]; got != want {
  128. t.Errorf("TagKeys[%d].Name() = %q; want %q", j, got, want)
  129. }
  130. }
  131. }
  132. }