l7.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. package l7
  2. import (
  3. "inet.af/netaddr"
  4. "strconv"
  5. "time"
  6. )
  7. type Protocol uint8
  8. const (
  9. ProtocolTrace Protocol = 200
  10. ProtocolHTTP Protocol = 1
  11. ProtocolPostgres Protocol = 2
  12. ProtocolRedis Protocol = 3
  13. ProtocolMemcached Protocol = 4
  14. ProtocolMysql Protocol = 5
  15. ProtocolMongo Protocol = 6
  16. ProtocolKafka Protocol = 7
  17. ProtocolCassandra Protocol = 8
  18. ProtocolRabbitmq Protocol = 9
  19. ProtocolNats Protocol = 10
  20. ProtocolHTTP2 Protocol = 11
  21. ProtocolDubbo2 Protocol = 12
  22. ProtocolDNS Protocol = 13
  23. ProtocolDM Protocol = 14
  24. ProtocolGrpc Protocol = 15
  25. )
  26. func (p Protocol) Int() int {
  27. return int(p)
  28. }
  29. func (p Protocol) String() string {
  30. switch p {
  31. case ProtocolTrace:
  32. return "TRACE"
  33. case ProtocolHTTP:
  34. return "HTTP"
  35. case ProtocolPostgres:
  36. return "Postgres"
  37. case ProtocolRedis:
  38. return "Redis"
  39. case ProtocolMemcached:
  40. return "Memcached"
  41. case ProtocolMysql:
  42. return "Mysql"
  43. case ProtocolMongo:
  44. return "Mongo"
  45. case ProtocolKafka:
  46. return "Kafka"
  47. case ProtocolCassandra:
  48. return "Cassandra"
  49. case ProtocolRabbitmq:
  50. return "Rabbitmq"
  51. case ProtocolNats:
  52. return "NATS"
  53. case ProtocolHTTP2:
  54. return "HTTP2"
  55. case ProtocolDubbo2:
  56. return "Dubbo2"
  57. case ProtocolDNS:
  58. return "DNS"
  59. case ProtocolDM:
  60. return "DM"
  61. case ProtocolGrpc:
  62. return "GRPC"
  63. }
  64. return "UNKNOWN:" + strconv.Itoa(int(p))
  65. }
  66. func (p Protocol) ServiceNameString() string {
  67. switch p {
  68. case ProtocolTrace:
  69. return "TRACE"
  70. case ProtocolHTTP:
  71. return "HTTP"
  72. case ProtocolPostgres:
  73. return "POSTGRESQL"
  74. case ProtocolRedis:
  75. return "REDIS"
  76. case ProtocolMemcached:
  77. return "Memcached"
  78. case ProtocolMysql:
  79. return "MYSQL"
  80. case ProtocolMongo:
  81. return "Mongo"
  82. case ProtocolKafka:
  83. return "Kafka"
  84. case ProtocolCassandra:
  85. return "Cassandra"
  86. case ProtocolRabbitmq:
  87. return "Rabbitmq"
  88. case ProtocolNats:
  89. return "NATS"
  90. case ProtocolHTTP2:
  91. return "HTTP2"
  92. case ProtocolDubbo2:
  93. return "Dubbo2"
  94. case ProtocolDNS:
  95. return "DNS"
  96. case ProtocolDM:
  97. return "DM"
  98. case ProtocolGrpc:
  99. return "GRPC"
  100. }
  101. return "UNKNOWN:" + strconv.Itoa(int(p))
  102. }
  103. type Method uint8
  104. const (
  105. MethodUnknown Method = 0
  106. MethodProduce Method = 1
  107. MethodConsume Method = 2
  108. MethodStatementPrepare Method = 3
  109. MethodStatementClose Method = 4
  110. MethodHttp2ClientFrames Method = 5
  111. MethodHttp2ServerFrames Method = 6
  112. )
  113. func (m Method) String() string {
  114. switch m {
  115. case MethodUnknown:
  116. return "unknown"
  117. case MethodProduce:
  118. return "produce"
  119. case MethodConsume:
  120. return "consume"
  121. case MethodStatementPrepare:
  122. return "statement_prepare"
  123. case MethodStatementClose:
  124. return "statement_close"
  125. case MethodHttp2ClientFrames:
  126. return "http2_client_frames"
  127. case MethodHttp2ServerFrames:
  128. return "http2_server_frames"
  129. }
  130. return "UNKNOWN:" + strconv.Itoa(int(m))
  131. }
  132. type Status int
  133. const (
  134. StatusUnknown Status = 0
  135. StatusOk Status = 200
  136. StatusFailed Status = 500
  137. )
  138. func (s Status) String() string {
  139. switch s {
  140. case StatusUnknown:
  141. return "unknown"
  142. case StatusOk:
  143. return "ok"
  144. case StatusFailed:
  145. return "failed"
  146. }
  147. return strconv.Itoa(int(s))
  148. }
  149. func (s Status) Http() string {
  150. return strconv.Itoa(int(s))
  151. }
  152. func (s Status) DNS() string {
  153. switch s {
  154. case 0:
  155. return "ok"
  156. case 1:
  157. return "format_error"
  158. case 2:
  159. return "servfail"
  160. case 3:
  161. return "nxdomain"
  162. case 4:
  163. return "not_implemented"
  164. case 5:
  165. return "refused"
  166. }
  167. return ""
  168. }
  169. func (s Status) Error() bool {
  170. return s == StatusFailed
  171. }
  172. type RequestData struct {
  173. Protocol Protocol
  174. Status Status
  175. Duration time.Duration
  176. Method Method
  177. StatementId uint32
  178. Payload []byte
  179. TraceId uint64
  180. TraceStart uint32
  181. TraceEnd uint32
  182. TraceType uint32
  183. EventCount uint32
  184. AssumedAppId string
  185. SpanId string
  186. StartAt uint64
  187. EndAt uint64
  188. SAddr netaddr.IPPort
  189. DAddr netaddr.IPPort
  190. ComponentSAddr netaddr.IPPort
  191. ComponentDAddr netaddr.IPPort
  192. RPCTarget string
  193. ParentSpanContext struct {
  194. TraceIdFrom string
  195. CalledId string
  196. InstanceIdFrom string
  197. AppIdFrom string
  198. SpanIdFrom string
  199. TypeFrom string
  200. }
  201. }