Use the Instrumentation Application Insight key to enter the service name in Azure

How can I programmatically determine the instance name of Application Insights using a toolkit key?

Our company has a large number of application instances in Azure. When troubleshooting application problems, it can take quite a while to find the correct instance of the application for a particular application.

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

+8
source share
5 answers

This can be done using PowerShell with AzureRm cmdlets. If you're new to this, check out the Azure Resource Manager here .

You must first log in using Login-AzureRmAccount and then select a subscription using Select-AzureRmSubscription

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

 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 like microsoft.insight / components from your group
  2. Expand the properties of this
  3. Find the tool key and name in the properties
+5
source

According to @mafue's comment, the following powershell commands let you find tool keys in resource groups:

 Import-Module -Name AzureRM Login-AzureRmAccount Select-AzureRmSubscription <subscription id> Find-AzureRmResource -ExpandProperties -ResourceType "microsoft.insights/components" | select -ExpandProperty Properties | Select Name, InstrumentationKey 
+2
source

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

Install the Az module

Install-Module -Name Az -AllowClobber as administrator or

Install-Module -Name Az -AllowClobber -Scope CurrentUser as not an administrator

Full instructions are here: https://docs.microsoft.com/en-us/powershell/azure/install-az-ps?view=azps-1.6.0

If necessary, remove the old AzureRM module

If you receive installation warnings and AzureRM as Az and AzureRM , you can remove the old module by running the following command as administrator : Uninstall-AzureRm

Log in to Azure and select Tools.

 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 
0
source

As for getting the name of your instance of App Insights using Instrumentation Key via C #, I was able to build the following program. The Azure SDK documentation is very simple, and NuGet packages are still in preview.

Install NuGet Packages

 PM> Install-Package Microsoft.Azure.Management.ApplicationInsights -IncludePrerelease PM> Install-Package Microsoft.Azure.Services.AppAuthentication -IncludePrerelease 

Get App Insights Components

 using System; using System.Collections.Generic; using System.Threading.Tasks; using Microsoft.Azure.Management.ApplicationInsights.Management; using Microsoft.Azure.Management.ApplicationInsights.Management.Models; using Microsoft.Azure.Services.AppAuthentication; using Microsoft.Rest; namespace CoreConsoleApp { internal class Program { private static async Task Main(string[] args) { // NOTE - see below var auth = new AzureServiceTokenProvider(); const string url = "https://management.azure.com/"; var token = await auth.GetAccessTokenAsync(url); var cred = new TokenCredentials(token); var client = new ApplicationInsightsManagementClient(cred) { SubscriptionId = "<your-subscription-id>", }; var list = new List<ApplicationInsightsComponent>(); var all = await client.Components.ListAsync(); list.AddRange(all); foreach(var item in list) { Console.WriteLine($"{item.Name}: {item.InstrumentationKey}"); } } } } 

(Note that you need to use C # 7.1 or later to have async Task Main in your console application).

A note about authentication. The AzureServiceTokenProvider constructor accepts an optional connection string for authentication in Azure. This worked for me without a single one, since I used az login through the Azure CLI . There are quite a few other ways to obtain credentials, some of which are discussed in the Java client documentation .

I am sure there is a more efficient way to request only the InstrumentationKey that you want using the OData query, but I could not figure out how to make this work.

The Microsoft.Azure.Management.ResourceManager package has a more general ResourceManagementClient that allows you to do something like the following:

 var client = new Microsoft.Azure.Management.ResourceManager.ResourceManagementClient(cred) { SubscriptionId = "<your-subscription-id>" }; var query = new ODataQuery<GenericResourceFilter>(o => o.ResourceType == "microsoft.insights/components") { Filter = "", // filter by Instrumentation Key here? Expand = "$expand=Properties", }; using (var resp = await client.Resources.ListWithHttpMessagesAsync(query)) { foreach (var item in resp.Body) { Console.WriteLine($"Instance name is {item.Name}"); } } 

Finally, this project has several other examples that may be helpful.

0
source

Using the Azure cloud shell (or any shell in which Azure-Cli is installed):

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

This is a search for your current subscription. You can see your current subscription with

 az account show 

There are probably more fancy ways to use --query, but the above approach is general.

0
source

All Articles