Class LogsIngestionClient

java.lang.Object
com.azure.monitor.ingestion.LogsIngestionClient
All Implemented Interfaces:
AutoCloseable

public final class LogsIngestionClient extends Object implements AutoCloseable

This class provides a synchronous client for uploading custom logs to an Azure Monitor Log Analytics workspace. This client encapsulates REST API calls, used to send data to a Log Analytics workspace, into a set of synchronous operations.

Getting Started

To create an instance of the LogsIngestionClient, use the LogsIngestionClientBuilder and configure the various options provided by the builder to customize the client as per your requirements. There are two required properties that should be set to build a client:

  1. endpoint - The data collection endpoint. See endpoint method for more details.
  2. credential - The AAD authentication credential that has the "Monitoring Metrics Publisher" role assigned to it. Azure Identity provides a variety of AAD credential types that can be used. See credential method for more details.

Instantiating a synchronous Logs ingestion client

LogsIngestionClient logsIngestionClient = new LogsIngestionClientBuilder()
        .credential(tokenCredential)
        .endpoint("<data-collection-endpoint>")
        .buildClient();

Client Usage

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

See Also:
  • Method Details

    • upload

      public void upload(String ruleId, String streamName, Iterable<Object> logs)
      Uploads logs to Azure Monitor with specified data collection rule id and stream name. The input logs may be too large to be sent as a single request to the Azure Monitor service. In such cases, this method will split the input logs into multiple smaller requests before sending to the service. This method will block until all the logs are uploaded or an error occurs.

      Each log in the input collection must be a valid JSON object. The JSON object should match the schema defined by the stream name. The stream's schema can be found in the Azure portal.

      Upload logs to Azure Monitor

      List<Object> logs = getLogs();
      logsIngestionClient.upload("<data-collection-rule-id>", "<stream-name>", logs);
      System.out.println("Logs uploaded successfully");
      
      Parameters:
      ruleId - the data collection rule id that is configured to collect and transform the logs.
      streamName - the stream name configured in data collection rule that matches defines the structure of the logs sent in this request.
      logs - the collection of logs to be uploaded.
      Throws:
      NullPointerException - if any of ruleId, streamName or logs are null.
      IllegalArgumentException - if logs is empty.
    • upload

      public void upload(String ruleId, String streamName, Iterable<Object> logs, LogsUploadOptions options)
      Uploads logs to Azure Monitor with specified data collection rule id and stream name. The input logs may be too large to be sent as a single request to the Azure Monitor service. In such cases, this method will split the input logs into multiple smaller requests before sending to the service. This method will block until all the logs are uploaded or an error occurs. If an error handler is set, then the service errors are surfaced to the error handler and this method won't throw an exception.

      Each log in the input collection must be a valid JSON object. The JSON object should match the schema defined by the stream name. The stream's schema can be found in the Azure portal.

      Upload logs to Azure Monitor

      List<Object> logs = getLogs();
      LogsUploadOptions logsUploadOptions = new LogsUploadOptions().setMaxConcurrency(4);
      logsIngestionClient.upload("<data-collection-rule-id>", "<stream-name>", logs,
              logsUploadOptions, Context.NONE);
      System.out.println("Logs uploaded successfully");
      
      Parameters:
      ruleId - the data collection rule id that is configured to collect and transform the logs.
      streamName - the stream name configured in data collection rule that matches defines the structure of the logs sent in this request.
      logs - the collection of logs to be uploaded.
      options - the options to configure the upload request.
      Throws:
      NullPointerException - if any of ruleId, streamName or logs are null.
      IllegalArgumentException - if logs is empty.
    • upload

      public void upload(String ruleId, String streamName, Iterable<Object> logs, LogsUploadOptions options, com.azure.core.util.Context context)
      Uploads logs to Azure Monitor with specified data collection rule id and stream name. The input logs may be too large to be sent as a single request to the Azure Monitor service. In such cases, this method will split the input logs into multiple smaller requests before sending to the service. This method will block until all the logs are uploaded or an error occurs. If an error handler is set, then the service errors are surfaced to the error handler and this method won't throw an exception.

      Each log in the input collection must be a valid JSON object. The JSON object should match the schema defined by the stream name. The stream's schema can be found in the Azure portal.

      Parameters:
      ruleId - the data collection rule id that is configured to collect and transform the logs.
      streamName - the stream name configured in data collection rule that matches defines the structure of the logs sent in this request.
      logs - the collection of logs to be uploaded.
      options - the options to configure the upload request.
      context - additional context that is passed through the Http pipeline during the service call. If no additional context is required, pass Context.NONE instead.
      Throws:
      NullPointerException - if any of ruleId, streamName or logs are null.
      IllegalArgumentException - if logs is empty.
    • uploadWithResponse

      public com.azure.core.http.rest.Response<Void> uploadWithResponse(String ruleId, String streamName, com.azure.core.util.BinaryData logs, com.azure.core.http.rest.RequestOptions requestOptions)
      This method is used to upload logs to Azure Monitor Log Analytics with specified data collection rule id and stream name. This upload method provides a more granular control of the HTTP request sent to the service. Use RequestOptions to configure the HTTP request.

      The input logs should be a JSON array with each element in the array matching the schema defined by the stream name. The stream's schema can be found in the Azure portal. This content will be gzipped before sending to the service. If the content is already gzipped, then set the Content-Encoding header to gzip using requestOptions and pass the content as is.

      Header Parameters

      Header Parameters
      NameTypeRequiredDescription
      Content-EncodingStringNogzip
      x-ms-client-request-idStringNoClient request Id

      Request Body Schema

      [
          Object
      ]
      
      Parameters:
      ruleId - The immutable Id of the Data Collection Rule resource.
      streamName - The streamDeclaration name as defined in the Data Collection Rule.
      logs - An array of objects matching the schema defined by the provided stream.
      requestOptions - The options to configure the HTTP request before HTTP client sends it.
      Returns:
      the Response.
      Throws:
      com.azure.core.exception.HttpResponseException - thrown if the request is rejected by server.
      com.azure.core.exception.ClientAuthenticationException - thrown if the request is rejected by server on status code 401.
      com.azure.core.exception.ResourceNotFoundException - thrown if the request is rejected by server on status code 404.
      com.azure.core.exception.ResourceModifiedException - thrown if the request is rejected by server on status code 409.
    • close

      public void close()
      Specified by:
      close in interface AutoCloseable