mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
feat(lambda): give each durable execution an X-Ray trace header (#8161)
This commit is contained in:
1 parent
55b2fb7bd3
commit
f5e581a872
23 files changed
+510
-53
No files matched your search
@@ -0,0 +1,37 @@
|
||||
package io.floci.aws.core.common;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertNotEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
class XRayTraceIdsTest {
|
||||
|
||||
@Test
|
||||
void traceIdHasTheRootShapeAndEmbedsTheEpochSeconds() {
|
||||
String id = XRayTraceIds.traceId(0x6af70000L);
|
||||
|
||||
assertTrue(id.matches("1-6af70000-[0-9a-f]{24}"), id);
|
||||
}
|
||||
|
||||
@Test
|
||||
void traceIdPadsSmallEpochSecondsToEightDigits() {
|
||||
assertTrue(XRayTraceIds.traceId(255).startsWith("1-000000ff-"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void traceIdsDiffer() {
|
||||
assertNotEquals(XRayTraceIds.traceId(1), XRayTraceIds.traceId(1));
|
||||
}
|
||||
|
||||
@Test
|
||||
void segmentIdIsSixteenLowercaseHexDigitsAndNeverAllZeros() {
|
||||
for (int i = 0; i < 1000; i++) {
|
||||
String id = XRayTraceIds.segmentId();
|
||||
assertEquals(16, id.length());
|
||||
assertTrue(id.matches("[0-9a-f]{16}"), id);
|
||||
assertNotEquals("0000000000000000", id);
|
||||
}
|
||||
}
|
||||
}
|
||||
+4
-4
@@ -31,7 +31,7 @@ class LambdaInvokeClientContextIntegrationTest {
|
||||
}
|
||||
|
||||
private void stubInvoke() {
|
||||
when(lambdaService.invoke(any(), any(), any(), any(), any(), any(), any()))
|
||||
when(lambdaService.invoke(any(), any(), any(), any(), any(), any(), any(), any()))
|
||||
.thenReturn(new InvokeResult(200, null, "{}".getBytes(), null, "req"));
|
||||
}
|
||||
|
||||
@@ -47,7 +47,7 @@ class LambdaInvokeClientContextIntegrationTest {
|
||||
.then().statusCode(200);
|
||||
|
||||
verify(lambdaService).invoke(any(), eq("ctx-fn"), any(), any(),
|
||||
eq(InvocationType.RequestResponse), eq(CONTEXT_JSON), isNull());
|
||||
eq(InvocationType.RequestResponse), eq(CONTEXT_JSON), isNull(), isNull());
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -63,7 +63,7 @@ class LambdaInvokeClientContextIntegrationTest {
|
||||
.then().statusCode(200);
|
||||
|
||||
verify(lambdaService).invoke(any(), eq("ctx-fn-event"), any(), any(),
|
||||
eq(InvocationType.Event), isNull(), isNull());
|
||||
eq(InvocationType.Event), isNull(), isNull(), isNull());
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -80,6 +80,6 @@ class LambdaInvokeClientContextIntegrationTest {
|
||||
.then().statusCode(200);
|
||||
|
||||
verify(lambdaService).invoke(any(), eq("ctx-fn-utf"), any(), any(),
|
||||
eq(InvocationType.RequestResponse), eq("{\"custom\":{\"name\":\"caf\\u00E9\"}}"), isNull());
|
||||
eq(InvocationType.RequestResponse), eq("{\"custom\":{\"name\":\"caf\\u00E9\"}}"), isNull(), isNull());
|
||||
}
|
||||
}
|
||||
+1
-1
@@ -82,7 +82,7 @@ class LambdaSynchronousInvokeDispatchIntegrationTest {
|
||||
// Each answer only records what it saw and waits for its own release; the checks run on the
|
||||
// test thread, so a mismatch fails there with its own message instead of a 500 to the client.
|
||||
doAnswer(call -> hold(call.getArgument(1), call.getArgument(4), held, invoking, releases))
|
||||
.when(lambdaService).invoke(anyString(), anyString(), any(), any(), any(), any(), any());
|
||||
.when(lambdaService).invoke(anyString(), anyString(), any(), any(), any(), any(), any(), any());
|
||||
doAnswer(call -> hold(call.getArgument(0), call.getArgument(2), held, invoking, releases))
|
||||
.when(lambdaService).invokeArn(anyString(), any(), any());
|
||||
|
||||
|
||||
+10
-9
@@ -63,7 +63,7 @@ class DurableExecutionServicePersistenceTest {
|
||||
return "{\"Status\":\"PENDING\"}";
|
||||
});
|
||||
String executionArn = first.start(new StartRequest(ACCOUNT, "us-east-1", "durable-fn", "1", "exec-1", "{\"a\":1}",
|
||||
false)).getExecutionArn();
|
||||
false, null)).getExecutionArn();
|
||||
String oldToken = token(before.events.get(0));
|
||||
firstStorage.flushAll();
|
||||
|
||||
@@ -105,7 +105,7 @@ class DurableExecutionServicePersistenceTest {
|
||||
return "{\"Status\":\"PENDING\"}";
|
||||
});
|
||||
String executionArn = first.start(new StartRequest(ACCOUNT, "us-east-1", "durable-fn", "1", "exec-1", "{}",
|
||||
false)).getExecutionArn();
|
||||
false, null)).getExecutionArn();
|
||||
String callbackId = first.get(executionArn).getOperations().get("approve").getCallbackId();
|
||||
firstStorage.flushAll();
|
||||
|
||||
@@ -149,7 +149,7 @@ class DurableExecutionServicePersistenceTest {
|
||||
return "{\"Status\":\"PENDING\"}";
|
||||
});
|
||||
String executionArn = first.start(new StartRequest(ACCOUNT, "us-east-1", "durable-fn", "1", "exec-1", "{}",
|
||||
false)).getExecutionArn();
|
||||
false, null)).getExecutionArn();
|
||||
assertEquals("STARTED", first.get(executionArn).getOperations().get("i1").getStatus().name());
|
||||
firstStorage.flushAll();
|
||||
|
||||
@@ -184,7 +184,7 @@ class DurableExecutionServicePersistenceTest {
|
||||
return "{\"Status\":\"PENDING\"}";
|
||||
});
|
||||
String executionArn = first.start(new StartRequest(ACCOUNT, "us-east-1", "durable-fn", "1", "exec-1", "{}",
|
||||
false)).getExecutionArn();
|
||||
false, null)).getExecutionArn();
|
||||
String childArn = first.get(executionArn).getOperations().get("i1").getChildExecutionArn();
|
||||
firstStorage.flushAll();
|
||||
|
||||
@@ -226,7 +226,7 @@ class DurableExecutionServicePersistenceTest {
|
||||
return "{\"Status\":\"PENDING\"}";
|
||||
});
|
||||
String parentArn = first.start(new StartRequest(ACCOUNT, "us-east-1", "durable-fn", "1", "exec-1", "{}",
|
||||
false)).getExecutionArn();
|
||||
false, null)).getExecutionArn();
|
||||
String childArn = first.get(parentArn).getOperations().get("i1").getChildExecutionArn();
|
||||
executions(firstStorage).deleteForAccount(ACCOUNT,
|
||||
"us-east-1/" + DurableExecutionService.parseArn(childArn).executionId());
|
||||
@@ -267,7 +267,7 @@ class DurableExecutionServicePersistenceTest {
|
||||
null)));
|
||||
return "{\"Status\":\"SUCCEEDED\",\"Result\":\"\"}";
|
||||
});
|
||||
first.start(new StartRequest(ACCOUNT, "us-east-1", "durable-fn", "1", "exec-1", "{}", false));
|
||||
first.start(new StartRequest(ACCOUNT, "us-east-1", "durable-fn", "1", "exec-1", "{}", false, null));
|
||||
firstStorage.flushAll();
|
||||
|
||||
ScriptedInvoker second = new ScriptedInvoker();
|
||||
@@ -361,7 +361,7 @@ class DurableExecutionServicePersistenceTest {
|
||||
public ResolvedDurableTarget resolve(String accountId, String region, String functionName, String qualifier) {
|
||||
if ("plain-fn".equals(functionName)) {
|
||||
return new ResolvedDurableTarget(accountId, region, functionName,
|
||||
"arn:aws:lambda:us-east-1:000000000000:function:plain-fn", "$LATEST", false, 0, 0);
|
||||
"arn:aws:lambda:us-east-1:000000000000:function:plain-fn", "$LATEST", false, 0, 0, false);
|
||||
}
|
||||
String version = "live".equals(qualifier) ? liveVersion : "1";
|
||||
if (qualifier != null && !"live".equals(qualifier)) {
|
||||
@@ -369,11 +369,12 @@ class DurableExecutionServicePersistenceTest {
|
||||
}
|
||||
return new ResolvedDurableTarget(accountId, region, functionName,
|
||||
"arn:aws:lambda:us-east-1:000000000000:function:" + functionName + ":" + version, version, true,
|
||||
3600, 7);
|
||||
3600, 7, false);
|
||||
}
|
||||
|
||||
@Override
|
||||
public DurableInvocationResult invoke(ResolvedDurableTarget target, byte[] payload, String requestId) {
|
||||
public DurableInvocationResult invoke(ResolvedDurableTarget target, byte[] payload, String requestId,
|
||||
String traceId) {
|
||||
if (!target.durable()) {
|
||||
plainPayloads.add(new String(payload, StandardCharsets.UTF_8));
|
||||
return new DurableInvocationResult("req", plainOutput.getBytes(StandardCharsets.UTF_8), null);
|
||||
|
||||
+62
-6
@@ -34,9 +34,12 @@ import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.function.Function;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
import static org.junit.jupiter.api.Assertions.assertNotEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertNotNull;
|
||||
import static org.junit.jupiter.api.Assertions.assertNull;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
@@ -139,6 +142,51 @@ class DurableExecutionServiceTest {
|
||||
assertTrue(eventTypes(service.get(arn)).containsAll(List.of("WaitStarted", "WaitSucceeded")));
|
||||
}
|
||||
|
||||
@Test
|
||||
void aReceivedTraceHeaderIsStoredAndPassedUnchangedToAPassThroughInvocation() {
|
||||
String header = "Root=1-6af70000-0000000000000000000000aa;Parent=00000000000000a1;Sampled=1";
|
||||
invoker.script(event -> succeeded("\"done\""));
|
||||
|
||||
DurableExecution execution = startWithTraceHeader("traced", header);
|
||||
|
||||
assertEquals(header, service.get(execution.getExecutionArn()).getTraceHeader());
|
||||
assertEquals(List.of(header), invoker.traceIds);
|
||||
}
|
||||
|
||||
@Test
|
||||
void anActiveFunctionKeepsRootAndSampledAndGetsANewParentOnEachInvocation() {
|
||||
invoker.activeTracing = true;
|
||||
invoker.script(event -> {
|
||||
checkpoint(event, token(event), List.of(waitStart("w1", 5)));
|
||||
return pending();
|
||||
});
|
||||
invoker.script(event -> succeeded("\"done\""));
|
||||
String header = "Root=1-6af70000-0000000000000000000000aa;Parent=00000000000000a1;Sampled=1";
|
||||
|
||||
startWithTraceHeader("active", header);
|
||||
clock.advance(Duration.ofSeconds(5));
|
||||
service.sweep();
|
||||
|
||||
assertEquals(2, invoker.traceIds.size());
|
||||
Pattern shape = Pattern.compile("Root=1-6af70000-0000000000000000000000aa;Parent=([0-9a-f]{16});Sampled=1");
|
||||
Matcher first = shape.matcher(invoker.traceIds.get(0));
|
||||
Matcher second = shape.matcher(invoker.traceIds.get(1));
|
||||
assertTrue(first.matches());
|
||||
assertTrue(second.matches());
|
||||
assertNotEquals(first.group(1), second.group(1));
|
||||
assertNotEquals("00000000000000a1", first.group(1));
|
||||
}
|
||||
|
||||
@Test
|
||||
void aStartWithoutATraceHeaderStoresANewOneForAPassThroughFunction() {
|
||||
invoker.script(event -> succeeded("\"done\""));
|
||||
|
||||
DurableExecution execution = start("untraced", "{}", true);
|
||||
|
||||
assertTrue(service.get(execution.getExecutionArn()).getTraceHeader()
|
||||
.matches("Root=1-[0-9a-f]{8}-[0-9a-f]{24};Parent=[0-9a-f]{16};Sampled=0"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void stepRetryBecomesReadyAfterItsDelay() {
|
||||
invoker.script(event -> {
|
||||
@@ -281,7 +329,7 @@ class DurableExecutionServiceTest {
|
||||
assertEquals(409, conflict.getHttpStatus());
|
||||
|
||||
AwsException otherVersion = assertThrows(AwsException.class, () -> service.start(
|
||||
new StartRequest(ACCOUNT, REGION, FUNCTION, "2", "same", "{\"a\":1}", true)));
|
||||
new StartRequest(ACCOUNT, REGION, FUNCTION, "2", "same", "{\"a\":1}", true, null)));
|
||||
assertEquals("Execution already started: " + first.getExecutionArn(), otherVersion.getMessage());
|
||||
}
|
||||
|
||||
@@ -489,7 +537,7 @@ class DurableExecutionServiceTest {
|
||||
|
||||
invoker.functionName = "other-fn";
|
||||
DurableExecution second = service.start(new StartRequest(ACCOUNT, REGION, "other-fn", "1", "shared", "{\"x\":1}",
|
||||
true));
|
||||
true, null));
|
||||
|
||||
assertFalse(first.getExecutionArn().equals(second.getExecutionArn()));
|
||||
assertEquals("\"b\"", service.get(second.getExecutionArn()).getResult());
|
||||
@@ -893,7 +941,11 @@ class DurableExecutionServiceTest {
|
||||
}
|
||||
|
||||
private DurableExecution start(String name, String payload, boolean synchronous) {
|
||||
return service.start(new StartRequest(ACCOUNT, REGION, FUNCTION, "1", name, payload, synchronous));
|
||||
return service.start(new StartRequest(ACCOUNT, REGION, FUNCTION, "1", name, payload, synchronous, null));
|
||||
}
|
||||
|
||||
private DurableExecution startWithTraceHeader(String name, String traceHeader) {
|
||||
return service.start(new StartRequest(ACCOUNT, REGION, FUNCTION, "1", name, "{}", false, traceHeader));
|
||||
}
|
||||
|
||||
private CheckpointResult checkpoint(JsonNode event, String token, List<DurableOperationUpdate> updates) {
|
||||
@@ -979,6 +1031,8 @@ class DurableExecutionServiceTest {
|
||||
int childExecutionTimeoutSeconds = 3600;
|
||||
String functionName = FUNCTION;
|
||||
int retentionPeriodInDays = 7;
|
||||
boolean activeTracing;
|
||||
final List<String> traceIds = new ArrayList<>();
|
||||
|
||||
void script(Function<JsonNode, DurableInvocationResult> script) {
|
||||
scripts.add(script);
|
||||
@@ -993,7 +1047,7 @@ class DurableExecutionServiceTest {
|
||||
String version = qualifier == null ? "$LATEST" : qualifier;
|
||||
if (plainFunctions.containsKey(functionName)) {
|
||||
return new ResolvedDurableTarget(accountId, region, functionName,
|
||||
"arn:aws:lambda:us-east-1:000000000000:function:" + functionName, version, false, 0, 0);
|
||||
"arn:aws:lambda:us-east-1:000000000000:function:" + functionName, version, false, 0, 0, false);
|
||||
}
|
||||
String name = FUNCTION.equals(functionName) ? this.functionName : functionName;
|
||||
if (!name.equals(this.functionName) && !childScripts.containsKey(name)) {
|
||||
@@ -1003,11 +1057,12 @@ class DurableExecutionServiceTest {
|
||||
return new ResolvedDurableTarget(accountId, region, name,
|
||||
"arn:aws:lambda:us-east-1:000000000000:function:" + name + ":" + durableVersion, durableVersion,
|
||||
true, name.equals(this.functionName) ? executionTimeoutSeconds : childExecutionTimeoutSeconds,
|
||||
retentionPeriodInDays);
|
||||
retentionPeriodInDays, activeTracing);
|
||||
}
|
||||
|
||||
@Override
|
||||
public DurableInvocationResult invoke(ResolvedDurableTarget target, byte[] payload, String requestId) {
|
||||
public DurableInvocationResult invoke(ResolvedDurableTarget target, byte[] payload, String requestId,
|
||||
String traceId) {
|
||||
JsonNode event;
|
||||
try {
|
||||
event = MAPPER.readTree(payload);
|
||||
@@ -1024,6 +1079,7 @@ class DurableExecutionServiceTest {
|
||||
}
|
||||
events.add(event);
|
||||
requestIds.add(requestId);
|
||||
traceIds.add(traceId);
|
||||
Function<JsonNode, DurableInvocationResult> script = scripts.poll();
|
||||
if (script == null) {
|
||||
return handlerResponse("{\"Status\":\"FAILED\",\"Error\":{\"ErrorMessage\":\"no script for "
|
||||
|
||||
@@ -0,0 +1,156 @@
|
||||
package io.floci.aws.services.lambda.durable;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertNotEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertNull;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
class DurableTraceHeaderTest {
|
||||
|
||||
private static final String ROOT = "1-6af70000-0000000000000000000000aa";
|
||||
private static final String PARENT = "00000000000000a1";
|
||||
private static final long NOW = 0x6af80000L;
|
||||
private static final String NEW_ROOT = "1-6af80000-[0-9a-f]{24}";
|
||||
private static final String NEW_ID = "[0-9a-f]{16}";
|
||||
|
||||
private static String execution(String received, boolean active) {
|
||||
return DurableTraceHeader.forExecution(received, active, NOW);
|
||||
}
|
||||
|
||||
@Test
|
||||
void noHeaderOnPassThroughGetsNewRootAndParentUnsampled() {
|
||||
assertTrue(execution(null, false).matches("Root=" + NEW_ROOT + ";Parent=" + NEW_ID + ";Sampled=0"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void noHeaderOnActiveGetsNewRootAndParentSampled() {
|
||||
assertTrue(execution(null, true).matches("Root=" + NEW_ROOT + ";Parent=" + NEW_ID + ";Sampled=1"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void fullSampledHeaderOnPassThroughIsKept() {
|
||||
String header = "Root=" + ROOT + ";Parent=" + PARENT + ";Sampled=1";
|
||||
assertEquals(header, execution(header, false));
|
||||
}
|
||||
|
||||
@Test
|
||||
void fullUnsampledHeaderOnActiveIsKept() {
|
||||
String header = "Root=" + ROOT + ";Parent=" + PARENT + ";Sampled=0";
|
||||
assertEquals(header, execution(header, true));
|
||||
}
|
||||
|
||||
@Test
|
||||
void fullSampledHeaderOnActiveGetsANewParent() {
|
||||
String stored = execution("Root=" + ROOT + ";Parent=" + PARENT + ";Sampled=1", true);
|
||||
|
||||
assertTrue(stored.matches("Root=" + ROOT + ";Parent=" + NEW_ID + ";Sampled=1"));
|
||||
assertNotEquals("Root=" + ROOT + ";Parent=" + PARENT + ";Sampled=1", stored);
|
||||
}
|
||||
|
||||
@Test
|
||||
void headerWithoutSampledOnActiveGetsANewParentAndSampled() {
|
||||
String stored = execution("Root=" + ROOT + ";Parent=" + PARENT, true);
|
||||
|
||||
assertTrue(stored.matches("Root=" + ROOT + ";Parent=" + NEW_ID + ";Sampled=1"));
|
||||
assertNotEquals("Root=" + ROOT + ";Parent=" + PARENT + ";Sampled=1", stored);
|
||||
}
|
||||
|
||||
@Test
|
||||
void rootOnlyOnActiveGetsANewParentAndSampled() {
|
||||
assertTrue(execution("Root=" + ROOT, true).matches("Root=" + ROOT + ";Parent=" + NEW_ID + ";Sampled=1"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void rootOnlyOnPassThroughGetsANewParentAndSampledZero() {
|
||||
assertTrue(execution("Root=" + ROOT, false).matches("Root=" + ROOT + ";Parent=" + NEW_ID + ";Sampled=0"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void parentWithoutSampledOnPassThroughKeepsTheParentAndIsUnsampled() {
|
||||
assertEquals("Root=" + ROOT + ";Parent=" + PARENT + ";Sampled=0",
|
||||
execution("Root=" + ROOT + ";Parent=" + PARENT, false));
|
||||
}
|
||||
|
||||
@Test
|
||||
void unsampledHeaderWithoutParentOnActiveStaysWithoutParent() {
|
||||
assertEquals("Root=" + ROOT + ";Sampled=0", execution("Root=" + ROOT + ";Sampled=0", true));
|
||||
}
|
||||
|
||||
@Test
|
||||
void sampledHeaderWithoutParentOnPassThroughStaysWithoutParent() {
|
||||
assertEquals("Root=" + ROOT + ";Sampled=1", execution("Root=" + ROOT + ";Sampled=1", false));
|
||||
}
|
||||
|
||||
@Test
|
||||
void invalidSampledCountsAsAbsent() {
|
||||
assertEquals("Root=" + ROOT + ";Parent=" + PARENT + ";Sampled=0",
|
||||
execution("Root=" + ROOT + ";Parent=" + PARENT + ";Sampled=?", false));
|
||||
}
|
||||
|
||||
@Test
|
||||
void parentAndSampledWithoutRootGetANewRoot() {
|
||||
assertTrue(execution("Parent=" + PARENT + ";Sampled=1", false)
|
||||
.matches("Root=" + NEW_ROOT + ";Parent=" + PARENT + ";Sampled=1"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void garbageOnPassThroughIsReplacedByANewHeader() {
|
||||
assertTrue(execution("garbage", false).matches("Root=" + NEW_ROOT + ";Parent=" + NEW_ID + ";Sampled=0"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void garbageOnActiveIsReplacedByANewHeader() {
|
||||
assertTrue(execution("garbage", true).matches("Root=" + NEW_ROOT + ";Parent=" + NEW_ID + ";Sampled=1"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void anInvalidRootIsReplacedAndOnlyTheFirstOfAKeyCounts() {
|
||||
String stored = execution("Root=bad;Parent=" + PARENT + ";Parent=" + "1".repeat(16) + ";Sampled=1", false);
|
||||
|
||||
assertTrue(stored.matches("Root=" + NEW_ROOT + ";Parent=" + PARENT + ";Sampled=1"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void fieldsAreTrimmedAndOtherKeysAreIgnored() {
|
||||
assertEquals("Root=" + ROOT + ";Parent=" + PARENT + ";Sampled=1",
|
||||
execution(" Self=1-x ; Root=" + ROOT + " ; Parent=" + PARENT + " ;Sampled=1", false));
|
||||
}
|
||||
|
||||
@Test
|
||||
void activeInvocationsShareRootAndSampledAndDifferInParent() {
|
||||
String stored = "Root=" + ROOT + ";Parent=" + PARENT + ";Sampled=1";
|
||||
|
||||
String first = DurableTraceHeader.forInvocation(stored, true);
|
||||
String second = DurableTraceHeader.forInvocation(stored, true);
|
||||
|
||||
assertTrue(first.matches("Root=" + ROOT + ";Parent=" + NEW_ID + ";Sampled=1"));
|
||||
assertTrue(second.matches("Root=" + ROOT + ";Parent=" + NEW_ID + ";Sampled=1"));
|
||||
assertNotEquals(first, second);
|
||||
}
|
||||
|
||||
@Test
|
||||
void activeInvocationKeepsAnUnsampledHeaderWithAParent() {
|
||||
String header = DurableTraceHeader.forInvocation("Root=" + ROOT + ";Sampled=0", true);
|
||||
|
||||
assertTrue(header.matches("Root=" + ROOT + ";Parent=" + NEW_ID + ";Sampled=0"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void passThroughInvocationReturnsTheStoredHeader() {
|
||||
String stored = "Root=" + ROOT + ";Parent=" + PARENT + ";Sampled=1";
|
||||
assertEquals(stored, DurableTraceHeader.forInvocation(stored, false));
|
||||
}
|
||||
|
||||
@Test
|
||||
void passThroughInvocationWithoutAParentGetsTheRootOnly() {
|
||||
assertEquals("Root=" + ROOT, DurableTraceHeader.forInvocation("Root=" + ROOT + ";Sampled=0", false));
|
||||
}
|
||||
|
||||
@Test
|
||||
void invocationWithoutAStoredHeaderGetsNone() {
|
||||
assertNull(DurableTraceHeader.forInvocation(null, true));
|
||||
assertNull(DurableTraceHeader.forInvocation(null, false));
|
||||
}
|
||||
}
|
||||
+25
@@ -65,6 +65,31 @@ class LambdaDurableProtocolIntegrationTest {
|
||||
invoker.drain();
|
||||
}
|
||||
|
||||
@Test
|
||||
void anInvokeTraceHeaderReachesTheInvocationAndTheExecution() throws Exception {
|
||||
String traceHeader = "Root=1-6af70000-0000000000000000000000aa;Parent=00000000000000a1;Sampled=1";
|
||||
CompletableFuture<Response> invoke = CompletableFuture.supplyAsync(() -> given()
|
||||
.header("X-Amz-Durable-Execution-Name", "traced-1")
|
||||
.header("X-Amzn-Trace-Id", traceHeader)
|
||||
.body("{}")
|
||||
.post(LAMBDA + "/functions/" + FUNCTION + ":$LATEST/invocations"));
|
||||
|
||||
Invocation invocation = invoker.awaitInvocation(Duration.ofSeconds(30));
|
||||
String arn = MAPPER.readTree(invocation.payload()).get("DurableExecutionArn").asText();
|
||||
assertEquals(traceHeader, invocation.traceId());
|
||||
|
||||
given()
|
||||
.urlEncodingEnabled(false)
|
||||
.when()
|
||||
.get(DURABLE + "/durable-executions/" + arn)
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.body("TraceHeader.XAmznTraceId", equalTo(traceHeader));
|
||||
|
||||
invocation.response().complete(handlerResponse("{\"Status\": \"SUCCEEDED\", \"Result\": \"{}\"}"));
|
||||
assertEquals(200, invoke.get(30, TimeUnit.SECONDS).statusCode());
|
||||
}
|
||||
|
||||
@Test
|
||||
void aSynchronousInvokeReturnsTheResultOfACheckpointedExecution() throws Exception {
|
||||
CompletableFuture<Response> invoke = CompletableFuture.supplyAsync(() -> given()
|
||||
|
||||
+4
-3
@@ -21,7 +21,7 @@ import java.util.concurrent.TimeoutException;
|
||||
@ApplicationScoped
|
||||
public class ScriptedDurableFunctionInvoker implements DurableFunctionInvoker {
|
||||
|
||||
public record Invocation(ResolvedDurableTarget target, byte[] payload,
|
||||
public record Invocation(ResolvedDurableTarget target, byte[] payload, String traceId,
|
||||
CompletableFuture<DurableInvocationResult> response) {
|
||||
}
|
||||
|
||||
@@ -39,8 +39,9 @@ public class ScriptedDurableFunctionInvoker implements DurableFunctionInvoker {
|
||||
}
|
||||
|
||||
@Override
|
||||
public DurableInvocationResult invoke(ResolvedDurableTarget target, byte[] payload, String requestId) {
|
||||
Invocation invocation = new Invocation(target, payload, new CompletableFuture<>());
|
||||
public DurableInvocationResult invoke(ResolvedDurableTarget target, byte[] payload, String requestId,
|
||||
String traceId) {
|
||||
Invocation invocation = new Invocation(target, payload, traceId, new CompletableFuture<>());
|
||||
invocations.add(invocation);
|
||||
try {
|
||||
return invocation.response().get(30, TimeUnit.SECONDS);
|
||||
|
||||
@@ -161,6 +161,31 @@ class RuntimeApiServerTest {
|
||||
assertTrue(second.headers().firstValue("Lambda-Runtime-Client-Context").isEmpty());
|
||||
}
|
||||
|
||||
@Test
|
||||
@Timeout(10)
|
||||
void nextEndpoint_emitsTraceIdHeaderOnlyWhenPresent() throws Exception {
|
||||
String traceId = "Root=1-6af70000-0000000000000000000000aa;Parent=00000000000000a1;Sampled=1";
|
||||
PendingInvocation withTrace = new PendingInvocation(
|
||||
"req-trace", "{}".getBytes(), System.currentTimeMillis() + 60_000,
|
||||
"arn:aws:lambda:us-east-1:000000000000:function:test",
|
||||
new CompletableFuture<>());
|
||||
withTrace.setTraceId(traceId);
|
||||
server.enqueue(withTrace);
|
||||
|
||||
HttpRequest next = HttpRequest.newBuilder()
|
||||
.uri(URI.create("http://localhost:" + port + "/2018-06-01/runtime/invocation/next"))
|
||||
.GET().build();
|
||||
HttpResponse<String> first = httpClient.send(next, HttpResponse.BodyHandlers.ofString());
|
||||
assertEquals(traceId, first.headers().firstValue("Lambda-Runtime-Trace-Id").orElse(null));
|
||||
|
||||
server.enqueue(new PendingInvocation(
|
||||
"req-notrace", "{}".getBytes(), System.currentTimeMillis() + 60_000,
|
||||
"arn:aws:lambda:us-east-1:000000000000:function:test",
|
||||
new CompletableFuture<>()));
|
||||
HttpResponse<String> second = httpClient.send(next, HttpResponse.BodyHandlers.ofString());
|
||||
assertTrue(second.headers().firstValue("Lambda-Runtime-Trace-Id").isEmpty());
|
||||
}
|
||||
|
||||
@Test
|
||||
@Timeout(10)
|
||||
void nextEndpoint_startsDeadlineWhenInvocationIsDispatched() throws Exception {
|
||||
|
||||
Reference in new issue
Block a user