Categorygithub.com/Azure/azure-sdk-for-go/sdk/monitor/azquery
modulepackage
1.2.0-beta.1
Repository: https://github.com/azure/azure-sdk-for-go.git
Documentation: pkg.go.dev

# README

Azure Monitor Query client module for Go

The Azure Monitor Query client module is used to execute read-only queries against Azure Monitor's two data platforms:

  • Logs - 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 apps can be consolidated into a single Azure Log Analytics workspace. The various data types can be analyzed together using the Kusto Query Language. See the Kusto to SQL cheat sheet for more information.
  • Metrics - 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 are lightweight and capable of supporting near real-time scenarios, making them particularly useful for alerting and fast detection of issues.

Source code | Package (pkg.go.dev) | REST API documentation | Product documentation | Samples

Getting started

Prerequisites

  • Go, version 1.18 or higher - Install Go
  • Azure subscription - Create a free account
  • To query Logs, you need one of the following things:
  • To query Metrics, the resource URI of an Azure resource (Storage Account, Key Vault, CosmosDB, etc.) that you plan to monitor

Install the packages

Install the azquery and azidentity modules with go get:

go get github.com/Azure/azure-sdk-for-go/sdk/monitor/azquery
go get github.com/Azure/azure-sdk-for-go/sdk/azidentity

The azidentity module is used for Azure Active Directory authentication during client construction.

Authentication

An authenticated client object is required to execute a query. The examples demonstrate using azidentity.NewDefaultAzureCredential to authenticate; however, the client accepts any azidentity credential. See the azidentity documentation for more information about other credential types.

The clients default to the Azure public cloud. For other cloud configurations, see the cloud package documentation.

Create a logs client

Example logs client

Create a metrics client

Example metrics client

Create a metrics batch client

Example metrics batch client

Key concepts

Timespan

It's best practice to always query with a timespan (type TimeInterval) to prevent excessive queries of the entire logs or metrics data set. Log queries use the ISO8601 Time Interval Standard. All time should be represented in UTC. If the timespan is included in both the Kusto query string and Timespan field, the timespan is the intersection of the two values.

Use the NewTimeInterval() method for easy creation.

Metrics data structure

Each set of metric values is a time series with the following characteristics:

  • The time the value was collected
  • The resource associated with the value
  • A namespace that acts like a category for the metric
  • A metric name
  • The value itself
  • Some metrics may have multiple dimensions as described in multi-dimensional metrics. Custom metrics can have up to 10 dimensions.

Metrics batch query

A user can also query metrics from multiple resources at once using the query_batch method of MetricsBatchClient. This uses a different API than the MetricsClient and requires that a user pass in a regional endpoint when instantiating the client (for example, "https://westus3.metrics.monitor.azure.com").

Note, each resource must be in the same region as the endpoint passed in when instantiating the client, and each resource must be in the same Azure subscription. Furthermore, the metric namespace that contains the metrics to be queried must also be passed. A list of metric namespaces can be found here.

Logs query rate limits and throttling

The Log Analytics service applies throttling when the request rate is too high. Limits, such as the maximum number of rows returned, are also applied on the Kusto queries. For more information, see Query API.

If you're executing a batch logs query, a throttled request will return a ErrorInfo object. That object's code value will be ThrottledError.

Advanced logs queries

Query multiple workspaces

To run the same query against multiple Log Analytics workspaces, add the additional workspace ID strings to the AdditionalWorkspaces slice in the Body struct.

When multiple workspaces are included in the query, the logs in the result table are not grouped according to the workspace from which they were retrieved.

Increase wait time, include statistics, include render (visualization)

The LogsQueryOptions type is used for advanced logs options.

  • By default, your query will run for up to three minutes. To increase the default timeout, set LogsQueryOptions.Wait to the desired number of seconds. The maximum wait time is 10 minutes (600 seconds).

  • To get logs query execution statistics, such as CPU and memory consumption, set LogsQueryOptions.Statistics to true.

  • To get visualization data for logs queries, set LogsQueryOptions.Visualization to true.

azquery.LogsClientQueryWorkspaceOptions{
			Options: &azquery.LogsQueryOptions{
				Statistics:    to.Ptr(true),
				Visualization: to.Ptr(true),
				Wait:          to.Ptr(600),
			},
		}

To do the same with QueryBatch, set the values in the BatchQueryRequest.Headers map with a key of "prefer", or use the NewBatchQueryRequest method.

Examples

Get started with our examples.

  • For the majority of log queries, use the LogsClient.QueryWorkspace or the LogsClient.QueryResource method. Only use the LogsClient.QueryBatch method in advanced scenerios.

  • Use MetricsClient.QueryResource for metric queries.

Troubleshooting

See our troubleshooting guide for details on how to diagnose various failure scenarios.

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.

When you submit a pull request, a CLA-bot will automatically determine whether you need to provide a CLA and decorate the PR appropriately (e.g., label, comment). Simply follow the instructions provided by the bot. You will only need to do this once across all repos using our CLA.

