Skip to content

Commit 81328c0

Browse files
add OpenTracingTracer to Skipper Options (zalando#2525)
Signed-off-by: Pradeep Krishna Govindaraju <[email protected]>
1 parent 9a3c595 commit 81328c0

File tree

2 files changed

+71
-25
lines changed

2 files changed

+71
-25
lines changed

skipper.go

+41-25
Original file line numberDiff line numberDiff line change
@@ -667,6 +667,10 @@ type Options struct {
667667
// for a route when it's available (e.g. for RouteGroups)
668668
OpenTracingBackendNameTag bool
669669

670+
// OpenTracingTracer allows pre-created tracer to be passed on to skipper. Providing the
671+
// tracer instance overrides options provided under OpenTracing property.
672+
OpenTracingTracer ot.Tracer
673+
670674
// PluginDir defines the directory to load plugins from, DEPRECATED, use PluginDirs
671675
PluginDir string
672676
// PluginDirs defines the directories to load plugins from
@@ -1145,6 +1149,27 @@ func (o *Options) tlsConfig(cr *certregistry.CertRegistry) (*tls.Config, error)
11451149
return config, nil
11461150
}
11471151

1152+
func (o *Options) openTracingTracerInstance() (ot.Tracer, error) {
1153+
if o.OpenTracingTracer != nil {
1154+
return o.OpenTracingTracer, nil
1155+
}
1156+
1157+
if len(o.OpenTracing) > 0 {
1158+
return tracing.InitTracer(o.OpenTracing)
1159+
} else {
1160+
// always have a tracer available, so filter authors can rely on the
1161+
// existence of a tracer
1162+
tracer, err := tracing.LoadTracingPlugin(o.PluginDirs, []string{"noop"})
1163+
if err != nil {
1164+
return nil, err
1165+
} else if tracer == nil {
1166+
// LoadTracingPlugin unfortunately may return nil tracer
1167+
return nil, fmt.Errorf("failed to load tracing plugin from %v", o.PluginDirs)
1168+
}
1169+
return tracer, nil
1170+
}
1171+
}
1172+
11481173
func listen(o *Options, address string, mtr metrics.Metrics) (net.Listener, error) {
11491174

11501175
if !o.EnableTCPQueue {
@@ -1468,33 +1493,24 @@ func run(o Options, sig chan os.Signal, idleConnsCH chan struct{}) error {
14681493

14691494
o.PluginDirs = append(o.PluginDirs, o.PluginDir)
14701495

1471-
var tracer ot.Tracer
1472-
if len(o.OpenTracing) > 0 {
1473-
tracer, err = tracing.InitTracer(o.OpenTracing)
1474-
if err != nil {
1475-
return err
1476-
}
1477-
} else {
1478-
// always have a tracer available, so filter authors can rely on the
1479-
// existence of a tracer
1480-
tracer, _ = tracing.LoadTracingPlugin(o.PluginDirs, []string{"noop"})
1496+
tracer, err := o.openTracingTracerInstance()
1497+
if err != nil {
1498+
return err
14811499
}
14821500

1483-
// tee filters override if we have a tracer
1484-
if len(o.OpenTracing) > 0 {
1485-
o.CustomFilters = append(o.CustomFilters,
1486-
// tee()
1487-
teefilters.WithOptions(teefilters.Options{
1488-
Tracer: tracer,
1489-
NoFollow: false,
1490-
}),
1491-
// teenf()
1492-
teefilters.WithOptions(teefilters.Options{
1493-
NoFollow: true,
1494-
Tracer: tracer,
1495-
}),
1496-
)
1497-
}
1501+
// tee filters override with initialized tracer
1502+
o.CustomFilters = append(o.CustomFilters,
1503+
// tee()
1504+
teefilters.WithOptions(teefilters.Options{
1505+
Tracer: tracer,
1506+
NoFollow: false,
1507+
}),
1508+
// teenf()
1509+
teefilters.WithOptions(teefilters.Options{
1510+
NoFollow: true,
1511+
Tracer: tracer,
1512+
}),
1513+
)
14981514

14991515
if o.OAuthTokeninfoURL != "" {
15001516
tio := auth.TokeninfoOptions{

skipper_test.go

+30
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,36 @@ func TestOptionsFilterRegistry(t *testing.T) {
109109
assert.NotContains(t, fr, filters.BearerInjectorName)
110110
}
111111

112+
func TestOptionsOpenTracingTracerInstanceOverridesOpenTracing(t *testing.T) {
113+
tracer := &tracingtest.Tracer{}
114+
o := Options{
115+
OpenTracingTracer: tracer,
116+
OpenTracing: []string{"noop"},
117+
}
118+
119+
tr, err := o.openTracingTracerInstance()
120+
assert.NoError(t, err)
121+
assert.Same(t, tracer, tr)
122+
}
123+
124+
func TestOptionsOpenTracingTracerInstanceFallbacksToOpenTracingWhenTracerIsNil(t *testing.T) {
125+
o := Options{
126+
OpenTracing: []string{"noop"},
127+
}
128+
129+
tr, err := o.openTracingTracerInstance()
130+
assert.NoError(t, err)
131+
assert.NotNil(t, tr)
132+
}
133+
134+
func TestOptionsOpenTracingTracerInstanceReturnsErrorWhenNoTracerConfigIsSpecified(t *testing.T) {
135+
o := Options{}
136+
137+
tr, err := o.openTracingTracerInstance()
138+
assert.Error(t, err)
139+
assert.Nil(t, tr)
140+
}
141+
112142
func TestOptionsTLSConfig(t *testing.T) {
113143
cr := certregistry.NewCertRegistry()
114144
proxyTLS := &tls.Config{}

0 commit comments

Comments
 (0)