Skip to content

Commit ca16f78

Browse files
authored
Add Invocation Id to Set-FunctionInvocationContext and propagate trace information in HTTP requests to correlate client and orchestration functions (#99)
1 parent 169ff78 commit ca16f78

File tree

2 files changed

+64
-1
lines changed

2 files changed

+64
-1
lines changed

src/AzureFunctions.PowerShell.Durable.SDK.psm1

Lines changed: 52 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,16 @@ function GetDurableClientFromModulePrivateData {
2222
}
2323
}
2424

25+
function GetInvocationIdFromModulePrivateData {
26+
$PrivateData = $PSCmdlet.MyInvocation.MyCommand.Module.PrivateData
27+
if ($null -eq $PrivateData -or $null -eq $PrivateData['InvocationId']) {
28+
return $null
29+
}
30+
else {
31+
return $PrivateData['InvocationId']
32+
}
33+
}
34+
2535
function Get-DurableStatus {
2636
[CmdletBinding()]
2737
param(
@@ -103,6 +113,9 @@ function Start-DurableOrchestration {
103113
$InstanceId = (New-Guid).Guid
104114
}
105115

116+
$invocationId = GetInvocationIdFromModulePrivateData
117+
$headers = Get-TraceHeaders -InvocationId $invocationId
118+
106119
$Uri =
107120
if ($DurableClient.rpcBaseUrl) {
108121
# Fast local RPC path
@@ -115,11 +128,49 @@ function Start-DurableOrchestration {
115128

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

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

120133
return $instanceId
121134
}
122135

136+
function Get-TraceHeaders {
137+
param(
138+
[string] $InvocationId
139+
)
140+
141+
if ($null -eq $InvocationId -or $InvocationId -eq "") {
142+
return @{} # Return an empty headers object
143+
}
144+
145+
# Check if Get-CurrentActivityForInvocation is available
146+
if (-not (Get-Command -Name Get-CurrentActivityForInvocation -ErrorAction SilentlyContinue)) {
147+
Write-Warning "Get-CurrentActivityForInvocation is not available. Skipping call."
148+
return @{} # Return an empty headers object
149+
}
150+
151+
$activityResponse = Get-CurrentActivityForInvocation -InvocationId $invocationId
152+
$activity = $activityResponse.activity
153+
154+
$traceId = $activity.TraceId
155+
$spanId = $activity.SpanId
156+
$traceFlags = $activity.TraceFlags
157+
$traceState = $activity.TraceStateString
158+
159+
$flag = "00"
160+
if ($null -ne $traceFlags -and $traceFlags -eq "Recorded") {
161+
$flag = "01"
162+
}
163+
164+
$traceparent = "00-$traceId-$spanId-$flag"
165+
166+
$headers = @{
167+
"traceparent" = $traceparent
168+
"tracestate" = $traceState
169+
}
170+
171+
return $headers
172+
}
173+
123174
function Stop-DurableOrchestration {
124175
[CmdletBinding()]
125176
param(

src/DurableSDK/Commands/Internals/SetFunctionInvocationContextCommand.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ public class SetFunctionInvocationContextCommand : PSCmdlet
2323
{
2424
private const string ContextKey = "OrchestrationContext";
2525
private const string DurableClientKey = "DurableClient";
26+
private const string InvocationIdKey = "InvocationId";
2627

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

40+
/// <summary>
41+
/// The function invocation ID.
42+
/// </summary>
43+
[Parameter(Mandatory = true, ParameterSetName = InvocationIdKey)]
44+
public string InvocationId { get; set; }
45+
3946
/// <summary>
4047
/// Whether or not to clear the privateData of this module.
4148
/// </summary>
@@ -70,11 +77,16 @@ protected override void EndProcessing()
7077
privateData[DurableClientKey] = DurableClient;
7178
break;
7279

80+
case InvocationIdKey:
81+
privateData[InvocationIdKey] = InvocationId;
82+
break;
83+
7384
default:
7485
if (Clear.IsPresent)
7586
{
7687
privateData.Remove(ContextKey);
7788
privateData.Remove(DurableClientKey);
89+
privateData.Remove(InvocationIdKey);
7890
}
7991
break;
8092
}

0 commit comments

Comments
 (0)