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
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