From ec29b469e4738088f4aa0bac472852b63e124f87 Mon Sep 17 00:00:00 2001 From: bergquist Date: Thu, 14 Sep 2017 19:01:22 +0200 Subject: [PATCH] adds custom tags from settings --- conf/defaults.ini | 2 ++ conf/sample.ini | 2 ++ pkg/middleware/request_tracing.go | 4 +--- pkg/tracing/tracing.go | 32 ++++++++++++++++++++++----- pkg/tracing/tracing_test.go | 36 +++++++++++++++++++++++++++++++ 5 files changed, 68 insertions(+), 8 deletions(-) create mode 100644 pkg/tracing/tracing_test.go diff --git a/conf/defaults.ini b/conf/defaults.ini index a594bd018de..f2b177079d8 100644 --- a/conf/defaults.ini +++ b/conf/defaults.ini @@ -456,6 +456,8 @@ url = https://grafana.com [tracing.jaeger] # jaeger destination (ex localhost:6831) address = +# tag that will always be included in when creating new spans. ex (tag1:value1,tag2:value2) +always_included_tag = #################################### External Image Storage ############## [external_image_storage] diff --git a/conf/sample.ini b/conf/sample.ini index 4c83860bb7f..582eaad55b4 100644 --- a/conf/sample.ini +++ b/conf/sample.ini @@ -395,6 +395,8 @@ [tracing.jaeger] # Enable by setting the address sending traces to jaeger (ex localhost:6831) ;address = localhost:6831 +# Tag that will always be included in when creating new spans. ex (tag1:value1,tag2:value2) +;always_included_tag = tag1:value1 #################################### Grafana.com integration ########################## # Url used to to import dashboards directly from Grafana.com diff --git a/pkg/middleware/request_tracing.go b/pkg/middleware/request_tracing.go index 61537f42636..bf25308a2a3 100644 --- a/pkg/middleware/request_tracing.go +++ b/pkg/middleware/request_tracing.go @@ -14,11 +14,9 @@ func RequestTracing(handler string) macaron.Handler { return func(res http.ResponseWriter, req *http.Request, c *macaron.Context) { rw := res.(macaron.ResponseWriter) - var span opentracing.Span tracer := opentracing.GlobalTracer() wireContext, _ := tracer.Extract(opentracing.HTTPHeaders, opentracing.HTTPHeadersCarrier(req.Header)) - spanName := fmt.Sprintf("HTTP %s", handler) - span = tracer.StartSpan(spanName, ext.RPCServerOption(wireContext)) + span := tracer.StartSpan(fmt.Sprintf("HTTP %s", handler), ext.RPCServerOption(wireContext)) defer span.Finish() ctx := opentracing.ContextWithSpan(req.Context(), span) diff --git a/pkg/tracing/tracing.go b/pkg/tracing/tracing.go index b5caf36c91a..b73cbc15b24 100644 --- a/pkg/tracing/tracing.go +++ b/pkg/tracing/tracing.go @@ -3,6 +3,7 @@ package tracing import ( "io" "io/ioutil" + "strings" "github.com/grafana/grafana/pkg/log" "github.com/grafana/grafana/pkg/setting" @@ -15,7 +16,8 @@ import ( ) var ( - logger log.Logger = log.New("tracing") + logger log.Logger = log.New("tracing") + customTags map[string]string = map[string]string{} ) type TracingSettings struct { @@ -41,6 +43,8 @@ func parseSettings(file *ini.File) *TracingSettings { settings.Enabled = true } + customTags = splitTagSettings(section.Key("always_included_tag").MustString("")) + return settings } @@ -63,10 +67,14 @@ func internalInit(settings *TracingSettings) (io.Closer, error) { jLogger := jaegerlog.StdLogger - tracer, closer, err := cfg.New( - "grafana", - jaegercfg.Logger(jLogger), - ) + options := []jaegercfg.Option{} + options = append(options, jaegercfg.Logger(jLogger)) + + for tag, value := range customTags { + options = append(options, jaegercfg.Tag(tag, value)) + } + + tracer, closer, err := cfg.New("grafana", options...) if err != nil { return nil, err } @@ -75,3 +83,17 @@ func internalInit(settings *TracingSettings) (io.Closer, error) { opentracing.InitGlobalTracer(tracer) return closer, nil } + +func splitTagSettings(input string) map[string]string { + res := map[string]string{} + + tags := strings.Split(input, ",") + for _, v := range tags { + kv := strings.Split(v, ":") + if len(kv) > 1 { + res[kv[0]] = kv[1] + } + } + + return res +} diff --git a/pkg/tracing/tracing_test.go b/pkg/tracing/tracing_test.go new file mode 100644 index 00000000000..27e4de777a3 --- /dev/null +++ b/pkg/tracing/tracing_test.go @@ -0,0 +1,36 @@ +package tracing + +import "testing" + +func TestGroupSplit(t *testing.T) { + tests := []struct { + input string + expected map[string]string + }{ + { + input: "tag1:value1,tag2:value2", + expected: map[string]string{ + "tag1": "value1", + "tag2": "value2", + }, + }, + { + input: "", + expected: map[string]string{}, + }, + { + input: "tag1", + expected: map[string]string{}, + }, + } + + for _, test := range tests { + tags := splitTagSettings(test.input) + for k, v := range test.expected { + value, exists := tags[k] + if !exists || value != v { + t.Errorf("tags does not match %v ", test) + } + } + } +}