Skip to content

Commit e36c6f1

Browse files
committed
Merge branch 'carlosedp-jaeger-env'
2 parents c6fd37a + bbdb01f commit e36c6f1

File tree

3 files changed

+66
-41
lines changed

3 files changed

+66
-41
lines changed

go.mod

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ require (
1313
github.com/prometheus/client_golang v1.1.0
1414
github.com/stretchr/testify v1.3.0
1515
github.com/uber-go/atomic v1.4.0 // indirect
16-
github.com/uber/jaeger-client-go v2.17.0+incompatible
17-
github.com/uber/jaeger-lib v2.1.1+incompatible // indirect
16+
github.com/uber/jaeger-client-go v2.19.1-0.20191002155754-0be28c34dabf+incompatible
17+
github.com/uber/jaeger-lib v2.2.0+incompatible // indirect
1818
go.uber.org/atomic v1.4.0 // indirect
1919
)
2020

go.sum

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ github.com/couchbase/go-couchbase v0.0.0-20181122212707-3e9b6e1258bb/go.mod h1:T
2525
github.com/couchbase/gomemcached v0.0.0-20181122193126-5125a94a666c/go.mod h1:srVSlQLB8iXBVXHgnqemxUXqN6FCvClgCMPCsjBDR7c=
2626
github.com/couchbase/goutils v0.0.0-20180530154633-e865a1461c8a/go.mod h1:BQwMFlJzDjFDG3DJUdU0KORxn88UlsOULuxLExMh3Hs=
2727
github.com/cupcake/rdb v0.0.0-20161107195141-43ba34106c76/go.mod h1:vYwsqCOLxGiisLwp9rITslkFNpZD5rz43tf41QFkTWY=
28-
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
2928
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
3029
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
3130
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
@@ -64,7 +63,6 @@ github.com/json-iterator/go v1.1.6/go.mod h1:+SdeFBvtyEkXs7REEP0seUULqWtbJapLOCV
6463
github.com/json-iterator/go v1.1.7 h1:KfgG9LzI+pYjr4xvmz/5H4FXjokeP+rlHLhv3iH62Fo=
6564
github.com/json-iterator/go v1.1.7/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/uOdHXbAo4=
6665
github.com/julienschmidt/httprouter v1.2.0/go.mod h1:SYymIcj16QtmaHHD7aYtjjsJG7VTCxuUUipMqKk8s4w=
67-
github.com/konsorten/go-windows-terminal-sequences v1.0.1 h1:mweAR1A6xJ3oS2pRaGiHgQ4OO8tzTaLawm8vnODuwDk=
6866
github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ=
6967
github.com/kr/logfmt v0.0.0-20140226030751-b84e30acd515/go.mod h1:+0opPa2QZZtGFBFZlji/RkVcI2GknAs/DXo4wKdlNEc=
7068
github.com/labstack/echo v3.3.10+incompatible h1:pGRcYk231ExFAyoAjAfD85kQzRJCRI8bbnE7CX5OEgg=
@@ -93,7 +91,6 @@ github.com/mwitkow/go-conntrack v0.0.0-20161129095857-cc309e4a2223/go.mod h1:qRW
9391
github.com/opentracing/opentracing-go v1.1.0 h1:pWlfV3Bxv7k65HYwkikxat0+s3pV4bsqf19k25Ur8rU=
9492
github.com/opentracing/opentracing-go v1.1.0/go.mod h1:UkNAQd3GIcIGf0SeVgPpRdFStlNbqXla1AfSYxPUl2o=
9593
github.com/pelletier/go-toml v1.2.0/go.mod h1:5z9KED0ma1S8pY6P1sdut58dfprrGBbd/94hg7ilaic=
96-
github.com/pkg/errors v0.8.0 h1:WdK/asTD0HN+q6hsWO3/vpuAkAr+tw6aNJNDFFf0+qw=
9794
github.com/pkg/errors v0.8.0/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
9895
github.com/pkg/errors v0.8.1 h1:iURUrRGxPUNPdy5/HRSm+Yj6okJ6UtLINN0Q9M4+h3I=
9996
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
@@ -120,7 +117,6 @@ github.com/sirupsen/logrus v1.2.0/go.mod h1:LxeOpSwHxABJmUn/MG1IvRgCAasNZTLOkJPx
120117
github.com/ssdb/gossdb v0.0.0-20180723034631-88f6b59b84ec/go.mod h1:QBvMkMya+gXctz3kmljlUCu/yB3GZ6oee+dUozsezQE=
121118
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
122119
github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
123-
github.com/stretchr/objx v0.2.0 h1:Hbg2NidpLE8veEBkEZTL3CvlkUIVzuU9jDplZO54c48=
124120
github.com/stretchr/objx v0.2.0/go.mod h1:qt09Ya8vawLte6SNmTgCsAVtYtaKzEcn8ATUoHMkEqE=
125121
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
126122
github.com/stretchr/testify v1.3.0 h1:TivCn/peBQ7UY8ooIcPgZFpTNSz0Q2U6UrFlUfqbe0Q=
@@ -134,10 +130,10 @@ github.com/tidwall/pretty v0.0.0-20190325153808-1166b9ac2b65 h1:rQ229MBgvW68s1/g
134130
github.com/tidwall/pretty v0.0.0-20190325153808-1166b9ac2b65/go.mod h1:XNkn88O1ChpSDQmQeStsy+sBenx6DDtFZJxhVysOjyk=
135131
github.com/uber-go/atomic v1.4.0 h1:yOuPqEq4ovnhEjpHmfFwsqBXDYbQeT6Nb0bwD6XnD5o=
136132
github.com/uber-go/atomic v1.4.0/go.mod h1:/Ct5t2lcmbJ4OSe/waGBoaVvVqtO0bmtfVNex1PFV8g=
137-
github.com/uber/jaeger-client-go v2.17.0+incompatible h1:35tpDuT3k0oBiN/aGoSWuiFaqKgKZSciSMnWrazhSHE=
138-
github.com/uber/jaeger-client-go v2.17.0+incompatible/go.mod h1:WVhlPFC8FDjOFMMWRy2pZqQJSXxYSwNYOkTr/Z6d3Kk=
139-
github.com/uber/jaeger-lib v2.1.1+incompatible h1:VY/6p2WopO09BPnw787RbaCIlfKbCRC/kq3p5D0F168=
140-
github.com/uber/jaeger-lib v2.1.1+incompatible/go.mod h1:ComeNDZlWwrWnDv8aPp0Ba6+uUTzImX/AauajbLI56U=
133+
github.com/uber/jaeger-client-go v2.19.1-0.20191002155754-0be28c34dabf+incompatible h1:jmYcA5GfuedOiQoAN2v5M5NC91d+IDazw/TnZidAyRY=
134+
github.com/uber/jaeger-client-go v2.19.1-0.20191002155754-0be28c34dabf+incompatible/go.mod h1:WVhlPFC8FDjOFMMWRy2pZqQJSXxYSwNYOkTr/Z6d3Kk=
135+
github.com/uber/jaeger-lib v2.2.0+incompatible h1:MxZXOiR2JuoANZ3J6DE/U0kSFv/eJ/GfSYVCjK7dyaw=
136+
github.com/uber/jaeger-lib v2.2.0+incompatible/go.mod h1:ComeNDZlWwrWnDv8aPp0Ba6+uUTzImX/AauajbLI56U=
141137
github.com/ugorji/go v1.1.4 h1:j4s+tAvLfL3bZyefP2SEWmhBzmuIlH/eqNuPdFPgngw=
142138
github.com/ugorji/go v1.1.4/go.mod h1:uQMGLiO92mf5W77hV/PUCpI3pbzQx3CRekS0kk+RGrc=
143139
github.com/valyala/bytebufferpool v1.0.0 h1:GqA5TC/0021Y/b9FG4Oi9Mr3q7XYx6KllzawFIhcdPw=
@@ -156,7 +152,6 @@ golang.org/x/net v0.0.0-20181114220301-adae6a3d119a/go.mod h1:mL1N/T3taQHkDXs73r
156152
golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
157153
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
158154
golang.org/x/net v0.0.0-20190503192946-f4e77d36d62c/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
159-
golang.org/x/net v0.0.0-20190607181551-461777fb6f67 h1:rJJxsykSlULwd2P2+pg/rtnwN2FrWp4IuCxOSyS0V00=
160155
golang.org/x/net v0.0.0-20190607181551-461777fb6f67/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
161156
golang.org/x/net v0.0.0-20190613194153-d28f0bde5980 h1:dfGZHvZk057jK2MCeWus/TowKpJ8y4AmooUzdBSR9GU=
162157
golang.org/x/net v0.0.0-20190613194153-d28f0bde5980/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=

