From 0532febff0342022d9bc7c11c5e92ae3dcee9412 Mon Sep 17 00:00:00 2001 From: Bartlomiej Plotka Date: Mon, 28 Oct 2019 15:47:10 +0000 Subject: [PATCH] Added new constructor for tracer that propagates error. (#228) Signed-off-by: Bartek Plotka --- tracer.go | 23 ++++++++++++++++------- 1 file changed, 16 insertions(+), 7 deletions(-) diff --git a/tracer.go b/tracer.go index 2dd1ece9b..e2678681b 100644 --- a/tracer.go +++ b/tracer.go @@ -90,12 +90,22 @@ type tracerImpl struct { } // NewTracer creates and starts a new Lightstep Tracer. +// In case of error, we emit event and return nil. func NewTracer(opts Options) Tracer { - err := opts.Initialize() - if err != nil { + tr, err := CreateTracer(opts) + if err != nil { emitEvent(newEventStartError(err)) return nil } + return tr +} + +// CreateTracer creates and starts a new Lightstep Tracer. +// It is meant to replace NewTracer which does not propagate the error. +func CreateTracer(opts Options) (Tracer, error) { + if err := opts.Initialize(); err != nil { + return nil, fmt.Errorf("init; err: %v", err) + } attributes := map[string]string{} for k, v := range opts.Tags { @@ -121,16 +131,15 @@ func NewTracer(opts Options) Tracer { impl.buffer.setCurrent(now) + var err error impl.client, err = newCollectorClient(opts) if err != nil { - fmt.Println("Failed to create to Collector client!", err) - return nil + return nil, fmt.Errorf("create collector client; err: %v", err) } conn, err := impl.client.ConnectClient() if err != nil { - emitEvent(newEventStartError(err)) - return nil + return nil, err } impl.connection = conn @@ -151,7 +160,7 @@ func NewTracer(opts Options) Tracer { impl.propagators[opentracing.HTTPHeaders] = theB3Propagator } - return impl + return impl, nil } func (tracer *tracerImpl) Options() Options {