trace.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. /*
  2. *
  3. * Copyright 2015 gRPC authors.
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. *
  17. */
  18. package grpc
  19. import (
  20. "bytes"
  21. "fmt"
  22. "io"
  23. "net"
  24. "strings"
  25. "sync"
  26. "time"
  27. "golang.org/x/net/trace"
  28. )
  29. // EnableTracing controls whether to trace RPCs using the golang.org/x/net/trace package.
  30. // This should only be set before any RPCs are sent or received by this program.
  31. var EnableTracing bool
  32. // methodFamily returns the trace family for the given method.
  33. // It turns "/pkg.Service/GetFoo" into "pkg.Service".
  34. func methodFamily(m string) string {
  35. m = strings.TrimPrefix(m, "/") // remove leading slash
  36. if i := strings.Index(m, "/"); i >= 0 {
  37. m = m[:i] // remove everything from second slash
  38. }
  39. return m
  40. }
  41. // traceInfo contains tracing information for an RPC.
  42. type traceInfo struct {
  43. tr trace.Trace
  44. firstLine firstLine
  45. }
  46. // firstLine is the first line of an RPC trace.
  47. // It may be mutated after construction; remoteAddr specifically may change
  48. // during client-side use.
  49. type firstLine struct {
  50. mu sync.Mutex
  51. client bool // whether this is a client (outgoing) RPC
  52. remoteAddr net.Addr
  53. deadline time.Duration // may be zero
  54. }
  55. func (f *firstLine) SetRemoteAddr(addr net.Addr) {
  56. f.mu.Lock()
  57. f.remoteAddr = addr
  58. f.mu.Unlock()
  59. }
  60. func (f *firstLine) String() string {
  61. f.mu.Lock()
  62. defer f.mu.Unlock()
  63. var line bytes.Buffer
  64. io.WriteString(&line, "RPC: ")
  65. if f.client {
  66. io.WriteString(&line, "to")
  67. } else {
  68. io.WriteString(&line, "from")
  69. }
  70. fmt.Fprintf(&line, " %v deadline:", f.remoteAddr)
  71. if f.deadline != 0 {
  72. fmt.Fprint(&line, f.deadline)
  73. } else {
  74. io.WriteString(&line, "none")
  75. }
  76. return line.String()
  77. }
  78. const truncateSize = 100
  79. func truncate(x string, l int) string {
  80. if l > len(x) {
  81. return x
  82. }
  83. return x[:l]
  84. }
  85. // payload represents an RPC request or response payload.
  86. type payload struct {
  87. sent bool // whether this is an outgoing payload
  88. msg interface{} // e.g. a proto.Message
  89. // TODO(dsymonds): add stringifying info to codec, and limit how much we hold here?
  90. }
  91. func (p payload) String() string {
  92. if p.sent {
  93. return truncate(fmt.Sprintf("sent: %v", p.msg), truncateSize)
  94. }
  95. return truncate(fmt.Sprintf("recv: %v", p.msg), truncateSize)
  96. }
  97. type fmtStringer struct {
  98. format string
  99. a []interface{}
  100. }
  101. func (f *fmtStringer) String() string {
  102. return fmt.Sprintf(f.format, f.a...)
  103. }
  104. type stringer string
  105. func (s stringer) String() string { return string(s) }