forked from alpe/cosmos-tracing
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcli.go
66 lines (61 loc) · 1.73 KB
/
cli.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
package tracing
import (
"io"
"github.com/opentracing/opentracing-go"
"github.com/spf13/cobra"
"github.com/uber/jaeger-client-go"
"github.com/uber/jaeger-client-go/config"
"github.com/uber/jaeger-lib/metrics"
)
func RunWithTracer(startCmd *cobra.Command, appName string) {
otherRunE := startCmd.RunE
var tracer io.Closer
startCmd.RunE = func(cmd *cobra.Command, args []string) error {
if hasTracerFlagSet(cmd) {
tracer = StartTracer(appName)
}
return otherRunE(cmd, args)
}
otherPostRun := startCmd.PostRun
startCmd.PostRun = func(cmd *cobra.Command, args []string) {
if tracer != nil {
tracer.Close()
}
if otherPostRun != nil {
otherPostRun(cmd, args)
}
}
AddModuleInitFlags(startCmd)
}
// todo: make this configurable
func StartTracer(appName string) io.Closer {
// Sample configuration for testing. Use constant sampling to sample every trace
// and enable LogSpan to log every span via configured Logger.
cfg := config.Configuration{
ServiceName: appName,
Sampler: &config.SamplerConfig{
Type: jaeger.SamplerTypeConst,
Param: 1,
},
Reporter: &config.ReporterConfig{
LogSpans: true,
QueueSize: 100,
},
}
// 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 := jaegerlog.StdLogger
jMetricsFactory := metrics.NullFactory
// Initialize tracer with a logger and a metrics factory
tracer, closer, err := cfg.NewTracer(
// jaegercfg.Logger(jLogger),
config.Metrics(jMetricsFactory),
)
if err != nil {
panic(err.Error())
}
// Set the singleton opentracing.Tracer with the Jaeger tracer.
opentracing.SetGlobalTracer(tracer)
return closer
}