-
Notifications
You must be signed in to change notification settings - Fork 238
Add tenant id to lambda context and structured log messages #540
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 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 |
---|---|---|
|
@@ -15,6 +15,7 @@ | |
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.assertThrows; | ||
import com.amazonaws.services.lambda.runtime.api.client.runtimeapi.dto.ErrorRequest; | ||
import com.amazonaws.services.lambda.runtime.api.client.runtimeapi.dto.InvocationRequest; | ||
import com.amazonaws.services.lambda.runtime.api.client.runtimeapi.dto.StackElement; | ||
import com.amazonaws.services.lambda.runtime.api.client.runtimeapi.dto.XRayErrorCause; | ||
import com.amazonaws.services.lambda.runtime.api.client.runtimeapi.dto.XRayException; | ||
|
@@ -312,27 +313,62 @@ public void restoreNextWrongStatusCodeTest() { | |
} | ||
|
||
@Test | ||
public void nextTest() { | ||
public void nextWithoutTenantIdHeaderTest() { | ||
try { | ||
MockResponse mockResponse = new MockResponse(); | ||
mockResponse.setResponseCode(HTTP_ACCEPTED); | ||
mockResponse.setHeader("lambda-runtime-aws-request-id", "1234567890"); | ||
mockResponse.setHeader("Content-Type", "application/json"); | ||
MockResponse mockResponse = buildMockResponseForNextInvocation(); | ||
mockWebServer.enqueue(mockResponse); | ||
|
||
lambdaRuntimeApiClientImpl.nextInvocation(); | ||
RecordedRequest recordedRequest = mockWebServer.takeRequest(); | ||
HttpUrl actualUrl = recordedRequest.getRequestUrl(); | ||
String expectedUrl = "http://" + getHostnamePort() + "/2018-06-01/runtime/invocation/next"; | ||
assertEquals(expectedUrl, actualUrl.toString()); | ||
InvocationRequest invocationRequest = lambdaRuntimeApiClientImpl.nextInvocation(); | ||
verifyNextInvocationRequest(); | ||
assertNull(invocationRequest.getTenantId()); | ||
} catch(Exception e) { | ||
fail(); | ||
} | ||
} | ||
|
||
@Test | ||
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. great test! 💯 |
||
public void nextWithTenantIdHeaderTest() { | ||
try { | ||
MockResponse mockResponse = buildMockResponseForNextInvocation(); | ||
String expectedTenantId = "my-tenant-id"; | ||
mockResponse.setHeader("lambda-runtime-aws-tenant-id", expectedTenantId); | ||
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. I think it would be great to have another set of tests 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. Thanks for the suggestion. Added these test cases. |
||
mockWebServer.enqueue(mockResponse); | ||
|
||
InvocationRequest invocationRequest = lambdaRuntimeApiClientImpl.nextInvocation(); | ||
verifyNextInvocationRequest(); | ||
assertEquals(expectedTenantId, invocationRequest.getTenantId()); | ||
|
||
String actualBody = recordedRequest.getBody().readUtf8(); | ||
assertEquals("", actualBody); | ||
} catch(Exception e) { | ||
fail(); | ||
} | ||
} | ||
|
||
@Test | ||
public void nextWithEmptyTenantIdHeaderTest() { | ||
try { | ||
MockResponse mockResponse = buildMockResponseForNextInvocation(); | ||
mockResponse.setHeader("lambda-runtime-aws-tenant-id", ""); | ||
mockWebServer.enqueue(mockResponse); | ||
|
||
InvocationRequest invocationRequest = lambdaRuntimeApiClientImpl.nextInvocation(); | ||
verifyNextInvocationRequest(); | ||
assertNull(invocationRequest.getTenantId()); | ||
} catch(Exception e) { | ||
fail(); | ||
} | ||
} | ||
|
||
@Test | ||
public void nextWithNullTenantIdHeaderTest() { | ||
try { | ||
MockResponse mockResponse = buildMockResponseForNextInvocation(); | ||
assertThrows(NullPointerException.class, () -> { | ||
mockResponse.setHeader("lambda-runtime-aws-tenant-id", null); | ||
}); | ||
} catch(Exception e) { | ||
fail(); | ||
} | ||
} | ||
|
||
@Test | ||
public void createUrlMalformedTest() { | ||
|
@@ -376,6 +412,24 @@ public void lambdaReportErrorXRayHeaderTooLongTest() { | |
} | ||
} | ||
|
||
private MockResponse buildMockResponseForNextInvocation() { | ||
MockResponse mockResponse = new MockResponse(); | ||
mockResponse.setResponseCode(HTTP_ACCEPTED); | ||
mockResponse.setHeader("lambda-runtime-aws-request-id", "1234567890"); | ||
mockResponse.setHeader("Content-Type", "application/json"); | ||
return mockResponse; | ||
} | ||
|
||
private void verifyNextInvocationRequest() throws Exception { | ||
RecordedRequest recordedRequest = mockWebServer.takeRequest(); | ||
HttpUrl actualUrl = recordedRequest.getRequestUrl(); | ||
String expectedUrl = "http://" + getHostnamePort() + "/2018-06-01/runtime/invocation/next"; | ||
assertEquals(expectedUrl, actualUrl.toString()); | ||
|
||
String actualBody = recordedRequest.getBody().readUtf8(); | ||
assertEquals("", actualBody); | ||
} | ||
|
||
private String getHostnamePort() { | ||
return mockWebServer.getHostName() + ":" + mockWebServer.getPort(); | ||
} | ||
|
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.
hopefully at some point, we won't have a copy of aws-lambda-cpp in here, so let's keep that change here but I think you also need to modify the source of truth here: https://github.com/awslabs/aws-lambda-cpp? (Not blocking for this PR to be merged) I've also created an issue for us to track that change at some point: #541
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.
Ack.
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.
Agreed, one day, we could get rid of this. (Not blocking PR from my end either).