Search code examples
c#.netazureazure-functionsazure-application-insights

TelemetryClient.StartOperation always failed when passing an Activity as parameter in Azure function


I have a simple Azure function using BlobTrigger and want to use Application Insights SDK to work with custom telemetry.

    [FunctionName("Create-Thumbnail")]
    [StorageAccount("AzureWebJobsStorage")]
    public static void CreateThumbnail([BlobTrigger("input/{name}")] Stream image,
        [Blob("output/{name}", FileAccess.Write)] Stream imageSmall,
        ILogger log)
    {
        var requestActivity = new Activity("Create-Thumbnail");
        requestActivity.SetParentId("SOME PARENT ID");

        var newConfig = TelemetryConfiguration.CreateDefault();
        TelemetryClient telemetryClient = new TelemetryClient(newConfig);
        var requestOperation = telemetryClient.StartOperation<RequestTelemetry>(requestActivity);
        
        ...

        telemetryClient.StopOperation(requestOperation);

    }

However, this always failed with following error:

Method not found: 'Microsoft.ApplicationInsights.Extensibility.IOperationHolder`1<!!0> Microsoft.ApplicationInsights.TelemetryClientExtensions.StartOperation(Microsoft.ApplicationInsights.TelemetryClient, System.Diagnostics.Activity)'.

According to this GitHub issue: https://github.com/Azure/azure-functions-core-tools/issues/2399, it can be resolved by reverting to 2.14.0. However, I have tried all versions back to 2.8.1. None of them worked.

Passing string instead of Activity did work.

var requestOperation = telemetryClient.StartOperation<RequestTelemetry>("Create-Thumbnail");

But I need to pass Activity for configuring more context.

Does anyone know what can be the issue?


Solution

  • Per my searching, I found this document and it said

    There is a Functions-specific version of the Application Insights SDK that you can use to send custom telemetry data from your functions to Application Insights: Microsoft.Azure.WebJobs.Logging.ApplicationInsights

    So I followed the sample code and test, but the latest stable version(3.0.27) of this package will return the same error as yours, but when I changed to v3.0.25, the error disappeared.