Skip to content

Commit

Permalink
API consistency review updates (#24130)
Browse files Browse the repository at this point in the history
* API consistency review updates

* update type names

* revert experimental changes

* Fix codesnippet name
  • Loading branch information
srnagar authored Sep 16, 2021
1 parent ade6e91 commit 990d528
Show file tree
Hide file tree
Showing 27 changed files with 463 additions and 139 deletions.
68 changes: 39 additions & 29 deletions sdk/monitor/azure-monitor-query/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,16 +46,26 @@ An authenticated client is required to query Logs or Metrics. The library includ

<!-- embedme ./src/samples/java/com/azure/monitor/query/ReadmeSamples.java#L39-L41 -->
```java
LogsQueryClient logsQueryClient = new LogsQueryClientBuilder()
public void createLogsClients() {
LogsQueryClient logsQueryClient = new LogsQueryClientBuilder()
.credential(new DefaultAzureCredentialBuilder().build())
```

### Create Logs query async client

<!-- embedme ./src/samples/java/com/azure/monitor/query/ReadmeSamples.java#L43-L45 -->
```java

LogsQueryAsyncClient logsQueryAsyncClient = new LogsQueryClientBuilder()
.credential(new DefaultAzureCredentialBuilder().build())
.buildClient();
```
### Create Metrics query client

<!-- embedme ./src/samples/java/com/azure/monitor/query/ReadmeSamples.java#L52-L54 -->
```java
MetricsQueryClient metricsQueryClient = new MetricsQueryClientBuilder()
.credential(new DefaultAzureCredentialBuilder().build())
.buildClient();
public void createMetricsClients() {
MetricsQueryClient metricsQueryClient = new MetricsQueryClientBuilder()
.credential(new DefaultAzureCredentialBuilder().build())
```

#### Asynchronous clients
Expand All @@ -69,9 +79,9 @@ LogsQueryAsyncClient logsQueryAsyncClient = new LogsQueryClientBuilder()

<!-- embedme ./src/samples/java/com/azure/monitor/query/ReadmeSamples.java#L56-L58 -->
```java

MetricsQueryAsyncClient metricsQueryAsyncClient = new MetricsQueryClientBuilder()
.credential(new DefaultAzureCredentialBuilder().build())
.buildAsyncClient();
```

### Execute the query
Expand Down Expand Up @@ -118,48 +128,48 @@ Each set of metric values is a time series with the following characteristics:

<!-- embedme ./src/samples/java/com/azure/monitor/query/ReadmeSamples.java#L65-L74 -->
```java
ic void queryLogs() {
LogsQueryClient logsQueryClient = new LogsQueryClientBuilder()
.credential(new DefaultAzureCredentialBuilder().build())
.buildClient();

LogsQueryResult queryResults = logsQueryClient.query("{workspace-id}", "{kusto-query}",
new TimeInterval(Duration.ofDays(2)));
new QueryTimeInterval(Duration.ofDays(2)));

for (LogsTableRow row : queryResults.getTable().getRows()) {
System.out.println(row.getColumnValue("OperationName") + " " + row.getColumnValue("ResourceGroup"));
}
```

#### Map logs query results to a model

<!-- embedme ./src/samples/java/com/azure/monitor/query/ReadmeSamples.java#L80-L91 -->
```java
public class CustomLogModel {
private String resourceGroup;
private String operationName;

public String getResourceGroup() {
return resourceGroup;
}
ic class CustomLogModel {
private String resourceGroup;
private String operationName;

public String getOperationName() {
return operationName;
}
public String getResourceGroup() {
return resourceGroup;
}

public String getOperationName() {
return operationName;
}
```

<!-- embedme ./src/samples/java/com/azure/monitor/query/ReadmeSamples.java#L97-L106 -->
```java
ic void queryLogsAsModel() {
LogsQueryClient logsQueryClient = new LogsQueryClientBuilder()
.credential(new DefaultAzureCredentialBuilder().build())
.buildClient();

List<CustomLogModel> customLogModels = logsQueryClient.query("{workspace-id}", "{kusto-query}",
new TimeInterval(Duration.ofDays(2)), CustomLogModel.class);
new QueryTimeInterval(Duration.ofDays(2)), CustomLogModel.class);

for (CustomLogModel customLogModel : customLogModels) {
System.out.println(customLogModel.getOperationName() + " " + customLogModel.getResourceGroup());
}
```

#### Handle logs query response
Expand Down Expand Up @@ -187,14 +197,15 @@ LogsQueryResult / LogsBatchQueryResult
<!-- embedme ./src/samples/java/com/azure/monitor/query/ReadmeSamples.java#L113-L138 -->
```java
ic void queryBatch() {
LogsQueryClient logsQueryClient = new LogsQueryClientBuilder()
.credential(new DefaultAzureCredentialBuilder().build())
.buildClient();
LogsBatchQuery logsBatchQuery = new LogsBatchQuery();
String query1 = logsBatchQuery.addQuery("{workspace-id}", "{query-1}", new TimeInterval(Duration.ofDays(2)));
String query2 = logsBatchQuery.addQuery("{workspace-id}", "{query-2}", new TimeInterval(Duration.ofDays(30)));
String query3 = logsBatchQuery.addQuery("{workspace-id}", "{query-3}", new TimeInterval(Duration.ofDays(10)));
String query1 = logsBatchQuery.addQuery("{workspace-id}", "{query-1}", new QueryTimeInterval(Duration.ofDays(2)));
String query2 = logsBatchQuery.addQuery("{workspace-id}", "{query-2}", new QueryTimeInterval(Duration.ofDays(30)));
String query3 = logsBatchQuery.addQuery("{workspace-id}", "{query-3}", new QueryTimeInterval(Duration.ofDays(10)));
LogsBatchQueryResultCollection batchResults = logsQueryClient
.queryBatchWithResponse(logsBatchQuery, Context.NONE).getValue();
Expand All @@ -210,9 +221,8 @@ for (CustomLogModel customLogModel : customLogModels) {
}
LogsBatchQueryResult query3Result = batchResults.getResult(query3);
if (query3Result.hasFailed()) {
if (query3Result.getQueryResultStatus() == LogsQueryResultStatus.FAILURE) {
System.out.println(query3Result.getError().getMessage());
}
```
### Advanced logs query scenarios
Expand All @@ -221,6 +231,7 @@ if (query3Result.hasFailed()) {
<!-- embedme ./src/samples/java/com/azure/monitor/query/ReadmeSamples.java#L146-L155 -->
```java
ic void getLogsWithServerTimeout() {
LogsQueryClient logsQueryClient = new LogsQueryClientBuilder()
.credential(new DefaultAzureCredentialBuilder().build())
.buildClient();
Expand All @@ -230,7 +241,6 @@ LogsQueryOptions options = new LogsQueryOptions()
.setServerTimeout(Duration.ofMinutes(10));
Response<LogsQueryResult> response = logsQueryClient.queryWithResponse("{workspace-id}",
"{kusto-query}", new TimeInterval(Duration.ofDays(2)), options, Context.NONE);
```
#### Query multiple workspaces
Expand All @@ -244,15 +254,15 @@ to include this column.
<!-- embedme ./src/samples/java/com/azure/monitor/query/ReadmeSamples.java#L162-L170 -->
```java
ic void getLogsQueryFromMultipleWorkspaces() {
LogsQueryClient logsQueryClient = new LogsQueryClientBuilder()
.credential(new DefaultAzureCredentialBuilder().build())
.buildClient();
Response<LogsQueryResult> response = logsQueryClient.queryWithResponse("{workspace-id}", "{kusto-query}",
new TimeInterval(Duration.ofDays(2)), new LogsQueryOptions()
new QueryTimeInterval(Duration.ofDays(2)), new LogsQueryOptions()
.setAdditionalWorkspaces(Arrays.asList("{additional-workspace-identifiers}")),
Context.NONE);
LogsQueryResult result = response.getValue();
```
### Metrics query
Expand All @@ -265,6 +275,7 @@ A resource ID, as denoted by the `{resource-id}` placeholder in the sample below

<!-- embedme ./src/samples/java/com/azure/monitor/query/ReadmeSamples.java#L178-L193 -->
```java
ic void getMetrics() {
MetricsQueryClient metricsQueryClient = new MetricsQueryClientBuilder()
.credential(new DefaultAzureCredentialBuilder().build())
.buildClient();
Expand All @@ -280,7 +291,6 @@ for (MetricResult metric : metricsQueryResult.getMetrics()) {
System.out.println(metricValue.getTimeStamp() + " " + metricValue.getTotal());
}
}
}
```

#### Handle metrics query response
Expand Down Expand Up @@ -314,6 +324,7 @@ MetricsQueryResult

<!-- embedme ./src/samples/java/com/azure/monitor/query/ReadmeSamples.java#L200-L221 -->
```java
ic void getMetricsWithOptions() {
MetricsQueryClient metricsQueryClient = new MetricsQueryClientBuilder()
.credential(new DefaultAzureCredentialBuilder().build())
.buildClient();
Expand All @@ -335,7 +346,6 @@ for (MetricResult metric : metricsQueryResult.getMetrics()) {
System.out.println(metricValue.getTimeStamp() + " " + metricValue.getTotal());
}
}
}
```

## Troubleshooting
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
import com.azure.core.annotation.ServiceClient;
import com.azure.core.annotation.ServiceMethod;
import com.azure.core.exception.HttpResponseException;
import com.azure.core.exception.ServiceResponseException;
import com.azure.core.experimental.models.HttpResponseError;
import com.azure.core.experimental.models.TimeInterval;
import com.azure.core.http.rest.Response;
import com.azure.core.http.rest.SimpleResponse;
import com.azure.core.util.BinaryData;
Expand All @@ -31,11 +31,14 @@
import com.azure.monitor.query.models.LogsBatchQueryResultCollection;
import com.azure.monitor.query.models.LogsQueryOptions;
import com.azure.monitor.query.models.LogsQueryResult;
import com.azure.monitor.query.models.LogsQueryResultStatus;
import com.azure.monitor.query.models.LogsTable;
import com.azure.monitor.query.models.LogsTableCell;
import com.azure.monitor.query.models.LogsTableColumn;
import com.azure.monitor.query.models.LogsTableRow;
import com.azure.monitor.query.models.QueryTimeInterval;
import reactor.core.publisher.Mono;
import reactor.core.publisher.SynchronousSink;

import java.time.Duration;
import java.util.ArrayList;
Expand Down Expand Up @@ -69,15 +72,15 @@ public final class LogsQueryAsyncClient {
* Returns all the Azure Monitor logs matching the given query in the specified workspaceId.
*
* <p><strong>Query logs from the last 24 hours</strong></p>
* {@codesnippet com.azure.monitor.query.LogsQueryAsyncClient.query#String-String-TimeInterval}
* {@codesnippet com.azure.monitor.query.LogsQueryAsyncClient.query#String-String-QueryTimeInterval}
*
* @param workspaceId The workspaceId where the query should be executed.
* @param query The Kusto query to fetch the logs.
* @param timeInterval The time period for which the logs should be looked up.
* @return The logs matching the query.
*/
@ServiceMethod(returns = ReturnType.SINGLE)
public Mono<LogsQueryResult> query(String workspaceId, String query, TimeInterval timeInterval) {
public Mono<LogsQueryResult> query(String workspaceId, String query, QueryTimeInterval timeInterval) {
return queryWithResponse(workspaceId, query, timeInterval, new LogsQueryOptions())
.map(Response::getValue);
}
Expand All @@ -92,7 +95,7 @@ public Mono<LogsQueryResult> query(String workspaceId, String query, TimeInterva
* @return The logs matching the query as a list of objects of type T.
*/
@ServiceMethod(returns = ReturnType.SINGLE)
public <T> Mono<List<T>> query(String workspaceId, String query, TimeInterval timeInterval, Class<T> type) {
public <T> Mono<List<T>> query(String workspaceId, String query, QueryTimeInterval timeInterval, Class<T> type) {
return query(workspaceId, query, timeInterval)
.map(result -> LogsQueryHelper.toObject(result.getTable(), type));
}
Expand All @@ -108,7 +111,7 @@ public <T> Mono<List<T>> query(String workspaceId, String query, TimeInterval ti
* @return The logs matching the query as a list of objects of type T.
*/
@ServiceMethod(returns = ReturnType.SINGLE)
public <T> Mono<List<T>> query(String workspaceId, String query, TimeInterval timeInterval,
public <T> Mono<List<T>> query(String workspaceId, String query, QueryTimeInterval timeInterval,
Class<T> type, LogsQueryOptions options) {
return queryWithResponse(workspaceId, query, timeInterval, options, Context.NONE)
.map(response -> LogsQueryHelper.toObject(response.getValue().getTable(), type));
Expand All @@ -119,7 +122,7 @@ public <T> Mono<List<T>> query(String workspaceId, String query, TimeInterval ti
*
* <p><strong>Query logs from the last 7 days and set the service timeout to 2 minutes</strong></p>
*
* {@codesnippet com.azure.monitor.query.LogsQueryAsyncClient.queryWithResponse#String-String-TimeInterval-LogsQueryOptions}
* {@codesnippet com.azure.monitor.query.LogsQueryAsyncClient.queryWithResponse#String-String-QueryTimeInterval-LogsQueryOptions}
*
* @param workspaceId The workspaceId where the query should be executed.
* @param query The Kusto query to fetch the logs.
Expand All @@ -130,7 +133,7 @@ public <T> Mono<List<T>> query(String workspaceId, String query, TimeInterval ti
*/
@ServiceMethod(returns = ReturnType.SINGLE)
public Mono<Response<LogsQueryResult>> queryWithResponse(String workspaceId, String query,
TimeInterval timeInterval, LogsQueryOptions options) {
QueryTimeInterval timeInterval, LogsQueryOptions options) {
return withContext(context -> queryWithResponse(workspaceId, query, timeInterval, options, context));
}

Expand All @@ -147,7 +150,7 @@ public Mono<Response<LogsQueryResult>> queryWithResponse(String workspaceId, Str
* @return The logs matching the query including the HTTP response.
*/
@ServiceMethod(returns = ReturnType.SINGLE)
public <T> Mono<Response<List<T>>> queryWithResponse(String workspaceId, String query, TimeInterval timeInterval,
public <T> Mono<Response<List<T>>> queryWithResponse(String workspaceId, String query, QueryTimeInterval timeInterval,
Class<T> type, LogsQueryOptions options) {
return queryWithResponse(workspaceId, query, timeInterval, options)
.map(response -> new SimpleResponse<>(response.getRequest(),
Expand All @@ -164,7 +167,7 @@ public <T> Mono<Response<List<T>>> queryWithResponse(String workspaceId, String
* @return A collection of query results corresponding to the input batch of queries.
*/
Mono<LogsBatchQueryResultCollection> queryBatch(String workspaceId, List<String> queries,
TimeInterval timeInterval) {
QueryTimeInterval timeInterval) {
LogsBatchQuery logsBatchQuery = new LogsBatchQuery();
queries.forEach(query -> logsBatchQuery.addQuery(workspaceId, query, timeInterval));
return queryBatchWithResponse(logsBatchQuery).map(Response::getValue);
Expand Down Expand Up @@ -263,14 +266,14 @@ private HttpResponseError mapLogsQueryError(ErrorInfo errors) {
return null;
}

Mono<Response<LogsQueryResult>> queryWithResponse(String workspaceId, String query, TimeInterval timeInterval,
Mono<Response<LogsQueryResult>> queryWithResponse(String workspaceId, String query, QueryTimeInterval timeInterval,
LogsQueryOptions options, Context context) {
String preferHeader = LogsQueryHelper.buildPreferHeaderString(options);
context = updateContext(options.getServerTimeout(), context);

QueryBody queryBody = new QueryBody(query);
if (timeInterval != null) {
queryBody.setTimespan(timeInterval.toIso8601Format());
queryBody.setTimespan(LogsQueryHelper.toIso8601Format(timeInterval));
}
queryBody.setWorkspaces(getAllWorkspaces(options));
return innerClient
Expand All @@ -288,7 +291,16 @@ Mono<Response<LogsQueryResult>> queryWithResponse(String workspaceId, String que
}
return ex;
})
.map(this::convertToLogQueryResult);
.map(this::convertToLogQueryResult)
.handle((Response<LogsQueryResult> response, SynchronousSink<Response<LogsQueryResult>> sink) -> {
if (response.getValue().getQueryResultStatus() == LogsQueryResultStatus.PARTIAL_FAILURE) {
sink.error(new ServiceResponseException("Query execution returned partial errors. To "
+ "disable exceptions on partial errors, set disableExceptionOnPartialErrors in "
+ "LogsQueryOptions to true."));
} else {
sink.next(response);
}
});
}

private Response<LogsQueryResult> convertToLogQueryResult(Response<QueryResults> response) {
Expand Down
Loading

0 comments on commit 990d528

Please sign in to comment.