l7.go 4.0 KB

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