瀏覽代碼

adds custom tags from settings

bergquist 8 年之前
父節點
當前提交
ec29b469e4
共有 5 個文件被更改,包括 68 次插入8 次删除
  1. 2 0
      conf/defaults.ini
  2. 2 0
      conf/sample.ini
  3. 1 3
      pkg/middleware/request_tracing.go
  4. 27 5
      pkg/tracing/tracing.go
  5. 36 0
      pkg/tracing/tracing_test.go

+ 2 - 0
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]

+ 2 - 0
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

+ 1 - 3
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)

+ 27 - 5
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
+}

+ 36 - 0
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)
+			}
+		}
+	}
+}