-
Notifications
You must be signed in to change notification settings - Fork 5.1k
Readme and xmlk docs for Azure.Monitor.Query #20731
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 5 commits
33ae638
3a96faa
c792909
09a63df
dc090bc
b556c32
a2175b5
57bcbd4
cb59368
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,9 +1,62 @@ | ||
| # Azure Monitor Query client library for .NET | ||
|
|
||
| TODO | ||
| The `Azure.Monitor.Query` package provides an ability to query [Azure Monitor Logs](https://docs.microsoft.com/azure/azure-monitor/logs/data-platform-logs) and [Azure Monitor Metrics](https://docs.microsoft.com/azure/azure-monitor/essentials/data-platform-metrics) data. | ||
|
|
||
| Azure Monitor Logs is a feature of Azure Monitor that collects and organizes log and performance data from monitored resources. Data from different sources such as platform logs from Azure services, log and performance data from virtual machines agents, and usage and performance data from applications can be consolidated into a single workspace so they can be analyzed together using a sophisticated query language that's capable of quickly analyzing millions of records. | ||
|
|
||
| ## Samples | ||
| Azure Monitor Metrics is a feature of Azure Monitor that collects numeric data from monitored resources into a time series database. Metrics are numerical values that are collected at regular intervals and describe some aspect of a system at a particular time. Metrics in Azure Monitor are lightweight and capable of supporting near real-time scenarios making them particularly useful for alerting and fast detection of issues. | ||
pakrym marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| [Source code][query_client_src] | [Package (NuGet)][query_client_nuget_package] | ||
|
|
||
| ## Getting started | ||
|
|
||
| ### Install the package | ||
| Install the Azure Monitor Query client library for .NET with [NuGet][query_client_nuget_package]: | ||
|
|
||
| ``` | ||
| dotnet add package Azure.Monitor.Query --prerelease | ||
| ``` | ||
|
|
||
| ### Prerequisites | ||
| * An [Azure subscription][azure_sub]. | ||
| * To be able to query logs you would need an existing Log Analytics workspace. You can create it in one of the following approaches: | ||
| * [Azure Portal](https://docs.microsoft.com/azure/azure-monitor/logs/quick-create-workspace) | ||
| * [Azure CLI](https://docs.microsoft.com/azure/azure-monitor/logs/quick-create-workspace-cli) | ||
| * [PowerShell](https://docs.microsoft.com/azure/azure-monitor/logs/powershell-workspace-configuration) | ||
|
|
||
| * To be able to query metrics all you need is an Azure resource of any kind (Storage Account, KeyVault, CosmosDB etc.) | ||
|
|
||
| ### Authenticate the Client | ||
|
|
||
| In order to interact with the Azure Monitor service, you will need to create an instance of a [TokenCredential](https://docs.microsoft.com/dotnet/api/azure.core.tokencredential?view=azure-dotnet) class and pass it to the constructor of your `LogsClient` and `MetricsClient` class. | ||
pakrym marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| ## Key concepts | ||
|
|
||
| - `LogsClient` - Client that provides methods to query logs from Azure Monitor Logs. | ||
| - `MetricsClient` - Client that provides methods to query metrics from Azure Monitor Metrics. | ||
|
|
||
| ### Thread safety | ||
| We guarantee that all client instance methods are thread-safe and independent of each other ([guideline](https://azure.github.io/azure-sdk/dotnet_introduction.html#dotnet-service-methods-thread-safety)). This ensures that the recommendation of reusing client instances is always safe, even across threads. | ||
|
|
||
| ### Additional concepts | ||
| <!-- CLIENT COMMON BAR --> | ||
| [Client options](https://github.com/Azure/azure-sdk-for-net/blob/master/sdk/core/Azure.Core/README.md#configuring-service-clients-using-clientoptions) | | ||
| [Accessing the response](https://github.com/Azure/azure-sdk-for-net/blob/master/sdk/core/Azure.Core/README.md#accessing-http-response-details-using-responset) | | ||
| [Long-running operations](https://github.com/Azure/azure-sdk-for-net/blob/master/sdk/core/Azure.Core/README.md#consuming-long-running-operations-using-operationt) | | ||
| [Handling failures](https://github.com/Azure/azure-sdk-for-net/blob/master/sdk/core/Azure.Core/README.md#reporting-errors-requestfailedexception) | | ||
| [Diagnostics](https://github.com/Azure/azure-sdk-for-net/blob/master/sdk/core/Azure.Core/samples/Diagnostics.md) | | ||
| [Mocking](https://github.com/Azure/azure-sdk-for-net/blob/master/sdk/core/Azure.Core/README.md#mocking) | | ||
| [Client lifetime](https://devblogs.microsoft.com/azure-sdk/lifetime-management-and-thread-safety-guarantees-of-azure-sdk-net-clients/) | ||
| <!-- CLIENT COMMON BAR --> | ||
|
|
||
| ## Examples | ||
|
|
||
| - [Query logs](#query-logs) | ||
| - [Query logs as model](#query-logs-as-model) | ||
| - [Query logs as primitive](#query-logs-as-primitive) | ||
| - [Batch query](#batch-query) | ||
| - [Query dynamic table](#query-dynamic-table) | ||
| - [Increase query timeout](#increase-query-timeout) | ||
|
|
||
| ### Query logs | ||
|
|
||
|
|
@@ -92,7 +145,7 @@ foreach (var logEntryModel in topEntries) | |
| } | ||
| ``` | ||
|
|
||
| ### Query dynamic table | ||
| ### Query dynamic table | ||
|
|
||
| You can also dynamically inspect the list of columns. The following example prints the result of the query as a table: | ||
|
|
||
|
|
@@ -120,4 +173,104 @@ foreach (var row in table.Rows) | |
|
|
||
| Console.WriteLine(); | ||
| } | ||
| ``` | ||
| ``` | ||
|
|
||
| ### Increase query timeout | ||
|
|
||
| Some queries take longer to execute than the default service timeout allows. You can use the `LogsQueryOptions` parameter to specify the service timeout. | ||
|
|
||
| ```C# Snippet:QueryLogsPrintTable | ||
| LogsClient client = new LogsClient(new DefaultAzureCredential()); | ||
| string workspaceId = "<workspace_id>"; | ||
| Response<LogsQueryResult> response = await client.QueryAsync(workspaceId, "AzureActivity | top 10 by TimeGenerated"); | ||
|
|
||
| LogsQueryResultTable table = response.Value.PrimaryTable; | ||
|
|
||
| foreach (var column in table.Columns) | ||
| { | ||
| Console.Write(column.Name + ";"); | ||
| } | ||
|
|
||
| Console.WriteLine(); | ||
|
|
||
| var columnCount = table.Columns.Count; | ||
| foreach (var row in table.Rows) | ||
| { | ||
| for (int i = 0; i < columnCount; i++) | ||
| { | ||
| Console.Write(row[i] + ";"); | ||
| } | ||
|
|
||
| Console.WriteLine(); | ||
| } | ||
| ``` | ||
|
|
||
| ## Troubleshooting | ||
|
|
||
| ### General | ||
|
|
||
| When you interact with the Azure Monitor Query client library using the .NET SDK, errors returned by the service correspond to the same HTTP status codes returned for [REST API][monitor_rest_api] requests. | ||
|
|
||
| For example, if you submit an invalid query a `400` error is returned, indicating "Bad Request". | ||
|
|
||
| ```C# Snippet:BadRequest | ||
| string workspaceId = "<workspace_id>"; | ||
| LogsClient client = new LogsClient(new DefaultAzureCredential()); | ||
| try | ||
| { | ||
| await client.QueryAsync(workspaceId, "My Not So Valid Query"); | ||
| } | ||
| catch (Exception e) | ||
| { | ||
| Console.WriteLine(e); | ||
| throw; | ||
| } | ||
| ``` | ||
|
|
||
| The exception also contains some additional information like the full error content. | ||
|
|
||
| ``` | ||
| Azure.RequestFailedException : The request had some invalid properties | ||
| Status: 400 (Bad Request) | ||
| ErrorCode: BadArgumentError | ||
|
|
||
| Content: | ||
| {"error":{"message":"The request had some invalid properties","code":"BadArgumentError","correlationId":"34f5f93a-6007-48a4-904f-487ca4e62a82","innererror":{"code":"SyntaxError","message":"A recognition error occurred in the query.","innererror":{"code":"SYN0002","message":"Query could not be parsed at 'Not' on line [1,3]","line":1,"pos":3,"token":"Not"}}}} | ||
| ``` | ||
|
|
||
| ### Setting up console logging | ||
|
|
||
| The simplest way to see the logs is to enable the console logging. | ||
| To create an Azure SDK log listener that outputs messages to console use AzureEventSourceListener.CreateConsoleLogger method. | ||
|
|
||
| ```C# | ||
| // Setup a listener to monitor logged events. | ||
| using AzureEventSourceListener listener = AzureEventSourceListener.CreateConsoleLogger(); | ||
| ``` | ||
|
|
||
| To learn more about other logging mechanisms see [here][logging]. | ||
|
|
||
| ## Next steps | ||
|
|
||
| ## Contributing | ||
|
|
||
| This project welcomes contributions and suggestions. Most contributions require | ||
| you to agree to a Contributor License Agreement (CLA) declaring that you have | ||
| the right to, and actually do, grant us the rights to use your contribution. For | ||
| details, visit [cla.microsoft.com][cla]. | ||
|
|
||
| This project has adopted the [Microsoft Open Source Code of Conduct][coc]. | ||
| For more information see the [Code of Conduct FAQ][coc_faq] or contact | ||
| [[email protected]][coc_contact] with any additional questions or comments. | ||
|
|
||
| [query_client_src]: https://github.com/Azure/azure-sdk-for-net/blob/master/sdk/monitor/Azure.Monitor.Query/src | ||
| [query_client_nuget_package]: https://www.nuget.org/packages?q=Azure.Monitor.Query | ||
| [monitor_rest_api]: https://docs.microsoft.com/rest/api/monitor/ | ||
| [azure_cli]: https://docs.microsoft.com/cli/azure | ||
| [azure_sub]: https://azure.microsoft.com/free/ | ||
| [cla]: https://cla.microsoft.com | ||
| [coc]: https://opensource.microsoft.com/codeofconduct/ | ||
| [coc_faq]: https://opensource.microsoft.com/codeofconduct/faq/ | ||
| [coc_contact]: mailto:[email protected] | ||
|
|
||
|  | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,12 +2,25 @@ | |
| // Licensed under the MIT License. | ||
|
|
||
| using System; | ||
| using Azure.Monitor.Query.Models; | ||
|
|
||
| namespace Azure.Monitor.Query | ||
| { | ||
| /// <summary> | ||
| /// Options for <see cref="LogsClient.QueryAsync"/> that allow specifying the service timeout or whether to include | ||
| /// the query execution statistics. | ||
pakrym marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| /// </summary> | ||
| public class LogsQueryOptions | ||
| { | ||
| /// <summary> | ||
| /// Gets or sets the value indicating the service timeout for the query. Defaults to <c>null</c>. | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should we say what the default is rather than the default value of the type? For example,
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That's an interesting question. I was trying to avoid presuming what service does as it might change with the service version.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. How would someone know what to set this to?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Their query would fail. No matter whether we give them a default value or not they need to see their query timing out to start setting this value. |
||
| /// </summary> | ||
| public TimeSpan? Timeout { get; set; } | ||
|
|
||
| /// <summary> | ||
| /// Gets or sets the value indicating whether to include query execution statistics as part of the response. | ||
| /// Statistics can be retrieved via the <see cref="LogsQueryResult.Statistics"/> property. | ||
pakrym marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| /// </summary> | ||
| public bool IncludeStatistics { get; set; } | ||
| } | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.