mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
* remove opentracing and use otel instead * add various samplers for jaeger * remove useless test that is covered in otel now * we do not need a struct there * remove old tests * restore tests that parse various configurations * check errors in tests * Update pkg/infra/tracing/tracing_test.go fix typo Co-authored-by: Sofia Papagiannaki <1632407+papagian@users.noreply.github.com> * add test for both legacy and new config formats * use named constants --------- Co-authored-by: Sofia Papagiannaki <1632407+papagian@users.noreply.github.com>
168 lines
4.1 KiB
Go
168 lines
4.1 KiB
Go
package tracing
|
|
|
|
import (
|
|
"os"
|
|
"testing"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
"go.opentelemetry.io/otel/attribute"
|
|
|
|
"github.com/grafana/grafana/pkg/setting"
|
|
)
|
|
|
|
// TODO(zserge) Add proper tests for opentelemetry
|
|
|
|
func TestSplitCustomAttribs(t *testing.T) {
|
|
tests := []struct {
|
|
input string
|
|
expected []attribute.KeyValue
|
|
}{
|
|
{
|
|
input: "key1:value:1",
|
|
expected: []attribute.KeyValue{attribute.String("key1", "value:1")},
|
|
},
|
|
{
|
|
input: "key1:value1,key2:value2",
|
|
expected: []attribute.KeyValue{
|
|
attribute.String("key1", "value1"),
|
|
attribute.String("key2", "value2"),
|
|
},
|
|
},
|
|
{
|
|
input: "",
|
|
expected: []attribute.KeyValue{},
|
|
},
|
|
}
|
|
|
|
for _, test := range tests {
|
|
attribs, err := splitCustomAttribs(test.input)
|
|
assert.NoError(t, err)
|
|
assert.EqualValues(t, test.expected, attribs)
|
|
}
|
|
}
|
|
|
|
func TestSplitCustomAttribs_Malformed(t *testing.T) {
|
|
tests := []struct {
|
|
input string
|
|
}{
|
|
{input: "key1=value1"},
|
|
{input: "key1"},
|
|
}
|
|
|
|
for _, test := range tests {
|
|
_, err := splitCustomAttribs(test.input)
|
|
assert.Error(t, err)
|
|
}
|
|
}
|
|
|
|
func TestTracingConfig(t *testing.T) {
|
|
for _, test := range []struct {
|
|
Name string
|
|
Cfg string
|
|
Env map[string]string
|
|
ExpectedExporter string
|
|
ExpectedAddress string
|
|
ExpectedPropagator string
|
|
ExpectedAttrs []attribute.KeyValue
|
|
}{
|
|
{
|
|
Name: "default config uses noop exporter",
|
|
Cfg: "",
|
|
ExpectedExporter: noopExporter,
|
|
ExpectedAttrs: []attribute.KeyValue{},
|
|
},
|
|
{
|
|
Name: "custom attributes are parsed",
|
|
Cfg: `
|
|
[tracing.opentelemetry]
|
|
custom_attributes = key1:value1,key2:value2
|
|
`,
|
|
ExpectedExporter: noopExporter,
|
|
ExpectedAttrs: []attribute.KeyValue{attribute.String("key1", "value1"), attribute.String("key2", "value2")},
|
|
},
|
|
{
|
|
Name: "jaeger address is parsed",
|
|
Cfg: `
|
|
[tracing.opentelemetry.jaeger]
|
|
address = jaeger.example.com:6831
|
|
`,
|
|
ExpectedExporter: jaegerExporter,
|
|
ExpectedAddress: "jaeger.example.com:6831",
|
|
ExpectedAttrs: []attribute.KeyValue{},
|
|
},
|
|
{
|
|
Name: "OTLP address is parsed",
|
|
Cfg: `
|
|
[tracing.opentelemetry.otlp]
|
|
address = otlp.example.com:4317
|
|
`,
|
|
ExpectedExporter: otlpExporter,
|
|
ExpectedAddress: "otlp.example.com:4317",
|
|
ExpectedAttrs: []attribute.KeyValue{},
|
|
},
|
|
{
|
|
Name: "legacy config format is supported",
|
|
Cfg: `
|
|
[tracing.jaeger]
|
|
address = jaeger.example.com:6831
|
|
`,
|
|
ExpectedExporter: jaegerExporter,
|
|
ExpectedAddress: "jaeger.example.com:6831",
|
|
ExpectedAttrs: []attribute.KeyValue{},
|
|
},
|
|
{
|
|
Name: "legacy env variables are supproted",
|
|
Cfg: `[tracing.jaeger]`,
|
|
Env: map[string]string{
|
|
"JAEGER_AGENT_HOST": "example.com",
|
|
"JAEGER_AGENT_PORT": "12345",
|
|
},
|
|
ExpectedExporter: jaegerExporter,
|
|
ExpectedAddress: "example.com:12345",
|
|
ExpectedAttrs: []attribute.KeyValue{},
|
|
},
|
|
{
|
|
Name: "opentelemetry config format is prioritised over legacy jaeger",
|
|
Cfg: `
|
|
[tracing.jaeger]
|
|
address = foo.com:6831
|
|
custom_tags = a:b
|
|
[tracing.opentelemetry]
|
|
custom_attributes = c:d
|
|
[tracing.opentelemetry.jaeger]
|
|
address = bar.com:6831
|
|
`,
|
|
ExpectedExporter: jaegerExporter,
|
|
ExpectedAddress: "bar.com:6831",
|
|
ExpectedAttrs: []attribute.KeyValue{attribute.String("c", "d")},
|
|
},
|
|
} {
|
|
t.Run(test.Name, func(t *testing.T) {
|
|
// export envioronment variables
|
|
if test.Env != nil {
|
|
for k, v := range test.Env {
|
|
assert.NoError(t, os.Setenv(k, v))
|
|
}
|
|
defer func() {
|
|
for k := range test.Env {
|
|
assert.NoError(t, os.Unsetenv(k))
|
|
}
|
|
}()
|
|
}
|
|
// parse config sections
|
|
cfg := setting.NewCfg()
|
|
err := cfg.Raw.Append([]byte(test.Cfg))
|
|
assert.NoError(t, err)
|
|
// create tracer
|
|
tracer, err := ProvideService(cfg)
|
|
assert.NoError(t, err)
|
|
// make sure tracker is properly configured
|
|
otel := tracer.(*Opentelemetry)
|
|
assert.Equal(t, test.ExpectedExporter, otel.enabled)
|
|
assert.Equal(t, test.ExpectedAddress, otel.Address)
|
|
assert.Equal(t, test.ExpectedPropagator, otel.Propagation)
|
|
assert.Equal(t, test.ExpectedAttrs, otel.customAttribs)
|
|
})
|
|
}
|
|
}
|