diff --git a/documentation/examples/custom-sd/adapter-usage/main.go b/documentation/examples/custom-sd/adapter-usage/main.go index 9a5123eab..c8484891c 100644 --- a/documentation/examples/custom-sd/adapter-usage/main.go +++ b/documentation/examples/custom-sd/adapter-usage/main.go @@ -94,6 +94,7 @@ type discovery struct { clientDatacenter string tagSeparator string logger log.Logger + oldSourceList map[string]bool } func (d *discovery) parseServiceNodes(resp *http.Response, name string) (*targetgroup.Group, error) { @@ -180,6 +181,8 @@ func (d *discovery) Run(ctx context.Context, ch chan<- []*targetgroup.Group) { // list of targets simply because there may have been a timeout. If the service is actually // gone as far as consul is concerned, that will be picked up during the next iteration of // the outer loop. + + newSourceList := make(map[string]bool) for name := range srvs { if name == "consul" { continue @@ -195,7 +198,17 @@ func (d *discovery) Run(ctx context.Context, ch chan<- []*targetgroup.Group) { break } tgs = append(tgs, tg) + newSourceList[tg.Source] = true + } + // when targetGroup disappear, send an update with empty targetList + for key := range d.oldSourceList { + if !newSourceList[key] { + tgs = append(tgs, &targetgroup.Group{ + Source: key, + }) + } } + d.oldSourceList = newSourceList if err == nil { // We're returning all Consul services as a single targetgroup. ch <- tgs @@ -216,6 +229,7 @@ func newDiscovery(conf sdConfig) (*discovery, error) { refreshInterval: conf.RefreshInterval, tagSeparator: conf.TagSeparator, logger: logger, + oldSourceList: make(map[string]bool), } return cd, nil }