trace.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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. "time"
  26. "golang.org/x/net/trace"
  27. )
  28. // EnableTracing controls whether to trace RPCs using the golang.org/x/net/trace package.
  29. // This should only be set before any RPCs are sent or received by this program.
  30. var EnableTracing = true
  31. // methodFamily returns the trace family for the given method.
  32. // It turns "/pkg.Service/GetFoo" into "pkg.Service".
  33. func methodFamily(m string) string {
  34. m = strings.TrimPrefix(m, "/") // remove leading slash
  35. if i := strings.Index(m, "/"); i >= 0 {
  36. m = m[:i] // remove everything from second slash
  37. }
  38. if i := strings.LastIndex(m, "."); i >= 0 {
  39. m = m[i+1:] // cut down to last dotted component
  40. }
  41. return m
  42. }
  43. // traceInfo contains tracing information for an RPC.
  44. type traceInfo struct {
  45. tr trace.Trace
  46. firstLine firstLine
  47. }
  48. // firstLine is the first line of an RPC trace.
  49. type firstLine struct {
  50. client bool // whether this is a client (outgoing) RPC
  51. remoteAddr net.Addr
  52. deadline time.Duration // may be zero
  53. }
  54. func (f *firstLine) String() string {
  55. var line bytes.Buffer
  56. io.WriteString(&line, "RPC: ")
  57. if f.client {
  58. io.WriteString(&line, "to")
  59. } else {
  60. io.WriteString(&line, "from")
  61. }
  62. fmt.Fprintf(&line, " %v deadline:", f.remoteAddr)
  63. if f.deadline != 0 {
  64. fmt.Fprint(&line, f.deadline)
  65. } else {
  66. io.WriteString(&line, "none")
  67. }
  68. return line.String()
  69. }
  70. // payload represents an RPC request or response payload.
  71. type payload struct {
  72. sent bool // whether this is an outgoing payload
  73. msg interface{} // e.g. a proto.Message
  74. // TODO(dsymonds): add stringifying info to codec, and limit how much we hold here?
  75. }
  76. func (p payload) String() string {
  77. if p.sent {
  78. return fmt.Sprintf("sent: %v", p.msg)
  79. }
  80. return fmt.Sprintf("recv: %v", p.msg)
  81. }
  82. type fmtStringer struct {
  83. format string
  84. a []interface{}
  85. }
  86. func (f *fmtStringer) String() string {
  87. return fmt.Sprintf(f.format, f.a...)
  88. }
  89. type stringer string
  90. func (s stringer) String() string { return string(s) }