-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Customizing azure-core Http Header in RetryPolicy #6217 #6400
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
Changes from 12 commits
cb7cd8e
ceba8b1
1f0abed
14f37b0
08b37ff
cb1f4f4
f5333dc
333e36e
20eadd4
ad88092
063dafd
b4bbb15
23dee25
e3f3ccd
878ccff
498a616
c076457
9c07867
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 |
|---|---|---|
|
|
@@ -3,22 +3,61 @@ | |
|
|
||
| package com.azure.core.http.policy; | ||
|
|
||
| import com.azure.core.http.HttpHeaders; | ||
| import com.azure.core.http.HttpRequest; | ||
| import com.azure.core.http.HttpPipelineCallContext; | ||
| import com.azure.core.http.HttpPipelineNextPolicy; | ||
| import com.azure.core.http.HttpResponse; | ||
| import reactor.core.publisher.Mono; | ||
|
|
||
| import java.util.Objects; | ||
| import java.util.UUID; | ||
| import java.util.function.Supplier; | ||
|
|
||
| /** | ||
| * The pipeline policy that puts a UUID in the request header. Azure uses the request id as | ||
| * The pipeline policy that puts a UUID or user provided id in the request header. Azure uses the request id as | ||
| * the unique identifier for the request. | ||
| */ | ||
| public class RequestIdPolicy implements HttpPipelinePolicy { | ||
| private static final String REQUEST_ID_HEADER = "x-ms-client-request-id"; | ||
| private final Supplier<HttpHeaders> requestIdSupplier; | ||
|
|
||
| /** | ||
| * Creates default {@link RequestIdPolicy}. | ||
| */ | ||
| public RequestIdPolicy() { | ||
| requestIdSupplier = null; | ||
|
hemanttanwar marked this conversation as resolved.
Outdated
|
||
| } | ||
|
|
||
| /** | ||
| * Creates {@link RequestIdPolicy} with provided {@link Supplier} to dynamically generate request id for each | ||
| * {@link HttpRequest}. | ||
| * | ||
| * @param requestIdSupplier to dynamically generate to request id for each {@link HttpRequest}. {@code null} is | ||
| * valid value. It is suggested that this {@link Supplier} should provide unique value every time | ||
| * it is called. Example of these headers are 'x-ms-client-request-id', 'x-ms-correlation-request-id'. | ||
| */ | ||
| public RequestIdPolicy(Supplier<HttpHeaders> requestIdSupplier) { | ||
| this.requestIdSupplier = requestIdSupplier; | ||
|
Contributor
Author
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. We are using HttpHeaders because, There can be multiple headers which are of ID nature. We already have been asked for multiple id related headers by app config Thus
hemanttanwar marked this conversation as resolved.
Outdated
|
||
| } | ||
|
|
||
| @Override | ||
| public Mono<HttpResponse> process(HttpPipelineCallContext context, HttpPipelineNextPolicy next) { | ||
|
|
||
| if (Objects.nonNull(requestIdSupplier)) { | ||
| HttpHeaders httpHeaders = requestIdSupplier.get(); | ||
| if (Objects.nonNull(httpHeaders) && httpHeaders.getSize() > 0) { | ||
|
Member
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. Continuing from a previous comment, if the supplier is made non-nullable and we add a default header in the base constructor (and possible default to this is null is passed into the overloaded constructor), should we do nothing if the supplier returns nothing.
Contributor
Author
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. You answered , I think, In order to track request, we do need request id. And if client does not provide or give us empty .. we should provide it. |
||
| httpHeaders.stream().forEach(httpHeader -> { | ||
|
hemanttanwar marked this conversation as resolved.
Outdated
|
||
| String requestIdHeaderValue = context.getHttpRequest().getHeaders().getValue(httpHeader.getName()); | ||
| if (requestIdHeaderValue == null) { | ||
|
Member
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 a general question for all policies which mutate the request, should this just insert the header value? |
||
| context.getHttpRequest().getHeaders().put(httpHeader.getName(), httpHeader.getValue()); | ||
| } | ||
| }); | ||
| return next.process(); | ||
| } | ||
| } | ||
|
|
||
| // If we were not able to set client provided Request ID header, we will set default 'REQUEST_ID_HEADER'. | ||
| String requestId = context.getHttpRequest().getHeaders().getValue(REQUEST_ID_HEADER); | ||
| if (requestId == null) { | ||
| context.getHttpRequest().getHeaders().put(REQUEST_ID_HEADER, UUID.randomUUID().toString()); | ||
|
|
||
This file was deleted.
Uh oh!
There was an error while loading. Please reload this page.