Skip to content

Commit

Permalink
Azure Monitor Ingestion: Upgrade to stable service version and update…
Browse files Browse the repository at this point in the history
… client APIs (Azure#33371)

* update api

* Update API, tests and samples

* Upgrade to latest stable API version

* async error handling sample
  • Loading branch information
srnagar authored Feb 13, 2023
1 parent 65cc538 commit 56a4cf5
Show file tree
Hide file tree
Showing 44 changed files with 1,662 additions and 755 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2348,6 +2348,14 @@
<Bug pattern="EI_EXPOSE_REP2"/>
</Match>

<!-- This is an internal type used to hold the request body which will not be mutated outside this class -->
<Match>
<Or>
<Class name="com.azure.monitor.ingestion.implementation.LogsIngestionRequest"/>
</Or>
<Bug pattern="EI_EXPOSE_REP2,EI_EXPOSE_REP"/>
</Match>

<!-- Machine generated classes for MAA service generate spotBugs errors -->
<Match>
<Or>
Expand Down
8 changes: 4 additions & 4 deletions sdk/monitor/azure-monitor-ingestion/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -118,8 +118,8 @@ LogsIngestionClient client = new LogsIngestionClientBuilder()
.buildClient();

List<Object> logs = getLogs();
UploadLogsResult result = client.upload("<data-collection-rule-id>", "<stream-name>", logs);
System.out.println("Logs upload result status " + result.getStatus());
client.upload("<data-collection-rule-id>", "<stream-name>", logs);
System.out.println("Logs uploaded successfully");
```

### Upload custom logs with max concurrency
Expand All @@ -139,9 +139,9 @@ LogsIngestionClient client = new LogsIngestionClientBuilder()
List<Object> logs = getLogs();
UploadLogsOptions uploadLogsOptions = new UploadLogsOptions()
.setMaxConcurrency(3);
UploadLogsResult result = client.upload("<data-collection-rule-id>", "<stream-name>", logs, uploadLogsOptions,
client.upload("<data-collection-rule-id>", "<stream-name>", logs, uploadLogsOptions,
Context.NONE);
System.out.println("Logs upload result status " + result.getStatus());
System.out.println("Logs uploaded successfully");
```

### Handle logs ingestion response
Expand Down
1 change: 1 addition & 0 deletions sdk/monitor/azure-monitor-ingestion/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
<![CDATA[[INFO] Any downloads listed may be third party software. Microsoft grants you no rights for third party software.]]></legal>
<javaModulesSurefireArgLine>
--add-exports com.azure.core/com.azure.core.implementation.jackson=ALL-UNNAMED
--add-exports com.azure.core/com.azure.core.implementation.util=ALL-UNNAMED
--add-opens com.azure.monitor.ingestion/com.azure.monitor.ingestion=ALL-UNNAMED
</javaModulesSurefireArgLine>
</properties>
Expand Down

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,6 @@
import com.azure.core.util.BinaryData;
import com.azure.core.util.Context;
import com.azure.monitor.ingestion.models.UploadLogsOptions;
import com.azure.monitor.ingestion.models.UploadLogsResult;

import java.util.List;

/**
* The synchronous client for uploading logs to Azure Monitor.
Expand Down Expand Up @@ -50,22 +47,21 @@ public final class LogsIngestionClient {
* <!-- src_embed com.azure.monitor.ingestion.LogsIngestionClient.upload -->
* <pre>
* List&lt;Object&gt; logs = getLogs&#40;&#41;;
* UploadLogsResult result = logsIngestionClient.upload&#40;&quot;&lt;data-collection-rule-id&gt;&quot;, &quot;&lt;stream-name&gt;&quot;, logs&#41;;
* System.out.println&#40;&quot;Logs upload result status &quot; + result.getStatus&#40;&#41;&#41;;
* logsIngestionClient.upload&#40;&quot;&lt;data-collection-rule-id&gt;&quot;, &quot;&lt;stream-name&gt;&quot;, logs&#41;;
* System.out.println&#40;&quot;Logs uploaded successfully&quot;&#41;;
* </pre>
* <!-- end com.azure.monitor.ingestion.LogsIngestionClient.upload -->
*
* @param ruleId the data collection rule id that is configured to collect and transform the logs.
* @param streamName the stream name configured in data collection rule that matches defines the structure of the
* logs sent in this request.
* @param logs the collection of logs to be uploaded.
* @return the result of the logs upload request.
* @throws NullPointerException if any of {@code ruleId}, {@code streamName} or {@code logs} are null.
* @throws IllegalArgumentException if {@code logs} is empty.
*/
@ServiceMethod(returns = ReturnType.SINGLE)
public UploadLogsResult upload(String ruleId, String streamName, List<Object> logs) {
return asyncClient.upload(ruleId, streamName, logs).block();
public void upload(String ruleId, String streamName, Iterable<Object> logs) {
asyncClient.upload(ruleId, streamName, logs).block();
}

/**
Expand All @@ -78,24 +74,23 @@ public UploadLogsResult upload(String ruleId, String streamName, List<Object> lo
* <pre>
* List&lt;Object&gt; logs = getLogs&#40;&#41;;
* UploadLogsOptions uploadLogsOptions = new UploadLogsOptions&#40;&#41;.setMaxConcurrency&#40;4&#41;;
* UploadLogsResult result = logsIngestionClient.upload&#40;&quot;&lt;data-collection-rule-id&gt;&quot;, &quot;&lt;stream-name&gt;&quot;, logs,
* logsIngestionClient.upload&#40;&quot;&lt;data-collection-rule-id&gt;&quot;, &quot;&lt;stream-name&gt;&quot;, logs,
* uploadLogsOptions, Context.NONE&#41;;
* System.out.println&#40;&quot;Logs upload result status &quot; + result.getStatus&#40;&#41;&#41;;
* System.out.println&#40;&quot;Logs uploaded successfully&quot;&#41;;
* </pre>
* <!-- end com.azure.monitor.ingestion.LogsIngestionClient.uploadWithConcurrency -->
* @param ruleId the data collection rule id that is configured to collect and transform the logs.
* @param streamName the stream name configured in data collection rule that matches defines the structure of the
* logs sent in this request.
* @param logs the collection of logs to be uploaded.
* @param options the options to configure the upload request.
* @return the result of the logs upload request.
* @throws NullPointerException if any of {@code ruleId}, {@code streamName} or {@code logs} are null.
* @throws IllegalArgumentException if {@code logs} is empty.
*/
@ServiceMethod(returns = ReturnType.SINGLE)
public UploadLogsResult upload(String ruleId, String streamName,
List<Object> logs, UploadLogsOptions options) {
return asyncClient.upload(ruleId, streamName, logs, options, Context.NONE).block();
public void upload(String ruleId, String streamName,
Iterable<Object> logs, UploadLogsOptions options) {
asyncClient.upload(ruleId, streamName, logs, options, Context.NONE).block();
}

/**
Expand All @@ -110,14 +105,13 @@ public UploadLogsResult upload(String ruleId, String streamName,
* @param options the options to configure the upload request.
* @param context additional context that is passed through the Http pipeline during the service call. If no
* additional context is required, pass {@link Context#NONE} instead.
* @return the result of the logs upload request.
* @throws NullPointerException if any of {@code ruleId}, {@code streamName} or {@code logs} are null.
* @throws IllegalArgumentException if {@code logs} is empty.
*/
@ServiceMethod(returns = ReturnType.SINGLE)
public UploadLogsResult upload(String ruleId, String streamName,
List<Object> logs, UploadLogsOptions options, Context context) {
return asyncClient.upload(ruleId, streamName, logs, options, context).block();
public void upload(String ruleId, String streamName,
Iterable<Object> logs, UploadLogsOptions options, Context context) {
asyncClient.upload(ruleId, streamName, logs, options, context).block();
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@
*/
public enum LogsIngestionServiceVersion implements ServiceVersion {
/**
* Service version {@code v1}.
* Service version {@code 2023-01-01}.
*/
V2021_11_01_PREVIEW("2021-11-01-preview");
V2023_01_01("2023-01-01");

String version;

Expand All @@ -29,7 +29,7 @@ public enum LogsIngestionServiceVersion implements ServiceVersion {
* @return The latest supported service version by this library.
*/
public static LogsIngestionServiceVersion getLatest() {
return V2021_11_01_PREVIEW;
return V2023_01_01;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ public final class IngestionUsingDataCollectionRulesClientBuilder

@Generated private static final String SDK_VERSION = "version";

@Generated private static final String[] DEFAULT_SCOPES = new String[] {"https://monitor.azure.com//.default"};

@Generated
private static final Map<String, String> PROPERTIES = CoreUtils.getProperties("azure-monitor-ingestion.properties");

Expand Down Expand Up @@ -257,7 +259,7 @@ private HttpPipeline createHttpPipeline() {
policies.add(new AddDatePolicy());
policies.add(new CookiePolicy());
if (tokenCredential != null) {
policies.add(new BearerTokenAuthenticationPolicy(tokenCredential, "https://monitor.azure.com//.default"));
policies.add(new BearerTokenAuthenticationPolicy(tokenCredential, DEFAULT_SCOPES));
}
policies.addAll(
this.pipelinePolicies.stream()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@

/** Service version of IngestionUsingDataCollectionRulesClient. */
public enum IngestionUsingDataCollectionRulesServiceVersion implements ServiceVersion {
/** Enum value 2021-11-01-preview. */
V2021_11_01_PREVIEW("2021-11-01-preview");
/** Enum value 2023-01-01. */
V2023_01_01("2023-01-01");

private final String version;

Expand All @@ -29,6 +29,6 @@ public String getVersion() {
* @return The latest {@link IngestionUsingDataCollectionRulesServiceVersion}.
*/
public static IngestionUsingDataCollectionRulesServiceVersion getLatest() {
return V2021_11_01_PREVIEW;
return V2023_01_01;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

package com.azure.monitor.ingestion.implementation;

import java.util.List;

public class LogsIngestionRequest {

private final List<Object> logs;
private final byte[] requestBody;

public LogsIngestionRequest(List<Object> logs, byte[] requestBody) {
this.logs = logs;
this.requestBody = requestBody;
}

public List<Object> getLogs() {
return logs;
}

public byte[] getRequestBody() {
return requestBody;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,40 +4,28 @@
package com.azure.monitor.ingestion.implementation;

import com.azure.core.annotation.Immutable;
import com.azure.core.exception.HttpResponseException;
import com.azure.core.models.ResponseError;
import com.azure.monitor.ingestion.models.UploadLogsStatus;

/**
* The intermediate response holder for converting exceptions to {@link ResponseError} instances.
*/
@Immutable
public final class UploadLogsResponseHolder {
private UploadLogsStatus status;
private ResponseError responseError;

/**
* Creates and instance of {@link UploadLogsResponseHolder}.
* @param status The status of the logs upload request.
* @param responseError The error details of the upload request, if any.
*/
public UploadLogsResponseHolder(UploadLogsStatus status, ResponseError responseError) {
this.status = status;
this.responseError = responseError;
private final HttpResponseException exception;
private final LogsIngestionRequest request;

public UploadLogsResponseHolder(LogsIngestionRequest request, HttpResponseException ex) {
this.request = request;
this.exception = ex;
}

/**
* Returns the status of the logs upload request.
* @return the status of the logs upload request.
*/
public UploadLogsStatus getStatus() {
return status;
public LogsIngestionRequest getRequest() {
return request;
}

/**
* Returns the error details of the upload request, if any.
* @return the error details of the upload request, if any.
*/
public ResponseError getResponseError() {
return responseError;
public HttpResponseException getException() {
return exception;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
package com.azure.monitor.ingestion.models;

import com.azure.core.annotation.Immutable;
import com.azure.core.models.ResponseError;
import com.azure.core.exception.HttpResponseException;

import java.util.List;

Expand All @@ -14,25 +14,25 @@
*/
@Immutable
public final class UploadLogsError {
private final ResponseError responseError;
private final HttpResponseException responseException;
private final List<Object> failedLogs;

/**
* Creates an instance of error.
* @param error the response error containing the error details returned by the service.
* @param responseException the response exception containing the error details returned by the service.
* @param failedLogs the logs that failed to upload.
*/
public UploadLogsError(ResponseError error, List<Object> failedLogs) {
this.responseError = error;
public UploadLogsError(HttpResponseException responseException, List<Object> failedLogs) {
this.responseException = responseException;
this.failedLogs = failedLogs;
}

/**
* Returns the response error containing the error details returned by the service.
* @return the response error containing the error details returned by the service.
*/
public ResponseError getResponseError() {
return responseError;
public HttpResponseException getResponseException() {
return responseException;
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

package com.azure.monitor.ingestion.models;

import com.azure.core.exception.AzureException;
import com.azure.core.exception.HttpResponseException;

import java.util.List;

/**
* An aggregate exception containing all inner exceptions that were caused from uploading logs.
*/
public class UploadLogsException extends AzureException {
/**
* Total count of all logs that were not uploaded to Azure Monitor due to errors.
*/
private final long failedLogsCount;
/**
* A list of all HTTP errors that occured when uploading logs to Azure Monitor service.
*/
private final List<HttpResponseException> uploadLogsErrors;

/**
* Creates an instance of {@link UploadLogsException}.
* @param uploadLogsErrors list of all HTTP response exceptions.
* @param failedLogsCount the total number of logs that failed to upload.
*/
public UploadLogsException(List<HttpResponseException> uploadLogsErrors, long failedLogsCount) {
this.uploadLogsErrors = uploadLogsErrors;
this.failedLogsCount = failedLogsCount;
}

/**
* Returns the list of all HTTP response exceptions.
* @return The list of all errors.
*/
public List<HttpResponseException> getUploadLogsErrors() {
return this.uploadLogsErrors;
}

/**
* Returns the total number for logs that failed to upload.
* @return the total number of logs that failed to upload.
*/
public long getFailedLogsCount() {
return failedLogsCount;
}
}
Loading

0 comments on commit 56a4cf5

Please sign in to comment.