@@ -4,17 +4,19 @@ import (
4
4
"context"
5
5
"errors"
6
6
"github.com/go-kit/kit/log"
7
+ "github.com/go-kit/kit/log/level"
7
8
"github.com/xmidt-org/webpa-common/logging"
8
9
"github.com/xmidt-org/webpa-common/service/monitor"
9
10
"github.com/xmidt-org/webpa-common/xresolver"
11
+ "net/url"
10
12
"regexp"
11
13
)
12
14
13
15
var find = regexp .MustCompile ("(.*)" + regexp .QuoteMeta ("[" ) + "(.*)" + regexp .QuoteMeta ("]" ) + regexp .QuoteMeta ("{" ) + "(.*)" + regexp .QuoteMeta ("}" ))
14
16
15
17
type Options struct {
16
18
// Watch is what to url to match with the consul service
17
- // exp. { "beta.google.com" : "caduceus" }
19
+ // exp. { "http:// beta.google.com:8080/notify " : "caduceus" }
18
20
Watch map [string ]string `json:"watch"`
19
21
20
22
Logger log.Logger `json:"-"`
@@ -23,8 +25,6 @@ type Options struct {
23
25
type ConsulWatcher struct {
24
26
logger log.Logger
25
27
26
- config Options
27
-
28
28
watch map [string ]string
29
29
balancers map [string ]* xresolver.RoundRobin
30
30
}
@@ -35,10 +35,7 @@ func NewConsulWatcher(o Options) *ConsulWatcher {
35
35
}
36
36
37
37
watcher := & ConsulWatcher {
38
- logger : logging .Debug (o .Logger ),
39
-
40
- config : o ,
41
-
38
+ logger : log .WithPrefix (o .Logger , "component" , "consulWatcher" ),
42
39
balancers : make (map [string ]* xresolver.RoundRobin ),
43
40
watch : make (map [string ]string ),
44
41
}
@@ -53,7 +50,7 @@ func NewConsulWatcher(o Options) *ConsulWatcher {
53
50
}
54
51
55
52
func (watcher * ConsulWatcher ) MonitorEvent (e monitor.Event ) {
56
- logging . Debug (watcher .logger , logging .MessageKey (), "received update route event" , "event" , e )
53
+ log . WithPrefix (watcher .logger , level . Key (), level . DebugValue ()). Log ( logging .MessageKey (), "received update route event" , "event" , e )
57
54
58
55
// update balancers
59
56
str := find .FindStringSubmatch (e .Key )
@@ -63,35 +60,43 @@ func (watcher *ConsulWatcher) MonitorEvent(e monitor.Event) {
63
60
64
61
service := str [1 ]
65
62
if rr , found := watcher .balancers [service ]; found {
66
- routes := make ([]xresolver.Route , len ( e . Instances ) )
67
- for index , instance := range e .Instances {
63
+ routes := make ([]xresolver.Route , 0 )
64
+ for _ , instance := range e .Instances {
68
65
// find records
69
66
route , err := xresolver .CreateRoute (instance )
70
67
if err != nil {
71
- logging . Error (watcher .logger , logging .MessageKey (), "failed to create route" , logging .MessageKey (), err , "instance" , instance )
68
+ log . WithPrefix (watcher .logger , level . Key (), level . ErrorValue ()). Log ( logging .MessageKey (), "failed to create route" , logging .MessageKey (), err , "instance" , instance )
72
69
continue
73
70
}
74
- routes [ index ] = route
71
+ routes = append ( routes , route )
75
72
}
76
73
rr .Update (routes )
77
- logging . Info (watcher .logger , logging .MessageKey (), "updating routes" , "service" , service , "new-routes" , routes )
74
+ log . WithPrefix (watcher .logger , level . Key (), level . InfoValue ()). Log ( logging .MessageKey (), "updating routes" , "service" , service , "new-routes" , routes )
78
75
}
79
76
}
80
77
81
- func (watcher * ConsulWatcher ) WatchService (url string , service string ) {
82
- if _ , found := watcher .watch [url ]; ! found {
83
- watcher .watch [url ] = service
78
+ func (watcher * ConsulWatcher ) WatchService (watchURL string , service string ) {
79
+ parsedURL , err := url .Parse (watchURL )
80
+ if err != nil {
81
+ log .WithPrefix (watcher .logger , level .Key (), level .ErrorValue ()).Log ("failed to parse url" , "url" , watchURL , "service" , service )
82
+ return
83
+ }
84
+ log .WithPrefix (watcher .logger , level .Key (), level .InfoValue ()).Log (logging .MessageKey (), "Watching Service" , "url" , watchURL , "service" , service , "host" , parsedURL .Hostname ())
85
+
86
+ if _ , found := watcher .watch [parsedURL .Hostname ()]; ! found {
87
+ watcher .watch [parsedURL .Hostname ()] = service
84
88
if _ , found := watcher .balancers [service ]; ! found {
85
89
watcher .balancers [service ] = xresolver .NewRoundRobinBalancer ()
86
90
}
87
91
}
88
92
}
89
93
90
94
func (watcher * ConsulWatcher ) LookupRoutes (ctx context.Context , host string ) ([]xresolver.Route , error ) {
91
- if _ , found := watcher .config .Watch [host ]; ! found {
95
+ if _ , found := watcher .watch [host ]; ! found {
96
+ log .WithPrefix (watcher .logger , level .Key (), level .ErrorValue ()).Log ("watch not found " , "host" , host )
92
97
return []xresolver.Route {}, errors .New (host + " is not part of the consul listener" )
93
98
}
94
- records , err := watcher .balancers [watcher .config . Watch [host ]].Get ()
95
- logging . Debug (watcher .logger , logging .MessageKey (), "looking up routes" , "routes" , records , logging .ErrorKey (), err )
99
+ records , err := watcher.balancers [watcher.watch [host ]].Get ()
100
+ log . WithPrefix (watcher .logger , level . Key (), level . DebugValue ()). Log ( logging .MessageKey (), "looking up routes" , "routes" , records , logging .ErrorKey (), err )
96
101
return records , err
97
102
}
0 commit comments