Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable correlation request ID header in ARM API. #3253

Merged
merged 1 commit into from
May 22, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 1 addition & 22 deletions azurerm/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,6 @@ import (
"github.com/Azure/go-autorest/autorest/adal"
az "github.com/Azure/go-autorest/autorest/azure"
"github.com/hashicorp/go-azure-helpers/authentication"
"github.com/hashicorp/go-uuid"
"github.com/hashicorp/terraform/httpclient"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/azure"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/utils"
Expand Down Expand Up @@ -369,30 +368,10 @@ type ArmClient struct {
policySetDefinitionsClient policy.SetDefinitionsClient
}

var (
msClientRequestIDOnce sync.Once
msClientRequestID string
)

// clientRequestID generates a UUID to pass through `x-ms-client-request-id` header.
func clientRequestID() string {
msClientRequestIDOnce.Do(func() {
var err error
msClientRequestID, err = uuid.GenerateUUID()

if err != nil {
log.Printf("[WARN] Fail to generate uuid for msClientRequestID: %+v", err)
}
})

log.Printf("[DEBUG] AzureRM Client Request Id: %s", msClientRequestID)
return msClientRequestID
}

func (c *ArmClient) configureClient(client *autorest.Client, auth autorest.Authorizer) {
setUserAgent(client, c.partnerId)
client.Authorizer = auth
//client.RequestInspector = azure.WithClientID(clientRequestID())
client.RequestInspector = azure.WithCorrelationRequestID(azure.CorrelationRequestID())
client.Sender = azure.BuildSender()
client.SkipResourceProviderRegistration = c.skipProviderRegistration
client.PollingDuration = 60 * time.Minute
Expand Down
16 changes: 0 additions & 16 deletions azurerm/config_test.go

This file was deleted.

41 changes: 41 additions & 0 deletions azurerm/helpers/azure/request.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package azure

import (
"log"
"sync"

"github.com/Azure/go-autorest/autorest"
"github.com/hashicorp/go-uuid"
)

const (
// HeaderCorrelationRequestID is the Azure extension header to set a user-specified correlation request ID.
HeaderCorrelationRequestID = "x-ms-correlation-request-id"
)

var (
msCorrelationRequestIDOnce sync.Once
msCorrelationRequestID string
)

// WithCorrelationRequestID returns a PrepareDecorator that adds an HTTP extension header of
// `x-ms-correlation-request-id` whose value is passed, undecorated UUID (e.g.,
// `7F5A6223-F475-4A9C-B9D5-12575AA6B11B`).
func WithCorrelationRequestID(uuid string) autorest.PrepareDecorator {
return autorest.WithHeader(HeaderCorrelationRequestID, uuid)
}

// CorrelationRequestID generates an UUID to pass through `x-ms-correlation-request-id` header.
func CorrelationRequestID() string {
msCorrelationRequestIDOnce.Do(func() {
var err error
msCorrelationRequestID, err = uuid.GenerateUUID()

if err != nil {
log.Printf("[WARN] Fail to generate uuid for msCorrelationRequestID: %+v", err)
}
})

log.Printf("[DEBUG] AzureRM Correlation Request Id: %s", msCorrelationRequestID)
return msCorrelationRequestID
}
31 changes: 31 additions & 0 deletions azurerm/helpers/azure/request_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package azure

import (
"net/http"
"testing"

"github.com/Azure/go-autorest/autorest"
)

func TestCorrelationRequestID(t *testing.T) {
first := CorrelationRequestID()

if first == "" {
t.Fatal("no correlation request ID generated")
}

second := CorrelationRequestID()
if first != second {
t.Fatal("subsequent correlation request ID not the same as the first")
}
}

func TestWithCorrelationRequestID(t *testing.T) {
uuid := CorrelationRequestID()
req, _ := autorest.Prepare(&http.Request{}, WithCorrelationRequestID(uuid))

if req.Header.Get(HeaderCorrelationRequestID) != uuid {
t.Fatalf("azure: withCorrelationRequestID failed to set %s -- expected %s, received %s",
HeaderCorrelationRequestID, uuid, req.Header.Get(HeaderCorrelationRequestID))
}
}