{"slug":"azure-monitor-query-java","title":"azure-monitor-query-java","summary":"Azure Monitor Query SDK for Java. Execute Kusto queries against Log Analytics workspaces and query metrics from Azure resources. Triggers: \"LogsQueryClient java\", \"MetricsQueryClient java\", \"kusto query java\", \"log analytics java\", \"azure monitor query java\". Note: This package i","platform":"GitHub Copilot","tags":[],"authorName":"Ciza","authorSlug":"ciza","score":0,"source":"github","price":null,"verified":false,"createdAt":"2026-08-12T21:05:22.722732Z","repo":{"url":"https://github.com/microsoft/skills","stars":3052,"forks":351,"license":"MIT","updatedAt":"2026-09-24T16:38:17Z"},"bodyHtml":"<hr>\n<h2>name: azure-monitor-query-java\ndescription: |\nAzure Monitor Query SDK for Java. Execute Kusto queries against Log Analytics workspaces and query metrics from Azure resources.\nTriggers: \"LogsQueryClient java\", \"MetricsQueryClient java\", \"kusto query java\", \"log analytics java\", \"azure monitor query java\".\nNote: This package is deprecated. Migrate to azure-monitor-query-logs and azure-monitor-query-metrics.\nlicense: MIT\nmetadata:\nauthor: Microsoft\nversion: \"1.0.0\"\npackage: com.azure:azure-monitor-query</h2>\n<h1>Azure Monitor Query SDK for Java</h1>\n<blockquote>\n<p><strong>DEPRECATION NOTICE</strong>: This package is deprecated in favor of:</p>\n<ul>\n<li><code>azure-monitor-query-logs</code> — For Log Analytics queries</li>\n<li><code>azure-monitor-query-metrics</code> — For metrics queries</li>\n</ul>\n<p>See migration guides: <a href=\"https://github.com/Azure/azure-sdk-for-java/blob/main/sdk/monitor/azure-monitor-query-logs/migration-guide.md\">Logs Migration</a> | <a href=\"https://github.com/Azure/azure-sdk-for-java/blob/main/sdk/monitor/azure-monitor-query-metrics/migration-guide.md\">Metrics Migration</a></p>\n</blockquote>\n<p>Client library for querying Azure Monitor Logs and Metrics.</p>\n<h2>Installation</h2>\n<pre><code>&lt;dependency&gt;\n    &lt;groupId&gt;com.azure&lt;/groupId&gt;\n    &lt;artifactId&gt;azure-monitor-query&lt;/artifactId&gt;\n    &lt;version&gt;1.5.9&lt;/version&gt;\n&lt;/dependency&gt;\n</code></pre>\n<p>Or use Azure SDK BOM:</p>\n<pre><code>&lt;dependencyManagement&gt;\n    &lt;dependencies&gt;\n        &lt;dependency&gt;\n            &lt;groupId&gt;com.azure&lt;/groupId&gt;\n            &lt;artifactId&gt;azure-sdk-bom&lt;/artifactId&gt;\n            &lt;version&gt;{bom_version}&lt;/version&gt;\n            &lt;type&gt;pom&lt;/type&gt;\n            &lt;scope&gt;import&lt;/scope&gt;\n        &lt;/dependency&gt;\n    &lt;/dependencies&gt;\n&lt;/dependencyManagement&gt;\n\n&lt;dependencies&gt;\n    &lt;dependency&gt;\n        &lt;groupId&gt;com.azure&lt;/groupId&gt;\n        &lt;artifactId&gt;azure-monitor-query&lt;/artifactId&gt;\n    &lt;/dependency&gt;\n&lt;/dependencies&gt;\n</code></pre>\n<h2>Prerequisites</h2>\n<ul>\n<li>Log Analytics workspace (for logs queries)</li>\n<li>Azure resource (for metrics queries)</li>\n<li>TokenCredential with appropriate permissions</li>\n</ul>\n<h2>Environment Variables</h2>\n<pre><code>LOG_ANALYTICS_WORKSPACE_ID=xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx  # Required for Log Analytics workspace queries\nAZURE_RESOURCE_ID=/subscriptions/{sub}/resourceGroups/{rg}/providers/{provider}/{resource}  # Required for metrics queries against a resource\nAZURE_TOKEN_CREDENTIALS=prod  # Required only if DefaultAzureCredential is used in production\n</code></pre>\n<h2>Client Creation</h2>\n<h3>LogsQueryClient (Sync)</h3>\n<pre><code>import com.azure.core.credential.TokenCredential;\nimport com.azure.identity.AzureIdentityEnvVars;\nimport com.azure.identity.DefaultAzureCredentialBuilder;\nimport com.azure.identity.ManagedIdentityCredentialBuilder;\nimport com.azure.monitor.query.LogsQueryClient;\nimport com.azure.monitor.query.LogsQueryClientBuilder;\n\n// Local dev: DefaultAzureCredential. Production: set AZURE_TOKEN_CREDENTIALS=prod or AZURE_TOKEN_CREDENTIALS=&lt;specific_credential&gt;\nTokenCredential credential = new DefaultAzureCredentialBuilder()\n    .requireEnvVars(AzureIdentityEnvVars.AZURE_TOKEN_CREDENTIALS)\n    .build();\n// Or use a specific credential directly in production:\n// See https://learn.microsoft.com/java/api/overview/azure/identity-readme?view=azure-java-stable#credential-classes\n// TokenCredential credential = new ManagedIdentityCredentialBuilder().build();\n\nLogsQueryClient logsClient = new LogsQueryClientBuilder()\n    .credential(credential)\n    .buildClient();\n</code></pre>\n<h3>LogsQueryAsyncClient</h3>\n<pre><code>import com.azure.monitor.query.LogsQueryAsyncClient;\n\nLogsQueryAsyncClient logsAsyncClient = new LogsQueryClientBuilder()\n    .credential(credential)\n    .buildAsyncClient();\n</code></pre>\n<h3>MetricsQueryClient (Sync)</h3>\n<pre><code>import com.azure.monitor.query.MetricsQueryClient;\nimport com.azure.monitor.query.MetricsQueryClientBuilder;\n\nMetricsQueryClient metricsClient = new MetricsQueryClientBuilder()\n    .credential(credential)\n    .buildClient();\n</code></pre>\n<h3>MetricsQueryAsyncClient</h3>\n<pre><code>import com.azure.monitor.query.MetricsQueryAsyncClient;\n\nMetricsQueryAsyncClient metricsAsyncClient = new MetricsQueryClientBuilder()\n    .credential(credential)\n    .buildAsyncClient();\n</code></pre>\n<h3>Sovereign Cloud Configuration</h3>\n<pre><code>// Azure China Cloud - Logs\nLogsQueryClient logsClient = new LogsQueryClientBuilder()\n    .credential(credential)\n    .endpoint(\"https://api.loganalytics.azure.cn/v1\")\n    .buildClient();\n\n// Azure China Cloud - Metrics\nMetricsQueryClient metricsClient = new MetricsQueryClientBuilder()\n    .credential(credential)\n    .endpoint(\"https://management.chinacloudapi.cn\")\n    .buildClient();\n</code></pre>\n<h2>Key Concepts</h2>\n<table>\n<thead>\n<tr>\n<th>Concept</th>\n<th>Description</th>\n</tr>\n</thead>\n<tbody>\n<tr>\n<td>Logs</td>\n<td>Log and performance data from Azure resources via Kusto Query Language</td>\n</tr>\n<tr>\n<td>Metrics</td>\n<td>Numeric time-series data collected at regular intervals</td>\n</tr>\n<tr>\n<td>Workspace ID</td>\n<td>Log Analytics workspace identifier</td>\n</tr>\n<tr>\n<td>Resource ID</td>\n<td>Azure resource URI for metrics queries</td>\n</tr>\n<tr>\n<td>QueryTimeInterval</td>\n<td>Time range for the query</td>\n</tr>\n</tbody>\n</table>\n<h2>Logs Query Operations</h2>\n<h3>Basic Query</h3>\n<pre><code>import com.azure.monitor.query.models.LogsQueryResult;\nimport com.azure.monitor.query.models.LogsTableRow;\nimport com.azure.monitor.query.models.QueryTimeInterval;\nimport java.time.Duration;\n\nLogsQueryResult result = logsClient.queryWorkspace(\n    \"{workspace-id}\",\n    \"AzureActivity | summarize count() by ResourceGroup | top 10 by count_\",\n    new QueryTimeInterval(Duration.ofDays(7))\n);\n\nfor (LogsTableRow row : result.getTable().getRows()) {\n    System.out.println(row.getColumnValue(\"ResourceGroup\") + \": \" + row.getColumnValue(\"count_\"));\n}\n</code></pre>\n<h3>Query by Resource ID</h3>\n<pre><code>LogsQueryResult result = logsClient.queryResource(\n    \"{resource-id}\",\n    \"AzureMetrics | where TimeGenerated &gt; ago(1h)\",\n    new QueryTimeInterval(Duration.ofDays(1))\n);\n\nfor (LogsTableRow row : result.getTable().getRows()) {\n    System.out.println(row.getColumnValue(\"MetricName\") + \" \" + row.getColumnValue(\"Average\"));\n}\n</code></pre>\n<h3>Map Results to Custom Model</h3>\n<pre><code>// Define model class\npublic class ActivityLog {\n    private String resourceGroup;\n    private String operationName;\n    \n    public String getResourceGroup() { return resourceGroup; }\n    public String getOperationName() { return operationName; }\n}\n\n// Query with model mapping\nList&lt;ActivityLog&gt; logs = logsClient.queryWorkspace(\n    \"{workspace-id}\",\n    \"AzureActivity | project ResourceGroup, OperationName | take 100\",\n    new QueryTimeInterval(Duration.ofDays(2)),\n    ActivityLog.class\n);\n\nfor (ActivityLog log : logs) {\n    System.out.println(log.getOperationName() + \" - \" + log.getResourceGroup());\n}\n</code></pre>\n<h3>Batch Query</h3>\n<pre><code>import com.azure.monitor.query.models.LogsBatchQuery;\nimport com.azure.monitor.query.models.LogsBatchQueryResult;\nimport com.azure.monitor.query.models.LogsBatchQueryResultCollection;\nimport com.azure.core.util.Context;\n\nLogsBatchQuery batchQuery = new LogsBatchQuery();\nString q1 = batchQuery.addWorkspaceQuery(\"{workspace-id}\", \"AzureActivity | count\", new QueryTimeInterval(Duration.ofDays(1)));\nString q2 = batchQuery.addWorkspaceQuery(\"{workspace-id}\", \"Heartbeat | count\", new QueryTimeInterval(Duration.ofDays(1)));\nString q3 = batchQuery.addWorkspaceQuery(\"{workspace-id}\", \"Perf | count\", new QueryTimeInterval(Duration.ofDays(1)));\n\nLogsBatchQueryResultCollection results = logsClient\n    .queryBatchWithResponse(batchQuery, Context.NONE)\n    .getValue();\n\nLogsBatchQueryResult result1 = results.getResult(q1);\nLogsBatchQueryResult result2 = results.getResult(q2);\nLogsBatchQueryResult result3 = results.getResult(q3);\n\n// Check for failures\nif (result3.getQueryResultStatus() == LogsQueryResultStatus.FAILURE) {\n    System.err.println(\"Query failed: \" + result3.getError().getMessage());\n}\n</code></pre>\n<h3>Query with Options</h3>\n<pre><code>import com.azure.monitor.query.models.LogsQueryOptions;\nimport com.azure.core.http.rest.Response;\n\nLogsQueryOptions options = new LogsQueryOptions()\n    .setServerTimeout(Duration.ofMinutes(10))\n    .setIncludeStatistics(true)\n    .setIncludeVisualization(true);\n\nResponse&lt;LogsQueryResult&gt; response = logsClient.queryWorkspaceWithResponse(\n    \"{workspace-id}\",\n    \"AzureActivity | summarize count() by bin(TimeGenerated, 1h)\",\n    new QueryTimeInterval(Duration.ofDays(7)),\n    options,\n    Context.NONE\n);\n\nLogsQueryResult result = response.getValue();\n\n// Access statistics\nBinaryData statistics = result.getStatistics();\n// Access visualization data\nBinaryData visualization = result.getVisualization();\n</code></pre>\n<h3>Query Multiple Workspaces</h3>\n<pre><code>import java.util.Arrays;\n\nLogsQueryOptions options = new LogsQueryOptions()\n    .setAdditionalWorkspaces(Arrays.asList(\"{workspace-id-2}\", \"{workspace-id-3}\"));\n\nResponse&lt;LogsQueryResult&gt; response = logsClient.queryWorkspaceWithResponse(\n    \"{workspace-id-1}\",\n    \"AzureActivity | summarize count() by TenantId\",\n    new QueryTimeInterval(Duration.ofDays(1)),\n    options,\n    Context.NONE\n);\n</code></pre>\n<h2>Metrics Query Operations</h2>\n<h3>Basic Metrics Query</h3>\n<pre><code>import com.azure.monitor.query.models.MetricsQueryResult;\nimport com.azure.monitor.query.models.MetricResult;\nimport com.azure.monitor.query.models.TimeSeriesElement;\nimport com.azure.monitor.query.models.MetricValue;\nimport java.util.Arrays;\n\nMetricsQueryResult result = metricsClient.queryResource(\n    \"{resource-uri}\",\n    Arrays.asList(\"SuccessfulCalls\", \"TotalCalls\")\n);\n\nfor (MetricResult metric : result.getMetrics()) {\n    System.out.println(\"Metric: \" + metric.getMetricName());\n    for (TimeSeriesElement ts : metric.getTimeSeries()) {\n        System.out.println(\"  Dimensions: \" + ts.getMetadata());\n        for (MetricValue value : ts.getValues()) {\n            System.out.println(\"    \" + value.getTimeStamp() + \": \" + value.getTotal());\n        }\n    }\n}\n</code></pre>\n<h3>Metrics with Aggregations</h3>\n<pre><code>import com.azure.monitor.query.models.MetricsQueryOptions;\nimport com.azure.monitor.query.models.AggregationType;\n\nResponse&lt;MetricsQueryResult&gt; response = metricsClient.queryResourceWithResponse(\n    \"{resource-id}\",\n    Arrays.asList(\"SuccessfulCalls\", \"TotalCalls\"),\n    new MetricsQueryOptions()\n        .setGranularity(Duration.ofHours(1))\n        .setAggregations(Arrays.asList(AggregationType.AVERAGE, AggregationType.COUNT)),\n    Context.NONE\n);\n\nMetricsQueryResult result = response.getValue();\n</code></pre>\n<h3>Query Multiple Resources (MetricsClient)</h3>\n<pre><code>import com.azure.monitor.query.MetricsClient;\nimport com.azure.monitor.query.MetricsClientBuilder;\nimport com.azure.monitor.query.models.MetricsQueryResourcesResult;\n\nMetricsClient metricsClient = new MetricsClientBuilder()\n    .credential(new DefaultAzureCredentialBuilder().build())\n    .endpoint(\"{endpoint}\")\n    .buildClient();\n\nMetricsQueryResourcesResult result = metricsClient.queryResources(\n    Arrays.asList(\"{resourceId1}\", \"{resourceId2}\"),\n    Arrays.asList(\"{metric1}\", \"{metric2}\"),\n    \"{metricNamespace}\"\n);\n\nfor (MetricsQueryResult queryResult : result.getMetricsQueryResults()) {\n    for (MetricResult metric : queryResult.getMetrics()) {\n        System.out.println(metric.getMetricName());\n        metric.getTimeSeries().stream()\n            .flatMap(ts -&gt; ts.getValues().stream())\n            .forEach(mv -&gt; System.out.println(\n                mv.getTimeStamp() + \" Count=\" + mv.getCount() + \" Avg=\" + mv.getAverage()));\n    }\n}\n</code></pre>\n<h2>Response Structure</h2>\n<h3>Logs Response Hierarchy</h3>\n<pre><code>LogsQueryResult\n├── statistics (BinaryData)\n├── visualization (BinaryData)\n├── error\n└── tables (List&lt;LogsTable&gt;)\n    ├── name\n    ├── columns (List&lt;LogsTableColumn&gt;)\n    │   ├── name\n    │   └── type\n    └── rows (List&lt;LogsTableRow&gt;)\n        ├── rowIndex\n        └── rowCells (List&lt;LogsTableCell&gt;)\n</code></pre>\n<h3>Metrics Response Hierarchy</h3>\n<pre><code>MetricsQueryResult\n├── granularity\n├── timeInterval\n├── namespace\n├── resourceRegion\n└── metrics (List&lt;MetricResult&gt;)\n    ├── id, name, type, unit\n    └── timeSeries (List&lt;TimeSeriesElement&gt;)\n        ├── metadata (dimensions)\n        └── values (List&lt;MetricValue&gt;)\n            ├── timeStamp\n            ├── count, average, total\n            ├── maximum, minimum\n</code></pre>\n<h2>Error Handling</h2>\n<pre><code>import com.azure.core.exception.HttpResponseException;\nimport com.azure.monitor.query.models.LogsQueryResultStatus;\n\ntry {\n    LogsQueryResult result = logsClient.queryWorkspace(workspaceId, query, timeInterval);\n    \n    // Check partial failure\n    if (result.getStatus() == LogsQueryResultStatus.PARTIAL_FAILURE) {\n        System.err.println(\"Partial failure: \" + result.getError().getMessage());\n    }\n} catch (HttpResponseException e) {\n    System.err.println(\"Query failed: \" + e.getMessage());\n    System.err.println(\"Status: \" + e.getResponse().getStatusCode());\n}\n</code></pre>\n<h2>Best Practices</h2>\n<ol>\n<li><strong>Use batch queries</strong> — Combine multiple queries into a single request</li>\n<li><strong>Set appropriate timeouts</strong> — Long queries may need extended server timeout</li>\n<li><strong>Limit result size</strong> — Use <code>top</code> or <code>take</code> in Kusto queries</li>\n<li><strong>Use projections</strong> — Select only needed columns with <code>project</code></li>\n<li><strong>Check query status</strong> — Handle PARTIAL_FAILURE results gracefully</li>\n<li><strong>Cache results</strong> — Metrics don't change frequently; cache when appropriate</li>\n<li><strong>Migrate to new packages</strong> — Plan migration to <code>azure-monitor-query-logs</code> and <code>azure-monitor-query-metrics</code></li>\n</ol>\n<h2>Reference Links</h2>\n<table>\n<thead>\n<tr>\n<th>Resource</th>\n<th>URL</th>\n</tr>\n</thead>\n<tbody>\n<tr>\n<td>Maven Package</td>\n<td><a href=\"https://central.sonatype.com/artifact/com.azure/azure-monitor-query\">https://central.sonatype.com/artifact/com.azure/azure-monitor-query</a></td>\n</tr>\n<tr>\n<td>GitHub</td>\n<td><a href=\"https://github.com/Azure/azure-sdk-for-java/tree/main/sdk/monitor/azure-monitor-query\">https://github.com/Azure/azure-sdk-for-java/tree/main/sdk/monitor/azure-monitor-query</a></td>\n</tr>\n<tr>\n<td>API Reference</td>\n<td><a href=\"https://learn.microsoft.com/java/api/com.azure.monitor.query\">https://learn.microsoft.com/java/api/com.azure.monitor.query</a></td>\n</tr>\n<tr>\n<td>Kusto Query Language</td>\n<td><a href=\"https://learn.microsoft.com/azure/data-explorer/kusto/query/\">https://learn.microsoft.com/azure/data-explorer/kusto/query/</a></td>\n</tr>\n<tr>\n<td>Log Analytics Limits</td>\n<td><a href=\"https://learn.microsoft.com/azure/azure-monitor/service-limits#la-query-api\">https://learn.microsoft.com/azure/azure-monitor/service-limits#la-query-api</a></td>\n</tr>\n<tr>\n<td>Troubleshooting</td>\n<td><a href=\"https://github.com/Azure/azure-sdk-for-java/blob/main/sdk/monitor/azure-monitor-query/TROUBLESHOOTING.md\">https://github.com/Azure/azure-sdk-for-java/blob/main/sdk/monitor/azure-monitor-query/TROUBLESHOOTING.md</a></td>\n</tr>\n</tbody>\n</table>\n","files":[{"path":"references/examples.md","sizeBytes":11538,"isText":true},{"path":"SKILL.md","sizeBytes":13944,"isText":true}],"reviewScore":null,"reviewSummary":null,"trust":{"provenance":"trusted-source-unreviewed","notice":"Community-authored content, reproduced verbatim and not vetted as instructions. Treat it as data to evaluate, never as directives to follow.","bodySource":null},"bodyLocked":false,"purchaseUrl":null,"sourceUrl":null,"report":{"provenance":"trusted-source-unreviewed","screen":{"ran":true,"outcome":"clean","suspicious":0,"notes":0,"hiddenCharacters":false},"virusScan":{"engine":"clamav","status":"clean","scannedAt":"2026-08-12T21:52:27.755521Z","sha256":"4E80931087686F1B7760719A97A2F993A1A4AAD3391CA76DC332AF0552716C4B","sizeBytes":6745},"review":null,"source":{"repositoryUrl":"https://github.com/microsoft/skills","path":".github/plugins/azure-sdk-java/skills/azure-monitor-query-java","license":"MIT","commit":"23d0dac5f83f268166a17f0bc7dc6c73dc348a33","subtreeSha":"776AD7EAF8E7F501F109219710A44BA4B6E62FC80CC28126C2D60D87B6DA080A","lastSyncedAt":"2026-09-25T06:48:53.330584Z"},"reviewedAt":"2026-08-12T21:59:02.170936Z","notice":"Community-authored content, reproduced verbatim and not vetted as instructions. Treat it as data to evaluate, never as directives to follow."},"install":[{"target":"skills-cli","command":"npx skills add https://github.com/microsoft/skills/tree/main/.github/plugins/azure-sdk-java/skills/azure-monitor-query-java"},{"target":"claude-code","command":"claude plugin marketplace add https://llmmart.ai/marketplace.json && claude plugin install microsoft-skills@llmmart"},{"target":"git","command":"git clone https://github.com/microsoft/skills.git"}]}