Enable tracing when jaeger host and port are set (#33682)

* check for jaeger env variables if tracing address configuration is empty

* add tests to ensure jaeger host/port variables override the settings

* allow default host and custom port, too

* disallow missing tracing.jaeger section in the config

* check for all errors in tests

* make parseSettings() return an error
This commit is contained in:
Serge Zaitsev 2021-05-05 11:34:13 +02:00 committed by GitHub
parent 605bae8e2c
commit e244267b7d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 56 additions and 3 deletions

View File

@ -4,6 +4,7 @@ import (
"context"
"fmt"
"io"
"os"
"strings"
"github.com/grafana/grafana/pkg/infra/log"
@ -15,6 +16,11 @@ import (
"github.com/uber/jaeger-client-go/zipkin"
)
const (
envJaegerAgentHost = "JAEGER_AGENT_HOST"
envJaegerAgentPort = "JAEGER_AGENT_PORT"
)
func init() {
registry.RegisterService(&TracingService{})
}
@ -36,7 +42,9 @@ type TracingService struct {
func (ts *TracingService) Init() error {
ts.log = log.New("tracing")
ts.parseSettings()
if err := ts.parseSettings(); err != nil {
return err
}
if ts.enabled {
return ts.initGlobalTracer()
@ -45,13 +53,20 @@ func (ts *TracingService) Init() error {
return nil
}
func (ts *TracingService) parseSettings() {
func (ts *TracingService) parseSettings() error {
var section, err = ts.Cfg.Raw.GetSection("tracing.jaeger")
if err != nil {
return
return err
}
ts.address = section.Key("address").MustString("")
if ts.address == "" {
host := os.Getenv(envJaegerAgentHost)
port := os.Getenv(envJaegerAgentPort)
if host != "" || port != "" {
ts.address = fmt.Sprintf("%s:%s", host, port)
}
}
if ts.address != "" {
ts.enabled = true
}
@ -62,6 +77,7 @@ func (ts *TracingService) parseSettings() {
ts.zipkinPropagation = section.Key("zipkin_propagation").MustBool(false)
ts.disableSharedZipkinSpans = section.Key("disable_shared_zipkin_spans").MustBool(false)
ts.samplingServerURL = section.Key("sampling_server_url").MustString("")
return nil
}
func (ts *TracingService) initJaegerCfg() (jaegercfg.Configuration, error) {

View File

@ -4,6 +4,7 @@ import (
"os"
"testing"
"github.com/grafana/grafana/pkg/setting"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
@ -99,3 +100,39 @@ func TestInitJaegerCfg_InvalidEnvVar(t *testing.T) {
_, err = ts.initJaegerCfg()
require.EqualError(t, err, "cannot parse env var JAEGER_DISABLED=totallybogus: strconv.ParseBool: parsing \"totallybogus\": invalid syntax")
}
func TestInitJaegerCfg_EnabledViaHost(t *testing.T) {
require.NoError(t, os.Setenv("JAEGER_AGENT_HOST", "example.com"))
defer func() {
require.NoError(t, os.Unsetenv("JAEGER_AGENT_HOST"))
}()
ts := &TracingService{Cfg: setting.NewCfg()}
_, err := ts.Cfg.Raw.NewSection("tracing.jaeger")
require.NoError(t, err)
require.NoError(t, ts.parseSettings())
cfg, err := ts.initJaegerCfg()
require.NoError(t, err)
assert.False(t, cfg.Disabled)
assert.Equal(t, "example.com:6831", cfg.Reporter.LocalAgentHostPort)
}
func TestInitJaegerCfg_EnabledViaHostPort(t *testing.T) {
require.NoError(t, os.Setenv("JAEGER_AGENT_HOST", "example.com"))
require.NoError(t, os.Setenv("JAEGER_AGENT_PORT", "12345"))
defer func() {
require.NoError(t, os.Unsetenv("JAEGER_AGENT_HOST"))
require.NoError(t, os.Unsetenv("JAEGER_AGENT_PORT"))
}()
ts := &TracingService{Cfg: setting.NewCfg()}
_, err := ts.Cfg.Raw.NewSection("tracing.jaeger")
require.NoError(t, err)
require.NoError(t, ts.parseSettings())
cfg, err := ts.initJaegerCfg()
require.NoError(t, err)
assert.False(t, cfg.Disabled)
assert.Equal(t, "example.com:12345", cfg.Reporter.LocalAgentHostPort)
}