markdown App Insights和Log Analytics SDK + CLI更新

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了markdown App Insights和Log Analytics SDK + CLI更新相关的知识,希望对你有一定的参考价值。

# Application Insights Query SDK

The [Microsoft.Azure.ApplicationInsights NuGet package][1] contains the query SDK for App Insights. Here's a bare-minimum setup with a client ready to make calls:

```csharp
using System;
using Microsoft.Azure.ApplicationInsights;
using Microsoft.Rest.Azure.Authentication;
using System.Collections.Generic;

namespace AppInsightsSDKDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Starting...");

            var appId = "{appId}";
            var clientId = "{aadClientAppId}";
            var clientSecret = "{aadAppkey}";

            var domain = "microsoft.onmicrosoft.com";
            var authEndpoint = "https://login.microsoftonline.com";
            var tokenAudience = "https://api.applicationinsights.io/";

            var adSettings = new ActiveDirectoryServiceSettings
            {
                AuthenticationEndpoint = new Uri(authEndpoint),
                TokenAudience = new Uri(tokenAudience),
                ValidateAuthority = true
            };
            
            // Authenticate with client secret (app key)
            var creds = ApplicationTokenProvider.LoginSilentAsync(domain, clientId, clientSecret, adSettings).GetAwaiter().GetResult();

            // New up a client with credentials and AI application Id
            var client = new ApplicationInsightsDataClient(creds);
            client.AppId = appId
        }
    }
}
```


There are a few different ways to authenticate, which you can read about in the [AutoRest docs][2]. We can set up some **metrics** and a **query** to run:


```csharp
var metric = "availabilityResults/duration";
var query = "availabilityResults | summarize count() by name, bin(duration,500) | order by _count desc";
var metricA = new MetricsPostBodySchema("labelYourResult", new MetricsPostBodySchemaParameters("availabilityResults/duration", new TimeSpan(6, 0, 0))); // 6hr query
var metricB = new MetricsPostBodySchema("aDifferentLabel", new MetricsPostBodySchemaParameters("availabilityResults/count"));
var metrics = new List<MetricsPostBodySchema> { metricA, metricB };
```


And then we can make some calls (all of which have async counterparts): 


```csharp
var metricResult   = client.GetMetric(metric);
var metricResult2  = client.GetMetricAsync(metric).GetAwaiter().GetResult(); // same as above
var metricsResult  = client.GetMetrics(metrics);
var eventsResult   = client.GetEvents(EventType.Requests);
var queryResults   = client.Query(query);
```

Take a look at the **[generated models][3]** to see the details of the returned object shape of these methods. As an example on usage:

```csharp
for (var i = 0; i < queryResults.Tables[0].Rows.Count; i++) {
    // Do something with query results
    Console.WriteLine(String.Join("    ", queryResults.Tables[0].Rows[i]));
}

foreach (KeyValuePair<string, float> entry in metricResult.Value.MetricValues)
{
    // Print metric name and value
    Console.WriteLine(String.Format("{0}: {1}", entry.Key, entry.Value));
}
```



## Segmented and Intervaled Metrics

The generic GetMetric(s) methods return an object of type MetricResult. While this is sufficient to describe simple metrics results, it is not the most precise representation of all results. In cases using GetMetric to query with segments and intervals, the basic method will return a bag of **AdditionalProperties** attached to the metrics containing the properties which are part of that particular result, but not the basic metric response.

The SDK offers convenience methods which query for segmented, intervaled, or summary metrics and return types precise to the appropriate situation. These take the same arguments as the generic form, but return more specific types.

```csharp
var segmentResult  = client.GetSegmentedMetric(metric, "client/os"); // returns MetricsSegmentedResult
var intervalResult = client.GetIntervaledMetric(metric, "PT1H"); // MetricsIntervaledResult
var intervaledSegmented = client.GetIntervaledSegmentedMetric(metric, interval: new TimeSpan(6, 0, 0), segment: new List<string> { "client/os" })
var summaryResult = client.GetMetricSummary(metric)
```



# Log Analytics Query PowerShell Cmdlet

The [AzureRM.OperationalInsights module][4] contains a cmdlet for querying Log Analytics: [Invoke-AzureRmOperationalInsightsQuery][5]



## Prerequisites

[Azure PowerShell][6] is required to authenticate with the query cmdlet. On Windows, run:

