Skip to content

Commit cd21e55

Browse files
[internal] Export function to format user agent data so we can share it between non-HTTP and HTTP clients. (Azure#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 Azure#19673
1 parent f8cb16d commit cd21e55

File tree

3 files changed

+62
-0
lines changed

3 files changed

+62
-0
lines changed

sdk/internal/CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
### Features Added
66

7+
- Export user agent formatting code that used to be in azcore's policy_telemetry.go so it can be shared with non-HTTP clients (ie: azservicebus/azeventhubs). ([#19681](https://github.com/Azure/azure-sdk-for-go/pull/19681))
8+
79
### Breaking Changes
810

911
### Bugs Fixed
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT License.
3+
4+
package telemetry
5+
6+
import (
7+
"fmt"
8+
"os"
9+
"runtime"
10+
)
11+
12+
// Format creates the properly formatted SDK component for a User-Agent string.
13+
// Ex: azsdk-go-azservicebus/v1.0.0 (go1.19.3; linux)
14+
// comp - the package name for a component (ex: azservicebus)
15+
// ver - the version of the component (ex: v1.0.0)
16+
func Format(comp, ver string) string {
17+
// ex: azsdk-go-azservicebus/v1.0.0 (go1.19.3; windows)
18+
return fmt.Sprintf("azsdk-go-%s/%s %s", comp, ver, platformInfo)
19+
}
20+
21+
// platformInfo is the Go version and OS, formatted properly for insertion
22+
// into a User-Agent string. (ex: '(go1.19.3; windows')
23+
// NOTE: the ONLY function that should write to this variable is this func
24+
var platformInfo = func() string {
25+
operatingSystem := runtime.GOOS // Default OS string
26+
switch operatingSystem {
27+
case "windows":
28+
operatingSystem = os.Getenv("OS") // Get more specific OS information
29+
case "linux": // accept default OS info
30+
case "freebsd": // accept default OS info
31+
}
32+
return fmt.Sprintf("(%s; %s)", runtime.Version(), operatingSystem)
33+
}()
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT License.
3+
4+
package telemetry
5+
6+
import (
7+
"testing"
8+
9+
"github.com/stretchr/testify/require"
10+
)
11+
12+
func TestFormat(t *testing.T) {
13+
userAgent := Format("azservicebus", "v1.0.0")
14+
15+
// Examples:
16+
// * azsdk-go-azservicebus/v1.0.0 (go1.19.3; linux)
17+
// * azsdk-go-azservicebus/v1.0.0 (go1.19; Windows_NT)
18+
//
19+
// The OS varies based on the actual platform but it's a small set.
20+
re := `^azsdk-go-azservicebus/v1.0.0` +
21+
` ` +
22+
`\(` +
23+
`go\d+\.\d+(|\.\d+); (Windows_NT|linux|freebsd)` +
24+
`\)$`
25+
26+
require.Regexp(t, re, userAgent)
27+
}

0 commit comments

Comments
 (0)