trace.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. /*
  2. *
  3. * Copyright 2015, Google Inc.
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions are
  8. * met:
  9. *
  10. * * Redistributions of source code must retain the above copyright
  11. * notice, this list of conditions and the following disclaimer.
  12. * * Redistributions in binary form must reproduce the above
  13. * copyright notice, this list of conditions and the following disclaimer
  14. * in the documentation and/or other materials provided with the
  15. * distribution.
  16. * * Neither the name of Google Inc. nor the names of its
  17. * contributors may be used to endorse or promote products derived from
  18. * this software without specific prior written permission.
  19. *
  20. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  21. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  22. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  23. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  24. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  25. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  26. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  27. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  28. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  29. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  30. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31. *
  32. */
  33. package grpc
  34. import (
  35. "bytes"
  36. "fmt"
  37. "io"
  38. "net"
  39. "strings"
  40. "time"
  41. "golang.org/x/net/trace"
  42. )
  43. // EnableTracing controls whether to trace RPCs using the golang.org/x/net/trace package.
  44. // This should only be set before any RPCs are sent or received by this program.
  45. var EnableTracing = true
  46. // methodFamily returns the trace family for the given method.
  47. // It turns "/pkg.Service/GetFoo" into "pkg.Service".
  48. func methodFamily(m string) string {
  49. m = strings.TrimPrefix(m, "/") // remove leading slash
  50. if i := strings.Index(m, "/"); i >= 0 {
  51. m = m[:i] // remove everything from second slash
  52. }
  53. if i := strings.LastIndex(m, "."); i >= 0 {
  54. m = m[i+1:] // cut down to last dotted component
  55. }
  56. return m
  57. }
  58. // traceInfo contains tracing information for an RPC.
  59. type traceInfo struct {
  60. tr trace.Trace
  61. firstLine firstLine
  62. }
  63. // firstLine is the first line of an RPC trace.
  64. type firstLine struct {
  65. client bool // whether this is a client (outgoing) RPC
  66. remoteAddr net.Addr
  67. deadline time.Duration // may be zero
  68. }
  69. func (f *firstLine) String() string {
  70. var line bytes.Buffer
  71. io.WriteString(&line, "RPC: ")
  72. if f.client {
  73. io.WriteString(&line, "to")
  74. } else {
  75. io.WriteString(&line, "from")
  76. }
  77. fmt.Fprintf(&line, " %v deadline:", f.remoteAddr)
  78. if f.deadline != 0 {
  79. fmt.Fprint(&line, f.deadline)
  80. } else {
  81. io.WriteString(&line, "none")
  82. }
  83. return line.String()
  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 fmt.Sprintf("sent: %v", p.msg)
  94. } else {
  95. return fmt.Sprintf("recv: %v", p.msg)
  96. }
  97. }
  98. type fmtStringer struct {
  99. format string
  100. a []interface{}
  101. }
  102. func (f *fmtStringer) String() string {
  103. return fmt.Sprintf(f.format, f.a...)
  104. }
  105. type stringer string
  106. func (s stringer) String() string { return string(s) }