Class LogsQueryClient

java.lang.Object
com.azure.monitor.query.LogsQueryClient

public final class LogsQueryClient extends Object

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

The LogsQueryClient is a synchronous client that provides methods to execute Kusto queries against Azure Monitor logs. It provides methods to query logs in a specific workspace, execute a batch of queries, and query logs for a specific Azure resource.

Getting Started

Authenticating and building instances of this client are handled by LogsQueryClientBuilder. his sample shows how to authenticate and build a LogsQueryClient instance using LogQueryClientBuilder.

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

For more information on building and authenticating, see the LogsQueryClientBuilder documentation.

Client Usage

For more information on how to use this client, see the following method documentation:

See Also:
  • Method Details

    • queryWorkspace

      public LogsQueryResult queryWorkspace(String workspaceId, String query, QueryTimeInterval timeInterval)
      Returns all the Azure Monitor logs matching the given query in the specified workspaceId.

      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(",")));
       }
       
      Parameters:
      workspaceId - The workspaceId where the query should be executed.
      query - The Kusto query to fetch the logs.
      timeInterval - The time period for which the logs should be looked up.
      Returns:
      The logs matching the query.
      Throws:
      NullPointerException - if workspaceId or query is null.
    • queryWorkspace

      public <T> List<T> queryWorkspace(String workspaceId, String query, QueryTimeInterval timeInterval, Class<T> type)
      Returns all the Azure Monitor logs matching the given query in the specified workspaceId.
      Type Parameters:
      T - The type the result of this query should be mapped to.
      Parameters:
      workspaceId - The workspaceId where the query should be executed.
      query - The Kusto query to fetch the logs.
      timeInterval - The time period for which the logs should be looked up.
      type - The type the result of this query should be mapped to.
      Returns:
      The logs matching the query as a list of objects of type T.
      Throws:
      NullPointerException - if workspaceId or query is null.
    • queryWorkspace

      public <T> List<T> queryWorkspace(String workspaceId, String query, QueryTimeInterval timeInterval, Class<T> type, LogsQueryOptions options)
      Returns all the Azure Monitor logs matching the given query in the specified workspaceId.
      Type Parameters:
      T - The type the result of this query should be mapped to.
      Parameters:
      workspaceId - The workspaceId where the query should be executed.
      query - The Kusto query to fetch the logs.
      timeInterval - The time period for which the logs should be looked up.
      type - The type the result of this query should be mapped to.
      options - The log query options to configure server timeout, set additional workspaces or enable statistics and rendering information in response.
      Returns:
      The logs matching the query as a list of objects of type T.
      Throws:
      NullPointerException - if workspaceId or query is null.
    • queryWorkspaceWithResponse

      public com.azure.core.http.rest.Response<LogsQueryResult> queryWorkspaceWithResponse(String workspaceId, String query, QueryTimeInterval timeInterval, LogsQueryOptions options, com.azure.core.util.Context context)
      Returns all the Azure Monitor logs matching the given query in the specified workspaceId.

      Query logs from the last 7 days and set the service timeout to 2 minutes

       Response<LogsQueryResult> queryResult = logsQueryClient.queryWorkspaceWithResponse("{workspace-id}",
               "{kusto-query}",
               QueryTimeInterval.LAST_7_DAYS,
               new LogsQueryOptions().setServerTimeout(Duration.ofMinutes(2)),
               Context.NONE);
      
       for (LogsTableRow row : queryResult.getValue().getTable().getRows()) {
           System.out.println(row.getRow()
                   .stream()
                   .map(LogsTableCell::getValueAsString)
                   .collect(Collectors.joining(",")));
       }
       
      Parameters:
      workspaceId - The workspaceId where the query should be executed.
      query - The Kusto query to fetch the logs.
      timeInterval - The time period for which the logs should be looked up.
      options - The log query options to configure server timeout, set additional workspaces or enable statistics and rendering information in response.
      context - Additional context that is passed through the Http pipeline during the service call. If no additional context is required, pass Context.NONE instead.
      Returns:
      The logs matching the query including the HTTP response.
      Throws:
      NullPointerException - if workspaceId or query is null.
    • queryWorkspaceWithResponse

      public <T> com.azure.core.http.rest.Response<List<T>> queryWorkspaceWithResponse(String workspaceId, String query, QueryTimeInterval timeInterval, Class<T> type, LogsQueryOptions options, com.azure.core.util.Context context)
      Returns all the Azure Monitor logs matching the given query in the specified workspaceId.
      Type Parameters:
      T - The type the result of this query should be mapped to.
      Parameters:
      workspaceId - The workspaceId where the query should be executed.
      query - The Kusto query to fetch the logs.
      timeInterval - The time period for which the logs should be looked up.
      type - The type the result of this query should be mapped to.
      options - The log query options to configure server timeout, set additional workspaces or enable statistics and rendering information in response.
      context - Additional context that is passed through the Http pipeline during the service call. If no additional context is required, pass Context.NONE instead.
      Returns:
      The logs matching the query including the HTTP response.
      Throws:
      NullPointerException - if workspaceId or query is null.
    • queryBatch

      public LogsBatchQueryResultCollection queryBatch(LogsBatchQuery logsBatchQuery)
      Returns all the Azure Monitor logs matching the given batch of queries.

      Execute a batch of logs queries

       LogsBatchQuery batchQuery = new LogsBatchQuery();
       String queryId1 = batchQuery.addWorkspaceQuery("{workspace-id-1}", "{kusto-query-1}", QueryTimeInterval.LAST_DAY);
       String queryId2 = batchQuery.addWorkspaceQuery("{workspace-id-2}", "{kusto-query-2}",
               QueryTimeInterval.LAST_7_DAYS, new LogsQueryOptions().setServerTimeout(Duration.ofMinutes(2)));
      
       LogsBatchQueryResultCollection batchQueryResponse = logsQueryClient.queryBatch(batchQuery);
      
       for (LogsBatchQueryResult queryResult : batchQueryResponse.getBatchResults()) {
           System.out.println("Logs query result for query id " + queryResult.getId());
           for (LogsTableRow row : queryResult.getTable().getRows()) {
               System.out.println(row.getRow()
                       .stream()
                       .map(LogsTableCell::getValueAsString)
                       .collect(Collectors.joining(",")));
           }
       }
       
      Parameters:
      logsBatchQuery - LogsBatchQuery containing a batch of queries.
      Returns:
      A collection of query results corresponding to the input batch of queries.@return
      Throws:
      NullPointerException - if logsBatchQuery is null.
    • queryBatchWithResponse

      public com.azure.core.http.rest.Response<LogsBatchQueryResultCollection> queryBatchWithResponse(LogsBatchQuery logsBatchQuery, com.azure.core.util.Context context)
      Returns all the Azure Monitor logs matching the given batch of queries.
      Parameters:
      logsBatchQuery - LogsBatchQuery containing a batch of queries.
      context - Additional context that is passed through the Http pipeline during the service call. If no additional context is required, pass Context.NONE instead.
      Returns:
      A collection of query results corresponding to the input batch of queries.@return
      Throws:
      NullPointerException - if logsBatchQuery is null.
    • queryResource

      public LogsQueryResult queryResource(String resourceId, String query, QueryTimeInterval timeInterval)
      Returns all the Azure Monitor logs matching the given query for an Azure resource.

      Query logs from the last 24 hours

       LogsQueryResult queryResult = logsQueryClient.queryResource("{resource-id}", "{kusto-query}",
           QueryTimeInterval.LAST_DAY);
       for (LogsTableRow row : queryResult.getTable().getRows()) {
           System.out.println(row.getRow()
               .stream()
               .map(LogsTableCell::getValueAsString)
               .collect(Collectors.joining(",")));
       }
       
      Parameters:
      resourceId - The resourceId where the query should be executed.
      query - The Kusto query to fetch the logs.
      timeInterval - The time period for which the logs should be looked up.
      Returns:
      The logs matching the query.
      Throws:
      NullPointerException - if resourceId or query is null.
    • queryResource

      public <T> List<T> queryResource(String resourceId, String query, QueryTimeInterval timeInterval, Class<T> type)
      Returns all the Azure Monitor logs matching the given query for an Azure resource.
      Type Parameters:
      T - The type the result of this query should be mapped to.
      Parameters:
      resourceId - The resourceId where the query should be executed.
      query - The Kusto query to fetch the logs.
      timeInterval - The time period for which the logs should be looked up.
      type - The type the result of this query should be mapped to.
      Returns:
      The logs matching the query as a list of objects of type T.
      Throws:
      NullPointerException - if resourceId or query is null.
    • queryResource

      public <T> List<T> queryResource(String resourceId, String query, QueryTimeInterval timeInterval, Class<T> type, LogsQueryOptions options)
      Returns all the Azure Monitor logs matching the given query for an Azure resource.
      Type Parameters:
      T - The type the result of this query should be mapped to.
      Parameters:
      resourceId - The resourceId where the query should be executed.
      query - The Kusto query to fetch the logs.
      timeInterval - The time period for which the logs should be looked up.
      type - The type the result of this query should be mapped to.
      options - The log query options to configure server timeout, set additional workspaces or enable statistics and rendering information in response.
      Returns:
      The logs matching the query as a list of objects of type T.
      Throws:
      NullPointerException - if resourceId or query is null.
    • queryResourceWithResponse

      public com.azure.core.http.rest.Response<LogsQueryResult> queryResourceWithResponse(String resourceId, String query, QueryTimeInterval timeInterval, LogsQueryOptions options, com.azure.core.util.Context context)
      Returns all the Azure Monitor logs matching the given query for an Azure resource.

      Query logs from the last 7 days and set the service timeout to 2 minutes

       Response<LogsQueryResult> queryResult = logsQueryClient.queryResourceWithResponse("{resource-id}",
           "{kusto-query}",
           QueryTimeInterval.LAST_7_DAYS,
           new LogsQueryOptions().setServerTimeout(Duration.ofMinutes(2)),
           Context.NONE);
      
       for (LogsTableRow row : queryResult.getValue().getTable().getRows()) {
           System.out.println(row.getRow()
               .stream()
               .map(LogsTableCell::getValueAsString)
               .collect(Collectors.joining(",")));
       }
       
      Parameters:
      resourceId - The resourceId where the query should be executed.
      query - The Kusto query to fetch the logs.
      timeInterval - The time period for which the logs should be looked up.
      options - The log query options to configure server timeout, set additional workspaces or enable statistics and rendering information in response.
      context - Additional context that is passed through the Http pipeline during the service call. If no additional context is required, pass Context.NONE instead.
      Returns:
      The logs matching the query including the HTTP response.
      Throws:
      NullPointerException - if resourceId or query is null.
    • queryResourceWithResponse

      public <T> com.azure.core.http.rest.Response<List<T>> queryResourceWithResponse(String resourceId, String query, QueryTimeInterval timeInterval, Class<T> type, LogsQueryOptions options, com.azure.core.util.Context context)
      Returns all the Azure Monitor logs matching the given query for an Azure resource.
      Type Parameters:
      T - The type the result of this query should be mapped to.
      Parameters:
      resourceId - The resourceId where the query should be executed.
      query - The Kusto query to fetch the logs.
      timeInterval - The time period for which the logs should be looked up.
      type - The type the result of this query should be mapped to.
      options - The log query options to configure server timeout, set additional workspaces or enable statistics and rendering information in response.
      context - Additional context that is passed through the Http pipeline during the service call. If no additional context is required, pass Context.NONE instead.
      Returns:
      The logs matching the query including the HTTP response.
      Throws:
      NullPointerException - if resourceId or query is null.