Skip to content

Commit

Permalink
[internal] Export function to format user agent data so we can share …
Browse files Browse the repository at this point in the history
…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
richardpark-msft authored Dec 12, 2022
1 parent f8cb16d commit cd21e55
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 0 deletions.
2 changes: 2 additions & 0 deletions sdk/internal/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

### Features Added

- 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))

### Breaking Changes

### Bugs Fixed
Expand Down
33 changes: 33 additions & 0 deletions sdk/internal/telemetry/telemetry.go
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)
}()
27 changes: 27 additions & 0 deletions sdk/internal/telemetry/telemetry_test.go
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)
}

0 comments on commit cd21e55

Please sign in to comment.