Skip to content
48 changes: 47 additions & 1 deletion src/AzureFunctions.PowerShell.Durable.SDK.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,16 @@ function GetDurableClientFromModulePrivateData {
}
}

function GetInvocationIdFromModulePrivateData {
$PrivateData = $PSCmdlet.MyInvocation.MyCommand.Module.PrivateData
if ($null -eq $PrivateData -or $null -eq $PrivateData['InvocationId']) {
return $null
}
else {
$PrivateData['InvocationId']
}
}

function Get-DurableStatus {
[CmdletBinding()]
param(
Expand Down Expand Up @@ -103,6 +113,9 @@ function Start-DurableOrchestration {
$InstanceId = (New-Guid).Guid
}

$invocationId = GetInvocationIdFromModulePrivateData
$headers = Get-TraceHeaders -InvocationId $invocationId

$Uri =
if ($DurableClient.rpcBaseUrl) {
# Fast local RPC path
Expand All @@ -115,11 +128,44 @@ function Start-DurableOrchestration {

$Body = $InputObject | ConvertTo-Json -Compress -Depth 100

$null = Invoke-RestMethod -Uri $Uri -Method 'POST' -ContentType 'application/json' -Body $Body
$null = Invoke-RestMethod -Uri $Uri -Method 'POST' -ContentType 'application/json' -Body $Body -Headers $headers

return $instanceId
}

function Get-TraceHeaders {
param(
[Parameter(Mandatory = $true)]
[string] $InvocationId
)

if ($null -eq $InvocationId) {
return @{} # Return an empty headers object
}

$activityResponse = Get-CurrentActivityForInvocation -InvocationId $invocationId
$activity = $activityResponse.activity

$traceId = $activity.TraceId
$spanId = $activity.SpanId
$traceFlags = $activity.TraceFlags
$traceState = $activity.TraceStateString

$flag = "00"
if ($null -ne $traceFlags -and $traceFlags -eq "Recorded") {
$flag = "01"
}

$traceparent = "00-$traceId-$spanId-$flag"

$headers = @{
"traceparent" = $traceparent
"tracestate" = $traceState
}

return $headers
}

function Stop-DurableOrchestration {
[CmdletBinding()]
param(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ public class SetFunctionInvocationContextCommand : PSCmdlet
{
private const string ContextKey = "OrchestrationContext";
private const string DurableClientKey = "DurableClient";
private const string InvocationIdKey = "InvocationId";

/// <summary>
/// The orchestration context.
Expand All @@ -36,6 +37,12 @@ public class SetFunctionInvocationContextCommand : PSCmdlet
[Parameter(Mandatory = true, ParameterSetName = DurableClientKey)]
public object DurableClient { get; set; }

/// <summary>
/// The function invocation ID.
/// </summary>
[Parameter(Mandatory = true, ParameterSetName = InvocationIdKey)]
public string InvocationId { get; set; }

/// <summary>
/// Whether or not to clear the privateData of this module.
/// </summary>
Expand Down Expand Up @@ -70,11 +77,16 @@ protected override void EndProcessing()
privateData[DurableClientKey] = DurableClient;
break;

case InvocationIdKey:
privateData[InvocationIdKey] = InvocationId;
break;

default:
if (Clear.IsPresent)
{
privateData.Remove(ContextKey);
privateData.Remove(DurableClientKey);
privateData.Remove(InvocationIdKey);
}
break;
}
Expand Down
Loading