l7.go 4.3 KB

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