Skip to content

Commit aba76b7

Browse files
authored
Add Invocation Id to Set-FunctionInvocationContext and propagate trace information in HTTP requests to correlate client and orchestration functions (#1111)
1 parent bd1bfa6 commit aba76b7

File tree

2 files changed

+66
-3
lines changed

2 files changed

+66
-3
lines changed

src/DurableSDK/Commands/SetFunctionInvocationContextCommand.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ public class SetFunctionInvocationContextCommand : PSCmdlet
1818
{
1919
internal const string ContextKey = "OrchestrationContext";
2020
private const string DurableClientKey = "DurableClient";
21+
private const string InvocationIdKey = "InvocationId";
2122

2223
[Parameter(Mandatory = true, ParameterSetName = ContextKey)]
2324
public OrchestrationContext OrchestrationContext { get; set; }
@@ -28,6 +29,12 @@ public class SetFunctionInvocationContextCommand : PSCmdlet
2829
[Parameter(Mandatory = true, ParameterSetName = DurableClientKey)]
2930
public object DurableClient { get; set; }
3031

32+
/// <summary>
33+
/// The invocation id.
34+
/// </summary>
35+
[Parameter(Mandatory = true, ParameterSetName = InvocationIdKey)]
36+
public string InvocationId { get; set; }
37+
3138
[Parameter(Mandatory = true, ParameterSetName = "Clear")]
3239
public SwitchParameter Clear { get; set; }
3340

@@ -44,11 +51,16 @@ protected override void EndProcessing()
4451
privateData[DurableClientKey] = DurableClient;
4552
break;
4653

54+
case InvocationIdKey:
55+
privateData[InvocationIdKey] = InvocationId;
56+
break;
57+
4758
default:
4859
if (Clear.IsPresent)
4960
{
5061
privateData.Remove(ContextKey);
5162
privateData.Remove(DurableClientKey);
63+
privateData.Remove(InvocationIdKey);
5264
}
5365
break;
5466
}

src/Modules/Microsoft.Azure.Functions.PowerShellWorker/Microsoft.Azure.Functions.PowerShellWorker.psm1

Lines changed: 54 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,16 @@ function GetDurableClientFromModulePrivateData {
1919
}
2020
}
2121

22+
function GetInvocationIdFromModulePrivateData {
23+
$PrivateData = $PSCmdlet.MyInvocation.MyCommand.Module.PrivateData
24+
if ($null -eq $PrivateData -or $null -eq $PrivateData['InvocationId']) {
25+
return $null
26+
}
27+
else {
28+
return $PrivateData['InvocationId']
29+
}
30+
}
31+
2232
function Get-DurableStatus {
2333
[CmdletBinding()]
2434
param(
@@ -121,6 +131,9 @@ function Start-DurableOrchestration {
121131
$InstanceId = (New-Guid).Guid
122132
}
123133

134+
$invocationId = GetInvocationIdFromModulePrivateData
135+
$headers = Get-TraceHeaders -InvocationId $invocationId
136+
124137
$Uri =
125138
if ($DurableClient.rpcBaseUrl) {
126139
# Fast local RPC path
@@ -132,12 +145,50 @@ function Start-DurableOrchestration {
132145
}
133146

134147
$Body = $InputObject | ConvertTo-Json -Compress
135-
136-
$null = Invoke-RestMethod -Uri $Uri -Method 'POST' -ContentType 'application/json' -Body $Body
137-
148+
149+
$null = Invoke-RestMethod -Uri $Uri -Method 'POST' -ContentType 'application/json' -Body $Body -Headers $headers
150+
138151
return $instanceId
139152
}
140153

154+
function Get-TraceHeaders {
155+
param(
156+
[string] $InvocationId
157+
)
158+
159+
if ($null -eq $InvocationId -or $InvocationId -eq "") {
160+
return @{} # Return an empty headers object
161+
}
162+
163+
# Check if Get-CurrentActivityForInvocation is available
164+
if (-not (Get-Command -Name Get-CurrentActivityForInvocation -ErrorAction SilentlyContinue)) {
165+
Write-Warning "Get-CurrentActivityForInvocation is not available. Skipping call."
166+
return @{} # Return an empty headers object
167+
}
168+
169+
$activityResponse = Get-CurrentActivityForInvocation -InvocationId $invocationId
170+
$activity = $activityResponse.activity
171+
172+
$traceId = $activity.TraceId
173+
$spanId = $activity.SpanId
174+
$traceFlags = $activity.TraceFlags
175+
$traceState = $activity.TraceStateString
176+
177+
$flag = "00"
178+
if ($null -ne $traceFlags -and $traceFlags -eq "Recorded") {
179+
$flag = "01"
180+
}
181+
182+
$traceparent = "00-$traceId-$spanId-$flag"
183+
184+
$headers = @{
185+
"traceparent" = $traceparent
186+
"tracestate" = $traceState
187+
}
188+
189+
return $headers
190+
}
191+
141192
function Stop-DurableOrchestration {
142193
[CmdletBinding()]
143194
param(

0 commit comments

Comments
 (0)