Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/AzureFunctions.PowerShell.OpenTelemetry.SDK.psd1
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,8 @@
'Stop-OpenTelemetryInvocationInternal',
'New-FunctionsOpenTelemetryTracerBuilder',
'Start-FunctionsOpenTelemetrySpan',
'Stop-FunctionsOpenTelemetrySpan'
'Stop-FunctionsOpenTelemetrySpan',
'Get-CurrentActivityForInvocation'
)

# Variables to export from this module
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
//
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
//

using System.Management.Automation;
using OpenTelemetryEngine.Traces;
using OpenTelemetryEngine.ResponseObjects;

namespace AzureFunctions.PowerShell.OpenTelemetry.SDK
{
/// <summary>
/// This cmdlet is used to start an OpenTelemetry invocation.
/// Currently, that just means starting a (hidden) span which is a copy of the host span.
/// This allows all new spans created by the user or by dependent modules to link back to the host span using TraceParent.
/// </summary>
[Cmdlet(VerbsCommon.Get, "CurrentActivityForInvocation")]
[OutputType(typeof(GetActivityResponse))]
public class GetCurrentActivityForInvocation : PSCmdlet
{
/// <summary>
/// InvocationId for the current invocation
/// </summary>
[Parameter(Mandatory = true)]
public string InvocationId { get; set; } = string.Empty;

// 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
protected override void ProcessRecord()
{
WriteObject(FunctionsActivityBuilder.GetActivityForInvocation(InvocationId));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using System.Management.Automation;
using OpenTelemetryEngine.Traces;
using OpenTelemetryEngine.ResponseObjects;
using System.Management.Automation.Runspaces;

namespace AzureFunctions.PowerShell.OpenTelemetry.SDK
{
Expand Down Expand Up @@ -42,6 +43,15 @@ public class StartOpenTelemetryInvocationInternal : PSCmdlet
protected override void ProcessRecord()
{
var newActivity = FunctionsActivityBuilder.StartInternalActivity(InvocationId, TraceParent, TraceState);

try
{
string script = "param($id) Set-FunctionInvocationContext -InvocationId $id";
_ = this.InvokeCommand.InvokeScript(script, false, PipelineResultTypes.Output, null, new object[] { InvocationId });
}
catch (ParameterBindingException) { }
catch (CommandNotFoundException) { }

WriteObject(newActivity);
}
}
Expand Down
14 changes: 14 additions & 0 deletions src/OpenTelemetryEngine/ResponseObjects/GetActivityResponse.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using System.Diagnostics;

namespace OpenTelemetryEngine.ResponseObjects
{
public class GetActivityResponse
{
public Activity? activity;

public GetActivityResponse(Activity? activity)
{
this.activity = activity;
}
}
}
9 changes: 9 additions & 0 deletions src/OpenTelemetryEngine/Traces/FunctionsActivityBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,15 @@ public static void StopActivity(FunctionsActivityResponse? response)
{
response?.activity?.Stop();
}

public static GetActivityResponse GetActivityForInvocation(string invocationId)
{
if (internalActivitiesByInvocationId.ContainsKey(invocationId))
{
return new GetActivityResponse(internalActivitiesByInvocationId[invocationId]);
}
return new GetActivityResponse(Activity.Current);
}
}
}