Skip to content

Commit

Permalink
update init func
Browse files Browse the repository at this point in the history
  • Loading branch information
exelban committed Jan 29, 2021
1 parent 2afa7d2 commit 0da7b1e
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 11 deletions.
15 changes: 11 additions & 4 deletions helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,15 @@ import (
jaegercfg "github.com/uber/jaeger-client-go/config"
jaegerlog "github.com/uber/jaeger-client-go/log"
"github.com/uber/jaeger-lib/metrics"
"io"
"log"
"net/http"
)

// NewTracer - initializing opentracing tracer and set up global tracer.
func NewTracer(name string, host string) (opentracing.Tracer, io.Closer, error) {
func NewTracer(ctx context.Context, name string, host string) (opentracing.Tracer, error) {
udpTransport, err := jaeger.NewUDPTransport(fmt.Sprintf("%s:6831", host), 0)
if err != nil {
return nil, nil, errors.Wrap(err, "create transport error")
return nil, errors.Wrap(err, "create transport error")
}

cfg := jaegercfg.Configuration{
Expand All @@ -43,9 +43,16 @@ func NewTracer(name string, host string) (opentracing.Tracer, io.Closer, error)
jaegercfg.Metrics(jMetricsFactory),
)

go func() {
<-ctx.Done()
if err = closer.Close(); err != nil {
log.Panic(err)
}
}()

opentracing.SetGlobalTracer(tracer)

return tracer, closer, nil
return tracer, nil
}

// InjectToReq - inject opentracing span to *http.Requst
Expand Down
10 changes: 5 additions & 5 deletions helper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,20 @@ import (
)

func TestTracer(t *testing.T) {
tracer, closer, err := NewTracer("test", "")
ctx := context.Background()

tracer, err := NewTracer(ctx, "test", "")
require.NoError(t, err)
require.NotNil(t, tracer)
require.NotNil(t, closer)

tracer, closer, err = NewTracer("test", "wtf")
tracer, err = NewTracer(ctx, "test", "wtf")
require.Error(t, err)
}

func TestInjectToReq(t *testing.T) {
tracer, closer, err := NewTracer("test", "")
tracer, err := NewTracer(context.Background(), "test", "")
require.NoError(t, err)
require.NotNil(t, tracer)
require.NotNil(t, closer)

handler := func(w http.ResponseWriter, r *http.Request) {
_, _ = w.Write([]byte(r.Header.Get("Uber-Trace-Id")))
Expand Down
3 changes: 1 addition & 2 deletions middleware_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,9 @@ import (
)

func TestMiddleware(t *testing.T) {
tracer, closer, err := NewTracer("test", "")
tracer, err := NewTracer(context.Background(), "test", "")
require.NoError(t, err)
require.NotNil(t, tracer)
require.NotNil(t, closer)

type response struct {
SpanID string
Expand Down

0 comments on commit 0da7b1e

Please sign in to comment.