From f6284243cad71347d9b0e9e39377985639420387 Mon Sep 17 00:00:00 2001 From: Jair Myree Date: Thu, 27 Jul 2023 11:24:37 -0700 Subject: [PATCH] [Monitor] Enabling Sovereign Cloud Support for Monitor Query (#36063) * README.md adjustments * Adding Sovereign Cloud Support * Adding Sovereign Cloud Support - fixing linting errors * Update tests.yml * Update tests.yml * Adding Sovereign Cloud Support * Adding Sovereign Cloud Support * Adding Sovereign Cloud Support * Adding Sovereign Cloud Support * Adding Sovereign Cloud Support * Adding Sovereign Cloud Support * Update LogsQueryAsyncClientTest.java * Update LogsQueryAsyncClientTest.java * Update LogsQueryClientTest.java * Enable Testing in Sovereign Clouds and build failure fixes * Add cspell exception for test-resources.json * Addressing failing CI * Changes to enable testing in sovereign clouds * Addressing failing CI * Addressing failing CI * Addressing failing CI * Refactoring test static variables and updating test-resources.json * Test Edits * Test Edits * Test Edits * Tests.yml Edits * Add china testing and documentation * Updating tests and test resources * Updating CHANGELOG.md * Updating README.md * Revert README.md * Updating README.md * Update sdk/monitor/azure-monitor-query/README.md Co-authored-by: Srikanta <51379715+srnagar@users.noreply.github.com> * Updating MonitorQueryTestUtils.java * Updating MonitorQueryTestUtils.java --------- Co-authored-by: Srikanta <51379715+srnagar@users.noreply.github.com> --- .vscode/cspell.json | 1 + sdk/monitor/azure-monitor-query/CHANGELOG.md | 2 + sdk/monitor/azure-monitor-query/README.md | 24 ++++++ sdk/monitor/azure-monitor-query/assets.json | 2 +- .../azure/monitor/query/LogsQueryClient.java | 4 + .../logs/AzureLogAnalyticsImplBuilder.java | 15 +++- .../azure/monitor/query/ReadmeSamples.java | 24 ++++++ .../query/LogsQueryAsyncClientTest.java | 57 +++++++-------- .../monitor/query/LogsQueryClientTest.java | 59 +++++++-------- .../query/MetricsQueryAsyncClientTest.java | 73 +++++++++++++------ .../monitor/query/MetricsQueryClientTest.java | 72 ++++++++++++------ .../monitor/query/MonitorQueryTestUtils.java | 70 ++++++++++++++++++ .../src/main/java/LogsCustomization.java | 15 +++- sdk/monitor/azure-monitor-query/tests.yml | 2 +- sdk/monitor/test-resources.json | 25 ++++--- sdk/monitor/tests.yml | 6 ++ 16 files changed, 329 insertions(+), 122 deletions(-) create mode 100644 sdk/monitor/azure-monitor-query/src/test/java/com/azure/monitor/query/MonitorQueryTestUtils.java diff --git a/.vscode/cspell.json b/.vscode/cspell.json index 09848409fac2c..2eaa545f1af0e 100644 --- a/.vscode/cspell.json +++ b/.vscode/cspell.json @@ -164,6 +164,7 @@ "sdk/maps/azure-maps-search/**", "sdk/monitor/azure-monitor-query/**", "sdk/monitor/azure-monitor-opentelemetry-exporter/**", + "sdk/monitor/test-resources.json", "sdk/modelsrepository/azure-iot-modelsrepository/**", "sdk/parents/azure-client-sdk-parent/**", "sdk/parents/azure-sdk-parent/**", diff --git a/sdk/monitor/azure-monitor-query/CHANGELOG.md b/sdk/monitor/azure-monitor-query/CHANGELOG.md index 31bb9ebd3fb37..4b3b335929cee 100644 --- a/sdk/monitor/azure-monitor-query/CHANGELOG.md +++ b/sdk/monitor/azure-monitor-query/CHANGELOG.md @@ -8,6 +8,8 @@ ### Bugs Fixed +- Fixed bug that disabled sovereign cloud support. + ### Other Changes ## 1.2.2 (2023-07-25) diff --git a/sdk/monitor/azure-monitor-query/README.md b/sdk/monitor/azure-monitor-query/README.md index 40de30253ea4f..3c70e187636ae 100644 --- a/sdk/monitor/azure-monitor-query/README.md +++ b/sdk/monitor/azure-monitor-query/README.md @@ -122,6 +122,28 @@ MetricsQueryAsyncClient metricsQueryAsyncClient = new MetricsQueryClientBuilder( .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: + +Creating a `LogsQueryClient` for Azure China cloud: + +```java readme-sample-createLogsQueryClientWithSovereignCloud +LogsQueryClient logsQueryClient = new LogsQueryClientBuilder() + .credential(new DefaultAzureCredentialBuilder().build()) + .endpoint("https://api.loganalytics.azure.cn/v1") + .buildClient(); +``` + +Creating a `MetricsQueryClient` for Azure China cloud: + +```java readme-sample-createMetricsQueryClientWithSovereignCloud +MetricsQueryClient metricsQueryClient = new MetricsQueryClientBuilder() + .credential(new DefaultAzureCredentialBuilder().build()) + .endpoint("https://management.chinacloudapi.cn") + .buildClient(); +``` + ### Execute the query For examples of Logs and Metrics queries, see the [Examples](#examples) section. @@ -149,6 +171,7 @@ Each set of metric values is a time series with the following characteristics: - [Map logs query results to a model](#map-logs-query-results-to-a-model) - [Handle logs query response](#handle-logs-query-response) - [Query logs by resource id](#query-logs-by-resource-id) + - [Create a log client for non-public Azure clouds](#configure-clients-for-non-public-azure-clouds) - [Batch logs query](#batch-logs-query) - [Advanced logs query scenarios](#advanced-logs-query-scenarios) - [Set logs query timeout](#set-logs-query-timeout) @@ -158,6 +181,7 @@ Each set of metric values is a time series with the following characteristics: - [Metrics query](#metrics-query) - [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) ### Logs query diff --git a/sdk/monitor/azure-monitor-query/assets.json b/sdk/monitor/azure-monitor-query/assets.json index 293be65398d10..646765acedabf 100644 --- a/sdk/monitor/azure-monitor-query/assets.json +++ b/sdk/monitor/azure-monitor-query/assets.json @@ -2,5 +2,5 @@ "AssetsRepo": "Azure/azure-sdk-assets", "AssetsRepoPrefixPath": "java", "TagPrefix": "java/monitor/azure-monitor-query", - "Tag": "java/monitor/azure-monitor-query_a5e00f5064" + "Tag": "java/monitor/azure-monitor-query_9cf6f47b0a" } diff --git a/sdk/monitor/azure-monitor-query/src/main/java/com/azure/monitor/query/LogsQueryClient.java b/sdk/monitor/azure-monitor-query/src/main/java/com/azure/monitor/query/LogsQueryClient.java index e7c1c47f0c3e3..08b73749048f9 100644 --- a/sdk/monitor/azure-monitor-query/src/main/java/com/azure/monitor/query/LogsQueryClient.java +++ b/sdk/monitor/azure-monitor-query/src/main/java/com/azure/monitor/query/LogsQueryClient.java @@ -409,6 +409,10 @@ private Response queryResourceWithResponseInternal(String resou Objects.requireNonNull(resourceId, "'resourceId' cannot be null."); Objects.requireNonNull(query, "'query' cannot be null."); + if (resourceId.startsWith("/")) { + resourceId = resourceId.substring(1); + } + String preferHeader = buildPreferHeaderString(options); context = updateContext(options.getServerTimeout(), context); diff --git a/sdk/monitor/azure-monitor-query/src/main/java/com/azure/monitor/query/implementation/logs/AzureLogAnalyticsImplBuilder.java b/sdk/monitor/azure-monitor-query/src/main/java/com/azure/monitor/query/implementation/logs/AzureLogAnalyticsImplBuilder.java index d74994318132b..03caad53386d4 100644 --- a/sdk/monitor/azure-monitor-query/src/main/java/com/azure/monitor/query/implementation/logs/AzureLogAnalyticsImplBuilder.java +++ b/sdk/monitor/azure-monitor-query/src/main/java/com/azure/monitor/query/implementation/logs/AzureLogAnalyticsImplBuilder.java @@ -252,8 +252,19 @@ private HttpPipeline createHttpPipeline() { policies.add(new AddDatePolicy()); policies.add(new CookiePolicy()); if (tokenCredential != null) { - String localHost = (host != null) ? host : "https://api.loganalytics.io"; - policies.add(new BearerTokenAuthenticationPolicy(tokenCredential, String.format("%s/.default", localHost))); + String localHost; + if (host != null) { + try { + localHost = new java.net.URL(host).getHost(); + } catch (java.net.MalformedURLException e) { + throw new RuntimeException(e); + } + } else { + localHost = "api.loganalytics.io"; + } + policies.add( + new BearerTokenAuthenticationPolicy( + tokenCredential, String.format("https://%s/.default", localHost))); } this.pipelinePolicies.stream() .filter(p -> p.getPipelinePosition() == HttpPipelinePosition.PER_RETRY) diff --git a/sdk/monitor/azure-monitor-query/src/samples/java/com/azure/monitor/query/ReadmeSamples.java b/sdk/monitor/azure-monitor-query/src/samples/java/com/azure/monitor/query/ReadmeSamples.java index 23bf4f5ce6926..a7802803a3033 100644 --- a/sdk/monitor/azure-monitor-query/src/samples/java/com/azure/monitor/query/ReadmeSamples.java +++ b/sdk/monitor/azure-monitor-query/src/samples/java/com/azure/monitor/query/ReadmeSamples.java @@ -399,4 +399,28 @@ public void includeVisualization() throws IOException { System.out.println("Visualization graph type = " + visualizationJson.get("visualization").asText()); // END: readme-sample-includevisualization } + + /** + * Sample to show how to create a log query client using a sovereign cloud endpoint. + */ + public void createLogsClientWithSovereignCloud() { + // BEGIN: readme-sample-createLogsQueryClientWithSovereignCloud + LogsQueryClient logsQueryClient = new LogsQueryClientBuilder() + .credential(new DefaultAzureCredentialBuilder().build()) + .endpoint("https://api.loganalytics.azure.cn/v1") + .buildClient(); + // END: readme-sample-createLogsQueryClientWithSovereignCloud + } + + /** + * Sample to show how to create a metrics query client using a sovereign cloud endpoint. + */ + public void createMetricsClientWithSovereignCloud() { + // BEGIN: readme-sample-createMetricsQueryClientWithSovereignCloud + MetricsQueryClient metricsQueryClient = new MetricsQueryClientBuilder() + .credential(new DefaultAzureCredentialBuilder().build()) + .endpoint("https://management.chinacloudapi.cn") + .buildClient(); + // END: readme-sample-createMetricsQueryClientWithSovereignCloud + } } diff --git a/sdk/monitor/azure-monitor-query/src/test/java/com/azure/monitor/query/LogsQueryAsyncClientTest.java b/sdk/monitor/azure-monitor-query/src/test/java/com/azure/monitor/query/LogsQueryAsyncClientTest.java index 554f80a9c2505..90a88324a7503 100644 --- a/sdk/monitor/azure-monitor-query/src/test/java/com/azure/monitor/query/LogsQueryAsyncClientTest.java +++ b/sdk/monitor/azure-monitor-query/src/test/java/com/azure/monitor/query/LogsQueryAsyncClientTest.java @@ -12,7 +12,6 @@ import com.azure.core.test.TestMode; import com.azure.core.test.TestProxyTestBase; import com.azure.core.test.http.AssertingHttpClientBuilder; -import com.azure.core.util.Configuration; import com.azure.core.util.Context; import com.azure.core.util.serializer.TypeReference; import com.azure.identity.DefaultAzureCredentialBuilder; @@ -37,6 +36,8 @@ import java.util.LinkedHashMap; import java.util.List; +import static com.azure.monitor.query.MonitorQueryTestUtils.*; +import static com.azure.monitor.query.MonitorQueryTestUtils.getLogResourceId; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertNotNull; import static org.junit.jupiter.api.Assertions.assertNull; @@ -47,21 +48,16 @@ */ public class LogsQueryAsyncClientTest extends TestProxyTestBase { - private static final String WORKSPACE_ID = Configuration.getGlobalConfiguration() - .get("AZURE_MONITOR_LOGS_WORKSPACE_ID", "d2d0e126-fa1e-4b0a-b647-250cdd471e68"); + private LogsQueryAsyncClient client; - static final String RESOURCE_ID = Configuration.getGlobalConfiguration() - .get("AZURE_MONITOR_LOGS_RESOURCE_ID", "subscriptions/faa080af-c1d8-40ad-9cce-e1a450ca5b57/resourceGroups/srnagar-azuresdkgroup/providers/Microsoft.Storage/storageAccounts/srnagarstorage"); + private String workspaceId; - private LogsQueryAsyncClient client; - private static final String QUERY_STRING = "let dt = datatable (DateTime: datetime, Bool:bool, Guid: guid, Int: " - + "int, Long:long, Double: double, String: string, Timespan: timespan, Decimal: decimal, Dynamic: dynamic)\n" - + "[datetime(2015-12-31 23:59:59.9), false, guid(74be27de-1e4e-49d9-b579-fe0b331d3642), 12345, 1, 12345.6789," - + " 'string value', 10s, decimal(0.10101), dynamic({\"a\":123, \"b\":\"hello\", \"c\":[1,2,3], \"d\":{}})];" - + "range x from 1 to 100 step 1 | extend y=1 | join kind=fullouter dt on $left.y == $right.Long"; + private String resourceId; @BeforeEach public void setup() { + workspaceId = getLogWorkspaceId(interceptorManager.isPlaybackMode()); + resourceId = getLogResourceId(interceptorManager.isPlaybackMode()); LogsQueryClientBuilder clientBuilder = new LogsQueryClientBuilder() .retryPolicy(new RetryPolicy(new RetryStrategy() { @Override @@ -84,6 +80,7 @@ public Duration calculateRetryDelay(int i) { .credential(getCredential()); } else if (getTestMode() == TestMode.LIVE) { clientBuilder.credential(getCredential()); + clientBuilder.endpoint(MonitorQueryTestUtils.getLogEndpoint()); } this.client = clientBuilder .buildAsyncClient(); @@ -102,7 +99,7 @@ private TokenCredential getCredential() { @Test public void testLogsQuery() { - StepVerifier.create(client.queryWorkspace(WORKSPACE_ID, QUERY_STRING, + StepVerifier.create(client.queryWorkspace(workspaceId, QUERY_STRING, new QueryTimeInterval(OffsetDateTime.of(LocalDateTime.of(2021, 01, 01, 0, 0), ZoneOffset.UTC), OffsetDateTime.of(LocalDateTime.of(2021, 06, 10, 0, 0), ZoneOffset.UTC)))) .assertNext(queryResults -> { @@ -115,7 +112,7 @@ public void testLogsQuery() { @Test public void testLogsResourceQuery() { - StepVerifier.create(client.queryResource(RESOURCE_ID, QUERY_STRING, + StepVerifier.create(client.queryResource(resourceId, QUERY_STRING, QueryTimeInterval.ALL)) .assertNext(queryResults -> { assertEquals(1, queryResults.getAllTables().size()); @@ -138,7 +135,7 @@ public void testLogsQueryAllowPartialSuccess() { final QueryTimeInterval interval = QueryTimeInterval.LAST_DAY; // Act - StepVerifier.create(client.queryWorkspaceWithResponse(WORKSPACE_ID, + StepVerifier.create(client.queryWorkspaceWithResponse(workspaceId, query, interval, options, Context.NONE)) .assertNext(response -> { // Assert @@ -155,8 +152,8 @@ public void testLogsQueryAllowPartialSuccess() { @Test public void testLogsQueryBatch() { LogsBatchQuery logsBatchQuery = new LogsBatchQuery(); - logsBatchQuery.addWorkspaceQuery(WORKSPACE_ID, QUERY_STRING + " | take 2", null); - logsBatchQuery.addWorkspaceQuery(WORKSPACE_ID, QUERY_STRING + "| take 3", null); + logsBatchQuery.addWorkspaceQuery(workspaceId, QUERY_STRING + " | take 2", null); + logsBatchQuery.addWorkspaceQuery(workspaceId, QUERY_STRING + "| take 3", null); StepVerifier.create(client .queryBatchWithResponse(logsBatchQuery, Context.NONE)) @@ -179,10 +176,10 @@ public void testLogsQueryBatch() { @Test public void testLogsQueryBatchWithServerTimeout() { LogsBatchQuery logsBatchQuery = new LogsBatchQuery(); - logsBatchQuery.addWorkspaceQuery(WORKSPACE_ID, QUERY_STRING + " | take 2", null); - logsBatchQuery.addWorkspaceQuery(WORKSPACE_ID, QUERY_STRING + " | take 5", null, + logsBatchQuery.addWorkspaceQuery(workspaceId, QUERY_STRING + " | take 2", null); + logsBatchQuery.addWorkspaceQuery(workspaceId, QUERY_STRING + " | take 5", null, new LogsQueryOptions().setServerTimeout(Duration.ofSeconds(20))); - logsBatchQuery.addWorkspaceQuery(WORKSPACE_ID, QUERY_STRING + "| take 3", null, + logsBatchQuery.addWorkspaceQuery(workspaceId, QUERY_STRING + "| take 3", null, new LogsQueryOptions().setServerTimeout(Duration.ofSeconds(10))); StepVerifier.create(client @@ -211,7 +208,7 @@ public void testLogsQueryBatchWithServerTimeout() { @DisabledIfEnvironmentVariable(named = "AZURE_TEST_MODE", matches = "LIVE", disabledReason = "multi-workspace " + "queries require sending logs to Azure Monitor first. So, run this test in playback or record mode only.") public void testMultipleWorkspaces() { - StepVerifier.create(client.queryWorkspaceWithResponse(WORKSPACE_ID, + StepVerifier.create(client.queryWorkspaceWithResponse(workspaceId, "union * | where TimeGenerated > ago(100d) | project TenantId | summarize count() by TenantId", null, new LogsQueryOptions() .setAdditionalWorkspaces(Arrays.asList("9dad0092-fd13-403a-b367-a189a090a541")), @@ -233,8 +230,8 @@ public void testMultipleWorkspaces() { @Test public void testBatchQueryPartialSuccess() { LogsBatchQuery logsBatchQuery = new LogsBatchQuery(); - logsBatchQuery.addWorkspaceQuery(WORKSPACE_ID, QUERY_STRING + " | take 2", null); - logsBatchQuery.addWorkspaceQuery(WORKSPACE_ID, QUERY_STRING + " | take", null); + logsBatchQuery.addWorkspaceQuery(workspaceId, QUERY_STRING + " | take 2", null); + logsBatchQuery.addWorkspaceQuery(workspaceId, QUERY_STRING + " | take", null); StepVerifier.create(client .queryBatchWithResponse(logsBatchQuery, Context.NONE)) @@ -254,7 +251,7 @@ public void testBatchQueryPartialSuccess() { @Test public void testStatistics() { - StepVerifier.create(client.queryWorkspaceWithResponse(WORKSPACE_ID, + StepVerifier.create(client.queryWorkspaceWithResponse(workspaceId, QUERY_STRING, null, new LogsQueryOptions().setIncludeStatistics(true), Context.NONE)) .assertNext(response -> { LogsQueryResult queryResults = response.getValue(); @@ -266,7 +263,7 @@ QUERY_STRING, null, new LogsQueryOptions().setIncludeStatistics(true), Context.N @Test public void testStatisticsResourceQuery() { - StepVerifier.create(client.queryResourceWithResponse(RESOURCE_ID, + StepVerifier.create(client.queryResourceWithResponse(resourceId, QUERY_STRING, null, new LogsQueryOptions().setIncludeStatistics(true), Context.NONE)) .assertNext(response -> { LogsQueryResult queryResults = response.getValue(); @@ -279,9 +276,9 @@ QUERY_STRING, null, new LogsQueryOptions().setIncludeStatistics(true), Context.N @Test public void testBatchStatistics() { LogsBatchQuery logsBatchQuery = new LogsBatchQuery(); - logsBatchQuery.addWorkspaceQuery(WORKSPACE_ID, QUERY_STRING, null); - logsBatchQuery.addWorkspaceQuery(WORKSPACE_ID, QUERY_STRING, null, - new LogsQueryOptions().setIncludeStatistics(true)); + logsBatchQuery.addWorkspaceQuery(workspaceId, QUERY_STRING, null); + logsBatchQuery.addWorkspaceQuery(workspaceId, QUERY_STRING, null, + new LogsQueryOptions().setIncludeStatistics(true)); StepVerifier.create(client .queryBatchWithResponse(logsBatchQuery, Context.NONE)) @@ -314,7 +311,7 @@ public void testServerTimeout() { long count = 1000000006959L; // this query should take more than 5 seconds usually, but the server may have cached the // response and may return before 5 seconds. So, retry with another query (different count value) - StepVerifier.create(client.queryWorkspaceWithResponse(WORKSPACE_ID, "range x from 1 to " + count + " " + StepVerifier.create(client.queryWorkspaceWithResponse(workspaceId, "range x from 1 to " + count + " " + "step 1 | count", null, new LogsQueryOptions() @@ -334,7 +331,7 @@ public void testServerTimeout() { public void testVisualization() { String query = "datatable (s: string, i: long) [ \"a\", 1, \"b\", 2, \"c\", 3 ] " + "| render columnchart with (title=\"the chart title\", xtitle=\"the x axis title\")"; - StepVerifier.create(client.queryWorkspaceWithResponse(WORKSPACE_ID, + StepVerifier.create(client.queryWorkspaceWithResponse(workspaceId, query, null, new LogsQueryOptions().setIncludeStatistics(true).setIncludeVisualization(true), Context.NONE)) .assertNext(response -> { @@ -361,7 +358,7 @@ query, null, new LogsQueryOptions().setIncludeStatistics(true).setIncludeVisuali public void testVisualizationResourceQuery() { String query = "datatable (s: string, i: long) [ \"a\", 1, \"b\", 2, \"c\", 3 ] " + "| render columnchart with (title=\"the chart title\", xtitle=\"the x axis title\")"; - StepVerifier.create(client.queryResourceWithResponse(RESOURCE_ID, + StepVerifier.create(client.queryResourceWithResponse(resourceId, query, null, new LogsQueryOptions().setIncludeStatistics(true).setIncludeVisualization(true), Context.NONE)) .assertNext(response -> { diff --git a/sdk/monitor/azure-monitor-query/src/test/java/com/azure/monitor/query/LogsQueryClientTest.java b/sdk/monitor/azure-monitor-query/src/test/java/com/azure/monitor/query/LogsQueryClientTest.java index 6b99ea866709a..24f9ad09bdca9 100644 --- a/sdk/monitor/azure-monitor-query/src/test/java/com/azure/monitor/query/LogsQueryClientTest.java +++ b/sdk/monitor/azure-monitor-query/src/test/java/com/azure/monitor/query/LogsQueryClientTest.java @@ -13,7 +13,6 @@ import com.azure.core.test.TestMode; import com.azure.core.test.TestProxyTestBase; import com.azure.core.test.http.AssertingHttpClientBuilder; -import com.azure.core.util.Configuration; import com.azure.core.util.Context; import com.azure.core.util.serializer.TypeReference; import com.azure.identity.DefaultAzureCredentialBuilder; @@ -37,28 +36,30 @@ import java.util.LinkedHashMap; import java.util.List; -import static com.azure.monitor.query.LogsQueryAsyncClientTest.RESOURCE_ID; +import static com.azure.monitor.query.MonitorQueryTestUtils.QUERY_STRING; +import static com.azure.monitor.query.MonitorQueryTestUtils.getLogWorkspaceId; +import static com.azure.monitor.query.MonitorQueryTestUtils.getLogResourceId; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertNotNull; import static org.junit.jupiter.api.Assertions.assertNull; import static org.junit.jupiter.api.Assertions.assertTrue; + /** * Unit tests for {@link LogsQueryClient} */ public class LogsQueryClientTest extends TestProxyTestBase { - private static final String WORKSPACE_ID = Configuration.getGlobalConfiguration() - .get("AZURE_MONITOR_LOGS_WORKSPACE_ID", "d2d0e126-fa1e-4b0a-b647-250cdd471e68"); private LogsQueryClient client; - private static final String QUERY_STRING = "let dt = datatable (DateTime: datetime, Bool:bool, Guid: guid, Int: " - + "int, Long:long, Double: double, String: string, Timespan: timespan, Decimal: decimal, Dynamic: dynamic)\n" - + "[datetime(2015-12-31 23:59:59.9), false, guid(74be27de-1e4e-49d9-b579-fe0b331d3642), 12345, 1, 12345.6789," - + " 'string value', 10s, decimal(0.10101), dynamic({\"a\":123, \"b\":\"hello\", \"c\":[1,2,3], \"d\":{}})];" - + "range x from 1 to 100 step 1 | extend y=1 | join kind=fullouter dt on $left.y == $right.Long"; + + private String workspaceId; + + private String resourceId; @BeforeEach public void setup() { + workspaceId = getLogWorkspaceId(interceptorManager.isPlaybackMode()); + resourceId = getLogResourceId(interceptorManager.isPlaybackMode()); LogsQueryClientBuilder clientBuilder = new LogsQueryClientBuilder() .retryPolicy(new RetryPolicy(new RetryStrategy() { @Override @@ -81,6 +82,7 @@ public Duration calculateRetryDelay(int i) { .credential(getCredential()); } else if (getTestMode() == TestMode.LIVE) { clientBuilder.credential(getCredential()); + clientBuilder.endpoint(MonitorQueryTestUtils.getLogEndpoint()); } this.client = clientBuilder .buildClient(); @@ -99,7 +101,7 @@ private TokenCredential getCredential() { @Test public void testLogsQuery() { - LogsQueryResult queryResults = client.queryWorkspace(WORKSPACE_ID, QUERY_STRING, + LogsQueryResult queryResults = client.queryWorkspace(workspaceId, QUERY_STRING, new QueryTimeInterval(OffsetDateTime.of(LocalDateTime.of(2021, 01, 01, 0, 0), ZoneOffset.UTC), OffsetDateTime.of(LocalDateTime.of(2021, 06, 10, 0, 0), ZoneOffset.UTC))); assertEquals(1, queryResults.getAllTables().size()); @@ -109,7 +111,7 @@ public void testLogsQuery() { @Test public void testLogsQueryResource() { - LogsQueryResult queryResults = client.queryResource(RESOURCE_ID, QUERY_STRING, + LogsQueryResult queryResults = client.queryResource(resourceId, QUERY_STRING, new QueryTimeInterval(OffsetDateTime.of(LocalDateTime.of(2021, 01, 01, 0, 0), ZoneOffset.UTC), OffsetDateTime.of(LocalDateTime.of(2021, 06, 10, 0, 0), ZoneOffset.UTC))); assertEquals(1, queryResults.getAllTables().size()); @@ -131,7 +133,7 @@ public void testLogsQueryAllowPartialSuccess() { final QueryTimeInterval interval = QueryTimeInterval.LAST_DAY; // Act - final Response response = client.queryWorkspaceWithResponse(WORKSPACE_ID, query, interval, + final Response response = client.queryWorkspaceWithResponse(workspaceId, query, interval, options, Context.NONE); // Assert @@ -146,8 +148,8 @@ public void testLogsQueryAllowPartialSuccess() { @Test public void testLogsQueryBatch() { LogsBatchQuery logsBatchQuery = new LogsBatchQuery(); - logsBatchQuery.addWorkspaceQuery(WORKSPACE_ID, QUERY_STRING + " | take 2", null); - logsBatchQuery.addWorkspaceQuery(WORKSPACE_ID, QUERY_STRING + "| take 3", null); + logsBatchQuery.addWorkspaceQuery(workspaceId, QUERY_STRING + " | take 2", null); + logsBatchQuery.addWorkspaceQuery(workspaceId, QUERY_STRING + "| take 3", null); LogsBatchQueryResultCollection batchResultCollection = client .queryBatchWithResponse(logsBatchQuery, Context.NONE).getValue(); @@ -168,10 +170,10 @@ public void testLogsQueryBatch() { @Test public void testLogsQueryBatchWithServerTimeout() { LogsBatchQuery logsBatchQuery = new LogsBatchQuery(); - logsBatchQuery.addWorkspaceQuery(WORKSPACE_ID, QUERY_STRING + " | take 2", null); - logsBatchQuery.addWorkspaceQuery(WORKSPACE_ID, QUERY_STRING + " | take 5", null, + logsBatchQuery.addWorkspaceQuery(workspaceId, QUERY_STRING + " | take 2", null); + logsBatchQuery.addWorkspaceQuery(workspaceId, QUERY_STRING + " | take 5", null, new LogsQueryOptions().setServerTimeout(Duration.ofSeconds(20))); - logsBatchQuery.addWorkspaceQuery(WORKSPACE_ID, QUERY_STRING + "| take 3", null, + logsBatchQuery.addWorkspaceQuery(workspaceId, QUERY_STRING + "| take 3", null, new LogsQueryOptions().setServerTimeout(Duration.ofSeconds(10))); LogsBatchQueryResultCollection batchResultCollection = client @@ -198,7 +200,7 @@ public void testLogsQueryBatchWithServerTimeout() { @DisabledIfEnvironmentVariable(named = "AZURE_TEST_MODE", matches = "LIVE", disabledReason = "multi-workspace " + "queries require sending logs to Azure Monitor first. So, run this test in playback or record mode only.") public void testMultipleWorkspaces() { - LogsQueryResult queryResults = client.queryWorkspaceWithResponse(WORKSPACE_ID, + LogsQueryResult queryResults = client.queryWorkspaceWithResponse(workspaceId, "union * | where TimeGenerated > ago(100d) | project TenantId | summarize count() by TenantId", null, new LogsQueryOptions() .setAdditionalWorkspaces(Arrays.asList("9dad0092-fd13-403a-b367-a189a090a541")), Context.NONE) @@ -210,7 +212,6 @@ public void testMultipleWorkspaces() { .getRows() .stream() .map(row -> { - System.out.println(row.getColumnValue("TenantId").get().getValueAsString()); return row.getColumnValue("TenantId").get(); }) .distinct() @@ -220,8 +221,8 @@ public void testMultipleWorkspaces() { @Test public void testBatchQueryPartialSuccess() { LogsBatchQuery logsBatchQuery = new LogsBatchQuery(); - logsBatchQuery.addWorkspaceQuery(WORKSPACE_ID, QUERY_STRING + " | take 2", null); - logsBatchQuery.addWorkspaceQuery(WORKSPACE_ID, QUERY_STRING + " | take", null); + logsBatchQuery.addWorkspaceQuery(workspaceId, QUERY_STRING + " | take 2", null); + logsBatchQuery.addWorkspaceQuery(workspaceId, QUERY_STRING + " | take", null); LogsBatchQueryResultCollection batchResultCollection = client .queryBatchWithResponse(logsBatchQuery, Context.NONE).getValue(); @@ -238,7 +239,7 @@ public void testBatchQueryPartialSuccess() { @Test public void testStatistics() { - LogsQueryResult queryResults = client.queryWorkspaceWithResponse(WORKSPACE_ID, + LogsQueryResult queryResults = client.queryWorkspaceWithResponse(workspaceId, QUERY_STRING, null, new LogsQueryOptions().setIncludeStatistics(true), Context.NONE).getValue(); assertEquals(1, queryResults.getAllTables().size()); @@ -247,7 +248,7 @@ public void testStatistics() { @Test public void testStatisticsResourceQuery() { - LogsQueryResult queryResults = client.queryResourceWithResponse(RESOURCE_ID, + LogsQueryResult queryResults = client.queryResourceWithResponse(resourceId, QUERY_STRING, null, new LogsQueryOptions().setIncludeStatistics(true), Context.NONE) .getValue(); assertEquals(1, queryResults.getAllTables().size()); @@ -257,9 +258,9 @@ QUERY_STRING, null, new LogsQueryOptions().setIncludeStatistics(true), Context.N @Test public void testBatchStatistics() { LogsBatchQuery logsBatchQuery = new LogsBatchQuery(); - logsBatchQuery.addWorkspaceQuery(WORKSPACE_ID, QUERY_STRING, null); - logsBatchQuery.addWorkspaceQuery(WORKSPACE_ID, QUERY_STRING, null, - new LogsQueryOptions().setIncludeStatistics(true)); + logsBatchQuery.addWorkspaceQuery(workspaceId, QUERY_STRING, null); + logsBatchQuery.addWorkspaceQuery(workspaceId, QUERY_STRING, null, + new LogsQueryOptions().setIncludeStatistics(true)); LogsBatchQueryResultCollection batchResultCollection = client .queryBatchWithResponse(logsBatchQuery, Context.NONE).getValue(); @@ -291,7 +292,7 @@ public void testServerTimeout() { try { // this query should take more than 5 seconds usually, but the server may have cached the // response and may return before 5 seconds. So, retry with another query (different count value) - client.queryWorkspaceWithResponse(WORKSPACE_ID, "range x from 1 to " + count + " step 1 | count", null, + client.queryWorkspaceWithResponse(workspaceId, "range x from 1 to " + count + " step 1 | count", null, new LogsQueryOptions() .setServerTimeout(Duration.ofSeconds(5)), Context.NONE); @@ -310,7 +311,7 @@ public void testServerTimeout() { public void testVisualization() { String query = "datatable (s: string, i: long) [ \"a\", 1, \"b\", 2, \"c\", 3 ] " + "| render columnchart with (title=\"the chart title\", xtitle=\"the x axis title\")"; - LogsQueryResult queryResults = client.queryWorkspaceWithResponse(WORKSPACE_ID, + LogsQueryResult queryResults = client.queryWorkspaceWithResponse(workspaceId, query, null, new LogsQueryOptions().setIncludeStatistics(true).setIncludeVisualization(true), Context.NONE).getValue(); @@ -331,7 +332,7 @@ query, null, new LogsQueryOptions().setIncludeStatistics(true).setIncludeVisuali public void testVisualizationResourceQuery() { String query = "datatable (s: string, i: long) [ \"a\", 1, \"b\", 2, \"c\", 3 ] " + "| render columnchart with (title=\"the chart title\", xtitle=\"the x axis title\")"; - LogsQueryResult queryResults = client.queryResourceWithResponse(RESOURCE_ID, + LogsQueryResult queryResults = client.queryResourceWithResponse(resourceId, query, null, new LogsQueryOptions().setIncludeStatistics(true).setIncludeVisualization(true), Context.NONE).getValue(); assertEquals(1, queryResults.getAllTables().size()); diff --git a/sdk/monitor/azure-monitor-query/src/test/java/com/azure/monitor/query/MetricsQueryAsyncClientTest.java b/sdk/monitor/azure-monitor-query/src/test/java/com/azure/monitor/query/MetricsQueryAsyncClientTest.java index d22cf77e348e0..7304073d784b9 100644 --- a/sdk/monitor/azure-monitor-query/src/test/java/com/azure/monitor/query/MetricsQueryAsyncClientTest.java +++ b/sdk/monitor/azure-monitor-query/src/test/java/com/azure/monitor/query/MetricsQueryAsyncClientTest.java @@ -9,7 +9,6 @@ import com.azure.core.test.TestMode; import com.azure.core.test.TestProxyTestBase; import com.azure.core.test.http.AssertingHttpClientBuilder; -import com.azure.core.util.Configuration; import com.azure.core.util.Context; import com.azure.identity.DefaultAzureCredentialBuilder; import com.azure.monitor.query.models.AggregationType; @@ -36,6 +35,7 @@ import java.util.stream.Collectors; import java.util.stream.Stream; +import static com.azure.monitor.query.MonitorQueryTestUtils.getMetricResourceUri; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertTrue; @@ -43,11 +43,11 @@ * Unit tests for {@link MetricsQueryAsyncClient}. */ public class MetricsQueryAsyncClientTest extends TestProxyTestBase { - private static final String RESOURCE_URI = Configuration.getGlobalConfiguration() - .get("AZURE_MONITOR_METRICS_RESOURCE_URI", - "/subscriptions/faa080af-c1d8-40ad-9cce-e1a450ca5b57/resourceGroups/srnagar-azuresdkgroup/providers/Microsoft.CognitiveServices/accounts/srnagara-textanalytics"); + private MetricsQueryAsyncClient client; + private String resourceUri; + private static Stream getFilterPredicate() { return Arrays.asList( Arguments.of(AggregationType.AVERAGE, @@ -85,6 +85,7 @@ private static Stream getFilterPredicate() { @BeforeEach public void setup() { + resourceUri = getMetricResourceUri(interceptorManager.isPlaybackMode()); MetricsQueryClientBuilder clientBuilder = new MetricsQueryClientBuilder(); if (getTestMode() == TestMode.PLAYBACK) { clientBuilder @@ -96,6 +97,7 @@ public void setup() { .credential(getCredential()); } else if (getTestMode() == TestMode.LIVE) { clientBuilder.credential(getCredential()); + clientBuilder.endpoint(MonitorQueryTestUtils.getMetricEndpoint()); } this.client = clientBuilder .buildAsyncClient(); @@ -115,9 +117,9 @@ private TokenCredential getCredential() { @Test public void testMetricsQuery() { StepVerifier.create(client - .queryResourceWithResponse(RESOURCE_URI, Arrays.asList("SuccessfulCalls"), + .queryResourceWithResponse(resourceUri, Arrays.asList("SuccessfulRequests"), new MetricsQueryOptions() - .setMetricNamespace("Microsoft.CognitiveServices/accounts") + .setMetricNamespace("Microsoft.EventHub/namespaces") .setTimeInterval(new QueryTimeInterval(Duration.ofDays(10))) .setGranularity(Duration.ofHours(1)) .setTop(100) @@ -130,7 +132,7 @@ public void testMetricsQuery() { assertEquals(1, metrics.size()); MetricResult successfulCallsMetric = metrics.get(0); - assertEquals("SuccessfulCalls", successfulCallsMetric.getMetricName()); + assertEquals("SuccessfulRequests", successfulCallsMetric.getMetricName()); assertEquals("Microsoft.Insights/metrics", successfulCallsMetric.getResourceType()); assertEquals(1, successfulCallsMetric.getTimeSeries().size()); @@ -148,9 +150,9 @@ public void testMetricsQuery() { @MethodSource("getFilterPredicate") public void testAggregation(AggregationType aggregationType, Predicate metricValuePredicate) { StepVerifier.create(client - .queryResourceWithResponse(RESOURCE_URI, Arrays.asList("SuccessfulCalls"), + .queryResourceWithResponse(resourceUri, Arrays.asList("SuccessfulRequests"), new MetricsQueryOptions() - .setMetricNamespace("Microsoft.CognitiveServices/accounts") + .setMetricNamespace("Microsoft.EventHub/namespaces") .setTimeInterval(new QueryTimeInterval(Duration.ofDays(10))) .setGranularity(Duration.ofHours(1)) .setTop(100) @@ -172,23 +174,46 @@ public void testAggregation(AggregationType aggregationType, Predicate knownMetricsDefinitions = Arrays.asList( - "TotalCalls", - "SuccessfulCalls", - "TotalErrors", - "BlockedCalls", - "ServerErrors", - "ClientErrors", - "DataIn", - "DataOut", - "Latency", - "TotalTransactions", - "ProcessedTextRecords", - "ProcessedHealthTextRecords", - "QuestionAnsweringTextRecords" + "SuccessfulRequests", + "ServerErrors", + "UserErrors", + "QuotaExceededErrors", + "ThrottledRequests", + "IncomingRequests", + "IncomingMessages", + "OutgoingMessages", + "IncomingBytes", + "OutgoingBytes", + "ActiveConnections", + "ConnectionsOpened", + "ConnectionsClosed", + "CaptureBacklog", + "CapturedMessages", + "CapturedBytes", + "Size", + "INREQS", + "SUCCREQ", + "FAILREQ", + "SVRBSY", + "INTERR", + "MISCERR", + "INMSGS", + "EHINMSGS", + "OUTMSGS", + "EHOUTMSGS", + "EHINMBS", + "EHINBYTES", + "EHOUTMBS", + "EHOUTBYTES", + "EHABL", + "EHAMSGS", + "EHAMBS", + "NamespaceCpuUsage", + "NamespaceMemoryUsage" ); StepVerifier.create(client - .listMetricDefinitions(RESOURCE_URI) + .listMetricDefinitions(resourceUri) .collectList()) .assertNext(metricDefinitions -> assertTrue(metricDefinitions.stream() .map(MetricDefinition::getName) @@ -199,7 +224,7 @@ public void testMetricsDefinition() { @Test public void testMetricsNamespaces() { - StepVerifier.create(client.listMetricNamespaces(RESOURCE_URI, null).collectList()) + StepVerifier.create(client.listMetricNamespaces(resourceUri, null).collectList()) .assertNext(namespaces -> assertEquals(1, namespaces.size())) .verifyComplete(); } diff --git a/sdk/monitor/azure-monitor-query/src/test/java/com/azure/monitor/query/MetricsQueryClientTest.java b/sdk/monitor/azure-monitor-query/src/test/java/com/azure/monitor/query/MetricsQueryClientTest.java index d2538c8c173e9..4011118f11894 100644 --- a/sdk/monitor/azure-monitor-query/src/test/java/com/azure/monitor/query/MetricsQueryClientTest.java +++ b/sdk/monitor/azure-monitor-query/src/test/java/com/azure/monitor/query/MetricsQueryClientTest.java @@ -11,7 +11,6 @@ import com.azure.core.test.TestMode; import com.azure.core.test.TestProxyTestBase; import com.azure.core.test.http.AssertingHttpClientBuilder; -import com.azure.core.util.Configuration; import com.azure.core.util.Context; import com.azure.identity.DefaultAzureCredentialBuilder; import com.azure.monitor.query.models.AggregationType; @@ -38,6 +37,7 @@ import java.util.stream.Collectors; import java.util.stream.Stream; +import static com.azure.monitor.query.MonitorQueryTestUtils.getMetricResourceUri; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertTrue; @@ -45,11 +45,10 @@ * Unit tests for {@link MetricsQueryClient}. */ public class MetricsQueryClientTest extends TestProxyTestBase { - private static final String RESOURCE_URI = Configuration.getGlobalConfiguration() - .get("AZURE_MONITOR_METRICS_RESOURCE_URI", - "/subscriptions/faa080af-c1d8-40ad-9cce-e1a450ca5b57/resourceGroups/srnagar-azuresdkgroup/providers/Microsoft.CognitiveServices/accounts/srnagara-textanalytics"); private MetricsQueryClient client; + private String resourceUri; + private static Stream getFilterPredicate() { return Arrays.asList( Arguments.of(AggregationType.AVERAGE, @@ -87,6 +86,7 @@ private static Stream getFilterPredicate() { @BeforeEach public void setup() { + resourceUri = getMetricResourceUri(interceptorManager.isPlaybackMode()); MetricsQueryClientBuilder clientBuilder = new MetricsQueryClientBuilder(); if (getTestMode() == TestMode.PLAYBACK) { clientBuilder @@ -98,6 +98,7 @@ public void setup() { .credential(getCredential()); } else if (getTestMode() == TestMode.LIVE) { clientBuilder.credential(getCredential()); + clientBuilder.endpoint(MonitorQueryTestUtils.getMetricEndpoint()); } this.client = clientBuilder .buildClient(); @@ -117,9 +118,9 @@ private TokenCredential getCredential() { @Test public void testMetricsQuery() { Response metricsResponse = client - .queryResourceWithResponse(RESOURCE_URI, Arrays.asList("SuccessfulCalls"), + .queryResourceWithResponse(resourceUri, Arrays.asList("SuccessfulRequests"), new MetricsQueryOptions() - .setMetricNamespace("Microsoft.CognitiveServices/accounts") + .setMetricNamespace("Microsoft.EventHub/namespaces") .setTimeInterval(new QueryTimeInterval(Duration.ofDays(10))) .setGranularity(Duration.ofHours(1)) .setTop(100) @@ -132,7 +133,7 @@ public void testMetricsQuery() { assertEquals(1, metrics.size()); MetricResult successfulCallsMetric = metrics.get(0); - assertEquals("SuccessfulCalls", successfulCallsMetric.getMetricName()); + assertEquals("SuccessfulRequests", successfulCallsMetric.getMetricName()); assertEquals("Microsoft.Insights/metrics", successfulCallsMetric.getResourceType()); assertEquals(1, successfulCallsMetric.getTimeSeries().size()); @@ -146,9 +147,9 @@ public void testMetricsQuery() { @MethodSource("getFilterPredicate") public void testAggregation(AggregationType aggregationType, Predicate metricValuePredicate) { Response metricsResponse = client - .queryResourceWithResponse(RESOURCE_URI, Arrays.asList("SuccessfulCalls"), + .queryResourceWithResponse(resourceUri, Arrays.asList("SuccessfulRequests"), new MetricsQueryOptions() - .setMetricNamespace("Microsoft.CognitiveServices/accounts") + .setMetricNamespace("Microsoft.EventHub/namespaces") .setTimeInterval(new QueryTimeInterval(Duration.ofDays(10))) .setGranularity(Duration.ofHours(1)) .setTop(100) @@ -168,22 +169,45 @@ public void testAggregation(AggregationType aggregationType, Predicate metricsDefinitions = client - .listMetricDefinitions(RESOURCE_URI); + .listMetricDefinitions(resourceUri); List knownMetricsDefinitions = Arrays.asList( - "TotalCalls", - "SuccessfulCalls", - "TotalErrors", - "BlockedCalls", - "ServerErrors", - "ClientErrors", - "DataIn", - "DataOut", - "Latency", - "TotalTransactions", - "ProcessedTextRecords", - "ProcessedHealthTextRecords", - "QuestionAnsweringTextRecords" + "SuccessfulRequests", + "ServerErrors", + "UserErrors", + "QuotaExceededErrors", + "ThrottledRequests", + "IncomingRequests", + "IncomingMessages", + "OutgoingMessages", + "IncomingBytes", + "OutgoingBytes", + "ActiveConnections", + "ConnectionsOpened", + "ConnectionsClosed", + "CaptureBacklog", + "CapturedMessages", + "CapturedBytes", + "Size", + "INREQS", + "SUCCREQ", + "FAILREQ", + "SVRBSY", + "INTERR", + "MISCERR", + "INMSGS", + "EHINMSGS", + "OUTMSGS", + "EHOUTMSGS", + "EHINMBS", + "EHINBYTES", + "EHOUTMBS", + "EHOUTBYTES", + "EHABL", + "EHAMSGS", + "EHAMBS", + "NamespaceCpuUsage", + "NamespaceMemoryUsage" ); assertTrue(metricsDefinitions.stream() .map(MetricDefinition::getName) @@ -193,7 +217,7 @@ public void testMetricsDefinition() { @Test public void testMetricsNamespaces() { - PagedIterable metricsNamespaces = client.listMetricNamespaces(RESOURCE_URI, null); + PagedIterable metricsNamespaces = client.listMetricNamespaces(resourceUri, null); assertEquals(1, metricsNamespaces.stream().count()); } } diff --git a/sdk/monitor/azure-monitor-query/src/test/java/com/azure/monitor/query/MonitorQueryTestUtils.java b/sdk/monitor/azure-monitor-query/src/test/java/com/azure/monitor/query/MonitorQueryTestUtils.java new file mode 100644 index 0000000000000..7826999b11596 --- /dev/null +++ b/sdk/monitor/azure-monitor-query/src/test/java/com/azure/monitor/query/MonitorQueryTestUtils.java @@ -0,0 +1,70 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. + +package com.azure.monitor.query; + +import com.azure.core.util.Configuration; + +import java.util.HashMap; + +public final class MonitorQueryTestUtils { + + private MonitorQueryTestUtils() { + + } + private static final String LOG_WORKSPACE_ID = Configuration.getGlobalConfiguration() + .get("AZURE_MONITOR_LOGS_WORKSPACE_ID"); + + private static final String LOG_RESOURCE_ID = Configuration.getGlobalConfiguration() + .get("AZURE_MONITOR_LOGS_RESOURCE_ID"); + + private static final String METRIC_RESOURCE_URI = Configuration.getGlobalConfiguration() + .get("AZURE_MONITOR_METRICS_RESOURCE_URI"); + + + public static final String QUERY_STRING = "let dt = datatable (DateTime: datetime, Bool:bool, Guid: guid, Int: " + + "int, Long:long, Double: double, String: string, Timespan: timespan, Decimal: decimal, Dynamic: dynamic)\n" + + "[datetime(2015-12-31 23:59:59.9), false, guid(74be27de-1e4e-49d9-b579-fe0b331d3642), 12345, 1, 12345.6789," + + " 'string value', 10s, decimal(0.10101), dynamic({\"a\":123, \"b\":\"hello\", \"c\":[1,2,3], \"d\":{}})];" + + "range x from 1 to 100 step 1 | extend y=1 | join kind=fullouter dt on $left.y == $right.Long"; + + public static final HashMap ENDPOINTS = new HashMap() { + { + put("AzureCloud", "https://api.loganalytics.io/v1"); + put("AzureChinaCloud", "https://api.loganalytics.azure.cn/v1"); + put("AzureUSGovernment", "https://api.loganalytics.us/v1"); + } + }; + + public static String getLogWorkspaceId(boolean isPlaybackMode) { + if (isPlaybackMode) { + return "d2d0e126-fa1e-4b0a-b647-250cdd471e68"; + } else { + return LOG_WORKSPACE_ID; + } + } + + public static String getLogResourceId(boolean isPlaybackMode) { + if (isPlaybackMode) { + return "subscriptions/faa080af-c1d8-40ad-9cce-e1a450ca5b57/resourceGroups/srnagar-azuresdkgroup/providers/Microsoft.Storage/storageAccounts/srnagarstorage"; + } else { + return LOG_RESOURCE_ID; + } + } + + public static String getMetricResourceUri(boolean isPlaybackMode) { + if (isPlaybackMode) { + return "/subscriptions/faa080af-c1d8-40ad-9cce-e1a450ca5b57/resourceGroups/rg-jairmyree-test/providers/Microsoft.EventHub/namespaces/eventhubt9a76fe3bdd7263f8"; + } else { + return METRIC_RESOURCE_URI; + } + } + + public static String getLogEndpoint() { + return ENDPOINTS.get(Configuration.getGlobalConfiguration().get("MONITOR_ENVIRONMENT")); + } + + public static String getMetricEndpoint() { + return Configuration.getGlobalConfiguration().get("MONITOR_RESOURCE_MANAGER_URL"); + } +} diff --git a/sdk/monitor/azure-monitor-query/swagger/src/main/java/LogsCustomization.java b/sdk/monitor/azure-monitor-query/swagger/src/main/java/LogsCustomization.java index 1f5c82e955dfa..dcdf9e936cca0 100644 --- a/sdk/monitor/azure-monitor-query/swagger/src/main/java/LogsCustomization.java +++ b/sdk/monitor/azure-monitor-query/swagger/src/main/java/LogsCustomization.java @@ -22,8 +22,19 @@ public void customize(LibraryCustomization libraryCustomization, Logger logger) "/logs/AzureLogAnalyticsImplBuilder.java") .replace("policies.add(new BearerTokenAuthenticationPolicy(tokenCredential, String.format(\"%s/" + ".default\", host)));", - "String localHost = (host != null) ? host : \"https://api.loganalytics.io\";\n" + - "policies.add(new BearerTokenAuthenticationPolicy(tokenCredential, String.format(\"%s/.default\", localHost)));"); + "String localHost;\n" + + " if (host != null) {\n" + + " try {\n" + + " localHost = new java.net.URL(host).getHost();\n" + + " } catch (java.net.MalformedURLException e) {\n" + + " throw new RuntimeException(e);\n" + + " }\n" + + " } else {\n" + + " localHost = \"api.loganalytics.io\";\n" + + " }\n" + + " policies.add(new BearerTokenAuthenticationPolicy(\n" + + " tokenCredential,\n" + + " String.format(\"https://%s/.default\", localHost)));"); libraryCustomization.getRawEditor().replaceFile("src/main/java/com/azure/monitor/query/implementation" + "/logs/AzureLogAnalyticsImplBuilder.java", replace); } diff --git a/sdk/monitor/azure-monitor-query/tests.yml b/sdk/monitor/azure-monitor-query/tests.yml index c1c574e8f1f1b..f67c958ac51af 100644 --- a/sdk/monitor/azure-monitor-query/tests.yml +++ b/sdk/monitor/azure-monitor-query/tests.yml @@ -11,4 +11,4 @@ stages: EnvVars: AZURE_CLIENT_ID: $(aad-azure-sdk-test-client-id) AZURE_TENANT_ID: $(aad-azure-sdk-test-tenant-id) - AZURE_CLIENT_SECRET: $(aad-azure-sdk-test-client-secret) \ No newline at end of file + AZURE_CLIENT_SECRET: $(aad-azure-sdk-test-client-secret) diff --git a/sdk/monitor/test-resources.json b/sdk/monitor/test-resources.json index 26cfd3b94967a..fbe96e7a3a680 100644 --- a/sdk/monitor/test-resources.json +++ b/sdk/monitor/test-resources.json @@ -31,7 +31,7 @@ "type": "String" }, "regionId": { - "defaultValue": "westus2", + "defaultValue": "[resourceGroup().location]", "type": "string", "metadata": { "description": "Which Azure Region to deploy the resource to. This must be a valid Azure regionId." @@ -73,12 +73,7 @@ }, "location": { "type": "string", - "defaultValue": "westus2", - "allowedValues": [ - "westus2", - "eastus2", - "eastus2euap" - ], + "defaultValue": "[resourceGroup().location]", "metadata": { "description": "Specifies the location in which to create the Data Collection Endpoint." } @@ -103,7 +98,7 @@ "uniqueAzConfigName": "[concat(parameters('baseName'),'-' , parameters('configurationStores_azconfig_java_name'))]", "uniqueAppInsightsName": "[concat(parameters('baseName'),'-' , parameters('appinsights_java_name'))]", "endpointValue": "[concat('https://', parameters('baseName'), '-', parameters('configurationStores_azconfig_java_name'), parameters('endpointSuffix'))]", - "location": "[resourceGroup().location]", + "location": "[parameters('location')]", "authorizationApiVersion": "2018-09-01-preview", "authorizationRulesName": "[concat('authr', parameters('baseName'))]", "eventHubsApiVersion": "2017-04-01", @@ -282,7 +277,7 @@ "type": "microsoft.operationalinsights/workspaces", "apiVersion": "2021-12-01-preview", "name": "[parameters('workspaceName')]", - "location": "westus2", + "location": "[variables('location')]", "properties": { "sku": { "name": "pergb2018" @@ -376,6 +371,18 @@ "AZURE_MONITOR_DCR_ID": { "type": "string", "value": "[reference(resourceId('Microsoft.Insights/dataCollectionRules', parameters('dataCollectionRuleName')), '2021-04-01').immutableId]" + }, + "AZURE_MONITOR_LOGS_WORKSPACE_ID": { + "type": "string", + "value": "[reference(resourceId('Microsoft.OperationalInsights/workspaces', parameters('workspaceName'))).customerId]" + }, + "AZURE_MONITOR_LOGS_RESOURCE_ID": { + "type": "string", + "value": "[resourceId('Microsoft.OperationalInsights/workspaces', parameters('workspaceName'))]" + }, + "AZURE_MONITOR_METRICS_RESOURCE_URI": { + "type": "string", + "value": "[resourceId('Microsoft.Eventhub/Namespaces', variables('eventHubsNamespaceName'))]" } } } diff --git a/sdk/monitor/tests.yml b/sdk/monitor/tests.yml index 72be3f8a7d2ac..208844b8aee5e 100644 --- a/sdk/monitor/tests.yml +++ b/sdk/monitor/tests.yml @@ -4,6 +4,7 @@ stages: - template: /eng/pipelines/templates/stages/archetype-sdk-tests.yml parameters: ServiceDirectory: monitor + SupportedClouds: 'Public,UsGov,China' Artifacts: - name: azure-monitor-ingestion groupId: com.azure @@ -11,3 +12,8 @@ stages: - name: azure-monitor-query groupId: com.azure safeName: azuremonitorquery + EnvVars: + AZURE_CLIENT_ID: $(MONITOR_CLIENT_ID) + AZURE_TENANT_ID: $(MONITOR_TENANT_ID) + AZURE_CLIENT_SECRET: $(MONITOR_CLIENT_SECRET) + AZURE_SUBSCRIPTION_ID: $(MONITOR_SUBSCRIPTION_ID)