azure-functions-powershell-worker icon indicating copy to clipboard operation
azure-functions-powershell-worker copied to clipboard

Access Telemetry Client to add customDimensions for App Insights

Open DannyArr opened this issue 3 years ago • 3 comments

Question. Is it possible to access and add customDimensions / properties to the existing telemetry client within the function app? Specifically for a request. I'm able to do so only when creating a new client and invoking trackRequest, like so:

class MyInitializer3 : Extensibility.ITelemetryInitializer {
    <# Define the class. Try constructors, properties, or methods. #>
    [void]Initialize([Channel.ITelemetry]$Telemetry){
        #$requestTelemetry = [DataContracts.RequestTelemetry]::new()
        $requestTelemetry = [DataContracts.RequestTelemetry]$Telemetry

        $requestTelemetry.Properties["test"] = "test value"
    }
}

$requestTelemetry = [DataContracts.RequestTelemetry]::new()

######################

$telemetryConfig = [Microsoft.ApplicationInsights.Extensibility.TelemetryConfiguration]::CreateDefault()
$telemetryConfig.ConnectionString = "XXXXXXXX"

$telemetryConfig.TelemetryInitializers.Add( [MyInitializer3]::new() )

$client = [Microsoft.ApplicationInsights.TelemetryClient]::new($telemetryConfig)

$client.TrackRequest("requestTest",$(get-date),100,201,$true)

But of course the function app already logs a request anyway, and I'd like to add to that.

DannyArr avatar Jun 07 '23 16:06 DannyArr

/cc @RohitRanjanMS

Francisco-Gamino avatar Jul 26 '23 20:07 Francisco-Gamino

After conferring with Rohit about this, it turns out that this is not possible. Request telemetry is sent using an application insights telemetry client on the host, not the worker, and it is not possible for function code to interact with the host's telemetry client. There is an issue open on the host right now, here that may add the functionality described here, but it is not certain how/if we will address it.

andystaples avatar Jul 31 '23 16:07 andystaples

I was also interested in having this possibility in Azure PowerShell functions. I have managed to come so far, but the only thing I'm missing is how to prevent having duplicate log entries when initializing a new application insights telemetry client on my own beside the one used by the host.

using namespace Microsoft.ApplicationInsights
using namespace Microsoft.ApplicationInsights.DataContracts
using namespace Microsoft.ApplicationInsights.Extensibility
using namespace Microsoft.ApplicationInsights.Channel
using namespace Microsoft.ApplicationInsights.Extensibility.Implementation

class CustomPropertyTelemetryInitializer : ITelemetryInitializer {
    [hashtable] $TriggerMetadata
    
    CustomPropertyTelemetryInitializer ([hashtable] $TriggerMetadata) {
        $this.TriggerMetadata = $TriggerMetadata
    }

    [void]Initialize([ITelemetry] $Telemetry) {
        $dependency = $Telemetry -as [DependencyTelemetry]
        if ($null -ne $dependency) {
            $dependency.Context.Cloud.RoleName = $Env:WEBSITE_SITE_NAME
            $dependency.Context.Cloud.RoleInstance = $Env:WEBSITE_INSTANCE_ID
            $dependency.Context.Operation.Id = $this.TriggerMetadata.InvocationId
            $dependency.Context.Operation.Name = $this.TriggerMetadata.FunctionName
            # $dependency.Context.Operation.ParentId = $this.TriggerMetadata.InvocationId            
        }
    }
}

class DependencyTelemetryProcessor : ITelemetryProcessor {
    hidden [ITelemetryProcessor] $Next

    DependencyTelemetryProcessor([ITelemetryProcessor] $next) {
        $this.Next = $next 
    }

    [void] Process([ITelemetry] $item) {
        if (!$this.OKtoSend($item)) { return }

        $this.Next.Process($item)
    }

    [bool] OKtoSend([ITelemetry] $item) {
        $dependency = $item -as [DependencyTelemetry]
        if ($null -eq $dependency) { return $false }
        return $true
    }
}

$telemetryConfig = [TelemetryConfiguration]::CreateDefault()
$telemetryConfig.InstrumentationKey = $Env:APPINSIGHTS_INSTRUMENTATIONKEY
$telemetryConfig.TelemetryInitializers.Add([CustomPropertyTelemetryInitializer]::new($TriggerMetadata))

$null = $telemetryConfig.TelemetryProcessorChainBuilder.Use({param($next) [DependencyTelemetryProcessor]::new($next) })
$telemetryConfig.TelemetryProcessorChainBuilder.Build()

$telemetryClient = [TelemetryClient]::new($telemetryConfig)

$startTime = [DateTime]::UtcNow
$statusCode = 200
$success = $true
Start-Sleep -Seconds 5
$duration = New-TimeSpan -Start $startTime -End ([DateTime]::UtcNow)
$telemetryClient.TrackDependency('MyDependency', "www.example.org", "GET www.example.org", "https://www.example.org", $startTime, $duration, $statusCode, $success)

Initially I thought it was because my logger was also processing the host log messages or the other way around, and that was the reason why I was seeing duplicate log entries, but after adding the above custom dependency telemetry I notice the same behavior was still happening. I added my own dependency telemetry processor because I was only interested in keeping track of some SQL bulk import activities I was executing using Invoke-Sqlcmd.

App Insights traces:

image

Durable Activity monitoring pane:

image

Kind regards,

Juni

juniinacio avatar Nov 08 '23 13:11 juniinacio