Repository: Azure/azure-service-bus-go
Pull request #100 (2019-02-11 22:13:00)

Add NamespaceOption to set custom telemetry value (user-agent)
This change adds an optional NamespaceOption that will append a custom telemetryValue to the rootUserAgent.  This feature is useful for abstraction libraries such as go-cloud, etc.


Patch:
diff --git a/namespace.go b/namespace.go
index 212e38c..2ded089 100644
--- a/namespace.go
+++ b/namespace.go
@@ -57,6 +57,7 @@ type (
 		Name          string
 		TokenProvider auth.TokenProvider
 		Environment   azure.Environment
+		userAgent     string
 	}
 
 	// NamespaceOption provides structure for configuring a new Service Bus namespace
@@ -82,6 +83,14 @@ func NamespaceWithConnectionString(connStr string) NamespaceOption {
 	}
 }
 
+// NamespaceWithUserAgent appends to the root user-agent value.
+func NamespaceWithUserAgent(userAgent string) NamespaceOption {
+	return func(ns *Namespace) error {
+		ns.userAgent = userAgent
+		return nil
+	}
+}
+
 // NewNamespace creates a new namespace configured through NamespaceOption(s)
 func NewNamespace(opts ...NamespaceOption) (*Namespace, error) {
 	ns := &Namespace{
@@ -107,7 +116,7 @@ func (ns *Namespace) newConnection() (*amqp.Client, error) {
 		amqp.ConnProperty("version", Version),
 		amqp.ConnProperty("platform", runtime.GOOS),
 		amqp.ConnProperty("framework", runtime.Version()),
-		amqp.ConnProperty("user-agent", rootUserAgent),
+		amqp.ConnProperty("user-agent", ns.getUserAgent()),
 	)
 }
 
@@ -130,3 +139,11 @@ func (ns *Namespace) getHTTPSHostURI() string {
 func (ns *Namespace) getEntityAudience(entityPath string) string {
 	return ns.getAMQPHostURI() + entityPath
 }
+
+func (ns *Namespace) getUserAgent() string {
+	userAgent := rootUserAgent
+	if ns.userAgent != "" {
+		userAgent = fmt.Sprintf("%s/%s", userAgent, ns.userAgent)
+	}
+	return userAgent
+}


Test patch:
diff --git a/namespace_useragent_test.go b/namespace_useragent_test.go
new file mode 100644
index 0000000..3c152df
--- /dev/null
+++ b/namespace_useragent_test.go
@@ -0,0 +1,45 @@
+package servicebus
+
+//	MIT License
+//
+//	Copyright (c) Microsoft Corporation. All rights reserved.
+//
+//	Permission is hereby granted, free of charge, to any person obtaining a copy
+//	of this software and associated documentation files (the "Software"), to deal
+//	in the Software without restriction, including without limitation the rights
+//	to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+//	copies of the Software, and to permit persons to whom the Software is
+//	furnished to do so, subject to the following conditions:
+//
+//	The above copyright notice and this permission notice shall be included in all
+//	copies or substantial portions of the Software.
+//
+//	THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+//	IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+//	FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+//	AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+//	LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+//	OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+//	SOFTWARE
+
+import (
+	"fmt"
+	"testing"
+
+	"github.com/stretchr/testify/assert"
+)
+func TestNamespaceWithUserAgentOption(t *testing.T) {
+	userAgent := "custom-user-agent"
+	nsUserAgentOption := NamespaceWithUserAgent(userAgent)
+	ns, err := NewNamespace(nsUserAgentOption)
+	assert.Nil(t, err)
+	assert.Equal(t, fmt.Sprintf("%s/%s", rootUserAgent, userAgent), ns.getUserAgent())
+}
+
+func TestNamespaceWithoutUserAgentOption(t *testing.T) {
+	userAgent := ""
+	nsUserAgentOption := NamespaceWithUserAgent(userAgent)
+	ns, err := NewNamespace(nsUserAgentOption)
+	assert.Nil(t, err)
+	assert.Equal(t, rootUserAgent, ns.getUserAgent())
+}
\ No newline at end of file