Commit 27e1c674 by Torkel Ödegaard Committed by GitHub

refactor: tracing service refactoring (#11907)

parent bc647543
...@@ -27,7 +27,6 @@ import ( ...@@ -27,7 +27,6 @@ import (
"github.com/grafana/grafana/pkg/setting" "github.com/grafana/grafana/pkg/setting"
"github.com/grafana/grafana/pkg/social" "github.com/grafana/grafana/pkg/social"
"github.com/grafana/grafana/pkg/tracing"
// self registering services // self registering services
_ "github.com/grafana/grafana/pkg/extensions" _ "github.com/grafana/grafana/pkg/extensions"
...@@ -38,6 +37,7 @@ import ( ...@@ -38,6 +37,7 @@ import (
_ "github.com/grafana/grafana/pkg/services/notifications" _ "github.com/grafana/grafana/pkg/services/notifications"
_ "github.com/grafana/grafana/pkg/services/provisioning" _ "github.com/grafana/grafana/pkg/services/provisioning"
_ "github.com/grafana/grafana/pkg/services/search" _ "github.com/grafana/grafana/pkg/services/search"
_ "github.com/grafana/grafana/pkg/tracing"
) )
func NewGrafanaServer() *GrafanaServerImpl { func NewGrafanaServer() *GrafanaServerImpl {
...@@ -77,12 +77,6 @@ func (g *GrafanaServerImpl) Run() error { ...@@ -77,12 +77,6 @@ func (g *GrafanaServerImpl) Run() error {
login.Init() login.Init()
social.NewOAuthService() social.NewOAuthService()
tracingCloser, err := tracing.Init(g.cfg.Raw)
if err != nil {
return fmt.Errorf("Tracing settings is not valid. error: %v", err)
}
defer tracingCloser.Close()
serviceGraph := inject.Graph{} serviceGraph := inject.Graph{}
serviceGraph.Provide(&inject.Object{Value: bus.GetBus()}) serviceGraph.Provide(&inject.Object{Value: bus.GetBus()})
serviceGraph.Provide(&inject.Object{Value: g.cfg}) serviceGraph.Provide(&inject.Object{Value: g.cfg})
......
package tracing package tracing
import ( import (
"context"
"io" "io"
"strings" "strings"
"github.com/grafana/grafana/pkg/log" "github.com/grafana/grafana/pkg/log"
"github.com/grafana/grafana/pkg/registry"
"github.com/grafana/grafana/pkg/setting" "github.com/grafana/grafana/pkg/setting"
opentracing "github.com/opentracing/opentracing-go" opentracing "github.com/opentracing/opentracing-go"
jaegercfg "github.com/uber/jaeger-client-go/config" jaegercfg "github.com/uber/jaeger-client-go/config"
ini "gopkg.in/ini.v1"
) )
var ( func init() {
logger log.Logger = log.New("tracing") registry.RegisterService(&TracingService{})
)
type TracingSettings struct {
Enabled bool
Address string
CustomTags map[string]string
SamplerType string
SamplerParam float64
} }
func Init(file *ini.File) (io.Closer, error) { type TracingService struct {
settings := parseSettings(file) enabled bool
return internalInit(settings) address string
customTags map[string]string
samplerType string
samplerParam float64
log log.Logger
closer io.Closer
Cfg *setting.Cfg `inject:""`
} }
func parseSettings(file *ini.File) *TracingSettings { func (ts *TracingService) Init() error {
settings := &TracingSettings{} ts.log = log.New("tracing")
ts.parseSettings()
var section, err = setting.Raw.GetSection("tracing.jaeger") if ts.enabled {
if err != nil { ts.initGlobalTracer()
return settings
} }
settings.Address = section.Key("address").MustString("") return nil
if settings.Address != "" { }
settings.Enabled = true
func (ts *TracingService) parseSettings() {
var section, err = ts.Cfg.Raw.GetSection("tracing.jaeger")
if err != nil {
return
} }
settings.CustomTags = splitTagSettings(section.Key("always_included_tag").MustString("")) ts.address = section.Key("address").MustString("")
settings.SamplerType = section.Key("sampler_type").MustString("") if ts.address != "" {
settings.SamplerParam = section.Key("sampler_param").MustFloat64(1) ts.enabled = true
}
return settings ts.customTags = splitTagSettings(section.Key("always_included_tag").MustString(""))
ts.samplerType = section.Key("sampler_type").MustString("")
ts.samplerParam = section.Key("sampler_param").MustFloat64(1)
} }
func internalInit(settings *TracingSettings) (io.Closer, error) { func (ts *TracingService) initGlobalTracer() error {
if !settings.Enabled {
return &nullCloser{}, nil
}
cfg := jaegercfg.Configuration{ cfg := jaegercfg.Configuration{
Disabled: !settings.Enabled, Disabled: !ts.enabled,
Sampler: &jaegercfg.SamplerConfig{ Sampler: &jaegercfg.SamplerConfig{
Type: settings.SamplerType, Type: ts.samplerType,
Param: settings.SamplerParam, Param: ts.samplerParam,
}, },
Reporter: &jaegercfg.ReporterConfig{ Reporter: &jaegercfg.ReporterConfig{
LogSpans: false, LogSpans: false,
LocalAgentHostPort: settings.Address, LocalAgentHostPort: ts.address,
}, },
} }
...@@ -71,18 +74,31 @@ func internalInit(settings *TracingSettings) (io.Closer, error) { ...@@ -71,18 +74,31 @@ func internalInit(settings *TracingSettings) (io.Closer, error) {
options := []jaegercfg.Option{} options := []jaegercfg.Option{}
options = append(options, jaegercfg.Logger(jLogger)) options = append(options, jaegercfg.Logger(jLogger))
for tag, value := range settings.CustomTags { for tag, value := range ts.customTags {
options = append(options, jaegercfg.Tag(tag, value)) options = append(options, jaegercfg.Tag(tag, value))
} }
tracer, closer, err := cfg.New("grafana", options...) tracer, closer, err := cfg.New("grafana", options...)
if err != nil { if err != nil {
return nil, err return err
} }
opentracing.InitGlobalTracer(tracer) opentracing.InitGlobalTracer(tracer)
logger.Info("Initializing Jaeger tracer", "address", settings.Address)
return closer, nil ts.closer = closer
return nil
}
func (ts *TracingService) Run(ctx context.Context) error {
<-ctx.Done()
if ts.closer != nil {
ts.log.Info("Closing tracing")
ts.closer.Close()
}
return nil
} }
func splitTagSettings(input string) map[string]string { func splitTagSettings(input string) map[string]string {
...@@ -110,7 +126,3 @@ func (jlw *jaegerLogWrapper) Error(msg string) { ...@@ -110,7 +126,3 @@ func (jlw *jaegerLogWrapper) Error(msg string) {
func (jlw *jaegerLogWrapper) Infof(msg string, args ...interface{}) { func (jlw *jaegerLogWrapper) Infof(msg string, args ...interface{}) {
jlw.logger.Info(msg, args) jlw.logger.Info(msg, args)
} }
type nullCloser struct{}
func (*nullCloser) Close() error { return nil }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment