-
Notifications
You must be signed in to change notification settings - Fork 91
/
jaegertracing.go
335 lines (281 loc) · 8.74 KB
/
jaegertracing.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
/*
Package jaegertracing provides middleware to Opentracing using Jaeger.
Example:
```
package main
import (
"github.com/labstack/echo-contrib/jaegertracing"
"github.com/labstack/echo/v4"
)
func main() {
e := echo.New()
// Enable tracing middleware
c := jaegertracing.New(e, nil)
defer c.Close()
e.Logger.Fatal(e.Start(":1323"))
}
```
*/
package jaegertracing
import (
"bytes"
"crypto/rand"
"errors"
"fmt"
"io"
"net/http"
"reflect"
"runtime"
"time"
"github.com/labstack/echo/v4"
"github.com/labstack/echo/v4/middleware"
"github.com/opentracing/opentracing-go"
"github.com/opentracing/opentracing-go/ext"
"github.com/uber/jaeger-client-go/config"
)
const defaultComponentName = "echo/v4"
type (
// TraceConfig defines the config for Trace middleware.
TraceConfig struct {
// Skipper defines a function to skip middleware.
Skipper middleware.Skipper
// OpenTracing Tracer instance which should be got before
Tracer opentracing.Tracer
// ComponentName used for describing the tracing component name
ComponentName string
// add req body & resp body to tracing tags
IsBodyDump bool
// prevent logging long http request bodies
LimitHTTPBody bool
// http body limit size (in bytes)
// NOTE: don't specify values larger than 60000 as jaeger can't handle values in span.LogKV larger than 60000 bytes
LimitSize int
// OperationNameFunc composes operation name based on context. Can be used to override default naming
OperationNameFunc func(c echo.Context) string
}
)
var (
// DefaultTraceConfig is the default Trace middleware config.
DefaultTraceConfig = TraceConfig{
Skipper: middleware.DefaultSkipper,
ComponentName: defaultComponentName,
IsBodyDump: false,
LimitHTTPBody: true,
LimitSize: 60_000,
OperationNameFunc: defaultOperationName,
}
)
// New creates an Opentracing tracer and attaches it to Echo middleware.
// Returns Closer do be added to caller function as `defer closer.Close()`
func New(e *echo.Echo, skipper middleware.Skipper) io.Closer {
// Add Opentracing instrumentation
defcfg := config.Configuration{
ServiceName: "echo-tracer",
Sampler: &config.SamplerConfig{
Type: "const",
Param: 1,
},
Reporter: &config.ReporterConfig{
LogSpans: true,
BufferFlushInterval: 1 * time.Second,
},
}
cfg, err := defcfg.FromEnv()
if err != nil {
panic("Could not parse Jaeger env vars: " + err.Error())
}
tracer, closer, err := cfg.NewTracer()
if err != nil {
panic("Could not initialize jaeger tracer: " + err.Error())
}
opentracing.SetGlobalTracer(tracer)
e.Use(TraceWithConfig(TraceConfig{
Tracer: tracer,
Skipper: skipper,
}))
return closer
}
// Trace returns a Trace middleware.
// Trace middleware traces http requests and reporting errors.
func Trace(tracer opentracing.Tracer) echo.MiddlewareFunc {
c := DefaultTraceConfig
c.Tracer = tracer
c.ComponentName = defaultComponentName
return TraceWithConfig(c)
}
// TraceWithConfig returns a Trace middleware with config.
// See: `Trace()`.
func TraceWithConfig(config TraceConfig) echo.MiddlewareFunc {
if config.Tracer == nil {
panic("echo: trace middleware requires opentracing tracer")
}
if config.Skipper == nil {
config.Skipper = middleware.DefaultSkipper
}
if config.ComponentName == "" {
config.ComponentName = defaultComponentName
}
if config.OperationNameFunc == nil {
config.OperationNameFunc = defaultOperationName
}
return func(next echo.HandlerFunc) echo.HandlerFunc {
return func(c echo.Context) error {
if config.Skipper(c) {
return next(c)
}
req := c.Request()
opname := config.OperationNameFunc(c)
realIP := c.RealIP()
requestID := getRequestID(c) // request-id generated by reverse-proxy
var sp opentracing.Span
var err error
ctx, err := config.Tracer.Extract(
opentracing.HTTPHeaders,
opentracing.HTTPHeadersCarrier(req.Header),
)
if err != nil {
sp = config.Tracer.StartSpan(opname)
} else {
sp = config.Tracer.StartSpan(opname, ext.RPCServerOption(ctx))
}
defer sp.Finish()
ext.HTTPMethod.Set(sp, req.Method)
ext.HTTPUrl.Set(sp, req.URL.String())
ext.Component.Set(sp, config.ComponentName)
sp.SetTag("client_ip", realIP)
sp.SetTag("request_id", requestID)
// Dump request & response body
var respDumper *responseDumper
if config.IsBodyDump {
// request
reqBody := []byte{}
if c.Request().Body != nil {
reqBody, _ = io.ReadAll(c.Request().Body)
if config.LimitHTTPBody {
sp.LogKV("http.req.body", limitString(string(reqBody), config.LimitSize))
} else {
sp.LogKV("http.req.body", string(reqBody))
}
}
req.Body = io.NopCloser(bytes.NewBuffer(reqBody)) // reset original request body
// response
respDumper = newResponseDumper(c.Response())
c.Response().Writer = respDumper
}
// setup request context - add opentracing span
req = req.WithContext(opentracing.ContextWithSpan(req.Context(), sp))
c.SetRequest(req)
// call next middleware / controller
err = next(c)
if err != nil {
c.Error(err) // call custom registered error handler
}
status := c.Response().Status
ext.HTTPStatusCode.Set(sp, uint16(status))
if err != nil {
logError(sp, err)
}
// Dump response body
if config.IsBodyDump {
if config.LimitHTTPBody {
sp.LogKV("http.resp.body", limitString(respDumper.GetResponse(), config.LimitSize))
} else {
sp.LogKV("http.resp.body", respDumper.GetResponse())
}
}
return nil // error was already processed with ctx.Error(err)
}
}
}
func limitString(str string, size int) string {
if len(str) > size {
return str[:size/2] + "\n---- skipped ----\n" + str[len(str)-size/2:]
}
return str
}
func logError(span opentracing.Span, err error) {
var httpError *echo.HTTPError
if errors.As(err, &httpError) {
span.LogKV("error.message", httpError.Message)
} else {
span.LogKV("error.message", err.Error())
}
span.SetTag("error", true)
}
func getRequestID(ctx echo.Context) string {
requestID := ctx.Request().Header.Get(echo.HeaderXRequestID) // request-id generated by reverse-proxy
if requestID == "" {
requestID = generateToken() // missed request-id from proxy, we generate it manually
}
return requestID
}
func generateToken() string {
b := make([]byte, 16)
rand.Read(b)
return fmt.Sprintf("%x", b)
}
func defaultOperationName(c echo.Context) string {
req := c.Request()
return "HTTP " + req.Method + " URL: " + c.Path()
}
// TraceFunction wraps funtion with opentracing span adding tags for the function name and caller details
func TraceFunction(ctx echo.Context, fn interface{}, params ...interface{}) (result []reflect.Value) {
// Get function name
name := runtime.FuncForPC(reflect.ValueOf(fn).Pointer()).Name()
// Create child span
parentSpan := opentracing.SpanFromContext(ctx.Request().Context())
sp := opentracing.StartSpan(
"Function - "+name,
opentracing.ChildOf(parentSpan.Context()))
defer sp.Finish()
sp.SetTag("function", name)
// Get caller function name, file and line
pc := make([]uintptr, 15)
n := runtime.Callers(2, pc)
frames := runtime.CallersFrames(pc[:n])
frame, _ := frames.Next()
callerDetails := fmt.Sprintf("%s - %s#%d", frame.Function, frame.File, frame.Line)
sp.SetTag("caller", callerDetails)
// Check params and call function
f := reflect.ValueOf(fn)
if f.Type().NumIn() != len(params) {
e := fmt.Sprintf("Incorrect number of parameters calling wrapped function %s", name)
panic(e)
}
inputs := make([]reflect.Value, len(params))
for k, in := range params {
inputs[k] = reflect.ValueOf(in)
}
return f.Call(inputs)
}
// CreateChildSpan creates a new opentracing span adding tags for the span name and caller details.
// User must call defer `sp.Finish()`
func CreateChildSpan(ctx echo.Context, name string) opentracing.Span {
parentSpan := opentracing.SpanFromContext(ctx.Request().Context())
sp := opentracing.StartSpan(
name,
opentracing.ChildOf(parentSpan.Context()))
sp.SetTag("name", name)
// Get caller function name, file and line
pc := make([]uintptr, 15)
n := runtime.Callers(2, pc)
frames := runtime.CallersFrames(pc[:n])
frame, _ := frames.Next()
callerDetails := fmt.Sprintf("%s - %s#%d", frame.Function, frame.File, frame.Line)
sp.SetTag("caller", callerDetails)
return sp
}
// NewTracedRequest generates a new traced HTTP request with opentracing headers injected into it
func NewTracedRequest(method string, url string, body io.Reader, span opentracing.Span) (*http.Request, error) {
req, err := http.NewRequest(method, url, body)
if err != nil {
panic(err.Error())
}
ext.SpanKindRPCClient.Set(span)
ext.HTTPUrl.Set(span, url)
ext.HTTPMethod.Set(span, method)
span.Tracer().Inject(span.Context(),
opentracing.HTTPHeaders,
opentracing.HTTPHeadersCarrier(req.Header))
return req, err
}