trace.go 3.7 KB

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