Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 643
johnweeder Avatar asked Aug 31 '16 17:08

johnweeder


3 Answers

The older AzureRM PowerShell module is being replaced by the new cross-platform Az module. Based on the answers of @tobias and @ranieuwe, the following can fetch all your InstrumentationKeys using the newer module.

Install the Az module

Install-Module -Name Az -AllowClobber as admin, or

Install-Module -Name Az -AllowClobber -Scope CurrentUser as non-admin

Full instructions here: https://docs.microsoft.com/en-us/powershell/azure/install-az-ps

Remove older AzureRM module if needed

If you get warnings about both Az and AzureRM being installed/loaded, you can uninstall the old module by running the following as admin: Uninstall-AzureRm

Login to Azure and select Instrumentation Keys

Import-Module Az
Connect-AzAccount
Get-AzSubscription # will list all currently connected subscriptions
Select-AzSubscription <subscription-id>

# Retrieve all Instrumentation Keys along with name of AppInsights resource
Get-AzResource -ExpandProperties -ResourceType "microsoft.insights/components" | Select -ExpandProperty Properties | Select Name, InstrumentationKey

# Find a specific Instrumentation Key
Get-AzResource -ExpandProperties -ResourceType "microsoft.insights/components" | Select -ExpandProperty Properties | Where InstrumentationKey -eq "abe66a40-c437-4af1-bfe9-4b72bd6b94a1"| Select Name, InstrumentationKey
like image 80
Tobias J Avatar answered Nov 14 '22 05:11

Tobias J


Using azure cloud shell (or any shell where you have azure-cli ^2.0.64 installed):

az extension add --name application-insights
az monitor app-insights component show --output table | grep <instrumentation_key>

This searches across your current subscription. You can see your current subscription with

az account show

There are probably fancier ways to use --query but the above approach is general purpose.

like image 26
kayjtea Avatar answered Nov 14 '22 05:11

kayjtea


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
like image 40
ranieuwe Avatar answered Nov 14 '22 05:11

ranieuwe