Skip to content

Commit

Permalink
Rename query APIs (#24523)
Browse files Browse the repository at this point in the history
  • Loading branch information
srnagar authored Oct 4, 2021
1 parent 6bb5653 commit a9d4719
Show file tree
Hide file tree
Showing 32 changed files with 235 additions and 256 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,14 @@ public LogsBatchQueryTest(PerfStressOptions options) {
@Override
public void run() {
LogsBatchQuery batchQuery = new LogsBatchQuery();
LOGS_BATCH_QUERIES.forEach(query -> batchQuery.addQuery(workspaceId, query, null));
LOGS_BATCH_QUERIES.forEach(query -> batchQuery.addWorkspaceQuery(workspaceId, query, null));
logsQueryClient.queryBatch(batchQuery);
}

@Override
public Mono<Void> runAsync() {
LogsBatchQuery batchQuery = new LogsBatchQuery();
LOGS_BATCH_QUERIES.forEach(query -> batchQuery.addQuery(workspaceId, query, null));
LOGS_BATCH_QUERIES.forEach(query -> batchQuery.addWorkspaceQuery(workspaceId, query, null));
return logsQueryAsyncClient.queryBatch(batchQuery).then();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,11 @@ public LogsQueryAsModelTest(PerfStressOptions options) {

@Override
public void run() {
logsQueryClient.query(workspaceId, LOGS_QUERY, null, CustomModel.class);
logsQueryClient.queryWorkspace(workspaceId, LOGS_QUERY, null, CustomModel.class);
}

@Override
public Mono<Void> runAsync() {
return logsQueryAsyncClient.query(workspaceId, LOGS_QUERY, null, CustomModel.class).then();
return logsQueryAsyncClient.queryWorkspace(workspaceId, LOGS_QUERY, null, CustomModel.class).then();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,11 @@ public LogsQueryTest(PerfStressOptions options) {

@Override
public void run() {
logsQueryClient.query(workspaceId, LOGS_QUERY, null);
logsQueryClient.queryWorkspace(workspaceId, LOGS_QUERY, null);
}

@Override
public Mono<Void> runAsync() {
return logsQueryAsyncClient.query(workspaceId, LOGS_QUERY, null).then();
return logsQueryAsyncClient.queryWorkspace(workspaceId, LOGS_QUERY, null).then();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,13 @@ public MetricsQueryTest(PerfStressOptions options) {

@Override
public void run() {
metricsQueryClient.queryWithResponse(resourceId, Arrays.asList("SuccessfulCalls"),
metricsQueryClient.queryResourceWithResponse(resourceId, Arrays.asList("SuccessfulCalls"),
new MetricsQueryOptions().setTop(100), Context.NONE);
}

@Override
public Mono<Void> runAsync() {
return metricsQueryAsyncClient.queryWithResponse(resourceId, Arrays.asList("SuccessfulCalls"),
return metricsQueryAsyncClient.queryResourceWithResponse(resourceId, Arrays.asList("SuccessfulCalls"),
new MetricsQueryOptions().setTop(100)).then();
}
}
92 changes: 46 additions & 46 deletions sdk/monitor/azure-monitor-query/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,44 +44,44 @@ An authenticated client is required to query Logs or Metrics. The library includ

#### Synchronous clients

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

### Create Logs query async client

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

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

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

#### Asynchronous clients

<!-- embedme ./src/samples/java/com/azure/monitor/query/ReadmeSamples.java#L43-L45 -->
<!-- embedme ./src/samples/java/com/azure/monitor/query/ReadmeSamples.java#L44-L46 -->
```java
LogsQueryAsyncClient logsQueryAsyncClient = new LogsQueryClientBuilder()
.credential(new DefaultAzureCredentialBuilder().build())
.buildAsyncClient();
```

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

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

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

### Logs query

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

LogsQueryResult queryResults = logsQueryClient.query("{workspace-id}", "{kusto-query}",
LogsQueryResult queryResults = logsQueryClient.queryWorkspace("{workspace-id}", "{kusto-query}",
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 -->
<!-- embedme ./src/samples/java/com/azure/monitor/query/ReadmeSamples.java#L81-L92 -->
```java
public class CustomLogModel {
private String resourceGroup;
private String operationName;

ic class CustomLogModel {
private String resourceGroup;
private String operationName;

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

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

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

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

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

#### Handle logs query response
Expand All @@ -189,17 +189,16 @@ LogsQueryResult / LogsBatchQueryResult

### Batch logs query

<!-- embedme ./src/samples/java/com/azure/monitor/query/ReadmeSamples.java#L113-L138 -->
<!-- embedme ./src/samples/java/com/azure/monitor/query/ReadmeSamples.java#L114-L139 -->
```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 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)));
String query1 = logsBatchQuery.addWorkspaceQuery("{workspace-id}", "{query-1}", new QueryTimeInterval(Duration.ofDays(2)));
String query2 = logsBatchQuery.addWorkspaceQuery("{workspace-id}", "{query-2}", new QueryTimeInterval(Duration.ofDays(30)));
String query3 = logsBatchQuery.addWorkspaceQuery("{workspace-id}", "{query-3}", new QueryTimeInterval(Duration.ofDays(10)));

LogsBatchQueryResultCollection batchResults = logsQueryClient
.queryBatchWithResponse(logsBatchQuery, Context.NONE).getValue();
Expand All @@ -217,15 +216,15 @@ for (CustomLogModel customLogModel : customLogModels) {
LogsBatchQueryResult query3Result = batchResults.getResult(query3);
if (query3Result.getQueryResultStatus() == LogsQueryResultStatus.FAILURE) {
System.out.println(query3Result.getError().getMessage());
}
```

### Advanced logs query scenarios

#### Set logs query timeout

<!-- embedme ./src/samples/java/com/azure/monitor/query/ReadmeSamples.java#L146-L155 -->
<!-- embedme ./src/samples/java/com/azure/monitor/query/ReadmeSamples.java#L147-L156 -->
```java
ic void getLogsWithServerTimeout() {
LogsQueryClient logsQueryClient = new LogsQueryClientBuilder()
.credential(new DefaultAzureCredentialBuilder().build())
.buildClient();
Expand All @@ -234,7 +233,8 @@ LogsQueryClient logsQueryClient = new LogsQueryClientBuilder()
LogsQueryOptions options = new LogsQueryOptions()
.setServerTimeout(Duration.ofMinutes(10));

Response<LogsQueryResult> response = logsQueryClient.queryWithResponse("{workspace-id}",
Response<LogsQueryResult> response = logsQueryClient.queryWorkspaceWithResponse("{workspace-id}",
"{kusto-query}", new QueryTimeInterval(Duration.ofDays(2)), options, Context.NONE);
```

#### Query multiple workspaces
Expand All @@ -246,17 +246,17 @@ workspace from which it was retrieved. To identify the workspace of a row in the
"TenantId" column in the result table. If this column is not in the table, then you may have to update your query string
to include this column.

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

Response<LogsQueryResult> response = logsQueryClient.queryWithResponse("{workspace-id}", "{kusto-query}",
Response<LogsQueryResult> response = logsQueryClient.queryWorkspaceWithResponse("{workspace-id}", "{kusto-query}",
new QueryTimeInterval(Duration.ofDays(2)), new LogsQueryOptions()
.setAdditionalWorkspaces(Arrays.asList("{additional-workspace-identifiers}")),
Context.NONE);
LogsQueryResult result = response.getValue();
```

### Metrics query
Expand All @@ -267,14 +267,13 @@ A resource ID, as denoted by the `{resource-id}` placeholder in the sample below
2. From the **Overview** blade, select the **JSON View** link.
3. In the resulting JSON, copy the value of the `id` property.

<!-- embedme ./src/samples/java/com/azure/monitor/query/ReadmeSamples.java#L178-L193 -->
<!-- embedme ./src/samples/java/com/azure/monitor/query/ReadmeSamples.java#L179-L194 -->
```java
ic void getMetrics() {
MetricsQueryClient metricsQueryClient = new MetricsQueryClientBuilder()
.credential(new DefaultAzureCredentialBuilder().build())
.buildClient();

MetricsQueryResult metricsQueryResult = metricsQueryClient.query("{resource-uri}",
MetricsQueryResult metricsQueryResult = metricsQueryClient.queryResource("{resource-uri}",
Arrays.asList("SuccessfulCalls", "TotalCalls"));

for (MetricResult metric : metricsQueryResult.getMetrics()) {
Expand All @@ -285,6 +284,7 @@ for (MetricResult metric : metricsQueryResult.getMetrics()) {
System.out.println(metricValue.getTimeStamp() + " " + metricValue.getTotal());
}
}
}
```

#### Handle metrics query response
Expand Down Expand Up @@ -316,15 +316,14 @@ MetricsQueryResult

#### Get average and count metrics

<!-- embedme ./src/samples/java/com/azure/monitor/query/ReadmeSamples.java#L200-L221 -->
<!-- embedme ./src/samples/java/com/azure/monitor/query/ReadmeSamples.java#L201-L222 -->
```java
ic void getMetricsWithOptions() {
MetricsQueryClient metricsQueryClient = new MetricsQueryClientBuilder()
.credential(new DefaultAzureCredentialBuilder().build())
.buildClient();

Response<MetricsQueryResult> metricsResponse = metricsQueryClient
.queryWithResponse("{resource-id}", Arrays.asList("SuccessfulCalls", "TotalCalls"),
.queryResourceWithResponse("{resource-id}", Arrays.asList("SuccessfulCalls", "TotalCalls"),
new MetricsQueryOptions()
.setGranularity(Duration.ofHours(1))
.setAggregations(Arrays.asList(AggregationType.AVERAGE, AggregationType.COUNT)),
Expand All @@ -340,6 +339,7 @@ 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 @@ -80,8 +80,8 @@ public final class LogsQueryAsyncClient {
* @return The logs matching the query.
*/
@ServiceMethod(returns = ReturnType.SINGLE)
public Mono<LogsQueryResult> query(String workspaceId, String query, QueryTimeInterval timeInterval) {
return queryWithResponse(workspaceId, query, timeInterval, new LogsQueryOptions())
public Mono<LogsQueryResult> queryWorkspace(String workspaceId, String query, QueryTimeInterval timeInterval) {
return queryWorkspaceWithResponse(workspaceId, query, timeInterval, new LogsQueryOptions())
.map(Response::getValue);
}

Expand All @@ -95,8 +95,8 @@ public Mono<LogsQueryResult> query(String workspaceId, String query, QueryTimeIn
* @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, QueryTimeInterval timeInterval, Class<T> type) {
return query(workspaceId, query, timeInterval)
public <T> Mono<List<T>> queryWorkspace(String workspaceId, String query, QueryTimeInterval timeInterval, Class<T> type) {
return queryWorkspace(workspaceId, query, timeInterval)
.map(result -> LogsQueryHelper.toObject(result.getTable(), type));
}

Expand All @@ -111,9 +111,9 @@ public <T> Mono<List<T>> query(String workspaceId, String query, QueryTimeInterv
* @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, QueryTimeInterval timeInterval,
Class<T> type, LogsQueryOptions options) {
return queryWithResponse(workspaceId, query, timeInterval, options, Context.NONE)
public <T> Mono<List<T>> queryWorkspace(String workspaceId, String query, QueryTimeInterval timeInterval,
Class<T> type, LogsQueryOptions options) {
return queryWorkspaceWithResponse(workspaceId, query, timeInterval, options, Context.NONE)
.map(response -> LogsQueryHelper.toObject(response.getValue().getTable(), type));
}

Expand All @@ -132,9 +132,9 @@ public <T> Mono<List<T>> query(String workspaceId, String query, QueryTimeInterv
* @return The logs matching the query.
*/
@ServiceMethod(returns = ReturnType.SINGLE)
public Mono<Response<LogsQueryResult>> queryWithResponse(String workspaceId, String query,
QueryTimeInterval timeInterval, LogsQueryOptions options) {
return withContext(context -> queryWithResponse(workspaceId, query, timeInterval, options, context));
public Mono<Response<LogsQueryResult>> queryWorkspaceWithResponse(String workspaceId, String query,
QueryTimeInterval timeInterval, LogsQueryOptions options) {
return withContext(context -> queryWorkspaceWithResponse(workspaceId, query, timeInterval, options, context));
}

/**
Expand All @@ -150,9 +150,9 @@ 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, QueryTimeInterval timeInterval,
Class<T> type, LogsQueryOptions options) {
return queryWithResponse(workspaceId, query, timeInterval, options)
public <T> Mono<Response<List<T>>> queryWorkspaceWithResponse(String workspaceId, String query, QueryTimeInterval timeInterval,
Class<T> type, LogsQueryOptions options) {
return queryWorkspaceWithResponse(workspaceId, query, timeInterval, options)
.map(response -> new SimpleResponse<>(response.getRequest(),
response.getStatusCode(), response.getHeaders(),
LogsQueryHelper.toObject(response.getValue().getTable(), type)));
Expand All @@ -169,7 +169,7 @@ public <T> Mono<Response<List<T>>> queryWithResponse(String workspaceId, String
Mono<LogsBatchQueryResultCollection> queryBatch(String workspaceId, List<String> queries,
QueryTimeInterval timeInterval) {
LogsBatchQuery logsBatchQuery = new LogsBatchQuery();
queries.forEach(query -> logsBatchQuery.addQuery(workspaceId, query, timeInterval));
queries.forEach(query -> logsBatchQuery.addWorkspaceQuery(workspaceId, query, timeInterval));
return queryBatchWithResponse(logsBatchQuery).map(Response::getValue);
}

Expand Down Expand Up @@ -266,8 +266,8 @@ private ResponseError mapLogsQueryError(ErrorInfo errors) {
return null;
}

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

Expand Down
Loading

0 comments on commit a9d4719

Please sign in to comment.