-
Notifications
You must be signed in to change notification settings - Fork 855
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[internal] Export function to format user agent data so we can share …
…it between non-HTTP and HTTP clients. (#19681) Export some code from `internal` so we that we can use the same user agent formatting code in both azservicebus+azeventhubs as we do in our HTTP clients. Part of the work for #19673
- Loading branch information
1 parent
f8cb16d
commit cd21e55
Showing
3 changed files
with
62 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License. | ||
|
||
package telemetry | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
"runtime" | ||
) | ||
|
||
// Format creates the properly formatted SDK component for a User-Agent string. | ||
// Ex: azsdk-go-azservicebus/v1.0.0 (go1.19.3; linux) | ||
// comp - the package name for a component (ex: azservicebus) | ||
// ver - the version of the component (ex: v1.0.0) | ||
func Format(comp, ver string) string { | ||
// ex: azsdk-go-azservicebus/v1.0.0 (go1.19.3; windows) | ||
return fmt.Sprintf("azsdk-go-%s/%s %s", comp, ver, platformInfo) | ||
} | ||
|
||
// platformInfo is the Go version and OS, formatted properly for insertion | ||
// into a User-Agent string. (ex: '(go1.19.3; windows') | ||
// NOTE: the ONLY function that should write to this variable is this func | ||
var platformInfo = func() string { | ||
operatingSystem := runtime.GOOS // Default OS string | ||
switch operatingSystem { | ||
case "windows": | ||
operatingSystem = os.Getenv("OS") // Get more specific OS information | ||
case "linux": // accept default OS info | ||
case "freebsd": // accept default OS info | ||
} | ||
return fmt.Sprintf("(%s; %s)", runtime.Version(), operatingSystem) | ||
}() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License. | ||
|
||
package telemetry | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestFormat(t *testing.T) { | ||
userAgent := Format("azservicebus", "v1.0.0") | ||
|
||
// Examples: | ||
// * azsdk-go-azservicebus/v1.0.0 (go1.19.3; linux) | ||
// * azsdk-go-azservicebus/v1.0.0 (go1.19; Windows_NT) | ||
// | ||
// The OS varies based on the actual platform but it's a small set. | ||
re := `^azsdk-go-azservicebus/v1.0.0` + | ||
` ` + | ||
`\(` + | ||
`go\d+\.\d+(|\.\d+); (Windows_NT|linux|freebsd)` + | ||
`\)$` | ||
|
||
require.Regexp(t, re, userAgent) | ||
} |