backport of commit 05604eeec1186a4c8a6156e7519a579171659c1a Partial backport to include fix without logging changes (logging targets code not included in 1.16.x). Co-authored-by: Michael Zalimeni <michael.zalimeni@hashicorp.com>