trace.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. package trace
  2. import (
  3. "crypto/rand"
  4. "encoding/hex"
  5. "io"
  6. "sync"
  7. )
  8. const Header = "TRACE-ID"
  9. var _ T = (*Trace)(nil)
  10. type T interface {
  11. i()
  12. ID() string
  13. WithRequest(req *Request) *Trace
  14. WithResponse(resp *Response) *Trace
  15. AppendDialog(dialog *Dialog) *Trace
  16. AppendSQL(sql *SQL) *Trace
  17. AppendRedis(redis *Redis) *Trace
  18. }
  19. // Trace 记录的参数
  20. type Trace struct {
  21. mux sync.Mutex
  22. Identifier string `json:"trace_id"` // 链路ID
  23. Request *Request `json:"request"` // 请求信息
  24. Response *Response `json:"response"` // 返回信息
  25. ThirdPartyRequests []*Dialog `json:"third_party_requests"` // 调用第三方接口的信息
  26. Debugs []*Debug `json:"debugs"` // 调试信息
  27. SQLs []*SQL `json:"sqls"` // 执行的 SQL 信息
  28. Redis []*Redis `json:"redis"` // 执行的 Redis 信息
  29. Success bool `json:"success"` // 请求结果 true or false
  30. CostSeconds float64 `json:"cost_seconds"` // 执行时长(单位秒)
  31. }
  32. // Request 请求信息
  33. type Request struct {
  34. TTL string `json:"ttl"` // 请求超时时间
  35. Method string `json:"method"` // 请求方式
  36. DecodedURL string `json:"decoded_url"` // 请求地址
  37. Header interface{} `json:"header"` // 请求 Header 信息
  38. Body interface{} `json:"body"` // 请求 Body 信息
  39. }
  40. // Response 响应信息
  41. type Response struct {
  42. Header interface{} `json:"header"` // Header 信息
  43. Body interface{} `json:"body"` // Body 信息
  44. BusinessCode int `json:"business_code,omitempty"` // 业务码
  45. BusinessCodeMsg string `json:"business_code_msg,omitempty"` // 提示信息
  46. HttpCode int `json:"http_code"` // HTTP 状态码
  47. HttpCodeMsg string `json:"http_code_msg"` // HTTP 状态码信息
  48. CostSeconds float64 `json:"cost_seconds"` // 执行时间(单位秒)
  49. }
  50. func New(id string) *Trace {
  51. if id == "" {
  52. buf := make([]byte, 10)
  53. io.ReadFull(rand.Reader, buf)
  54. id = hex.EncodeToString(buf)
  55. }
  56. return &Trace{
  57. Identifier: id,
  58. }
  59. }
  60. func (t *Trace) i() {}
  61. // ID 唯一标识符
  62. func (t *Trace) ID() string {
  63. return t.Identifier
  64. }
  65. // WithRequest 设置request
  66. func (t *Trace) WithRequest(req *Request) *Trace {
  67. t.Request = req
  68. return t
  69. }
  70. // WithResponse 设置response
  71. func (t *Trace) WithResponse(resp *Response) *Trace {
  72. t.Response = resp
  73. return t
  74. }
  75. // AppendDialog 安全的追加内部调用过程dialog
  76. func (t *Trace) AppendDialog(dialog *Dialog) *Trace {
  77. if dialog == nil {
  78. return t
  79. }
  80. t.mux.Lock()
  81. defer t.mux.Unlock()
  82. t.ThirdPartyRequests = append(t.ThirdPartyRequests, dialog)
  83. return t
  84. }
  85. // AppendDebug 追加 debug
  86. func (t *Trace) AppendDebug(debug *Debug) *Trace {
  87. if debug == nil {
  88. return t
  89. }
  90. t.mux.Lock()
  91. defer t.mux.Unlock()
  92. t.Debugs = append(t.Debugs, debug)
  93. return t
  94. }
  95. // AppendSQL 追加 SQL
  96. func (t *Trace) AppendSQL(sql *SQL) *Trace {
  97. if sql == nil {
  98. return t
  99. }
  100. t.mux.Lock()
  101. defer t.mux.Unlock()
  102. t.SQLs = append(t.SQLs, sql)
  103. return t
  104. }
  105. // AppendRedis 追加 Redis
  106. func (t *Trace) AppendRedis(redis *Redis) *Trace {
  107. if redis == nil {
  108. return t
  109. }
  110. t.mux.Lock()
  111. defer t.mux.Unlock()
  112. t.Redis = append(t.Redis, redis)
  113. return t
  114. }