给定Applications Insight Instrumentation密钥,获取Azure中的服务名称

问题描述 投票:8回答:5

如何在给定检测密钥的情况下以编程方式确定Application Insights实例的名称?

我们公司在Azure中有大量的应用程序洞察实例。在对应用程序进行疑难解答时,可能需要很长时间才能找到特定应用的正确应用洞察实例。

我应该指定(不仅仅是使用C#标签),我正在寻找一个C#解决方案。理想情况下,我想嵌入一些东西,以便我可以实现像'myapp.com/appinsights'这样的页面,这将为我提供给定应用程序的正确应用程序洞察实例(我们拥有的数百个)。

c# azure azure-application-insights
5个回答
5
投票

您可以使用PowerShell和AzureRm cmdlet执行此操作。如果你是新手,请看看Azure Resource Manager

您首先需要使用Login-AzureRmAccount登录,然后选择Select-AzureRmSubscription订阅

以下脚本将获取每个Application Insights实例及其instrumentationkey的名称列表:

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

其工作原理如下:

  1. 从您的组中获取所有类型的microsoft.insight / components类型的资源
  2. 展开它的属性
  3. 在属性中查找instrumentationkey和名称

3
投票

根据@ mafue的注释,以下powershell命令可让您在资源组中查找检测项:

Import-Module -Name AzureRM
Login-AzureRmAccount
Select-AzureRmSubscription <subscription id> 

Find-AzureRmResource -ExpandProperties -ResourceType "microsoft.insights/components" | select -ExpandProperty Properties | Select Name, InstrumentationKey

0
投票

新的跨平台AzureRM模块的旧is being replaced PowerShell模块Az。基于上面的@tobias和@ranieuwe答案,以下内容可以使用较新的模块获取所有InstrumentationKeys。

Install the Az module

Install-Module -Name Az -AllowClobber作为管理员,或

Install-Module -Name Az -AllowClobber -Scope CurrentUser为非管理员

完整说明:https://docs.microsoft.com/en-us/powershell/azure/install-az-ps?view=azps-1.6.0

Remove older AzureRM module if needed

如果您收到有关安装/加载AzAzureRM的警告,您可以通过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

0
投票

至于通过C#使用Instrumentation Key获取App Insights实例的名称,我能够拼凑出以下程序。 Azure SDK的文档非常受欢迎,NuGet包仍处于预览状态。

Install the NuGet Packages

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

Retrieve 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}");
            }
        }
    }
}

(请注意,您需要使用C#7.1或更高版本才能在控制台应用程序中使用async Task Main)。

关于身份验证的注意事项:AzureServiceTokenProvider构造函数使用可选的连接字符串来对Azure进行身份验证。因为我通过az login使用了Azure CLI,所以它对我没用。还有很多其他方法可以获得凭证,其中一些是讨论in the Java client docs

我确信有一种更有效的方法可以使用OData查询查询您想要的InstrumentationKey,但我无法弄清楚如何使其工作。

ResourceManagementClient包中有一个更通用的Microsoft.Azure.Management.ResourceManager,它可以让你做如下的事情:

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}");
    }
}

最后,this project还有其他一些可能有用的例子。


0
投票

使用azure云外壳(或安装了azure-cli ^ 2.0.64的任何外壳):

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

这会搜索您当前的订阅。你可以看到你当前的订阅

az account show

可能有更好的方式使用--query,但上述方法是通用的。

© www.soinside.com 2019 - 2024. All rights reserved.