com.azure.monitor.query

Azure Monitor Query service is a powerful tool that allows you to query and analyze log data from various sources in Azure. It is built on top of the Kusto Query Language (KQL), which is a powerful query language that allows you to perform complex queries on large datasets. With Azure Monitor Query, you can easily search and analyze log data from various sources, including virtual machines, containers, and applications.

Azure Monitor Query java client library is a library that allows you to execute read-only queries against Azure Monitor\u2019s two data platforms: Logs and Metrics. The library provides both synchronous and asynchronous forms of the clients.

  • 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.
  • 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.

Getting Started

In order to interact with the Monitor service you'll need to create an instance of the LogsQueryClient or MetricsQueryClient class. To make this possible you'll need to use AAD authentication via Azure Identity to connect to the service.

Sample: Construct Asynchronous Clients

The following code sample demonstrates the creation of a LogsQueryAsyncClient using the LogsQueryClientBuilder.

LogsQueryAsyncClient logsQueryAsyncClient = new LogsQueryClientBuilder()
         .credential(tokenCredential)
         .buildAsyncClient();

The following code sample demonstrates the creation of a MetricsQueryAsyncClient using the MetricsQueryClientBuilder.

MetricsQueryAsyncClient metricsQueryAsyncClient = new MetricsQueryClientBuilder()
         .credential(tokenCredential)
         .buildAsyncClient();

Sample: Construct Synchronous Clients

The following code sample demonstrates the creation of a LogsQueryClient using the LogsQueryClientBuilder.

LogsQueryClient logsQueryClient = new LogsQueryClientBuilder()
         .credential(tokenCredential)
         .buildClient();

The following code sample demonstrates the creation of a MetricsQueryClient using the MetricsQueryClientBuilder.

MetricsQueryClient metricsQueryClient = new MetricsQueryClientBuilder()
         .credential(tokenCredential)
         .buildClient();

Query Workspace

The queryWorkspace(String workspaceId, String query, QueryTimeInterval timeInterval) method can be used to query logs from a given workspace.

The sample below shows how to query logs from the last 24 hours

LogsQueryResult queryResult = logsQueryClient.queryWorkspace("{workspace-id}", "{kusto-query}",
         QueryTimeInterval.LAST_DAY);
 for (LogsTableRow row : queryResult.getTable().getRows()) {
     System.out.println(row.getRow()
             .stream()
             .map(LogsTableCell::getValueAsString)
             .collect(Collectors.joining(",")));
 }

Note: For asynchronous sample, refer to queryWorkspace(String workspaceId, String query, QueryTimeInterval timeInterval).


Classes

LogsQueryAsyncClient

Provides an asynchronous service client for querying logs in the Azure Monitor Service.

LogsQueryClient

Provides a synchronous service client for querying logs in the Azure Monitor Service.

LogsQueryClientBuilder

Fluent builder for creating instances of LogsQueryClient and LogsQueryAsyncClient.

MetricsAsyncClient

This class provides an asynchronous client that contains all the query operations that use batch requests to retrieve metrics for multiple resources.

MetricsClient

This class provides an asynchronous client that contains all the query operations that use batch requests to retrieve metrics for multiple resources.

MetricsClientBuilder

Fluent builder for creating instances of MetricsClient and MetricsAsyncClient.

MetricsQueryAsyncClient

The asynchronous client for querying Azure Monitor metrics.

MetricsQueryClient

The synchronous client for querying Azure Monitor metrics.

MetricsQueryClientBuilder

Fluent builder for creating instances of MetricsQueryClient and MetricsQueryAsyncClient.

Enums

LogsQueryServiceVersion

The service version of the Logs service that can be queried to retrieved Azure Monitor logs.

MetricsQueryServiceVersion

The service version of the Metrics service that can be queried to retrieved Azure Monitor metrics.

MetricsServiceVersion

The versions of Azure Monitor Metrics Query supported by this client library.