tracing.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. package tracing
  2. import (
  3. "context"
  4. "fmt"
  5. "io"
  6. "strings"
  7. "github.com/grafana/grafana/pkg/infra/log"
  8. "github.com/grafana/grafana/pkg/registry"
  9. "github.com/grafana/grafana/pkg/setting"
  10. opentracing "github.com/opentracing/opentracing-go"
  11. jaegercfg "github.com/uber/jaeger-client-go/config"
  12. "github.com/uber/jaeger-client-go/zipkin"
  13. )
  14. func init() {
  15. registry.RegisterService(&TracingService{})
  16. }
  17. type TracingService struct {
  18. enabled bool
  19. address string
  20. customTags map[string]string
  21. samplerType string
  22. samplerParam float64
  23. log log.Logger
  24. closer io.Closer
  25. zipkinPropagation bool
  26. disableSharedZipkinSpans bool
  27. Cfg *setting.Cfg `inject:""`
  28. }
  29. func (ts *TracingService) Init() error {
  30. ts.log = log.New("tracing")
  31. ts.parseSettings()
  32. if ts.enabled {
  33. ts.initGlobalTracer()
  34. }
  35. return nil
  36. }
  37. func (ts *TracingService) parseSettings() {
  38. var section, err = ts.Cfg.Raw.GetSection("tracing.jaeger")
  39. if err != nil {
  40. return
  41. }
  42. ts.address = section.Key("address").MustString("")
  43. if ts.address != "" {
  44. ts.enabled = true
  45. }
  46. ts.customTags = splitTagSettings(section.Key("always_included_tag").MustString(""))
  47. ts.samplerType = section.Key("sampler_type").MustString("")
  48. ts.samplerParam = section.Key("sampler_param").MustFloat64(1)
  49. ts.zipkinPropagation = section.Key("zipkin_propagation").MustBool(false)
  50. ts.disableSharedZipkinSpans = section.Key("disable_shared_zipkin_spans").MustBool(false)
  51. }
  52. func (ts *TracingService) initGlobalTracer() error {
  53. cfg := jaegercfg.Configuration{
  54. ServiceName: "grafana",
  55. Disabled: !ts.enabled,
  56. Sampler: &jaegercfg.SamplerConfig{
  57. Type: ts.samplerType,
  58. Param: ts.samplerParam,
  59. },
  60. Reporter: &jaegercfg.ReporterConfig{
  61. LogSpans: false,
  62. LocalAgentHostPort: ts.address,
  63. },
  64. }
  65. jLogger := &jaegerLogWrapper{logger: log.New("jaeger")}
  66. options := []jaegercfg.Option{}
  67. options = append(options, jaegercfg.Logger(jLogger))
  68. for tag, value := range ts.customTags {
  69. options = append(options, jaegercfg.Tag(tag, value))
  70. }
  71. if ts.zipkinPropagation {
  72. zipkinPropagator := zipkin.NewZipkinB3HTTPHeaderPropagator()
  73. options = append(options,
  74. jaegercfg.Injector(opentracing.HTTPHeaders, zipkinPropagator),
  75. jaegercfg.Extractor(opentracing.HTTPHeaders, zipkinPropagator),
  76. )
  77. if !ts.disableSharedZipkinSpans {
  78. options = append(options, jaegercfg.ZipkinSharedRPCSpan(true))
  79. }
  80. }
  81. tracer, closer, err := cfg.NewTracer(options...)
  82. if err != nil {
  83. return err
  84. }
  85. opentracing.InitGlobalTracer(tracer)
  86. ts.closer = closer
  87. return nil
  88. }
  89. func (ts *TracingService) Run(ctx context.Context) error {
  90. <-ctx.Done()
  91. if ts.closer != nil {
  92. ts.log.Info("Closing tracing")
  93. ts.closer.Close()
  94. }
  95. return nil
  96. }
  97. func splitTagSettings(input string) map[string]string {
  98. res := map[string]string{}
  99. tags := strings.Split(input, ",")
  100. for _, v := range tags {
  101. kv := strings.Split(v, ":")
  102. if len(kv) > 1 {
  103. res[kv[0]] = kv[1]
  104. }
  105. }
  106. return res
  107. }
  108. type jaegerLogWrapper struct {
  109. logger log.Logger
  110. }
  111. func (jlw *jaegerLogWrapper) Error(msg string) {
  112. jlw.logger.Error(msg)
  113. }
  114. func (jlw *jaegerLogWrapper) Infof(format string, args ...interface{}) {
  115. msg := fmt.Sprintf(format, args...)
  116. jlw.logger.Info(msg)
  117. }