Skip to content

Commit 990d528

Browse files
authored
API consistency review updates (Azure#24130)
* API consistency review updates * update type names * revert experimental changes * Fix codesnippet name
1 parent ade6e91 commit 990d528

27 files changed

+463
-139
lines changed

sdk/monitor/azure-monitor-query/README.md

Lines changed: 39 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -46,16 +46,26 @@ An authenticated client is required to query Logs or Metrics. The library includ
4646

4747
<!-- embedme ./src/samples/java/com/azure/monitor/query/ReadmeSamples.java#L39-L41 -->
4848
```java
49-
LogsQueryClient logsQueryClient = new LogsQueryClientBuilder()
49+
public void createLogsClients() {
50+
LogsQueryClient logsQueryClient = new LogsQueryClientBuilder()
51+
.credential(new DefaultAzureCredentialBuilder().build())
52+
```
53+
54+
### Create Logs query async client
55+
56+
<!-- embedme ./src/samples/java/com/azure/monitor/query/ReadmeSamples.java#L43-L45 -->
57+
```java
58+
59+
LogsQueryAsyncClient logsQueryAsyncClient = new LogsQueryClientBuilder()
5060
.credential(new DefaultAzureCredentialBuilder().build())
51-
.buildClient();
5261
```
62+
### Create Metrics query client
5363

5464
<!-- embedme ./src/samples/java/com/azure/monitor/query/ReadmeSamples.java#L52-L54 -->
5565
```java
56-
MetricsQueryClient metricsQueryClient = new MetricsQueryClientBuilder()
57-
.credential(new DefaultAzureCredentialBuilder().build())
58-
.buildClient();
66+
public void createMetricsClients() {
67+
MetricsQueryClient metricsQueryClient = new MetricsQueryClientBuilder()
68+
.credential(new DefaultAzureCredentialBuilder().build())
5969
```
6070

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

7080
<!-- embedme ./src/samples/java/com/azure/monitor/query/ReadmeSamples.java#L56-L58 -->
7181
```java
82+
7283
MetricsQueryAsyncClient metricsQueryAsyncClient = new MetricsQueryClientBuilder()
7384
.credential(new DefaultAzureCredentialBuilder().build())
74-
.buildAsyncClient();
7585
```
7686

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

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

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

128139
for (LogsTableRow row : queryResults.getTable().getRows()) {
129140
System.out.println(row.getColumnValue("OperationName") + " " + row.getColumnValue("ResourceGroup"));
130-
}
131141
```
132142

133143
#### Map logs query results to a model
134144

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

141-
public String getResourceGroup() {
142-
return resourceGroup;
143-
}
148+
ic class CustomLogModel {
149+
private String resourceGroup;
150+
private String operationName;
144151

145-
public String getOperationName() {
146-
return operationName;
147-
}
152+
public String getResourceGroup() {
153+
return resourceGroup;
154+
}
155+
156+
public String getOperationName() {
157+
return operationName;
148158
}
149159
```
150160

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

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

160171
for (CustomLogModel customLogModel : customLogModels) {
161172
System.out.println(customLogModel.getOperationName() + " " + customLogModel.getResourceGroup());
162-
}
163173
```
164174

165175
#### Handle logs query response
@@ -187,14 +197,15 @@ LogsQueryResult / LogsBatchQueryResult
187197
188198
<!-- embedme ./src/samples/java/com/azure/monitor/query/ReadmeSamples.java#L113-L138 -->
189199
```java
200+
ic void queryBatch() {
190201
LogsQueryClient logsQueryClient = new LogsQueryClientBuilder()
191202
.credential(new DefaultAzureCredentialBuilder().build())
192203
.buildClient();
193204
194205
LogsBatchQuery logsBatchQuery = new LogsBatchQuery();
195-
String query1 = logsBatchQuery.addQuery("{workspace-id}", "{query-1}", new TimeInterval(Duration.ofDays(2)));
196-
String query2 = logsBatchQuery.addQuery("{workspace-id}", "{query-2}", new TimeInterval(Duration.ofDays(30)));
197-
String query3 = logsBatchQuery.addQuery("{workspace-id}", "{query-3}", new TimeInterval(Duration.ofDays(10)));
206+
String query1 = logsBatchQuery.addQuery("{workspace-id}", "{query-1}", new QueryTimeInterval(Duration.ofDays(2)));
207+
String query2 = logsBatchQuery.addQuery("{workspace-id}", "{query-2}", new QueryTimeInterval(Duration.ofDays(30)));
208+
String query3 = logsBatchQuery.addQuery("{workspace-id}", "{query-3}", new QueryTimeInterval(Duration.ofDays(10)));
198209
199210
LogsBatchQueryResultCollection batchResults = logsQueryClient
200211
.queryBatchWithResponse(logsBatchQuery, Context.NONE).getValue();
@@ -210,9 +221,8 @@ for (CustomLogModel customLogModel : customLogModels) {
210221
}
211222
212223
LogsBatchQueryResult query3Result = batchResults.getResult(query3);
213-
if (query3Result.hasFailed()) {
224+
if (query3Result.getQueryResultStatus() == LogsQueryResultStatus.FAILURE) {
214225
System.out.println(query3Result.getError().getMessage());
215-
}
216226
```
217227
218228
### Advanced logs query scenarios
@@ -221,6 +231,7 @@ if (query3Result.hasFailed()) {
221231
222232
<!-- embedme ./src/samples/java/com/azure/monitor/query/ReadmeSamples.java#L146-L155 -->
223233
```java
234+
ic void getLogsWithServerTimeout() {
224235
LogsQueryClient logsQueryClient = new LogsQueryClientBuilder()
225236
.credential(new DefaultAzureCredentialBuilder().build())
226237
.buildClient();
@@ -230,7 +241,6 @@ LogsQueryOptions options = new LogsQueryOptions()
230241
.setServerTimeout(Duration.ofMinutes(10));
231242
232243
Response<LogsQueryResult> response = logsQueryClient.queryWithResponse("{workspace-id}",
233-
"{kusto-query}", new TimeInterval(Duration.ofDays(2)), options, Context.NONE);
234244
```
235245
236246
#### Query multiple workspaces
@@ -244,15 +254,15 @@ to include this column.
244254
245255
<!-- embedme ./src/samples/java/com/azure/monitor/query/ReadmeSamples.java#L162-L170 -->
246256
```java
257+
ic void getLogsQueryFromMultipleWorkspaces() {
247258
LogsQueryClient logsQueryClient = new LogsQueryClientBuilder()
248259
.credential(new DefaultAzureCredentialBuilder().build())
249260
.buildClient();
250261
251262
Response<LogsQueryResult> response = logsQueryClient.queryWithResponse("{workspace-id}", "{kusto-query}",
252-
new TimeInterval(Duration.ofDays(2)), new LogsQueryOptions()
263+
new QueryTimeInterval(Duration.ofDays(2)), new LogsQueryOptions()
253264
.setAdditionalWorkspaces(Arrays.asList("{additional-workspace-identifiers}")),
254265
Context.NONE);
255-
LogsQueryResult result = response.getValue();
256266
```
257267
258268
### Metrics query
@@ -265,6 +275,7 @@ A resource ID, as denoted by the `{resource-id}` placeholder in the sample below
265275

266276
<!-- embedme ./src/samples/java/com/azure/monitor/query/ReadmeSamples.java#L178-L193 -->
267277
```java
278+
ic void getMetrics() {
268279
MetricsQueryClient metricsQueryClient = new MetricsQueryClientBuilder()
269280
.credential(new DefaultAzureCredentialBuilder().build())
270281
.buildClient();
@@ -280,7 +291,6 @@ for (MetricResult metric : metricsQueryResult.getMetrics()) {
280291
System.out.println(metricValue.getTimeStamp() + " " + metricValue.getTotal());
281292
}
282293
}
283-
}
284294
```
285295

286296
#### Handle metrics query response
@@ -314,6 +324,7 @@ MetricsQueryResult
314324

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

341351
## Troubleshooting

sdk/monitor/azure-monitor-query/src/main/java/com/azure/monitor/query/LogsQueryAsyncClient.java

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@
77
import com.azure.core.annotation.ServiceClient;
88
import com.azure.core.annotation.ServiceMethod;
99
import com.azure.core.exception.HttpResponseException;
10+
import com.azure.core.exception.ServiceResponseException;
1011
import com.azure.core.experimental.models.HttpResponseError;
11-
import com.azure.core.experimental.models.TimeInterval;
1212
import com.azure.core.http.rest.Response;
1313
import com.azure.core.http.rest.SimpleResponse;
1414
import com.azure.core.util.BinaryData;
@@ -31,11 +31,14 @@
3131
import com.azure.monitor.query.models.LogsBatchQueryResultCollection;
3232
import com.azure.monitor.query.models.LogsQueryOptions;
3333
import com.azure.monitor.query.models.LogsQueryResult;
34+
import com.azure.monitor.query.models.LogsQueryResultStatus;
3435
import com.azure.monitor.query.models.LogsTable;
3536
import com.azure.monitor.query.models.LogsTableCell;
3637
import com.azure.monitor.query.models.LogsTableColumn;
3738
import com.azure.monitor.query.models.LogsTableRow;
39+
import com.azure.monitor.query.models.QueryTimeInterval;
3840
import reactor.core.publisher.Mono;
41+
import reactor.core.publisher.SynchronousSink;
3942

4043
import java.time.Duration;
4144
import java.util.ArrayList;
@@ -69,15 +72,15 @@ public final class LogsQueryAsyncClient {
6972
* Returns all the Azure Monitor logs matching the given query in the specified workspaceId.
7073
*
7174
* <p><strong>Query logs from the last 24 hours</strong></p>
72-
* {@codesnippet com.azure.monitor.query.LogsQueryAsyncClient.query#String-String-TimeInterval}
75+
* {@codesnippet com.azure.monitor.query.LogsQueryAsyncClient.query#String-String-QueryTimeInterval}
7376
*
7477
* @param workspaceId The workspaceId where the query should be executed.
7578
* @param query The Kusto query to fetch the logs.
7679
* @param timeInterval The time period for which the logs should be looked up.
7780
* @return The logs matching the query.
7881
*/
7982
@ServiceMethod(returns = ReturnType.SINGLE)
80-
public Mono<LogsQueryResult> query(String workspaceId, String query, TimeInterval timeInterval) {
83+
public Mono<LogsQueryResult> query(String workspaceId, String query, QueryTimeInterval timeInterval) {
8184
return queryWithResponse(workspaceId, query, timeInterval, new LogsQueryOptions())
8285
.map(Response::getValue);
8386
}
@@ -92,7 +95,7 @@ public Mono<LogsQueryResult> query(String workspaceId, String query, TimeInterva
9295
* @return The logs matching the query as a list of objects of type T.
9396
*/
9497
@ServiceMethod(returns = ReturnType.SINGLE)
95-
public <T> Mono<List<T>> query(String workspaceId, String query, TimeInterval timeInterval, Class<T> type) {
98+
public <T> Mono<List<T>> query(String workspaceId, String query, QueryTimeInterval timeInterval, Class<T> type) {
9699
return query(workspaceId, query, timeInterval)
97100
.map(result -> LogsQueryHelper.toObject(result.getTable(), type));
98101
}
@@ -108,7 +111,7 @@ public <T> Mono<List<T>> query(String workspaceId, String query, TimeInterval ti
108111
* @return The logs matching the query as a list of objects of type T.
109112
*/
110113
@ServiceMethod(returns = ReturnType.SINGLE)
111-
public <T> Mono<List<T>> query(String workspaceId, String query, TimeInterval timeInterval,
114+
public <T> Mono<List<T>> query(String workspaceId, String query, QueryTimeInterval timeInterval,
112115
Class<T> type, LogsQueryOptions options) {
113116
return queryWithResponse(workspaceId, query, timeInterval, options, Context.NONE)
114117
.map(response -> LogsQueryHelper.toObject(response.getValue().getTable(), type));
@@ -119,7 +122,7 @@ public <T> Mono<List<T>> query(String workspaceId, String query, TimeInterval ti
119122
*
120123
* <p><strong>Query logs from the last 7 days and set the service timeout to 2 minutes</strong></p>
121124
*
122-
* {@codesnippet com.azure.monitor.query.LogsQueryAsyncClient.queryWithResponse#String-String-TimeInterval-LogsQueryOptions}
125+
* {@codesnippet com.azure.monitor.query.LogsQueryAsyncClient.queryWithResponse#String-String-QueryTimeInterval-LogsQueryOptions}
123126
*
124127
* @param workspaceId The workspaceId where the query should be executed.
125128
* @param query The Kusto query to fetch the logs.
@@ -130,7 +133,7 @@ public <T> Mono<List<T>> query(String workspaceId, String query, TimeInterval ti
130133
*/
131134
@ServiceMethod(returns = ReturnType.SINGLE)
132135
public Mono<Response<LogsQueryResult>> queryWithResponse(String workspaceId, String query,
133-
TimeInterval timeInterval, LogsQueryOptions options) {
136+
QueryTimeInterval timeInterval, LogsQueryOptions options) {
134137
return withContext(context -> queryWithResponse(workspaceId, query, timeInterval, options, context));
135138
}
136139

@@ -147,7 +150,7 @@ public Mono<Response<LogsQueryResult>> queryWithResponse(String workspaceId, Str
147150
* @return The logs matching the query including the HTTP response.
148151
*/
149152
@ServiceMethod(returns = ReturnType.SINGLE)
150-
public <T> Mono<Response<List<T>>> queryWithResponse(String workspaceId, String query, TimeInterval timeInterval,
153+
public <T> Mono<Response<List<T>>> queryWithResponse(String workspaceId, String query, QueryTimeInterval timeInterval,
151154
Class<T> type, LogsQueryOptions options) {
152155
return queryWithResponse(workspaceId, query, timeInterval, options)
153156
.map(response -> new SimpleResponse<>(response.getRequest(),
@@ -164,7 +167,7 @@ public <T> Mono<Response<List<T>>> queryWithResponse(String workspaceId, String
164167
* @return A collection of query results corresponding to the input batch of queries.
165168
*/
166169
Mono<LogsBatchQueryResultCollection> queryBatch(String workspaceId, List<String> queries,
167-
TimeInterval timeInterval) {
170+
QueryTimeInterval timeInterval) {
168171
LogsBatchQuery logsBatchQuery = new LogsBatchQuery();
169172
queries.forEach(query -> logsBatchQuery.addQuery(workspaceId, query, timeInterval));
170173
return queryBatchWithResponse(logsBatchQuery).map(Response::getValue);
@@ -263,14 +266,14 @@ private HttpResponseError mapLogsQueryError(ErrorInfo errors) {
263266
return null;
264267
}
265268

266-
Mono<Response<LogsQueryResult>> queryWithResponse(String workspaceId, String query, TimeInterval timeInterval,
269+
Mono<Response<LogsQueryResult>> queryWithResponse(String workspaceId, String query, QueryTimeInterval timeInterval,
267270
LogsQueryOptions options, Context context) {
268271
String preferHeader = LogsQueryHelper.buildPreferHeaderString(options);
269272
context = updateContext(options.getServerTimeout(), context);
270273

271274
QueryBody queryBody = new QueryBody(query);
272275
if (timeInterval != null) {
273-
queryBody.setTimespan(timeInterval.toIso8601Format());
276+
queryBody.setTimespan(LogsQueryHelper.toIso8601Format(timeInterval));
274277
}
275278
queryBody.setWorkspaces(getAllWorkspaces(options));
276279
return innerClient
@@ -288,7 +291,16 @@ Mono<Response<LogsQueryResult>> queryWithResponse(String workspaceId, String que
288291
}
289292
return ex;
290293
})
291-
.map(this::convertToLogQueryResult);
294+
.map(this::convertToLogQueryResult)
295+
.handle((Response<LogsQueryResult> response, SynchronousSink<Response<LogsQueryResult>> sink) -> {
296+
if (response.getValue().getQueryResultStatus() == LogsQueryResultStatus.PARTIAL_FAILURE) {
297+
sink.error(new ServiceResponseException("Query execution returned partial errors. To "
298+
+ "disable exceptions on partial errors, set disableExceptionOnPartialErrors in "
299+
+ "LogsQueryOptions to true."));
300+
} else {
301+
sink.next(response);
302+
}
303+
});
292304
}
293305

294306
private Response<LogsQueryResult> convertToLogQueryResult(Response<QueryResults> response) {

0 commit comments

Comments
 (0)