jaeger.go
2.2 KB
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
package tracer
import (
"fmt"
"gitlab.fjmaimaimai.com/mmm-go/gocomm/common"
"net/http"
"github.com/opentracing-contrib/go-stdlib/nethttp"
"github.com/opentracing/opentracing-go"
"github.com/uber/jaeger-client-go"
jaegercfg "github.com/uber/jaeger-client-go/config"
"github.com/uber/jaeger-lib/metrics"
"github.com/uber/jaeger-lib/metrics/metricstest"
"gitlab.fjmaimaimai.com/mmm-go/gocomm/pkg/log"
)
func Init(serviceName, addr string) (opentracing.Tracer, error) {
// Sample configuration for testing. Use constant sampling to sample every trace
// and enable LogSpan to log every span via configured Logger.
cfg := jaegercfg.Configuration{
Sampler: &jaegercfg.SamplerConfig{
Type: jaeger.SamplerTypeConst,
Param: 1,
},
Reporter: &jaegercfg.ReporterConfig{
LogSpans: true,
},
}
cfg.ServiceName = serviceName
// Example logger and metrics factory. Use github.com/uber/jaeger-client-go/log
// and github.com/uber/jaeger-lib/metrics respectively to bind to real logging and metrics
// frameworks.
jLogger := &jaegerLogger{}
jMetricsFactory := metrics.NullFactory
metricsFactory := metricstest.NewFactory(0)
metrics := jaeger.NewMetrics(metricsFactory, nil)
sender, err := jaeger.NewUDPTransport(addr, 0)
if err != nil {
log.Error("could not initialize jaeger sender:", err.Error())
return nil, err
}
repoter := jaeger.NewRemoteReporter(sender, jaeger.ReporterOptions.Metrics(metrics))
tracer, _, err := cfg.NewTracer(
jaegercfg.Logger(jLogger),
jaegercfg.Metrics(jMetricsFactory),
jaegercfg.Reporter(repoter),
)
if err != nil {
return nil, fmt.Errorf("new trace error: %v", err)
}
return tracer, nil
}
type jaegerLogger struct{}
func (l *jaegerLogger) Error(msg string) {
log.Error(common.LogF("ERROR: %s", msg))
}
// Infof logs a message at info priority
func (l *jaegerLogger) Infof(msg string, args ...interface{}) {
log.Info(common.LogF(msg,args))
}
func TracingMiddleware(handler http.Handler) http.Handler {
return nethttp.Middleware(
opentracing.GlobalTracer(),
handler,
nethttp.MWSpanObserver(func(span opentracing.Span, r *http.Request) {
}),
nethttp.OperationNameFunc(func(r *http.Request) string {
return "HTTP " + r.Method + " " + r.RequestURI
}),
)
}