Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Azure.Monitor.Query: invalid date string format in MetricsClient #45998

Merged
merged 16 commits into from
Oct 8, 2024
1 change: 1 addition & 0 deletions sdk/monitor/Azure.Monitor.Query/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
### Breaking Changes

### Bugs Fixed
- Fix bug in 'MetricsClient' QueryResourceAsync method where the 'QueryTimeRange' parameter was incorrectly set

### Other Changes

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": "net",
"TagPrefix": "net/monitor/Azure.Monitor.Query",
"Tag": "net/monitor/Azure.Monitor.Query_2eaa6ed059"
"Tag": "net/monitor/Azure.Monitor.Query_ee0f373f3b"
}
8 changes: 6 additions & 2 deletions sdk/monitor/Azure.Monitor.Query/src/MetricsClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -150,8 +150,12 @@ private async Task<Response<MetricsQueryResourcesResult>> ExecuteBatchAsync(IEnu
{
if (options.TimeRange != null)
{
startTime = options.TimeRange.Value.Start.ToString();
endTime = options.TimeRange.Value.End.ToString();
DateTimeOffset? startTimeDto = options.TimeRange.Value.Start;
DateTimeOffset? endTimeDto = options.TimeRange.Value.End;
TimeSpan duration = options.TimeRange.Value.Duration;

startTime = startTimeDto.ToIsoString() ?? endTimeDto?.Subtract(duration).ToIsoString();
endTime = endTimeDto.ToIsoString() ?? startTimeDto?.Add(duration).ToIsoString();
}
aggregations = MetricsClientExtensions.CommaJoin(options.Aggregations);
top = options.Size;
Expand Down
16 changes: 16 additions & 0 deletions sdk/monitor/Azure.Monitor.Query/src/MetricsClientExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using Azure.Monitor.Query.Models;
Expand Down Expand Up @@ -51,5 +52,20 @@ internal static IList<string> CommaSplit(string value) =>
new List<string>() :
// TODO: #10600 - Verify we don't need to worry about escaping
new List<string>(value.Split(','));

internal static string ToIsoString(this DateTimeOffset value)
nisha-bhatia marked this conversation as resolved.
Show resolved Hide resolved
{
if (value.Offset == TimeSpan.Zero)
{
// Some Azure service required 0-offset dates to be formatted without the
// -00:00 part
const string roundtripZFormat = "yyyy-MM-ddTHH:mm:ss.fffffffZ";
return value.ToString(roundtripZFormat, CultureInfo.InvariantCulture);
}

return value.ToString("O", CultureInfo.InvariantCulture);
}

internal static string ToIsoString(this DateTimeOffset? value) => value?.ToIsoString();
}
}
18 changes: 2 additions & 16 deletions sdk/monitor/Azure.Monitor.Query/src/QueryTimeRange.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
// Licensed under the MIT License.

using System;
using System.Globalization;
using System.Xml;
using Azure.Core;

Expand Down Expand Up @@ -105,21 +104,8 @@ public override string ToString()

