tracer.go 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. package base
  2. import (
  3. "github.com/gin-gonic/gin"
  4. "github.com/opentracing/opentracing-go"
  5. jaeger "github.com/uber/jaeger-client-go"
  6. "github.com/uber/jaeger-client-go/utils"
  7. )
  8. // enum list
  9. const (
  10. TraceContextKey string = "traceContext"
  11. RequestIDKey string = "requestId"
  12. )
  13. // InitTracer 生成全局tracer,链接agent,如果agent不可用,使用logger report
  14. // "jaeger-agent:5775", 0
  15. func InitTracer() {
  16. var report jaeger.Reporter
  17. transport, err := jaeger.NewUDPTransportWithParams(jaeger.UDPTransportParams{
  18. AgentClientUDPParams: utils.AgentClientUDPParams{
  19. HostPort: "jaeger-agent:5775",
  20. MaxPacketSize: 0,
  21. Logger: jaeger.NullLogger,
  22. },
  23. })
  24. if err != nil {
  25. report = jaeger.NewNullReporter()
  26. } else {
  27. report = jaeger.NewRemoteReporter(transport)
  28. }
  29. tracer, _ := jaeger.NewTracer(GetServiceName(), jaeger.NewConstSampler(true), report, jaeger.TracerOptions.CustomHeaderKeys(&jaeger.HeadersConfig{
  30. JaegerDebugHeader: jaeger.JaegerDebugHeader,
  31. JaegerBaggageHeader: jaeger.JaegerBaggageHeader,
  32. TraceContextHeaderName: "trace-id",
  33. TraceBaggageHeaderPrefix: "srg-",
  34. }))
  35. opentracing.SetGlobalTracer(tracer)
  36. return
  37. }
  38. // TracerStartSpan for API
  39. func TracerStartSpan(parent opentracing.SpanContext, operationName string, tags map[string]interface{}) opentracing.Span {
  40. var options []opentracing.StartSpanOption
  41. if parent != nil {
  42. options = append(options, opentracing.ChildOf(parent))
  43. }
  44. span := opentracing.StartSpan(operationName, options...)
  45. for k, v := range tags {
  46. span.SetTag(k, v)
  47. }
  48. return span
  49. }
  50. // TracerContext create one new
  51. func TracerContext(ctx *gin.Context, operationName string, tags map[string]interface{}) *gin.Context {
  52. if ctx == nil {
  53. return nil
  54. }
  55. var parent opentracing.SpanContext
  56. if ctx != nil {
  57. tempSpan, exist := ctx.Get(TraceContextKey)
  58. if exist && tempSpan != nil {
  59. parent = tempSpan.(opentracing.Span).Context()
  60. }
  61. }
  62. newOne := TracerStartSpan(parent, operationName, tags)
  63. newC := ctx.Copy()
  64. newC.Set(TraceContextKey, newOne)
  65. one, ok := newOne.Context().(jaeger.SpanContext)
  66. if ok {
  67. newC.Set(RequestIDKey, one.String())
  68. }
  69. return newC
  70. }
  71. // GetRequestIdFromTrace use trace id as request id
  72. func GetRequestIdFromTrace(ctx *gin.Context) string {
  73. var spanContext jaeger.SpanContext
  74. if ctx != nil {
  75. tempSpan, exist := ctx.Get(TraceContextKey)
  76. if exist && tempSpan != nil {
  77. spanContext = tempSpan.(opentracing.Span).Context().(jaeger.SpanContext)
  78. return spanContext.String()
  79. }
  80. }
  81. return ""
  82. }
  83. // GetRequestId for API
  84. func GetRequestId(ctx *gin.Context) string {
  85. if ctx == nil {
  86. return ""
  87. }
  88. requestID, exist := ctx.Get(RequestIDKey)
  89. if exist {
  90. return requestID.(string)
  91. }
  92. return ""
  93. }
  94. type traceLogger struct{}
  95. func (logger *traceLogger) Error(msg string) {
  96. ErrorLogger(nil, msg)
  97. }
  98. func (logger *traceLogger) Infof(msg string, args ...interface{}) {
  99. DebugfLogger(nil, msg, args...)
  100. }
  101. // TracerToString for API
  102. func TracerToString(ctx *gin.Context) string {
  103. return GetRequestIdFromTrace(ctx)
  104. }
  105. // StringToTracer for API
  106. //func StringToTracer(g *gin.Engine, input string, name string, tags map[string]interface{}) *gin.Context {
  107. // newC := gin.CreateNewContext(g)
  108. // if len(input) == 0 {
  109. // return TracerContext(newC, name, tags)
  110. // }
  111. // span, err := jaeger.ContextFromString(input)
  112. // if err != nil {
  113. // return TracerContext(newC, name, tags)
  114. // }
  115. //
  116. // newOne := TracerStartSpan(span, name, tags)
  117. // newC.Set(TraceContextKey, newOne)
  118. // one, ok := newOne.Context().(jaeger.SpanContext)
  119. // if ok {
  120. // newC.Set(RequestIDKey, one.String())
  121. // }
  122. //
  123. // return newC
  124. //}
  125. //
  126. //// TracerForGRPCServiceFromString for API
  127. //func TracerForGRPCServiceFromString(g *gin.Engine, traceID string, service, grpcHandle string) *gin.Context {
  128. // return StringToTracer(g, traceID, "GRPC_"+service+"."+grpcHandle, map[string]interface{}{
  129. // ext.SpanKindRPCServer.Key: ext.SpanKindRPCServer.Value,
  130. // })
  131. //}
  132. //
  133. //// TracerForGRPCClientFromString for API
  134. //func TracerForGRPCClientFromString(g *gin.Engine, traceID string, service, grpcHandle string) *gin.Context {
  135. // return StringToTracer(g, traceID, "GRPC_"+service+"."+grpcHandle, map[string]interface{}{
  136. // ext.SpanKindRPCClient.Key: ext.SpanKindRPCClient.Value,
  137. // })
  138. //}
  139. //
  140. //// TracerForRMQServiceFromString for API
  141. //func TracerForRMQServiceFromString(g *gin.Engine, traceID string, service, grpcHandle string) *gin.Context {
  142. // return StringToTracer(g, traceID, "RMQ_"+service+"."+grpcHandle, map[string]interface{}{
  143. // ext.SpanKindRPCServer.Key: ext.SpanKindRPCServer.Value,
  144. // })
  145. //}
  146. //
  147. //// TracerForRMQClientFromString for API
  148. //func TracerForRMQClientFromString(g *gin.Engine, traceID string, service, grpcHandle string) *gin.Context {
  149. // return StringToTracer(g, traceID, "RMQ_"+service+"."+grpcHandle, map[string]interface{}{
  150. // ext.SpanKindRPCClient.Key: ext.SpanKindRPCClient.Value,
  151. // })
  152. //}
  153. //