Skip to content

Commit

Permalink
Enable correlation request ID header in ARM API.
Browse files Browse the repository at this point in the history
  • Loading branch information
metacpp committed Apr 16, 2019
1 parent 2039b09 commit 27dbebb
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 38 deletions.
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))
}
}

0 comments on commit 27dbebb

Please sign in to comment.