Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.
 
 
 

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