Agent service discovery

—
There are four different variables name 'err' in various scopes here.   (Are you sure the defer at line 194 isn't referencing the err variable defined in #189?)  It brings up all sorts of questions about scoping and shadowing, and is not immediately apparent what the err in the defered function references.

Please either rename the return variable (err error) so that it doesn't collide with the others, or just use the return variable named 'err' throughout the entire function.