Commit 27e1c674 by Torkel Ödegaard Committed by GitHub

refactor: tracing service refactoring (#11907)

parent bc647543
......@@ -27,7 +27,6 @@ import (
"github.com/grafana/grafana/pkg/setting"
"github.com/grafana/grafana/pkg/social"
"github.com/grafana/grafana/pkg/tracing"
// self registering services
_ "github.com/grafana/grafana/pkg/extensions"
......@@ -38,6 +37,7 @@ import (
_ "github.com/grafana/grafana/pkg/services/notifications"
_ "github.com/grafana/grafana/pkg/services/provisioning"
_ "github.com/grafana/grafana/pkg/services/search"
_ "github.com/grafana/grafana/pkg/tracing"
)
func NewGrafanaServer() *GrafanaServerImpl {
......@@ -77,12 +77,6 @@ func (g *GrafanaServerImpl) Run() error {
login.Init()
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.Provide(&inject.Object{Value: bus.GetBus()})
serviceGraph.Provide(&inject.Object{Value: g.cfg})
......
package tracing
import (
"context"
"io"
"strings"
"github.com/grafana/grafana/pkg/log"
"github.com/grafana/grafana/pkg/registry"
"github.com/grafana/grafana/pkg/setting"
opentracing "github.com/opentracing/opentracing-go"
jaegercfg "github.com/uber/jaeger-client-go/config"
ini "gopkg.in/ini.v1"
)
var (
logger log.Logger = log.New("tracing")
)
type TracingSettings struct {
Enabled bool
Address string
CustomTags map[string]string
SamplerType string
SamplerParam float64
func init() {
registry.RegisterService(&TracingService{})
}
func Init(file *ini.File) (io.Closer, error) {
settings := parseSettings(file)
return internalInit(settings)
type TracingService struct {
enabled bool
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 {
settings := &TracingSettings{}
func (ts *TracingService) Init() error {
ts.log = log.New("tracing")
ts.parseSettings()
var section, err = setting.Raw.GetSection("tracing.jaeger")
if err != nil {
return settings
if ts.enabled {
ts.initGlobalTracer()
}
settings.Address = section.Key("address").MustString("")
if settings.Address != "" {
settings.Enabled = true
return nil
}
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(""))
settings.SamplerType = section.Key("sampler_type").MustString("")
settings.SamplerParam = section.Key("sampler_param").MustFloat64(1)
ts.address = section.Key("address").MustString("")
if ts.address != "" {
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) {
if !settings.Enabled {
return &nullCloser{}, nil
}
func (ts *TracingService) initGlobalTracer() error {
cfg := jaegercfg.Configuration{
Disabled: !settings.Enabled,
Disabled: !ts.enabled,
Sampler: &jaegercfg.SamplerConfig{
Type: settings.SamplerType,
Param: settings.SamplerParam,
Type: ts.samplerType,
Param: ts.samplerParam,
},
Reporter: &jaegercfg.ReporterConfig{
LogSpans: false,
LocalAgentHostPort: settings.Address,
LocalAgentHostPort: ts.address,
},
}
......@@ -71,18 +74,31 @@ func internalInit(settings *TracingSettings) (io.Closer, error) {
options := []jaegercfg.Option{}
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))
}
tracer, closer, err := cfg.New("grafana", options...)
if err != nil {
return nil, err
return err
}
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 {
......@@ -110,7 +126,3 @@ func (jlw *jaegerLogWrapper) Error(msg string) {
func (jlw *jaegerLogWrapper) Infof(msg string, args ...interface{}) {
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