internal string ToIsoString()
{
string ToString(DateTimeOffset value)
{
if (value.Offset == TimeSpan.Zero)
{
// Some Azure service required 0-offset dates to be formatted without the
// -00:00 part
const string roundtripZFormat = "yyyy-MM-ddTHH:mm:ss.fffffffZ";
return value.ToString(roundtripZFormat, CultureInfo.InvariantCulture);
}

return value.ToString("O", CultureInfo.InvariantCulture);
}

var startTime = Start != null ? ToString(Start.Value) : null;
var endTime = End != null ? ToString(End.Value) : null;
var startTime = Start.ToIsoString();
var endTime = End.ToIsoString();
var duration = XmlConvert.ToString(Duration);

switch (startTime, endTime, duration)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -707,7 +707,8 @@ public async Task CanSetServiceTimeout()
// or a partial failure 200 response
if (exception.Status == 200)
{
StringAssert.Contains("Query cancelled by the user's request", exception.Message);
//StringAssert.Contains("Query cancelled by the user's request", exception.Message);
nisha-bhatia marked this conversation as resolved.
Show resolved Hide resolved
StringAssert.Contains("PartialError", exception.Message);
}
else
{
Expand Down
181 changes: 181 additions & 0 deletions sdk/monitor/Azure.Monitor.Query/tests/MetricsClientLiveTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,181 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Azure.Core;
using Azure.Core.TestFramework;
using Azure.Monitor.Query.Models;
using NUnit.Framework;

namespace Azure.Monitor.Query.Tests
{
public class MetricsClientLiveTests : RecordedTestBase<MonitorQueryTestEnvironment>
{
private MetricsTestData _testData;

public MetricsClientLiveTests(bool isAsync) : base(isAsync)
{
}

private MetricsClient CreateMetricsClient()
{
return InstrumentClient(new MetricsClient(
new Uri(TestEnvironment.ConstructMetricsClientUri()),
TestEnvironment.Credential,
InstrumentClientOptions(new MetricsClientOptions()
{
Audience = TestEnvironment.GetMetricsClientAudience()
})
));
}

[SetUp]
public void SetUp()
{
_testData = new MetricsTestData(TestEnvironment, Recording.UtcNow);
}

[RecordedTest]
public async Task MetricsQueryResourcesAsync()
{
MetricsClient client = CreateMetricsClient();

var resourceId = TestEnvironment.StorageAccountId;

Response<MetricsQueryResourcesResult> metricsResultsResponse = await client.QueryResourcesAsync(
resourceIds: new List<ResourceIdentifier> { new ResourceIdentifier(resourceId) },
metricNames: new List<string> { "Ingress" },
metricNamespace: "Microsoft.Storage/storageAccounts").ConfigureAwait(false);

Assert.AreEqual(200, metricsResultsResponse.GetRawResponse().Status);
MetricsQueryResourcesResult metricsQueryResults = metricsResultsResponse.Value;
Assert.AreEqual(1, metricsQueryResults.Values.Count);
Assert.AreEqual(TestEnvironment.StorageAccountId + "/providers/Microsoft.Insights/metrics/Ingress", metricsQueryResults.Values[0].Metrics[0].Id);
Assert.AreEqual("Microsoft.Storage/storageAccounts", metricsQueryResults.Values[0].Namespace);
for (int i = 0; i < metricsQueryResults.Values.Count; i++)
{
foreach (MetricResult value in metricsQueryResults.Values[i].Metrics)
{
for (int j = 0; j < value.TimeSeries.Count; j++)
{
Assert.GreaterOrEqual(value.TimeSeries[j].Values[i].Total, 0);
}
}
}
}

[RecordedTest]
public async Task MetricsQueryResourcesWithStartEndTimeRangeAsync()
{
MetricsClient client = CreateMetricsClient();

var resourceId = TestEnvironment.StorageAccountId;

var timeRange = new QueryTimeRange(
start: DateTimeOffset.UtcNow,
end: DateTimeOffset.UtcNow.AddHours(4)
);

Response<MetricsQueryResourcesResult> metricsResultsResponse = await client.QueryResourcesAsync(
resourceIds: new List<ResourceIdentifier> { new ResourceIdentifier(resourceId) },
metricNames: new List<string> { "Ingress" },
metricNamespace: "Microsoft.Storage/storageAccounts",
options: new MetricsQueryResourcesOptions { TimeRange = timeRange} ).ConfigureAwait(false);

Assert.AreEqual(200, metricsResultsResponse.GetRawResponse().Status);
MetricsQueryResourcesResult metricsQueryResults = metricsResultsResponse.Value;
Assert.AreEqual(1, metricsQueryResults.Values.Count);
Assert.AreEqual(TestEnvironment.StorageAccountId + "/providers/Microsoft.Insights/metrics/Ingress", metricsQueryResults.Values[0].Metrics[0].Id);
Assert.AreEqual("Microsoft.Storage/storageAccounts", metricsQueryResults.Values[0].Namespace);
}

[RecordedTest]
public async Task MetricsQueryResourcesWithStartDurationTimeRangeAsync()
{
MetricsClient client = CreateMetricsClient();

var resourceId = TestEnvironment.StorageAccountId;

var timeRange = new QueryTimeRange(
start: DateTimeOffset.UtcNow,
duration: TimeSpan.FromHours(4)
);

Response<MetricsQueryResourcesResult> metricsResultsResponse = await client.QueryResourcesAsync(
resourceIds: new List<ResourceIdentifier> { new ResourceIdentifier(resourceId) },
metricNames: new List<string> { "Ingress" },
metricNamespace: "Microsoft.Storage/storageAccounts",
options: new MetricsQueryResourcesOptions { TimeRange = timeRange }).ConfigureAwait(false);

Assert.AreEqual(200, metricsResultsResponse.GetRawResponse().Status);
MetricsQueryResourcesResult metricsQueryResults = metricsResultsResponse.Value;
Assert.AreEqual(1, metricsQueryResults.Values.Count);
Assert.AreEqual(TestEnvironment.StorageAccountId + "/providers/Microsoft.Insights/metrics/Ingress", metricsQueryResults.Values[0].Metrics[0].Id);
Assert.AreEqual("Microsoft.Storage/storageAccounts", metricsQueryResults.Values[0].Namespace);
}

[RecordedTest]
public async Task MetricsQueryResourcesWithEndDurationTimeRangeAsync()
{
MetricsClient client = CreateMetricsClient();

var resourceId = TestEnvironment.StorageAccountId;

var timeRange = new QueryTimeRange(
end: DateTimeOffset.UtcNow,
duration: TimeSpan.FromHours(4)
);

Response<MetricsQueryResourcesResult> metricsResultsResponse = await client.QueryResourcesAsync(
resourceIds: new List<ResourceIdentifier> { new ResourceIdentifier(resourceId) },
metricNames: new List<string> { "Ingress" },
metricNamespace: "Microsoft.Storage/storageAccounts",
options: new MetricsQueryResourcesOptions { TimeRange = timeRange }).ConfigureAwait(false);

Assert.AreEqual(200, metricsResultsResponse.GetRawResponse().Status);
MetricsQueryResourcesResult metricsQueryResults = metricsResultsResponse.Value;
Assert.AreEqual(1, metricsQueryResults.Values.Count);
Assert.AreEqual(TestEnvironment.StorageAccountId + "/providers/Microsoft.Insights/metrics/Ingress", metricsQueryResults.Values[0].Metrics[0].Id);
Assert.AreEqual("Microsoft.Storage/storageAccounts", metricsQueryResults.Values[0].Namespace);
}

[RecordedTest]
public async Task MetricsQueryResourcesWithDurationTimeRangeAsync()
{
MetricsClient client = CreateMetricsClient();

var resourceId = TestEnvironment.StorageAccountId;

var timeRange = new QueryTimeRange(
duration: TimeSpan.FromHours(4)
);

Response<MetricsQueryResourcesResult> metricsResultsResponse = await client.QueryResourcesAsync(
resourceIds: new List<ResourceIdentifier> { new ResourceIdentifier(resourceId) },
metricNames: new List<string> { "Ingress" },
metricNamespace: "Microsoft.Storage/storageAccounts",
options: new MetricsQueryResourcesOptions { TimeRange = timeRange }).ConfigureAwait(false);

Assert.AreEqual(200, metricsResultsResponse.GetRawResponse().Status);
MetricsQueryResourcesResult metricsQueryResults = metricsResultsResponse.Value;
Assert.AreEqual(1, metricsQueryResults.Values.Count);
Assert.AreEqual(TestEnvironment.StorageAccountId + "/providers/Microsoft.Insights/metrics/Ingress", metricsQueryResults.Values[0].Metrics[0].Id);
Assert.AreEqual("Microsoft.Storage/storageAccounts", metricsQueryResults.Values[0].Namespace);
}

[Test]
[SyncOnly]
public void MetricsQueryResourcesInvalid()
{
MetricsClient client = CreateMetricsClient();

Assert.Throws<ArgumentException>(() =>
client.QueryResources(
resourceIds: new List<ResourceIdentifier>(),
metricNames: new List<string> { "Ingress" },
metricNamespace: "Microsoft.Storage/storageAccounts"));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,18 +34,6 @@ private MetricsQueryClient CreateClient()
));
}

private MetricsClient CreateMetricsClient()
{
return InstrumentClient(new MetricsClient(
new Uri(TestEnvironment.ConstructMetricsClientUri()),
TestEnvironment.Credential,
InstrumentClientOptions(new MetricsClientOptions()
{
Audience = TestEnvironment.GetMetricsClientAudience()
})
));
}

[SetUp]
public void SetUp()
{
Expand Down Expand Up @@ -327,46 +315,5 @@ public async Task CanGetMetricByNameInvalid()

Assert.Throws<KeyNotFoundException>(() => { results.Value.GetMetricByName("Guinness"); });
}

[RecordedTest]
public async Task MetricsQueryResourcesAsync()
{
MetricsClient client = CreateMetricsClient();

var resourceId = TestEnvironment.StorageAccountId;

Response<MetricsQueryResourcesResult> metricsResultsResponse = await client.QueryResourcesAsync(
resourceIds: new List<ResourceIdentifier> { new ResourceIdentifier(resourceId) },
metricNames: new List<string> { "Ingress" },
metricNamespace: "Microsoft.Storage/storageAccounts").ConfigureAwait(false);

MetricsQueryResourcesResult metricsQueryResults = metricsResultsResponse.Value;
Assert.AreEqual(1, metricsQueryResults.Values.Count);
Assert.AreEqual(TestEnvironment.StorageAccountId + "/providers/Microsoft.Insights/metrics/Ingress", metricsQueryResults.Values[0].Metrics[0].Id);
Assert.AreEqual("Microsoft.Storage/storageAccounts", metricsQueryResults.Values[0].Namespace);
for (int i = 0; i < metricsQueryResults.Values.Count; i++)
{
foreach (MetricResult value in metricsQueryResults.Values[i].Metrics)
{
for (int j = 0; j < value.TimeSeries.Count; j++)
{
Assert.GreaterOrEqual(value.TimeSeries[j].Values[i].Total, 0);
}
}
}
}

[Test]
[SyncOnly]
public void MetricsQueryResourcesInvalid()
{
MetricsClient client = CreateMetricsClient();

Assert.Throws<ArgumentException>(() =>
client.QueryResources(
resourceIds: new List<ResourceIdentifier>(),
metricNames: new List<string> { "Ingress" },
metricNamespace: "Microsoft.Storage/storageAccounts"));
}
}
}
28 changes: 25 additions & 3 deletions sdk/monitor/test-resources.bicep
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,6 @@ param location string = resourceGroup().location
@description('The client OID to grant access to test resources.')
param testApplicationOid string

