Skip to content

Commit 15b63e5

Browse files
authored
Add invocation id to module private data and add a Get-CurrentActivityForInvocation cmdlet (#25)
1 parent 54f595d commit 15b63e5

File tree

5 files changed

+68
-1
lines changed

5 files changed

+68
-1
lines changed

src/AzureFunctions.PowerShell.OpenTelemetry.SDK.psd1

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,8 @@
7070
'Stop-OpenTelemetryInvocationInternal',
7171
'New-FunctionsOpenTelemetryTracerBuilder',
7272
'Start-FunctionsOpenTelemetrySpan',
73-
'Stop-FunctionsOpenTelemetrySpan'
73+
'Stop-FunctionsOpenTelemetrySpan',
74+
'Get-CurrentActivityForInvocation'
7475
)
7576

7677
# Variables to export from this module
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
//
2+
// Copyright (c) Microsoft. All rights reserved.
3+
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
4+
//
5+
6+
using System.Management.Automation;
7+
using OpenTelemetryEngine.Traces;
8+
using OpenTelemetryEngine.ResponseObjects;
9+
10+
namespace AzureFunctions.PowerShell.OpenTelemetry.SDK
11+
{
12+
/// <summary>
13+
/// This cmdlet is used to start an OpenTelemetry invocation.
14+
/// Currently, that just means starting a (hidden) span which is a copy of the host span.
15+
/// This allows all new spans created by the user or by dependent modules to link back to the host span using TraceParent.
16+
/// </summary>
17+
[Cmdlet(VerbsCommon.Get, "CurrentActivityForInvocation")]
18+
[OutputType(typeof(GetActivityResponse))]
19+
public class GetCurrentActivityForInvocation : PSCmdlet
20+
{
21+
/// <summary>
22+
/// InvocationId for the current invocation
23+
/// </summary>
24+
[Parameter(Mandatory = true)]
25+
public string InvocationId { get; set; } = string.Empty;
26+
27+
// This method will be called for each input received from the pipeline to this cmdlet; if no input is received, this method is not called
28+
protected override void ProcessRecord()
29+
{
30+
WriteObject(FunctionsActivityBuilder.GetActivityForInvocation(InvocationId));
31+
}
32+
}
33+
}

src/AzureFunctions.PowerShell.OpenTelemetry.SDK/Traces/StartOpenTelemetryInvocationInternal.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
using System.Management.Automation;
77
using OpenTelemetryEngine.Traces;
88
using OpenTelemetryEngine.ResponseObjects;
9+
using System.Management.Automation.Runspaces;
910

1011
namespace AzureFunctions.PowerShell.OpenTelemetry.SDK
1112
{
@@ -42,6 +43,15 @@ public class StartOpenTelemetryInvocationInternal : PSCmdlet
4243
protected override void ProcessRecord()
4344
{
4445
var newActivity = FunctionsActivityBuilder.StartInternalActivity(InvocationId, TraceParent, TraceState);
46+
47+
try
48+
{
49+
string script = "param($id) Set-FunctionInvocationContext -InvocationId $id";
50+
_ = this.InvokeCommand.InvokeScript(script, false, PipelineResultTypes.Output, null, new object[] { InvocationId });
51+
}
52+
catch (ParameterBindingException) { }
53+
catch (CommandNotFoundException) { }
54+
4555
WriteObject(newActivity);
4656
}
4757
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
using System.Diagnostics;
2+
3+
namespace OpenTelemetryEngine.ResponseObjects
4+
{
5+
public class GetActivityResponse
6+
{
7+
public Activity? activity;
8+
9+
public GetActivityResponse(Activity? activity)
10+
{
11+
this.activity = activity;
12+
}
13+
}
14+
}

src/OpenTelemetryEngine/Traces/FunctionsActivityBuilder.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,15 @@ public static void StopActivity(FunctionsActivityResponse? response)
9494
{
9595
response?.activity?.Stop();
9696
}
97+
98+
public static GetActivityResponse GetActivityForInvocation(string invocationId)
99+
{
100+
if (internalActivitiesByInvocationId.ContainsKey(invocationId))
101+
{
102+
return new GetActivityResponse(internalActivitiesByInvocationId[invocationId]);
103+
}
104+
return new GetActivityResponse(Activity.Current);
105+
}
97106
}
98107
}
99108

0 commit comments

Comments
 (0)