This commit is contained in:
azure-sdk 2024-03-27 01:27:04 +00:00
Родитель ab08d75241
Коммит 1c89954534
2 изменённых файлов: 101 добавлений и 46 удалений

Просмотреть файл

@ -1,12 +1,12 @@
---
title: Azure Monitor Query client library for Java
keywords: Azure, java, SDK, API, azure-monitor-query, monitor
ms.date: 02/21/2024
ms.date: 03/27/2024
ms.topic: reference
ms.devlang: java
ms.service: monitor
---
# Azure Monitor Query client library for Java - version 1.2.9
# Azure Monitor Query client library for Java - version 1.3.0
The Azure Monitor Query client library is used to execute read-only queries against [Azure Monitor][azure_monitor_overview]'s two data platforms:
@ -38,7 +38,7 @@ The Azure Monitor Query client library is used to execute read-only queries agai
#### Include the BOM file
Please include the azure-sdk-bom to your project to take dependency on the General Availability (GA) version of the library. In the following snippet, replace the {bom_version_to_target} placeholder with the version number.
To learn more about the BOM, see the [AZURE SDK BOM README](https://github.com/Azure/azure-sdk-for-java/blob/azure-monitor-query_1.2.9/sdk/boms/azure-sdk-bom/README.md).
To learn more about the BOM, see the [AZURE SDK BOM README](https://github.com/Azure/azure-sdk-for-java/blob/azure-monitor-query_1.3.0/sdk/boms/azure-sdk-bom/README.md).
```xml
<dependencyManagement>
@ -75,7 +75,7 @@ add the direct dependency to your project as follows.
<dependency>
<groupId>com.azure</groupId>
<artifactId>azure-monitor-query</artifactId>
<version>1.2.9</version>
<version>1.3.0</version>
</dependency>
```
@ -96,7 +96,7 @@ To use the [DefaultAzureCredential][DefaultAzureCredential] provider shown below
<dependency>
<groupId>com.azure</groupId>
<artifactId>azure-identity</artifactId>
<version>1.9.0</version>
<version>1.11.2</version>
</dependency>
```
[//]: # ({x-version-update-end})
@ -117,6 +117,13 @@ MetricsQueryClient metricsQueryClient = new MetricsQueryClientBuilder()
.buildClient();
```
```java readme-sample-createMetricsClient
MetricsClient metricsClient = new MetricsClientBuilder()
.credential(new DefaultAzureCredentialBuilder().build())
.endpoint("{endpoint}")
.buildClient();
```
#### Asynchronous clients
```java readme-sample-createLogsQueryAsyncClient
@ -131,6 +138,13 @@ MetricsQueryAsyncClient metricsQueryAsyncClient = new MetricsQueryClientBuilder(
.buildAsyncClient();
```
```java readme-sample-createMetricsAsyncClient
MetricsAsyncClient metricsAsyncClient = new MetricsClientBuilder()
.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:
@ -191,16 +205,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 query resources](#metrics-query-resources)
- [Handle metrics query resources response](#handle-metrics-query-resources-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"));
@ -226,11 +241,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());
@ -279,8 +294,8 @@ 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)));
@ -288,7 +303,7 @@ String query2 = logsBatchQuery.addWorkspaceQuery("{workspace-id}", "{query-2}",
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()) {
@ -320,7 +335,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
@ -334,13 +349,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();
```
@ -354,13 +369,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();
@ -393,18 +408,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();
@ -438,6 +453,10 @@ raw JSON response. For example:
}
```
#### Overcome Log Analytics query size limitations
If your query exceeds the [service limits][service_limits], see the large log query documentation to learn how to overcome them.
### Metrics query
A resource ID, as denoted by the `{resource-id}` placeholder in the sample below, is required to query metrics. To find the resource ID:
@ -448,11 +467,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());
@ -519,9 +538,44 @@ for (MetricResult metric : metricsQueryResult.getMetrics()) {
}
```
### Metrics query resources
#### Handle metrics query resources response
```java readme-sample-metricsquerymultipleresources
MetricsClient metricsClient = new MetricsClientBuilder()
.credential(new DefaultAzureCredentialBuilder().build())
.endpoint("{endpoint}")
.buildClient();
MetricsQueryResourcesResult metricsQueryResourcesResult = metricsClient.queryResources(
Arrays.asList("{resourceId1}", "{resourceId2}"),
Arrays.asList("{metric1}", "{metric2}"),
"{metricNamespace}");
for (MetricsQueryResult metricsQueryResult : metricsQueryResourcesResult.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/azure-monitor-query_1.2.9/sdk/monitor/azure-monitor-query/TROUBLESHOOTING.md)
See our [troubleshooting guide](https://github.com/Azure/azure-sdk-for-java/blob/azure-monitor-query_1.3.0/sdk/monitor/azure-monitor-query/TROUBLESHOOTING.md)
for details on how to diagnose various failure scenarios.
## Next steps
@ -542,21 +596,22 @@ comments.
<!-- LINKS -->
[azure_identity]: https://github.com/Azure/azure-sdk-for-java/blob/azure-monitor-query_1.2.9/sdk/identity/azure-identity/README.md
[azure_identity]: https://github.com/Azure/azure-sdk-for-java/blob/azure-monitor-query_1.3.0/sdk/identity/azure-identity/README.md
[azure_monitor_create_using_portal]: https://learn.microsoft.com/azure/azure-monitor/logs/quick-create-workspace
[azure_monitor_overview]: https://learn.microsoft.com/azure/azure-monitor/overview
[azure_subscription]: https://azure.microsoft.com/free/java
[changelog]: https://github.com/Azure/azure-sdk-for-java/blob/azure-monitor-query_1.2.9/sdk/monitor/azure-monitor-query/CHANGELOG.md
[changelog]: https://github.com/Azure/azure-sdk-for-java/blob/azure-monitor-query_1.3.0/sdk/monitor/azure-monitor-query/CHANGELOG.md
[custom_subdomain]: https://learn.microsoft.com/azure/cognitive-services/authentication?tabs=powershell#create-a-resource-with-a-custom-subdomain
[DefaultAzureCredential]: https://github.com/Azure/azure-sdk-for-java/blob/azure-monitor-query_1.2.9/sdk/identity/azure-identity/README.md#defaultazurecredential
[DefaultAzureCredential]: https://github.com/Azure/azure-sdk-for-java/blob/azure-monitor-query_1.3.0/sdk/identity/azure-identity/README.md#defaultazurecredential
[jdk_link]: https://learn.microsoft.com/java/azure/jdk/?view=azure-java-stable
[kusto_query_language]: https://learn.microsoft.com/azure/data-explorer/kusto/query/
[log_levels]: https://github.com/Azure/azure-sdk-for-java/blob/azure-monitor-query_1.2.9/sdk/core/azure-core/src/main/java/com/azure/core/util/logging/ClientLogger.java
[log_levels]: https://github.com/Azure/azure-sdk-for-java/blob/azure-monitor-query_1.3.0/sdk/core/azure-core/src/main/java/com/azure/core/util/logging/ClientLogger.java
[msdocs_apiref]: https://learn.microsoft.com/java/api/com.azure.monitor.query?view=azure-java-stable
[package]: https://search.maven.org/artifact/com.azure/azure-monitor-query
[samples]: https://github.com/Azure/azure-sdk-for-java/blob/azure-monitor-query_1.2.9/sdk/monitor/azure-monitor-query/src/samples/java/README.md
[source]: https://github.com/Azure/azure-sdk-for-java/tree/azure-monitor-query_1.2.9/sdk/monitor/azure-monitor-query/src
[package]: https://central.sonatype.com/artifact/com.azure/azure-monitor-query
[samples]: https://github.com/Azure/azure-sdk-for-java/blob/azure-monitor-query_1.3.0/sdk/monitor/azure-monitor-query/src/samples/java/README.md
[source]: https://github.com/Azure/azure-sdk-for-java/tree/azure-monitor-query_1.3.0/sdk/monitor/azure-monitor-query/src
[performance_tuning]: https://github.com/Azure/azure-sdk-for-java/wiki/Performance-Tuning
[service_limits]: https://learn.microsoft.com/azure/azure-monitor/service-limits#log-queries-and-language
[cla]: https://cla.microsoft.com
[coc]: https://opensource.microsoft.com/codeofconduct/

Просмотреть файл

@ -1,6 +1,6 @@
{
"Name": "azure-monitor-query",
"Version": "1.2.9",
"Version": "1.3.0",
"DevVersion": null,
"DirectoryPath": "sdk/monitor/azure-monitor-query",
"ServiceDirectory": "monitor",
@ -10,5 +10,5 @@
"SdkType": "client",
"IsNewSdk": true,
"ArtifactName": "azure-monitor-query",
"ReleaseStatus": "2024-02-20"
"ReleaseStatus": "2024-03-26"
}