Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.
 
 
 

114 rindas
2.7 KiB

  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 bool
  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. const truncateSize = 100
  71. func truncate(x string, l int) string {
  72. if l > len(x) {
  73. return x
  74. }
  75. return x[:l]
  76. }
  77. // payload represents an RPC request or response payload.
  78. type payload struct {
  79. sent bool // whether this is an outgoing payload
  80. msg interface{} // e.g. a proto.Message
  81. // TODO(dsymonds): add stringifying info to codec, and limit how much we hold here?
  82. }
  83. func (p payload) String() string {
  84. if p.sent {
  85. return truncate(fmt.Sprintf("sent: %v", p.msg), truncateSize)
  86. }
  87. return truncate(fmt.Sprintf("recv: %v", p.msg), truncateSize)
  88. }
  89. type fmtStringer struct {
  90. format string
  91. a []interface{}
  92. }
  93. func (f *fmtStringer) String() string {
  94. return fmt.Sprintf(f.format, f.a...)
  95. }
  96. type stringer string
  97. func (s stringer) String() string { return string(s) }