From a5beb627fff646c716862ac7ed90e7ef82accb07 Mon Sep 17 00:00:00 2001 From: johncming Date: Tue, 25 Aug 2020 21:46:14 +0800 Subject: [PATCH] some fixies for consul sd. (#7799) * discovery/consul: make duration more accurate. Signed-off-by: johncming * discovery/consul: fix bug when context done. Signed-off-by: johncming --- discovery/consul/consul.go | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/discovery/consul/consul.go b/discovery/consul/consul.go index d8c41f5c1..ee2734def 100644 --- a/discovery/consul/consul.go +++ b/discovery/consul/consul.go @@ -363,13 +363,13 @@ func (d *Discovery) watchServices(ctx context.Context, ch chan<- []*targetgroup. catalog := d.client.Catalog() level.Debug(d.logger).Log("msg", "Watching services", "tags", strings.Join(d.watchedTags, ",")) - t0 := time.Now() opts := &consul.QueryOptions{ WaitIndex: *lastIndex, WaitTime: watchTimeout, AllowStale: d.allowStale, NodeMeta: d.watchedNodeMeta, } + t0 := time.Now() srvs, meta, err := catalog.Services(opts.WithContext(ctx)) elapsed := time.Since(t0) servicesRPCDuration.Observe(elapsed.Seconds()) @@ -456,18 +456,19 @@ func (d *Discovery) watchService(ctx context.Context, ch chan<- []*targetgroup.G go func() { ticker := time.NewTicker(d.refreshInterval) + defer ticker.Stop() var lastIndex uint64 health := srv.client.Health() for { select { case <-ctx.Done(): - ticker.Stop() return default: srv.watch(ctx, ch, health, &lastIndex) select { case <-ticker.C: case <-ctx.Done(): + return } } } @@ -478,7 +479,6 @@ func (d *Discovery) watchService(ctx context.Context, ch chan<- []*targetgroup.G func (srv *consulService) watch(ctx context.Context, ch chan<- []*targetgroup.Group, health *consul.Health, lastIndex *uint64) { level.Debug(srv.logger).Log("msg", "Watching service", "service", srv.name, "tags", strings.Join(srv.tags, ",")) - t0 := time.Now() opts := &consul.QueryOptions{ WaitIndex: *lastIndex, WaitTime: watchTimeout, @@ -486,6 +486,7 @@ func (srv *consulService) watch(ctx context.Context, ch chan<- []*targetgroup.Gr NodeMeta: srv.discovery.watchedNodeMeta, } + t0 := time.Now() serviceNodes, meta, err := health.ServiceMultipleTags(srv.name, srv.tags, false, opts.WithContext(ctx)) elapsed := time.Since(t0) serviceRPCDuration.Observe(elapsed.Seconds())