Merge pull request #4013 from sethvargo/sethvargo/user_agent
Add a helper for generating Consul's user-agent string
This commit is contained in:
commit
c4112f2b9a
|
@ -6,6 +6,7 @@ import (
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"github.com/hashicorp/consul/lib"
|
||||||
discover "github.com/hashicorp/go-discover"
|
discover "github.com/hashicorp/go-discover"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -67,7 +68,11 @@ func (r *retryJoiner) retryJoin() error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
disco := discover.Discover{}
|
disco, err := discover.New(discover.WithUserAgent(lib.UserAgent()))
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
r.logger.Printf("[INFO] agent: Retry join %s is supported for: %s", r.cluster, strings.Join(disco.Names(), " "))
|
r.logger.Printf("[INFO] agent: Retry join %s is supported for: %s", r.cluster, strings.Join(disco.Names(), " "))
|
||||||
r.logger.Printf("[INFO] agent: Joining %s cluster...", r.cluster)
|
r.logger.Printf("[INFO] agent: Joining %s cluster...", r.cluster)
|
||||||
attempt := 0
|
attempt := 0
|
||||||
|
|
|
@ -8,7 +8,10 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestGoDiscoverRegistration(t *testing.T) {
|
func TestGoDiscoverRegistration(t *testing.T) {
|
||||||
d := discover.Discover{}
|
d, err := discover.New()
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
got := d.Names()
|
got := d.Names()
|
||||||
want := []string{"aliyun", "aws", "azure", "digitalocean", "gce", "os", "scaleway", "softlayer", "triton"}
|
want := []string{"aliyun", "aws", "azure", "digitalocean", "gce", "os", "scaleway", "softlayer", "triton"}
|
||||||
if !reflect.DeepEqual(got, want) {
|
if !reflect.DeepEqual(got, want) {
|
||||||
|
|
|
@ -0,0 +1,29 @@
|
||||||
|
package lib
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"runtime"
|
||||||
|
|
||||||
|
"github.com/hashicorp/consul/version"
|
||||||
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
// projectURL is the project URL.
|
||||||
|
projectURL = "https://www.consul.io/"
|
||||||
|
|
||||||
|
// rt is the runtime - variable for tests.
|
||||||
|
rt = runtime.Version()
|
||||||
|
|
||||||
|
// versionFunc is the func that returns the current version. This is a
|
||||||
|
// function to take into account the different build processes and distinguish
|
||||||
|
// between enterprise and oss builds.
|
||||||
|
versionFunc = func() string {
|
||||||
|
return version.GetHumanVersion()
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
|
// UserAgent returns the consistent user-agent string for Consul.
|
||||||
|
func UserAgent() string {
|
||||||
|
return fmt.Sprintf("Consul/%s (+%s; %s)",
|
||||||
|
versionFunc(), projectURL, rt)
|
||||||
|
}
|
|
@ -0,0 +1,18 @@
|
||||||
|
package lib
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestUserAgent(t *testing.T) {
|
||||||
|
projectURL = "https://consul-test.com"
|
||||||
|
rt = "go5.0"
|
||||||
|
versionFunc = func() string { return "1.2.3" }
|
||||||
|
|
||||||
|
act := UserAgent()
|
||||||
|
|
||||||
|
exp := "Consul/1.2.3 (+https://consul-test.com; go5.0)"
|
||||||
|
if exp != act {
|
||||||
|
t.Errorf("expected %q to be %q", act, exp)
|
||||||
|
}
|
||||||
|
}
|
|
@ -52,4 +52,3 @@ DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
||||||
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||||
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||||
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||||
|
|
||||||
|
|
|
@ -11,8 +11,6 @@ under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
|
||||||
CONDITIONS OF ANY KIND, either express or implied. See the License for the
|
CONDITIONS OF ANY KIND, either express or implied. See the License for the
|
||||||
specific language governing permissions and limitations under the License.
|
specific language governing permissions and limitations under the License.
|
||||||
|
|
||||||
------
|
|
||||||
|
|
||||||
Apache License
|
Apache License
|
||||||
Version 2.0, January 2004
|
Version 2.0, January 2004
|
||||||
http://www.apache.org/licenses/
|
http://www.apache.org/licenses/
|
||||||
|
|
Loading…
Reference in New Issue