@description('Random string to generate storage account name.')
param utc string = utcNow()

@description('The base resource name.')
param baseName string = resourceGroup().name

Expand Down Expand Up @@ -276,11 +273,36 @@ resource dataCollectionEndpoint2 'Microsoft.Insights/dataCollectionEndpoints@202
}
}

//STORAGE ACCOUNT FOR METRICSCLIENT
@description('The base resource name.')
param storageAccountName string = uniqueString(baseName, 'storage')
@description('The base resource name.')
param storageAccountsku string = 'Standard_LRS'
resource storageAccount 'Microsoft.Storage/storageAccounts@2021-08-01' = {
name: storageAccountName
location: location
sku: {
name: storageAccountsku
}
kind: 'StorageV2'
tags: {
ObjectName: storageAccountName
}
properties: {}
}

// OUTPUT VALUES USED BY TEST ENVIRONMENT
output LOGS_ENDPOINT string = 'https://api.loganalytics.io'

output CONNECTION_STRING string = ApplicationInsightsResource1.properties.ConnectionString
output WORKSPACE_ID string = LogAnalyticsWorkspace1.properties.customerId
output WORKSPACE_PRIMARY_RESOURCE_ID string = LogAnalyticsWorkspace1.id

output SECONDARY_CONNECTION_STRING string = ApplicationInsightsResource2.properties.ConnectionString
output SECONDARY_WORKSPACE_ID string = LogAnalyticsWorkspace2.properties.customerId
output WORKSPACE_SECONDARY_RESOURCE_ID string = LogAnalyticsWorkspace2.id

output STORAGE_NAME string = storageAccount.name
output STORAGE_ID string = storageAccount.id
output METRICS_RESOURCE_ID string = LogAnalyticsWorkspace1.id
output METRICS_RESOURCE_NAMESPACE string = 'Microsoft.OperationalInsights/workspaces'
Loading