This project has adopted the Microsoft Open Source Code of Conduct. For more information, see the Code of Conduct FAQ or contact [email protected] with any additional questions or comments.

# Functions

NewBatchQueryRequest creates a new BatchQueryRequest.
NewLogsClient creates a client that accesses Azure Monitor logs data.
NewMetricsBatchClient creates a client that accesses Azure Monitor metrics data.
NewMetricsClient creates a client that accesses Azure Monitor metrics data.
NewTimeInterval creates a TimeInterval for use in a query.
PossibleAggregationTypeValues returns the possible values for the AggregationType const type.
PossibleBatchQueryRequestMethodValues returns the possible values for the BatchQueryRequestMethod const type.
PossibleBatchQueryRequestPathValues returns the possible values for the BatchQueryRequestPath const type.
PossibleLogsColumnTypeValues returns the possible values for the LogsColumnType const type.
PossibleMetricClassValues returns the possible values for the MetricClass const type.
PossibleMetricUnitValues returns the possible values for the MetricUnit const type.
PossibleNamespaceClassificationValues returns the possible values for the NamespaceClassification const type.
PossibleResultTypeValues returns the possible values for the ResultType const type.

# Constants

No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
Cloud Service Names for Monitor Query Logs and Metrics, used to identify the respective cloud.ServiceConfiguration.
Cloud Service Names for Monitor Query Logs and Metrics, used to identify the respective cloud.ServiceConfiguration.

# Structs

BatchQueryRequest - An single request in a batch.
BatchQueryResponse - Contains the batch query response and the headers, id, and status of the request.
BatchQueryResults - Contains the tables, columns & rows resulting from a query.
BatchRequest - An array of requests.
BatchResponse - Response to a batch query.
Body - The Analytics query.
Column - A column in a table.
ErrorInfo - The code and message for an error.
LocalizableString - The localizable string class.
LogsClient contains the methods for the Logs group.
LogsClientOptions contains optional settings for LogsClient.
LogsClientQueryBatchOptions contains the optional parameters for the LogsClient.QueryBatch method.
LogsClientQueryBatchResponse contains the response from method LogsClient.QueryBatch.
LogsClientQueryResourceOptions contains the optional parameters for the LogsClient.QueryResource method.
LogsClientQueryResourceResponse contains the response from method LogsClient.QueryResource.
LogsClientQueryWorkspaceOptions contains the optional parameters for the LogsClient.QueryWorkspace method.
LogsClientQueryWorkspaceResponse contains the response from method LogsClient.QueryWorkspace.
LogsQueryOptions sets server timeout, query statistics and visualization information.
MetadataValue - Represents a metric metadata value.
Metric - The result data of a query.
MetricAvailability - Metric availability specifies the time grain (aggregation interval or frequency) and the retention period for that time grain.
MetricDefinition - Metric definition class specifies the metadata for a metric.
MetricDefinitionCollection - Represents collection of metric definitions.
MetricNamespace - Metric namespace class specifies the metadata for a metric namespace.
MetricNamespaceCollection - Represents collection of metric namespaces.
MetricNamespaceName - The fully qualified metric namespace name.
MetricResults - The metrics result for a resource.
MetricsBatchClient contains the methods for the MetricsBatch group.
MetricsBatchClientOptions contains optional settings for MetricsBatchClient.
MetricsBatchClientQueryBatchOptions contains the optional parameters for the MetricsBatchClient.QueryBatch method.
MetricsBatchClientQueryBatchResponse contains the response from method MetricsBatchClient.QueryBatch.
MetricsClient contains the methods for the Metrics group.
MetricsClientListDefinitionsOptions contains the optional parameters for the MetricsClient.NewListDefinitionsPager method.
MetricsClientListDefinitionsResponse contains the response from method MetricsClient.NewListDefinitionsPager.
MetricsClientListNamespacesOptions contains the optional parameters for the MetricsClient.NewListNamespacesPager method.
MetricsClientListNamespacesResponse contains the response from method MetricsClient.NewListNamespacesPager.
MetricsClientOptions contains optional settings for MetricsClient.
MetricsClientQueryResourceOptions contains the optional parameters for the MetricsClient.QueryResource method.
MetricsClientQueryResourceResponse contains the response from method MetricsClient.QueryResource.
MetricValue - Represents a metric value.
MetricValues - Metric data values.
ResourceIDList - The comma separated list of resource IDs to query metrics for.
Response - The response to a metrics query.
Results - Contains the tables, columns & rows resulting from a query.
Table - Contains the columns and rows for one table in a query response.
TimeSeriesElement - A time series result type.

# Type aliases

AggregationType - the aggregation type of the metric.
BatchQueryRequestMethod - The method of a single request in a batch, defaults to POST.
BatchQueryRequestPath - The query path of a single request in a batch, defaults to /query.
LogsColumnType - The data type of this column.
MetricClass - The class of the metric.
MetricUnit - The unit of the metric.
NamespaceClassification - Kind of namespace.
ResultType - Reduces the set of data collected.
Row of data in a table, types of data used by service specified in LogsColumnType.
TimeInterval specifies the time range over which to query.