Skip to content

Commit

Permalink
Add telemetry string to User-Agent (#13732)
Browse files Browse the repository at this point in the history
  • Loading branch information
jhendrixMSFT committed Nov 20, 2020
1 parent db2bde2 commit 95b1a20
Show file tree
Hide file tree
Showing 7 changed files with 91 additions and 5 deletions.
5 changes: 5 additions & 0 deletions sdk/armcore/connection.go
Expand Up @@ -63,6 +63,11 @@ func NewConnection(endpoint string, cred azcore.TokenCredential, options *Connec
o := DefaultConnectionOptions()
options = &o
}
if options.Telemetry.Value == "" {
options.Telemetry.Value = UserAgent
} else {
options.Telemetry.Value += " " + UserAgent
}
p := azcore.NewPipeline(options.HTTPClient,
azcore.NewTelemetryPolicy(&options.Telemetry),
NewRPRegistrationPolicy(endpoint, cred, &options.RegisterRPOptions),
Expand Down
55 changes: 55 additions & 0 deletions sdk/armcore/connection_test.go
Expand Up @@ -8,6 +8,7 @@ package armcore
import (
"context"
"net/http"
"strings"
"testing"
"time"

Expand Down Expand Up @@ -46,6 +47,60 @@ func TestNewConnection(t *testing.T) {
}
}

func TestNewConnectionWithOptions(t *testing.T) {
srv, close := mock.NewServer()
defer close()
srv.AppendResponse()
opt := DefaultConnectionOptions()
opt.HTTPClient = srv
con := NewConnection(srv.URL(), mockTokenCred{}, &opt)
if ep := con.Endpoint(); ep != srv.URL() {
t.Fatalf("unexpected endpoint %s", ep)
}
req, err := azcore.NewRequest(context.Background(), http.MethodGet, srv.URL())
if err != nil {
t.Fatalf("Unexpected error: %v", err)
}
resp, err := con.Pipeline().Do(req)
if err != nil {
t.Fatalf("Unexpected error: %v", err)
}
if resp.StatusCode != http.StatusOK {
t.Fatalf("unexpected status code: %d", resp.StatusCode)
}
if ua := resp.Request.Header.Get(azcore.HeaderUserAgent); !strings.HasPrefix(ua, UserAgent) {
t.Fatalf("unexpected User-Agent %s", ua)
}
}

func TestNewConnectionWithCustomTelemetry(t *testing.T) {
const myTelemetry = "something"
srv, close := mock.NewServer()
defer close()
srv.AppendResponse()
opt := DefaultConnectionOptions()
opt.HTTPClient = srv
opt.Telemetry.Value = myTelemetry
con := NewConnection(srv.URL(), mockTokenCred{}, &opt)
if ep := con.Endpoint(); ep != srv.URL() {
t.Fatalf("unexpected endpoint %s", ep)
}
req, err := azcore.NewRequest(context.Background(), http.MethodGet, srv.URL())
if err != nil {
t.Fatalf("Unexpected error: %v", err)
}
resp, err := con.Pipeline().Do(req)
if err != nil {
t.Fatalf("Unexpected error: %v", err)
}
if resp.StatusCode != http.StatusOK {
t.Fatalf("unexpected status code: %d", resp.StatusCode)
}
if ua := resp.Request.Header.Get(azcore.HeaderUserAgent); !strings.HasPrefix(ua, myTelemetry+" "+UserAgent) {
t.Fatalf("unexpected User-Agent %s", ua)
}
}

func TestNewConnectionWithPipeline(t *testing.T) {
srv, close := mock.NewServer()
defer close()
Expand Down
2 changes: 1 addition & 1 deletion sdk/armcore/go.mod
Expand Up @@ -3,6 +3,6 @@ module github.com/Azure/azure-sdk-for-go/sdk/armcore
go 1.14

require (
github.com/Azure/azure-sdk-for-go/sdk/azcore v0.13.3
github.com/Azure/azure-sdk-for-go/sdk/azcore v0.13.4
github.com/Azure/azure-sdk-for-go/sdk/internal v0.5.0
)
4 changes: 2 additions & 2 deletions sdk/armcore/go.sum
@@ -1,5 +1,5 @@
github.com/Azure/azure-sdk-for-go/sdk/azcore v0.13.3 h1:Q/fMaKaJfKuwlQNELa2ttHS3zzn0KXf43HmrvAYRxMY=
github.com/Azure/azure-sdk-for-go/sdk/azcore v0.13.3/go.mod h1:pElNP+u99BvCZD+0jOlhI9OC/NB2IDTOTGZOZH0Qhq8=
github.com/Azure/azure-sdk-for-go/sdk/azcore v0.13.4 h1:7MfvHEWKfjZSKQNWERlXpHwCRoceEuQef/fB8CWmnQA=
github.com/Azure/azure-sdk-for-go/sdk/azcore v0.13.4/go.mod h1:pElNP+u99BvCZD+0jOlhI9OC/NB2IDTOTGZOZH0Qhq8=
github.com/Azure/azure-sdk-for-go/sdk/internal v0.5.0 h1:HG1ggl8L3ZkV/Ydanf7lKr5kkhhPGCpWdnr1J6v7cO4=
github.com/Azure/azure-sdk-for-go/sdk/internal v0.5.0/go.mod h1:k4KbFSunV/+0hOHL1vyFaPsiYQ1Vmvy1TBpmtvCDLZM=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
Expand Down
6 changes: 6 additions & 0 deletions sdk/armcore/policy_register_rp.go
Expand Up @@ -49,6 +49,10 @@ type RegistrationOptions struct {
// Retry configures the built-in retry policy behavior.
// Defaults to azcore.DefaultRetryOptions()
Retry azcore.RetryOptions

// Telemetry configures the built-in telemetry policy behavior.
// Defaults to azcore.DefaultTelemetryOptions()
Telemetry azcore.TelemetryOptions
}

// DefaultRegistrationOptions returns an instance of RegistrationOptions initialized with default values.
Expand All @@ -58,6 +62,7 @@ func DefaultRegistrationOptions() RegistrationOptions {
PollingDelay: 15 * time.Second,
PollingDuration: 5 * time.Minute,
Retry: azcore.DefaultRetryOptions(),
Telemetry: azcore.DefaultTelemetryOptions(),
}
}

Expand All @@ -72,6 +77,7 @@ func NewRPRegistrationPolicy(endpoint string, cred azcore.Credential, o *Registr
o = &def
}
p := azcore.NewPipeline(o.HTTPClient,
azcore.NewTelemetryPolicy(&o.Telemetry),
azcore.NewRetryPolicy(&o.Retry),
cred.AuthenticationPolicy(azcore.AuthenticationPolicyOptions{Options: azcore.TokenRequestOptions{Scopes: []string{endpointToScope(endpoint)}}}),
azcore.NewLogPolicy(nil))
Expand Down
10 changes: 8 additions & 2 deletions sdk/armcore/policy_register_rp_test.go
Expand Up @@ -68,7 +68,10 @@ func TestRPRegistrationPolicySuccess(t *testing.T) {
srv.AppendResponse(mock.WithStatusCode(http.StatusOK), mock.WithBody([]byte(rpRegisteredResp)))
// response for original request (different status code than any of the other responses)
srv.AppendResponse(mock.WithStatusCode(http.StatusAccepted))
pl := azcore.NewPipeline(srv, NewRPRegistrationPolicy(srv.URL(), azcore.AnonymousCredential(), testRPRegistrationOptions(srv)))
opt := DefaultConnectionOptions()
opt.HTTPClient = srv
opt.RegisterRPOptions = *testRPRegistrationOptions(srv)
con := NewConnection(srv.URL(), mockTokenCred{}, &opt)
req, err := azcore.NewRequest(context.Background(), http.MethodGet, azcore.JoinPaths(srv.URL(), requestEndpoint))
if err != nil {
t.Fatal(err)
Expand All @@ -83,7 +86,7 @@ func TestRPRegistrationPolicySuccess(t *testing.T) {
azcore.Log().SetListener(func(cls azcore.LogClassification, msg string) {
logEntries++
})
resp, err := pl.Do(req)
resp, err := con.Pipeline().Do(req)
if err != nil {
t.Fatal(err)
}
Expand All @@ -101,6 +104,9 @@ func TestRPRegistrationPolicySuccess(t *testing.T) {
if logEntries != 4 {
t.Fatalf("expected 4 log entries, got %d", logEntries)
}
if ua := resp.Request.Header.Get(azcore.HeaderUserAgent); !strings.HasPrefix(ua, UserAgent) {
t.Fatalf("unexpected User-Agent %s", ua)
}
}

func TestRPRegistrationPolicyNA(t *testing.T) {
Expand Down
14 changes: 14 additions & 0 deletions sdk/armcore/version.go
@@ -0,0 +1,14 @@
// +build go1.13

// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

package armcore

const (
// UserAgent is the string to be used in the user agent string when making requests.
UserAgent = "armcore/" + Version

// Version is the semantic version (see http://semver.org) of this module.
Version = "v0.5.1"
)

0 comments on commit 95b1a20

Please sign in to comment.