```powershell
Install-Module -Name AzureRM
```

 Log into your account before querying:

```powershell
Connect-AzureRmAccount
```

Usage

```powershell
SYNTAX

Invoke-AzureRmOperationalInsightsQuery -Query <String> -Workspace <PSWorkspace> [-AsJob] [-DefaultProfile <IAzureContextContainer>] [-IncludeRender] [-IncludeStatistics] [-Timespan <TimeSpan>] [-Wait <Int32>] 
  
Invoke-AzureRmOperationalInsightsQuery -Query <String> -WorkspaceId <String> [-AsJob] [-DefaultProfile <IAzureContextContainer>] [-IncludeRender] [-IncludeStatistics] [-Timespan <TimeSpan>] [-Wait <Int32>] 
```

The first form takes in the Workspace to query as a PSWorkspace object rather than the GUID representing the workspace as a string. To retrieve a workspace as an object, use the [Get-AzureRmOperationalInsightsWorkspace cmdlet][7].

The returned object contains a results property with the actual results. 

```powershell
$res = Invoke-AzureRmOperationalInsightsQuery -WorkspaceId "DEMO_WORKSPACE" -query "AzureActivity | take 1 "

$res.Results.OperationName
# e.g., Microsoft.Compute/restorePointCollections/restorePoints/retrieveSasUris/action
```

# Log Analytics Query C# SDK

The [Microsoft.Azure.OperationalInsights NuGet package][8] contains a client with a single Query method and its async counterpart. Basic usage:

```csharp
using System;
using Microsoft.Azure.OperationalInsights;
using Microsoft.Rest.Azure.Authentication;
using System.Collections.Generic;

namespace LogAnalyticsSDKDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Starting...");

            var workspaceId = "{workspaceId}";
            var clientId = "{aadClientAppId}";
            var clientSecret = "{aadAppkey}";

            var domain = "microsoft.onmicrosoft.com";
            var authEndpoint = "https://login.microsoftonline.com";
            var tokenAudience = "https://api.loganalytics.io/";

            var adSettings = new ActiveDirectoryServiceSettings
            {
                AuthenticationEndpoint = new Uri(authEndpoint),
                TokenAudience = new Uri(tokenAudience),
                ValidateAuthority = true
            };
            
            // Authenticate with client secret (app key)
            var creds = ApplicationTokenProvider.LoginSilentAsync(domain, clientId, clientSecret, adSettings).GetAwaiter().GetResult();

            // New up a client with credentials and LA workspace Id
            var client = new OperationalInsightsDataClient(creds);
            client.WorkspaceId = workspaceId;
            
            var results = client.Query("union * | take 5");
            Console.WriteLine(results);
            // TODO process the results
        }
    }
}
```

The [generated models][9] contain the details of the response shape. 

[1]: https://www.nuget.org/packages/Microsoft.Azure.ApplicationInsights/0.9.0-preview
[2]: https://github.com/Azure/autorest/blob/master/docs/client/auth.md
[3]: https://github.com/Azure/azure-sdk-for-net/tree/psSdkJson6/src/SDKs/ApplicationInsights/DataPlane/ApplicationInsights/Generated/Models
[4]: https://www.powershellgallery.com/packages/AzureRM.OperationalInsights/4.3.2
[5]: https://docs.microsoft.com/en-us/powershell/module/azurerm.operationalinsights/invoke-azurermoperationalinsightsquery?view=azurermps-5.7.0
[6]: https://github.com/Azure/azure-powershell
[7]: https://docs.microsoft.com/en-us/powershell/module/azurerm.operationalinsights/get-azurermoperationalinsightsworkspace?view=azurermps-5.7.0
[8]: https://www.nuget.org/packages/Microsoft.Azure.OperationalInsights/0.9.0.1-preview
[9]: https://github.com/Azure/azure-sdk-for-net/tree/psSdkJson6/src/SDKs/OperationalInsights/DataPlane/OperationalInsights/Generated/Models

以上是关于markdown App Insights和Log Analytics SDK + CLI更新的主要内容,如果未能解决你的问题,请参考以下文章

如何使用 App Insights 关联前端和后端?

App Insights:禁用 SQL 依赖遥测

text Azure App Insights Kubernetes日志

Azure App Insights REST API 获取内存使用情况

Application Insights log4j appender不会退出

Azure App Insights 中的 Flush()