mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
feat(lambda): pass the X-Ray trace header through durable chained invokes (#8189)
* feat(lambda): pass the X-Ray trace header through durable chained invokes * fix(lambda): give a PassThrough invocation a new Parent for an unsampled header without one
This commit is contained in:
1 parent
7d7c87338f
commit
ede4ebf6b0
12 files changed
+306
-38
No files matched your search
@@ -395,6 +395,7 @@ final class DurableCheckpointApplier {
|
||||
DurableOperation operation = create(update);
|
||||
operation.setChainedFunctionName(update.chainedFunctionName());
|
||||
operation.setChainedTenantId(update.chainedTenantId());
|
||||
operation.setChainedTraceHeader(update.chainedTraceHeader());
|
||||
operation.setInputPayload(update.payload());
|
||||
AwsException unreachable = unreachableTarget.apply(update.chainedFunctionName());
|
||||
if (unreachable == null) {
|
||||
|
||||
@@ -691,11 +691,12 @@ public class DurableExecutionService implements Resettable {
|
||||
// ──────────────────────────── state changes ────────────────────────────
|
||||
|
||||
private DurableExecution newExecution(StartRequest request, ResolvedDurableTarget target, String name, long now) {
|
||||
return newExecution(request, target, name, UUID.randomUUID().toString(), now);
|
||||
return newExecution(request, target, name, UUID.randomUUID().toString(),
|
||||
DurableTraceHeader.forExecution(request.traceHeader(), target.activeTracing(), now / 1000), now);
|
||||
}
|
||||
|
||||
private DurableExecution newExecution(StartRequest request, ResolvedDurableTarget target, String name,
|
||||
String executionId, long now) {
|
||||
String executionId, String traceHeader, long now) {
|
||||
DurableExecution execution = new DurableExecution();
|
||||
execution.setExecutionId(executionId);
|
||||
execution.setName(name);
|
||||
@@ -706,8 +707,7 @@ public class DurableExecutionService implements Resettable {
|
||||
execution.setVersion(target.version());
|
||||
execution.setExecutionArn(target.functionArn() + "/durable-execution/" + name + "/" + executionId);
|
||||
execution.setStartTimestamp(now);
|
||||
execution.setTraceHeader(DurableTraceHeader.forExecution(request.traceHeader(), target.activeTracing(),
|
||||
now / 1000));
|
||||
execution.setTraceHeader(traceHeader);
|
||||
execution.setInputPayload(request.inputPayload());
|
||||
execution.setExecutionTimeoutSeconds(target.executionTimeoutSeconds());
|
||||
execution.setRetentionPeriodInDays(target.retentionPeriodInDays());
|
||||
@@ -856,13 +856,20 @@ public class DurableExecutionService implements Resettable {
|
||||
List<Runnable> effects) {
|
||||
StartRequest request = new StartRequest(target.accountId(), target.region(), target.functionName(),
|
||||
target.version(), name, operation.getInputPayload(), false, null);
|
||||
DurableExecution child = newExecution(request, target, name, executionId, now);
|
||||
DurableExecution child = newExecution(request, target, name, executionId,
|
||||
chainedTraceHeader(parent, operation, target), now);
|
||||
child.setParentExecutionArn(parent.getExecutionArn());
|
||||
child.setParentOperationId(operation.getId());
|
||||
trigger(child, effects);
|
||||
return child;
|
||||
}
|
||||
|
||||
private static String chainedTraceHeader(DurableExecution parent, DurableOperation operation,
|
||||
ResolvedDurableTarget target) {
|
||||
return DurableTraceHeader.forChainedChild(operation.getChainedTraceHeader(), parent.getTraceHeader(),
|
||||
target.activeTracing());
|
||||
}
|
||||
|
||||
private Runnable plainChainedInvoke(DurableExecution parent, DurableOperation operation,
|
||||
ResolvedDurableTarget target) {
|
||||
String accountId = parent.getAccountId();
|
||||
@@ -871,19 +878,21 @@ public class DurableExecutionService implements Resettable {
|
||||
// A plain target gets {} when the update has no Payload. A durable child starts with no input.
|
||||
byte[] payload = (operation.getInputPayload() != null ? operation.getInputPayload() : "{}")
|
||||
.getBytes(StandardCharsets.UTF_8);
|
||||
String traceId = DurableTraceHeader.forInvocation(chainedTraceHeader(parent, operation, target),
|
||||
target.activeTracing());
|
||||
long launchGeneration = generation.get();
|
||||
return () -> launchExecutor.execute(() -> {
|
||||
ChainedOutcome outcome = invokePlainFunction(target, payload);
|
||||
ChainedOutcome outcome = invokePlainFunction(target, payload, traceId);
|
||||
if (launchGeneration == generation.get()) {
|
||||
finishChainedInvoke(accountId, key, operationId, null, outcome);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private ChainedOutcome invokePlainFunction(ResolvedDurableTarget target, byte[] payload) {
|
||||
private ChainedOutcome invokePlainFunction(ResolvedDurableTarget target, byte[] payload, String traceId) {
|
||||
DurableInvocationResult result;
|
||||
try {
|
||||
result = invoker.invoke(target, payload, UUID.randomUUID().toString(), null);
|
||||
result = invoker.invoke(target, payload, UUID.randomUUID().toString(), traceId);
|
||||
} catch (AwsException e) {
|
||||
return ChainedOutcome.failed(e);
|
||||
} catch (RuntimeException e) {
|
||||
|
||||
@@ -37,6 +37,22 @@ final class DurableTraceHeader {
|
||||
return format(root, parent, sampled);
|
||||
}
|
||||
|
||||
/**
|
||||
* Unlike Invoke, the Parent sent is kept as is, and an Active target gets none when none was sent.
|
||||
*/
|
||||
static String forChainedChild(String requested, String callerHeader, boolean activeTracing) {
|
||||
Fields fields = parse(requested);
|
||||
if (fields.root() == null) {
|
||||
return callerHeader;
|
||||
}
|
||||
String sampled = fields.sampled() != null ? fields.sampled() : activeTracing ? "1" : "0";
|
||||
String parent = fields.parent();
|
||||
if (parent == null && !activeTracing && fields.sampled() == null) {
|
||||
parent = XRayTraceIds.segmentId();
|
||||
}
|
||||
return format(fields.root(), parent, sampled);
|
||||
}
|
||||
|
||||
static String forInvocation(String executionHeader, boolean activeTracing) {
|
||||
if (executionHeader == null) {
|
||||
return null;
|
||||
@@ -48,6 +64,9 @@ final class DurableTraceHeader {
|
||||
if (fields.parent() != null) {
|
||||
return executionHeader;
|
||||
}
|
||||
if ("0".equals(fields.sampled())) {
|
||||
return format(fields.root(), XRayTraceIds.segmentId(), "0");
|
||||
}
|
||||
return "Root=" + fields.root();
|
||||
}
|
||||
|
||||
|
||||
@@ -287,7 +287,8 @@ public final class DurableWire {
|
||||
integer(callbackOptions, "HeartbeatTimeoutSeconds", path + "callbackOptions", 0,
|
||||
MAX_CALLBACK_TIMEOUT_SECONDS),
|
||||
chainedInvokeOptions == null ? null : string(chainedInvokeOptions, "FunctionName"),
|
||||
chainedInvokeOptions == null ? null : string(chainedInvokeOptions, "TenantId"));
|
||||
chainedInvokeOptions == null ? null : string(chainedInvokeOptions, "TenantId"),
|
||||
chainedInvokeOptions == null ? null : string(chainedInvokeOptions, "XAmznTraceId"));
|
||||
}
|
||||
|
||||
private static <E extends Enum<E>> E enumMember(Map<?, ?> map, String member, Class<E> type) {
|
||||
|
||||
@@ -37,6 +37,7 @@ public class DurableOperation {
|
||||
/** The FunctionName of a chained invoke, as the function sent it. */
|
||||
private String chainedFunctionName;
|
||||
private String chainedTenantId;
|
||||
private String chainedTraceHeader;
|
||||
/** Set when a chained invoke runs a durable function. Unset, the function runs as a plain invocation. */
|
||||
private String childExecutionArn;
|
||||
/** Position in the execution's change log. The handler is only shown operations newer than it has seen. */
|
||||
@@ -68,6 +69,7 @@ public class DurableOperation {
|
||||
copy.heartbeatDeadline = heartbeatDeadline;
|
||||
copy.chainedFunctionName = chainedFunctionName;
|
||||
copy.chainedTenantId = chainedTenantId;
|
||||
copy.chainedTraceHeader = chainedTraceHeader;
|
||||
copy.childExecutionArn = childExecutionArn;
|
||||
copy.changeSequence = changeSequence;
|
||||
return copy;
|
||||
@@ -138,6 +140,9 @@ public class DurableOperation {
|
||||
public String getChainedTenantId() { return chainedTenantId; }
|
||||
public void setChainedTenantId(String chainedTenantId) { this.chainedTenantId = chainedTenantId; }
|
||||
|
||||
public String getChainedTraceHeader() { return chainedTraceHeader; }
|
||||
public void setChainedTraceHeader(String chainedTraceHeader) { this.chainedTraceHeader = chainedTraceHeader; }
|
||||
|
||||
public String getChildExecutionArn() { return childExecutionArn; }
|
||||
public void setChildExecutionArn(String childExecutionArn) { this.childExecutionArn = childExecutionArn; }
|
||||
|
||||
|
||||
+2
-1
@@ -18,5 +18,6 @@ public record DurableOperationUpdate(String id,
|
||||
Integer callbackTimeoutSeconds,
|
||||
Integer callbackHeartbeatTimeoutSeconds,
|
||||
String chainedFunctionName,
|
||||
String chainedTenantId) {
|
||||
String chainedTenantId,
|
||||
String chainedTraceHeader) {
|
||||
}
|
||||
Reference in new issue
Block a user