trace.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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. )
  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. return m
  39. }
  40. // traceEventLog mirrors golang.org/x/net/trace.EventLog.
  41. //
  42. // It exists in order to avoid importing x/net/trace on grpcnotrace builds.
  43. type traceEventLog interface {
  44. Printf(format string, a ...any)
  45. Errorf(format string, a ...any)
  46. Finish()
  47. }
  48. // traceLog mirrors golang.org/x/net/trace.Trace.
  49. //
  50. // It exists in order to avoid importing x/net/trace on grpcnotrace builds.
  51. type traceLog interface {
  52. LazyLog(x fmt.Stringer, sensitive bool)
  53. LazyPrintf(format string, a ...any)
  54. SetError()
  55. SetRecycler(f func(any))
  56. SetTraceInfo(traceID, spanID uint64)
  57. SetMaxEvents(m int)
  58. Finish()
  59. }
  60. // traceInfo contains tracing information for an RPC.
  61. type traceInfo struct {
  62. tr traceLog
  63. firstLine firstLine
  64. }
  65. // firstLine is the first line of an RPC trace.
  66. // It may be mutated after construction; remoteAddr specifically may change
  67. // during client-side use.
  68. type firstLine struct {
  69. mu sync.Mutex
  70. client bool // whether this is a client (outgoing) RPC
  71. remoteAddr net.Addr
  72. deadline time.Duration // may be zero
  73. }
  74. func (f *firstLine) SetRemoteAddr(addr net.Addr) {
  75. f.mu.Lock()
  76. f.remoteAddr = addr
  77. f.mu.Unlock()
  78. }
  79. func (f *firstLine) String() string {
  80. f.mu.Lock()
  81. defer f.mu.Unlock()
  82. var line bytes.Buffer
  83. io.WriteString(&line, "RPC: ")
  84. if f.client {
  85. io.WriteString(&line, "to")
  86. } else {
  87. io.WriteString(&line, "from")
  88. }
  89. fmt.Fprintf(&line, " %v deadline:", f.remoteAddr)
  90. if f.deadline != 0 {
  91. fmt.Fprint(&line, f.deadline)
  92. } else {
  93. io.WriteString(&line, "none")
  94. }
  95. return line.String()
  96. }
  97. const truncateSize = 100
  98. func truncate(x string, l int) string {
  99. if l > len(x) {
  100. return x
  101. }
  102. return x[:l]
  103. }
  104. // payload represents an RPC request or response payload.
  105. type payload struct {
  106. sent bool // whether this is an outgoing payload
  107. msg any // e.g. a proto.Message
  108. // TODO(dsymonds): add stringifying info to codec, and limit how much we hold here?
  109. }
  110. func (p payload) String() string {
  111. if p.sent {
  112. return truncate(fmt.Sprintf("sent: %v", p.msg), truncateSize)
  113. }
  114. return truncate(fmt.Sprintf("recv: %v", p.msg), truncateSize)
  115. }
  116. type fmtStringer struct {
  117. format string
  118. a []any
  119. }
  120. func (f *fmtStringer) String() string {
  121. return fmt.Sprintf(f.format, f.a...)
  122. }
  123. type stringer string
  124. func (s stringer) String() string { return string(s) }