trace.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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. if i := strings.LastIndex(m, "."); i >= 0 {
  40. m = m[i+1:] // cut down to last dotted component
  41. }
  42. return m
  43. }
  44. // traceInfo contains tracing information for an RPC.
  45. type traceInfo struct {
  46. tr trace.Trace
  47. firstLine firstLine
  48. }
  49. // firstLine is the first line of an RPC trace.
  50. // It may be mutated after construction; remoteAddr specifically may change
  51. // during client-side use.
  52. type firstLine struct {
  53. mu sync.Mutex
  54. client bool // whether this is a client (outgoing) RPC
  55. remoteAddr net.Addr
  56. deadline time.Duration // may be zero
  57. }
  58. func (f *firstLine) SetRemoteAddr(addr net.Addr) {
  59. f.mu.Lock()
  60. f.remoteAddr = addr
  61. f.mu.Unlock()
  62. }
  63. func (f *firstLine) String() string {
  64. f.mu.Lock()
  65. defer f.mu.Unlock()
  66. var line bytes.Buffer
  67. io.WriteString(&line, "RPC: ")
  68. if f.client {
  69. io.WriteString(&line, "to")
  70. } else {
  71. io.WriteString(&line, "from")
  72. }
  73. fmt.Fprintf(&line, " %v deadline:", f.remoteAddr)
  74. if f.deadline != 0 {
  75. fmt.Fprint(&line, f.deadline)
  76. } else {
  77. io.WriteString(&line, "none")
  78. }
  79. return line.String()
  80. }
  81. const truncateSize = 100
  82. func truncate(x string, l int) string {
  83. if l > len(x) {
  84. return x
  85. }
  86. return x[:l]
  87. }
  88. // payload represents an RPC request or response payload.
  89. type payload struct {
  90. sent bool // whether this is an outgoing payload
  91. msg interface{} // e.g. a proto.Message
  92. // TODO(dsymonds): add stringifying info to codec, and limit how much we hold here?
  93. }
  94. func (p payload) String() string {
  95. if p.sent {
  96. return truncate(fmt.Sprintf("sent: %v", p.msg), truncateSize)
  97. }
  98. return truncate(fmt.Sprintf("recv: %v", p.msg), truncateSize)
  99. }
  100. type fmtStringer struct {
  101. format string
  102. a []interface{}
  103. }
  104. func (f *fmtStringer) String() string {
  105. return fmt.Sprintf(f.format, f.a...)
  106. }
  107. type stringer string
  108. func (s stringer) String() string { return string(s) }