-
Notifications
You must be signed in to change notification settings - Fork 10
feat: set requestId if not provided, log requestId on low level excep… #118
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
23 changes: 23 additions & 0 deletions
23
smartling-api-commons/src/main/java/com/smartling/api/v2/client/DefaultRequestIdFilter.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package com.smartling.api.v2.client; | ||
|
||
import org.apache.commons.lang3.StringUtils; | ||
|
||
import javax.ws.rs.client.ClientRequestContext; | ||
import javax.ws.rs.client.ClientRequestFilter; | ||
import java.io.IOException; | ||
import java.util.UUID; | ||
|
||
public class DefaultRequestIdFilter implements ClientRequestFilter | ||
{ | ||
private static final String REQUEST_ID_HEADER = "X-SL-RequestId"; | ||
|
||
@Override | ||
public void filter(ClientRequestContext requestContext) throws IOException | ||
{ | ||
String requestId = requestContext.getHeaderString(REQUEST_ID_HEADER); | ||
if (StringUtils.isBlank(requestId)) | ||
{ | ||
requestContext.getHeaders().addFirst(REQUEST_ID_HEADER, UUID.randomUUID().toString()); | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
24 changes: 24 additions & 0 deletions
24
smartling-api-commons/src/main/java/com/smartling/api/v2/client/request/RequestContext.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package com.smartling.api.v2.client.request; | ||
|
||
import lombok.Data; | ||
import org.jboss.resteasy.specimpl.MultivaluedMapImpl; | ||
|
||
import javax.ws.rs.client.ClientRequestContext; | ||
import javax.ws.rs.core.MultivaluedMap; | ||
import java.net.URI; | ||
|
||
@Data | ||
public class RequestContext | ||
{ | ||
private final String method; | ||
private final URI uri; | ||
private final MultivaluedMap<String, Object> headers; | ||
|
||
public static RequestContext fromClientRequestContext(ClientRequestContext context) | ||
{ | ||
MultivaluedMap<String, Object> headers = new MultivaluedMapImpl<>(); | ||
headers.putAll(context.getHeaders()); | ||
|
||
return new RequestContext(context.getMethod(), context.getUri(), headers); | ||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
...g-api-commons/src/main/java/com/smartling/api/v2/client/request/RequestContextFilter.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package com.smartling.api.v2.client.request; | ||
|
||
import javax.ws.rs.client.ClientRequestContext; | ||
import javax.ws.rs.client.ClientRequestFilter; | ||
import java.io.IOException; | ||
|
||
public class RequestContextFilter implements ClientRequestFilter | ||
{ | ||
@Override | ||
public void filter(ClientRequestContext clientRequestContext) throws IOException | ||
{ | ||
RequestContextHolder.setContext(RequestContext.fromClientRequestContext(clientRequestContext)); | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
...g-api-commons/src/main/java/com/smartling/api/v2/client/request/RequestContextHolder.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package com.smartling.api.v2.client.request; | ||
|
||
public class RequestContextHolder | ||
{ | ||
private static final ThreadLocal<RequestContext> REQUEST_CONTEXT = new ThreadLocal<>(); | ||
|
||
public static void setContext(RequestContext requestContext) | ||
{ | ||
REQUEST_CONTEXT.set(requestContext); | ||
} | ||
|
||
public static RequestContext getContext() | ||
{ | ||
return REQUEST_CONTEXT.get(); | ||
} | ||
|
||
public static void clearContext() | ||
{ | ||
REQUEST_CONTEXT.remove(); | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
...ns/src/main/java/com/smartling/api/v2/client/request/RequestContextInvocationHandler.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package com.smartling.api.v2.client.request; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
|
||
import java.lang.reflect.InvocationHandler; | ||
import java.lang.reflect.Method; | ||
|
||
@RequiredArgsConstructor | ||
public class RequestContextInvocationHandler implements InvocationHandler | ||
{ | ||
private final InvocationHandler delegate; | ||
|
||
@Override | ||
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable | ||
{ | ||
try | ||
{ | ||
return delegate.invoke(proxy, method, args); | ||
} | ||
finally | ||
{ | ||
RequestContextHolder.clearContext(); | ||
} | ||
} | ||
} |
91 changes: 91 additions & 0 deletions
91
...ing-api-commons/src/test/java/com/smartling/api/v2/client/DefaultRequestIdFilterTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
package com.smartling.api.v2.client; | ||
|
||
import org.junit.Before; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.mockito.Mock; | ||
import org.mockito.junit.MockitoJUnitRunner; | ||
|
||
import javax.ws.rs.client.ClientRequestContext; | ||
import javax.ws.rs.core.MultivaluedMap; | ||
|
||
import static org.mockito.ArgumentMatchers.eq; | ||
import static org.mockito.Mockito.any; | ||
import static org.mockito.Mockito.verify; | ||
import static org.mockito.Mockito.verifyNoMoreInteractions; | ||
import static org.mockito.Mockito.when; | ||
|
||
@RunWith(MockitoJUnitRunner.class) | ||
public class DefaultRequestIdFilterTest | ||
{ | ||
private static final String REQUEST_ID_HEADER = "X-SL-RequestId"; | ||
|
||
@Mock | ||
private ClientRequestContext requestContext; | ||
|
||
@Mock | ||
private MultivaluedMap<String, Object> headers; | ||
|
||
private final DefaultRequestIdFilter filter = new DefaultRequestIdFilter(); | ||
|
||
@Before | ||
public void setUp() | ||
{ | ||
when(requestContext.getHeaders()).thenReturn(headers); | ||
} | ||
|
||
@Test | ||
public void shouldNotAddHeaderWhenRequestIdHeaderExists() throws Exception | ||
{ | ||
// Given | ||
when(requestContext.getHeaderString(REQUEST_ID_HEADER)).thenReturn("existing-request-id"); | ||
|
||
// When | ||
filter.filter(requestContext); | ||
|
||
// Then | ||
verifyNoMoreInteractions(headers); | ||
} | ||
|
||
@Test | ||
public void shouldAddHeaderWhenRequestIdHeaderDoesNotExist() throws Exception | ||
{ | ||
// Given | ||
when(requestContext.getHeaderString(REQUEST_ID_HEADER)).thenReturn(null); | ||
|
||
// When | ||
filter.filter(requestContext); | ||
|
||
// Then | ||
verify(headers).addFirst(eq(REQUEST_ID_HEADER), any()); | ||
verifyNoMoreInteractions(headers); | ||
} | ||
|
||
@Test | ||
public void shouldAddHeaderWhenRequestIdHeaderIsEmpty() throws Exception | ||
{ | ||
// Given | ||
when(requestContext.getHeaderString(REQUEST_ID_HEADER)).thenReturn(""); | ||
|
||
// When | ||
filter.filter(requestContext); | ||
|
||
// Then | ||
verify(headers).addFirst(eq(REQUEST_ID_HEADER), any()); | ||
verifyNoMoreInteractions(headers); | ||
} | ||
|
||
@Test | ||
public void shouldAddHeaderWhenRequestIdHeaderIsWhitespace() throws Exception | ||
{ | ||
// Given | ||
when(requestContext.getHeaderString(REQUEST_ID_HEADER)).thenReturn(" "); | ||
|
||
// When | ||
filter.filter(requestContext); | ||
|
||
// Then | ||
verify(headers).addFirst(eq(REQUEST_ID_HEADER), any()); | ||
verifyNoMoreInteractions(headers); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.