Azure / azure-sdk-for-net

This repository is for active development of the Azure SDK for .NET. For consumers of the SDK we recommend visiting our public developer docs at https://learn.microsoft.com/dotnet/azure/ or our versioned developer docs at https://azure.github.io/azure-sdk-for-net.
MIT License
5.49k stars 4.81k forks source link

[BUG] Can't connect to AppInsights with Azure.Monitor.Query with ManagedIdentity. #47295

Open vpetit-reimagine opened 1 day ago

vpetit-reimagine commented 1 day ago

Library name and version

Azure.Monitor.Query 1.5.0

Describe the bug

Following the example, it is impossible for an Azure Function to actually connect to the AppInsights through the package with

var client = new MetricsQueryClient(new DefaultAzureCredential(options)); // Where options holds the managed identity ID with `ManagedIdentityClientId` property

Expected behavior

The Azure Function should be able to read the Azure AppInsights it is trying to access to.

Actual behavior

Depending on what I am trying to test, I get either:

Reproduction Steps

  1. Create an App Service on Azure
  2. Create an Application Insights resource on Azure linked to the App Service
  3. Create an Azure function (timer or http based) using the Azure.Monitor.Query and Azure.Identity packages, specifying the previous app resource as the one to query (in the form of /subscriptions/{id}/resourceGroups/{rg-name}/providers/microsoft.insights/components/{app-insights-name}).
  4. Assign either a system or user-defined managed identity to the Azure Function
  5. Run the Azure Function

Environment

No response

github-actions[bot] commented 1 day ago

Thank you for your feedback. Tagging and routing to the team member best able to assist.

christothes commented 1 day ago

Hi @vpetit-reimagine - In your repro steps you mention running the Azure Function - are you running it locally or deployed to Azure?

If deployed to Azure, can you get logging of the error while using the ManagedIdentityCredential?

For console output, you would just need to add the following line to your program.

using AzureEventSourceListener listener = AzureEventSourceListener.CreateTraceLogger();

If you need to log the output somewhere other than the console, this can be done as described in these docs.

for example:

using AzureEventSourceListener listener = new AzureEventSourceListener(
    (args, message) => myLogger.Log("[{0:HH:mm:ss:fff}][{1}] {2}", DateTimeOffset.Now, args.Level, message),
    level: EventLevel.Verbose);
github-actions[bot] commented 1 day ago

Hi @vpetit-reimagine. Thank you for opening this issue and giving us the opportunity to assist. To help our team better understand your issue and the details of your scenario please provide a response to the question asked above or the information requested above. This will help us more accurately address your issue.