l7.go 3.3 KB

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