jaegertracing/jaegertracing.go

Lines changed: 60 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@ import (
3232
"github.com/labstack/echo/v4/middleware"
3333
"github.com/opentracing/opentracing-go"
3434
"github.com/opentracing/opentracing-go/ext"
35-
"github.com/uber/jaeger-client-go"
3635
"github.com/uber/jaeger-client-go/config"
3736
)
3837

@@ -64,20 +63,26 @@ var (
6463
// Returns Closer do be added to caller function as `defer closer.Close()`
6564
func New(e *echo.Echo, skipper middleware.Skipper) io.Closer {
6665
// Add Opentracing instrumentation
67-
cfg := config.Configuration{
66+
defcfg := config.Configuration{
67+
ServiceName: "echo-tracer",
6868
Sampler: &config.SamplerConfig{
6969
Type: "const",
7070
Param: 1,
7171
},
7272
Reporter: &config.ReporterConfig{
73-
LogSpans: false,
73+
LogSpans: true,
7474
BufferFlushInterval: 1 * time.Second,
7575
},
7676
}
77-
tracer, closer, _ := cfg.New(
78-
"echo-tracer",
79-
config.Logger(jaeger.StdLogger),
80-
)
77+
cfg, err := defcfg.FromEnv()
78+
if err != nil {
79+
panic("Could not parse Jaeger env vars: " + err.Error())
80+
}
81+
tracer, closer, err := cfg.NewTracer()
82+
if err != nil {
83+
panic("Could not initialize jaeger tracer: " + err.Error())
84+
}
85+
8186
opentracing.SetGlobalTracer(tracer)
8287
e.Use(TraceWithConfig(TraceConfig{
8388
Tracer: tracer,
@@ -87,7 +92,6 @@ func New(e *echo.Echo, skipper middleware.Skipper) io.Closer {
8792
}
8893

8994
// Trace returns a Trace middleware.
90-
//
9195
// Trace middleware traces http requests and reporting errors.
9296
func Trace(tracer opentracing.Tracer) echo.MiddlewareFunc {
9397
c := DefaultTraceConfig
@@ -149,45 +153,71 @@ func TraceWithConfig(config TraceConfig) echo.MiddlewareFunc {
149153

150154
// TraceFunction wraps funtion with opentracing span adding tags for the function name and caller details
151155
func TraceFunction(ctx echo.Context, fn interface{}, params ...interface{}) (result []reflect.Value) {
152-
f := reflect.ValueOf(fn)
153-
if f.Type().NumIn() != len(params) {
154-
panic("incorrect number of parameters!")
155-
}
156-
inputs := make([]reflect.Value, len(params))
157-
for k, in := range params {
158-
inputs[k] = reflect.ValueOf(in)
159-
}
160-
pc, file, no, ok := runtime.Caller(1)
161-
details := runtime.FuncForPC(pc)
156+
// Get function name
162157
name := runtime.FuncForPC(reflect.ValueOf(fn).Pointer()).Name()
158+
// Create child span
163159
parentSpan := opentracing.SpanFromContext(ctx.Request().Context())
164160
sp := opentracing.StartSpan(
165161
"Function - "+name,
166162
opentracing.ChildOf(parentSpan.Context()))
167-
(opentracing.Tag{Key: "function", Value: name}).Set(sp)
168-
if ok {
169-
callerDetails := fmt.Sprintf("%s - %s#%d", details.Name(), file, no)
170-
(opentracing.Tag{Key: "caller", Value: callerDetails}).Set(sp)
163+
defer sp.Finish()
171164

165+
sp.SetTag("function", name)
166+
167+
// Get caller function name, file and line
168+
pc := make([]uintptr, 15)
169+
n := runtime.Callers(2, pc)
170+
frames := runtime.CallersFrames(pc[:n])
171+
frame, _ := frames.Next()
172+
callerDetails := fmt.Sprintf("%s - %s#%d", frame.Function, frame.File, frame.Line)
173+
sp.SetTag("caller", callerDetails)
174+
175+
// Check params and call function
176+
f := reflect.ValueOf(fn)
177+
if f.Type().NumIn() != len(params) {
178+
e := fmt.Sprintf("Incorrect number of parameters calling wrapped function %s", name)
179+
panic(e)
180+
}
181+
inputs := make([]reflect.Value, len(params))
182+
for k, in := range params {
183+
inputs[k] = reflect.ValueOf(in)
172184
}
173-
defer sp.Finish()
174185
return f.Call(inputs)
175186
}
176187

177188
// CreateChildSpan creates a new opentracing span adding tags for the span name and caller details.
178189
// User must call defer `sp.Finish()`
179190
func CreateChildSpan(ctx echo.Context, name string) opentracing.Span {
180-
pc, file, no, ok := runtime.Caller(1)
181-
details := runtime.FuncForPC(pc)
182191
parentSpan := opentracing.SpanFromContext(ctx.Request().Context())
183192
sp := opentracing.StartSpan(
184193
name,
185194
opentracing.ChildOf(parentSpan.Context()))
186-
(opentracing.Tag{Key: "name", Value: name}).Set(sp)
187-
if ok {
188-
callerDetails := fmt.Sprintf("%s - %s#%d", details.Name(), file, no)
189-
(opentracing.Tag{Key: "caller", Value: callerDetails}).Set(sp)
195+
sp.SetTag("name", name)
196+
197+
// Get caller function name, file and line
198+
pc := make([]uintptr, 15)
199+
n := runtime.Callers(2, pc)
200+
frames := runtime.CallersFrames(pc[:n])
201+
frame, _ := frames.Next()
202+
callerDetails := fmt.Sprintf("%s - %s#%d", frame.Function, frame.File, frame.Line)
203+
sp.SetTag("caller", callerDetails)
190204

191-
}
192205
return sp
193206
}
207+
208+
// NewTracedRequest generates a new traced HTTP request with opentracing headers injected into it
209+
func NewTracedRequest(method string, url string, body io.Reader, span opentracing.Span) (*http.Request, error) {
210+
req, err := http.NewRequest(method, url, nil)
211+
if err != nil {
212+
panic(err.Error())
213+
}
214+
215+
ext.SpanKindRPCClient.Set(span)
216+
ext.HTTPUrl.Set(span, url)
217+
ext.HTTPMethod.Set(span, method)
218+
span.Tracer().Inject(span.Context(),
219+
opentracing.HTTPHeaders,
220+
opentracing.HTTPHeadersCarrier(req.Header))
221+
222+
return req, err
223+
}

0 commit comments

Comments
 (0)