-
Notifications
You must be signed in to change notification settings - Fork 0
/
component.go
76 lines (63 loc) · 1.65 KB
/
component.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
package tracing
import (
"context"
"io"
"github.com/davecgh/go-spew/spew"
"github.com/opentracing/opentracing-go"
platformConf "github.com/kiga-hub/arc/conf"
"github.com/kiga-hub/arc/micro"
microConf "github.com/kiga-hub/arc/micro/conf"
)
// ElementKey is ElementKey for tracing
var ElementKey = micro.ElementKey("TracingComponent")
// Component is Component for tracing
type Component struct {
micro.EmptyComponent
closer io.Closer
enable bool
}
// Name of the component
func (c *Component) Name() string {
return "Trace"
}
// PreInit called before Init()
func (c *Component) PreInit(ctx context.Context) error {
_ = ctx
// load config
SetDefaultTraceConfig()
return nil
}
// SetDynamicConfig called when get dynamic config for the first time
func (c *Component) SetDynamicConfig(config *platformConf.NodeConfig) error {
c.enable = config.APM != nil && !config.APM.EnableTrace
return nil
}
// Init the component
func (c *Component) Init(server *micro.Server) error {
// init
var err error
var tracer opentracing.Tracer
// setup tracer
basicConf := microConf.GetBasicConfig()
traceConf := GetTraceConfig()
//logger := server.Get(&logging.Key).(logging.ILogger)
if c.enable {
server.RegisterElement(&ElementKey, opentracing.NoopTracer{})
return nil
}
tracer, c.closer, err = CreateTracer(*basicConf, *traceConf, nil)
if err != nil {
panic("Could not initialize jaeger tracer: " + err.Error())
}
server.RegisterElement(&ElementKey, tracer)
if basicConf.IsDevMode {
spew.Dump(traceConf)
}
return nil
}
// PostStop called after Stop()
func (c *Component) PostStop(ctx context.Context) error {
_ = ctx
// post stop
return c.closer.Close()
}