Skip to content

Commit

Permalink
Azure Monitor: Enable metrics batch query (#36314)
Browse files Browse the repository at this point in the history
Azure Monitor: Enable metrics batch query
  • Loading branch information
srnagar authored Aug 11, 2023
1 parent dee71a6 commit 0836d8d
Show file tree
Hide file tree
Showing 37 changed files with 3,021 additions and 144 deletions.
15 changes: 10 additions & 5 deletions sdk/monitor/azure-monitor-query/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,17 +1,23 @@
# Release History

## 1.3.0-beta.1 (Unreleased)
## 1.3.0-beta.1 (2023-08-10)

### Features Added
- Added `MetricsBatchQueryClient` and `MetricsBatchQueryAsyncClient` to support batch querying metrics.

### Breaking Changes
### Other Changes

#### Dependency Updates

- Upgraded `azure-core` from `1.41.0` to version `1.42.0`.
- Upgraded `azure-core-http-netty` from `1.13.5` to version `1.13.6`.

## 1.2.3 (2023-07-28)

### Bugs Fixed

- Fixed bug that disabled sovereign cloud support.

### Other Changes

## 1.2.2 (2023-07-25)

### Other Changes
Expand All @@ -21,7 +27,6 @@
- Upgraded `azure-core` from `1.40.0` to version `1.41.0`.
- Upgraded `azure-core-http-netty` from `1.13.4` to version `1.13.5`.


## 1.2.1 (2023-06-20)

### Other Changes
Expand Down
116 changes: 83 additions & 33 deletions sdk/monitor/azure-monitor-query/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ add the direct dependency to your project as follows.
<dependency>
<groupId>com.azure</groupId>
<artifactId>azure-monitor-query</artifactId>
<version>1.2.2</version>
<version>1.3.0-beta.1</version>
</dependency>
```

Expand All @@ -87,7 +87,7 @@ To use the [DefaultAzureCredential][DefaultAzureCredential] provider shown below
<dependency>
<groupId>com.azure</groupId>
<artifactId>azure-identity</artifactId>
<version>1.9.2</version>
<version>1.10.0</version>
</dependency>
```
[//]: # ({x-version-update-end})
Expand All @@ -108,6 +108,13 @@ MetricsQueryClient metricsQueryClient = new MetricsQueryClientBuilder()
.buildClient();
```

```java readme-sample-createMetricsBatchQueryClient
MetricsBatchQueryClient metricsBatchQueryClient = new MetricsBatchQueryClientBuilder()
.credential(new DefaultAzureCredentialBuilder().build())
.endpoint("{endpoint}")
.buildClient();
```

#### Asynchronous clients

```java readme-sample-createLogsQueryAsyncClient
Expand All @@ -122,6 +129,13 @@ MetricsQueryAsyncClient metricsQueryAsyncClient = new MetricsQueryClientBuilder(
.buildAsyncClient();
```

```java readme-sample-createMetricsBatchQueryAsyncClient
MetricsBatchQueryAsyncClient metricsBatchQueryAsyncClient = new MetricsBatchQueryClientBuilder()
.credential(new DefaultAzureCredentialBuilder().build())
.endpoint("{endpoint}")
.buildAsyncClient();
```

#### Configure clients for non-public Azure clouds

By default, `LogQueryClient` and `MetricQueryClient` are configured to connect to the public Azure Cloud. These can be configured to connect to non-public Azure clouds by setting the correct `endpoint` in the client builders: For example:
Expand Down Expand Up @@ -182,16 +196,17 @@ Each set of metric values is a time series with the following characteristics:
- [Handle metrics query response](#handle-metrics-query-response)
- [Get average and count metrics](#get-average-and-count-metrics)
- [Create a metrics client for non-public Azure clouds](#configure-clients-for-non-public-azure-clouds)

- [Metrics batch query](#metrics-batch-query)
- [Handle metrics batch query response](#handle-metrics-batch-query-response)
### Logs query

```java readme-sample-logsquery
LogsQueryClient logsQueryClient = new LogsQueryClientBuilder()
.credential(new DefaultAzureCredentialBuilder().build())
.buildClient();
.credential(new DefaultAzureCredentialBuilder().build())
.buildClient();

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

for (LogsTableRow row : queryResults.getTable().getRows()) {
System.out.println(row.getColumnValue("OperationName") + " " + row.getColumnValue("ResourceGroup"));
Expand All @@ -217,11 +232,11 @@ public class CustomLogModel {

```java readme-sample-logsquerycustommodel
LogsQueryClient logsQueryClient = new LogsQueryClientBuilder()
.credential(new DefaultAzureCredentialBuilder().build())
.buildClient();
.credential(new DefaultAzureCredentialBuilder().build())
.buildClient();

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

for (CustomLogModel customLogModel : customLogModels) {
System.out.println(customLogModel.getOperationName() + " " + customLogModel.getResourceGroup());
Expand Down Expand Up @@ -270,16 +285,16 @@ for (LogsTableRow row : queryResults.getTable().getRows()) {

```java readme-sample-batchlogsquery
LogsQueryClient logsQueryClient = new LogsQueryClientBuilder()
.credential(new DefaultAzureCredentialBuilder().build())
.buildClient();
.credential(new DefaultAzureCredentialBuilder().build())
.buildClient();

LogsBatchQuery logsBatchQuery = new LogsBatchQuery();
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();
.queryBatchWithResponse(logsBatchQuery, Context.NONE).getValue();

LogsBatchQueryResult query1Result = batchResults.getResult(query1);
for (LogsTableRow row : query1Result.getTable().getRows()) {
Expand Down Expand Up @@ -311,7 +326,7 @@ LogsQueryOptions options = new LogsQueryOptions()
.setServerTimeout(Duration.ofMinutes(10));

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

#### Query multiple workspaces
Expand All @@ -325,13 +340,13 @@ to include this column.

```java readme-sample-logsquerymultipleworkspaces
LogsQueryClient logsQueryClient = new LogsQueryClientBuilder()
.credential(new DefaultAzureCredentialBuilder().build())
.buildClient();
.credential(new DefaultAzureCredentialBuilder().build())
.buildClient();

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

Expand All @@ -345,13 +360,13 @@ To get logs query execution statistics, such as CPU and memory consumption:
The following example prints the query execution time:
```java readme-sample-includestatistics
LogsQueryClient client = new LogsQueryClientBuilder()
.credential(credential)
.buildClient();
.credential(credential)
.buildClient();

LogsQueryOptions options = new LogsQueryOptions()
.setIncludeStatistics(true);
.setIncludeStatistics(true);
Response<LogsQueryResult> response = client.queryWorkspaceWithResponse("{workspace-id}",
"AzureActivity | top 10 by TimeGenerated", QueryTimeInterval.LAST_1_HOUR, options, Context.NONE);
"AzureActivity | top 10 by TimeGenerated", QueryTimeInterval.LAST_1_HOUR, options, Context.NONE);
LogsQueryResult result = response.getValue();
BinaryData statistics = result.getStatistics();

Expand Down Expand Up @@ -384,18 +399,18 @@ To get visualization data for logs queries using the [render operator](https://l
For example:
```java readme-sample-includevisualization
LogsQueryClient client = new LogsQueryClientBuilder()
.credential(credential)
.buildClient();
.credential(credential)
.buildClient();

String visualizationQuery = "StormEvents"
+ "| summarize event_count = count() by State"
+ "| where event_count > 10"
+ "| project State, event_count"
+ "| render columnchart";
+ "| summarize event_count = count() by State"
+ "| where event_count > 10"
+ "| project State, event_count"
+ "| render columnchart";
LogsQueryOptions options = new LogsQueryOptions()
.setIncludeVisualization(true);
.setIncludeVisualization(true);
Response<LogsQueryResult> response = client.queryWorkspaceWithResponse("{workspace-id}", visualizationQuery,
QueryTimeInterval.LAST_7_DAYS, options, Context.NONE);
QueryTimeInterval.LAST_7_DAYS, options, Context.NONE);
LogsQueryResult result = response.getValue();
BinaryData visualization = result.getVisualization();

Expand Down Expand Up @@ -439,11 +454,11 @@ A resource ID, as denoted by the `{resource-id}` placeholder in the sample below

```java readme-sample-metricsquery
MetricsQueryClient metricsQueryClient = new MetricsQueryClientBuilder()
.credential(new DefaultAzureCredentialBuilder().build())
.buildClient();
.credential(new DefaultAzureCredentialBuilder().build())
.buildClient();

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

for (MetricResult metric : metricsQueryResult.getMetrics()) {
System.out.println("Metric name " + metric.getMetricName());
Expand Down Expand Up @@ -510,6 +525,41 @@ for (MetricResult metric : metricsQueryResult.getMetrics()) {
}
```

### Metrics batch query

#### Handle metrics batch query response

```java readme-sample-metricsquerybatch
MetricsBatchQueryClient metricsBatchQueryClient = new MetricsBatchQueryClientBuilder()
.credential(new DefaultAzureCredentialBuilder().build())
.endpoint("{endpoint}")
.buildClient();

MetricsBatchResult metricsBatchResult = metricsBatchQueryClient.queryBatch(
Arrays.asList("{resourceId1}", "{resourceId2}"),
Arrays.asList("{metric1}", "{metric2}"),
"{metricNamespace}");

for (MetricsQueryResult metricsQueryResult : metricsBatchResult.getMetricsQueryResults()) {
// Each MetricsQueryResult corresponds to one of the resourceIds in the batch request.
List<MetricResult> metrics = metricsQueryResult.getMetrics();
metrics.forEach(metric -> {
System.out.println(metric.getMetricName());
System.out.println(metric.getId());
System.out.println(metric.getResourceType());
System.out.println(metric.getUnit());
System.out.println(metric.getTimeSeries().size());
System.out.println(metric.getTimeSeries().get(0).getValues().size());
metric.getTimeSeries()
.stream()
.flatMap(ts -> ts.getValues().stream())
.forEach(mv -> System.out.println(mv.getTimeStamp().toString()
+ "; Count = " + mv.getCount()
+ "; Average = " + mv.getAverage()));
});
}
```

## Troubleshooting

See our [troubleshooting guide](https://github.com/Azure/azure-sdk-for-java/blob/main/sdk/monitor/azure-monitor-query/TROUBLESHOOTING.md)
Expand Down
32 changes: 16 additions & 16 deletions sdk/monitor/azure-monitor-query/TROUBLESHOOTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,14 @@ MetricsQueryClient can be configured as shown below:

```java readme-sample-enablehttplogging
LogsQueryClient logsQueryClient = new LogsQueryClientBuilder()
.credential(credential)
.httpLogOptions(new HttpLogOptions().setLogLevel(HttpLogDetailLevel.BODY_AND_HEADERS))
.buildClient();
.credential(credential)
.httpLogOptions(new HttpLogOptions().setLogLevel(HttpLogDetailLevel.BODY_AND_HEADERS))
.buildClient();
// or
MetricsQueryClient metricsQueryClient = new MetricsQueryClientBuilder()
.credential(credential)
.httpLogOptions(new HttpLogOptions().setLogLevel(HttpLogDetailLevel.BODY_AND_HEADERS))
.buildClient();
.credential(credential)
.httpLogOptions(new HttpLogOptions().setLogLevel(HttpLogDetailLevel.BODY_AND_HEADERS))
.buildClient();
```

Alternatively, you can configure logging HTTP requests and responses for your entire application by setting the
Expand Down Expand Up @@ -149,9 +149,9 @@ following.

```java readme-sample-responsetimeout
LogsQueryClient client = new LogsQueryClientBuilder()
.credential(credential)
.clientOptions(new HttpClientOptions().setResponseTimeout(Duration.ofSeconds(120)))
.buildClient();
.credential(credential)
.clientOptions(new HttpClientOptions().setResponseTimeout(Duration.ofSeconds(120)))
.buildClient();
```

The above code will create a LogsQueryClient with a Netty HTTP client that waits for a response for up to 120 seconds.
Expand All @@ -170,11 +170,11 @@ previous section.

```java readme-sample-servertimeout
LogsQueryClient client = new LogsQueryClientBuilder()
.credential(credential)
.buildClient();
.credential(credential)
.buildClient();

client.queryWorkspaceWithResponse("{workspaceId}", "{kusto-query-string}", QueryTimeInterval.LAST_DAY,
new LogsQueryOptions().setServerTimeout(Duration.ofMinutes(10)), Context.NONE);
new LogsQueryOptions().setServerTimeout(Duration.ofMinutes(10)), Context.NONE);
```

### Troubleshooting server timeouts on OkHTTP client
Expand All @@ -185,9 +185,9 @@ below. The downside to doing this is that every request from this client will ha

```java readme-sample-okhttpresponsetimeout
LogsQueryClient client = new LogsQueryClientBuilder()
.credential(credential)
.clientOptions(new HttpClientOptions().setResponseTimeout(Duration.ofSeconds(120)))
.buildClient();
.credential(credential)
.clientOptions(new HttpClientOptions().setResponseTimeout(Duration.ofSeconds(120)))
.buildClient();
```

### Troubleshooting partially successful logs query requests
Expand All @@ -199,7 +199,7 @@ in `LogsQueryOptions` as shown below:

```java readme-sample-allowpartialerrors
client.queryWorkspaceWithResponse("{workspaceId}", "{kusto-query-string}", QueryTimeInterval.LAST_DAY,
new LogsQueryOptions().setAllowPartialErrors(true), Context.NONE);
new LogsQueryOptions().setAllowPartialErrors(true), Context.NONE);
```

## Troubleshooting Metrics Query
Expand Down
2 changes: 1 addition & 1 deletion sdk/monitor/azure-monitor-query/assets.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@
"AssetsRepo": "Azure/azure-sdk-assets",
"AssetsRepoPrefixPath": "java",
"TagPrefix": "java/monitor/azure-monitor-query",
"Tag": "java/monitor/azure-monitor-query_9cf6f47b0a"
"Tag": "java/monitor/azure-monitor-query_c61b998a77"
}
Loading

0 comments on commit 0836d8d

Please sign in to comment.