-
Notifications
You must be signed in to change notification settings - Fork 431
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
Percolate Azure correlation IDs to REST API calls #1574
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,11 +17,16 @@ limitations under the License. | |
package azure | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"net/http" | ||
"net/http/httptest" | ||
"sync" | ||
"testing" | ||
|
||
"github.com/Azure/go-autorest/autorest" | ||
. "github.com/onsi/gomega" | ||
"sigs.k8s.io/cluster-api-provider-azure/util/tele" | ||
) | ||
|
||
func TestGetDefaultImageSKUID(t *testing.T) { | ||
|
@@ -235,3 +240,51 @@ func TestGetDefaultUbuntuImage(t *testing.T) { | |
}) | ||
} | ||
} | ||
|
||
func TestMSCorrelationIDSendDecorator(t *testing.T) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @CecileRobertMichon @devigned would you prefer that I include a test here for There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this is fine with me, ideally we would add both but for this specific functionality a specific test is good enough |
||
g := NewWithT(t) | ||
const corrID tele.CorrID = "TestMSCorrelationIDSendDecoratorCorrID" | ||
ctx := context.WithValue(context.Background(), tele.CorrIDKeyVal, corrID) | ||
|
||
// create a fake server so that the sender can send to | ||
// somewhere | ||
var wg sync.WaitGroup | ||
receivedReqs := []*http.Request{} | ||
wg.Add(1) | ||
originHandler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
receivedReqs = append(receivedReqs, r) | ||
wg.Done() | ||
}) | ||
|
||
testSrv := httptest.NewServer(originHandler) | ||
defer testSrv.Close() | ||
|
||
// create a sender that sends to the fake server, then | ||
// decorate the sender with the msCorrelationIDSendDecorator | ||
origSender := autorest.SenderFunc(func(r *http.Request) (*http.Response, error) { | ||
// preserve the incoming headers to the fake server, so that | ||
// we can test that the fake server received the right | ||
// correlation ID header. | ||
req, err := http.NewRequest("GET", testSrv.URL, nil) | ||
if err != nil { | ||
return nil, err | ||
} | ||
req.Header = r.Header | ||
return testSrv.Client().Do(req) | ||
}) | ||
newSender := autorest.DecorateSender(origSender, msCorrelationIDSendDecorator) | ||
|
||
// create a new HTTP request and send it via the new decorated sender | ||
req, err := http.NewRequest("GET", "/abc", nil) | ||
g.Expect(err).NotTo(HaveOccurred()) | ||
|
||
req = req.WithContext(ctx) | ||
_, err = newSender.Do(req) | ||
g.Expect(err).NotTo(HaveOccurred()) | ||
wg.Wait() | ||
g.Expect(len(receivedReqs)).To(Equal(1)) | ||
receivedReq := receivedReqs[0] | ||
g.Expect( | ||
receivedReq.Header.Get(string(tele.CorrIDKeyVal)), | ||
).To(Equal(string(corrID))) | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@devigned related to our (offline) discussion today - you pointed out that autorest client creation code is centralized around this function. I this just wraps (wraps! 😆) the raw client's sender in code that extracts the correlation ID out of the request context, puts it into the header, and then calls through to the underlying sender. I think this does the trick. See below for a related comment on tests.