Search code examples
c#azureazure-application-insights

Given an Applications Insight Instrumentation key, get the name of the service in Azure


How can I programmatically determine the name of the Application Insights instance given the instrumentation key?

Our company has a large number of application insights instances in Azure. When troubleshooting an application, it can take quite a while to track down the right app insights instance for a particular app.

I should have specified (more than just using C# tag), that I was looking for a C# solution. Ideally, I would like to embed something so I could implement a page like 'myapp.com/appinsights' and this would give me the correct app insights instance (of the hundreds that we have) for a given app.


Solution

  • You can do this using PowerShell with the AzureRm cmdlets. If you are new to that, take a look here at the Azure Resource Manager.

    You'll first need to login with Login-AzureRmAccount and then select a subscription with Select-AzureRmSubscription

    The following script will get a list of the name of each Application Insights instance and its instrumentationkey:

    Get-AzureRmResource -ExpandProperties -ResourceType "microsoft.insights/components"  -ResourceGroupName "your-resource-group" | select -ExpandProperty Properties  | Select Name, InstrumentationKey
    

    This works as follows:

    1. Get all resources of type microsoft.insight/components from within your group
    2. Expand the properties of it
    3. Find the instrumentationkey and name in the properties