grpc_test.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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. package ocgrpc
  15. import (
  16. "sync"
  17. "testing"
  18. "time"
  19. "context"
  20. "go.opencensus.io/stats/view"
  21. "google.golang.org/grpc/metadata"
  22. "go.opencensus.io/trace"
  23. "google.golang.org/grpc/stats"
  24. )
  25. func TestClientHandler(t *testing.T) {
  26. ctx := context.Background()
  27. te := &traceExporter{}
  28. trace.RegisterExporter(te)
  29. if err := view.Register(ClientSentMessagesPerRPCView); err != nil {
  30. t.Fatal(err)
  31. }
  32. defer view.Unregister(ClientSentMessagesPerRPCView)
  33. ctx, _ = trace.StartSpan(ctx, "/foo", trace.WithSampler(trace.AlwaysSample()))
  34. var handler ClientHandler
  35. ctx = handler.TagRPC(ctx, &stats.RPCTagInfo{
  36. FullMethodName: "/service.foo/method",
  37. })
  38. handler.HandleRPC(ctx, &stats.Begin{
  39. Client: true,
  40. BeginTime: time.Now(),
  41. })
  42. handler.HandleRPC(ctx, &stats.End{
  43. Client: true,
  44. EndTime: time.Now(),
  45. })
  46. stats, err := view.RetrieveData(ClientSentMessagesPerRPCView.Name)
  47. if err != nil {
  48. t.Fatal(err)
  49. }
  50. traces := te.buffer
  51. if got, want := len(stats), 1; got != want {
  52. t.Errorf("Got %v stats; want %v", got, want)
  53. }
  54. if got, want := len(traces), 1; got != want {
  55. t.Errorf("Got %v traces; want %v", got, want)
  56. }
  57. }
  58. func TestServerHandler(t *testing.T) {
  59. tests := []struct {
  60. name string
  61. newTrace bool
  62. expectTraces int
  63. }{
  64. {"trust_metadata", false, 1},
  65. {"no_trust_metadata", true, 0},
  66. }
  67. for _, test := range tests {
  68. t.Run(test.name, func(t *testing.T) {
  69. ctx := context.Background()
  70. handler := &ServerHandler{
  71. IsPublicEndpoint: test.newTrace,
  72. StartOptions: trace.StartOptions{
  73. Sampler: trace.ProbabilitySampler(0.0),
  74. },
  75. }
  76. te := &traceExporter{}
  77. trace.RegisterExporter(te)
  78. if err := view.Register(ServerCompletedRPCsView); err != nil {
  79. t.Fatal(err)
  80. }
  81. md := metadata.MD{
  82. "grpc-trace-bin": []string{string([]byte{0, 0, 62, 116, 14, 118, 117, 157, 126, 7, 114, 152, 102, 125, 235, 34, 114, 238, 1, 187, 201, 24, 210, 231, 20, 175, 241, 2, 1})},
  83. }
  84. ctx = metadata.NewIncomingContext(ctx, md)
  85. ctx = handler.TagRPC(ctx, &stats.RPCTagInfo{
  86. FullMethodName: "/service.foo/method",
  87. })
  88. handler.HandleRPC(ctx, &stats.Begin{
  89. BeginTime: time.Now(),
  90. })
  91. handler.HandleRPC(ctx, &stats.End{
  92. EndTime: time.Now(),
  93. })
  94. rows, err := view.RetrieveData(ServerCompletedRPCsView.Name)
  95. if err != nil {
  96. t.Fatal(err)
  97. }
  98. traces := te.buffer
  99. if got, want := len(rows), 1; got != want {
  100. t.Errorf("Got %v rows; want %v", got, want)
  101. }
  102. if got, want := len(traces), test.expectTraces; got != want {
  103. t.Errorf("Got %v traces; want %v", got, want)
  104. }
  105. // Cleanup.
  106. view.Unregister(ServerCompletedRPCsView)
  107. })
  108. }
  109. }
  110. type traceExporter struct {
  111. mu sync.Mutex
  112. buffer []*trace.SpanData
  113. }
  114. func (e *traceExporter) ExportSpan(sd *trace.SpanData) {
  115. e.mu.Lock()
  116. e.buffer = append(e.buffer, sd)
  117. e.mu.Unlock()
  118. }