Azure / azure-sdk-for-net

This repository is for active development of the Azure SDK for .NET. For consumers of the SDK we recommend visiting our public developer docs at https://learn.microsoft.com/dotnet/azure/ or our versioned developer docs at https://azure.github.io/azure-sdk-for-net.
MIT License
5.26k stars 4.61k forks source link

[BUG] QueryResourceAsync causing error if used with TimeRange of QueryTimeRange(Datetime, Datetime) #40047

Open egressyk opened 10 months ago

egressyk commented 10 months ago

Library name and version

Azure.Monitor.Query 1.2.0

Describe the bug

Using MetricsQueryClient.QueryResourceAsync() with option TimeRange of QueryTimeRange(startTime, endTime) causes and error, opposed to TimeRange of QueryTimeRange(duration).

Exception or Stack Trace 'Detected invalid time interval input: 2023-11-15T10:12:25.4174057 01:00/2023-11-15T11:12:25.4174057 01:00, supported Iso 8601 time interval format: (Datetime/Datetime, Datetime/Duration, Duration/Datetime, Duration) Status: 400 (Bad Request) ErrorCode: BadRequest

Expected behavior

Receive the same result as if used with

var options = new MetricsQueryOptions
{
    Aggregations = { MetricAggregationType.Average },
    Granularity = TimeSpan.FromMinutes(1),
    TimeRange = new QueryTimeRange(TimeSpan.FromHours(1))
};

Actual behavior

Receiving Status: 400 (Bad Request)

Reproduction Steps

var metricsQueryClient = new MetricsQueryClient(new DefaultAzureCredential());
var endTime = DateTimeOffset.Now;
var startTime = endTime.AddHours(-1);

var resourceId = <someAppServiceResourceId>;
var metricsNames  = new List<string> { "HttpResponseTime" };
var options = new MetricsQueryOptions
{
    Aggregations = { MetricAggregationType.Average },
    Granularity = TimeSpan.FromMinutes(1),
    TimeRange = new QueryTimeRange(startTime, endTime)
};

var result = await metricsQueryClient.QueryResourceAsync(resourceId, metricsNames, options);

Environment

jsquire commented 10 months ago

Thank you for your feedback. Tagging and routing to the team member best able to assist.

Lucasharskamp commented 4 months ago

Issue still present in 1.3.1:

DateTimeOffset now = DateTime.UtcNow;
DateTimeOffset beginningOfMonth = new DateTime(now.Year, now.Month, 1);
var options = new MetricsQueryOptions()
{
      Aggregations = { MetricAggregationType.Average },
      Filter = "Instance eq '*'",
      TimeRange = new QueryTimeRange(beginningOfMonth, now)
};

// Retrieve telemetry data
var response = await MetricsQueryClient.QueryResourceAsync(...)

Error thrown: 'Detected invalid time interval input, etc'

pritishnayak commented 3 months ago

Facing the same issue with MetricsClient. Because of this bug unable to set the TimeRange.

` string id = "/subscriptions//resourceGroups//providers/Microsoft.App/containerApps/";

MetricsQueryResourcesOptions options = new()
{
    Granularity = TimeSpan.FromMinutes(30),
    TimeRange = new QueryTimeRange(DateTimeOffset.UtcNow.AddDays(-7), DateTimeOffset.UtcNow)
};
options.Aggregations.Add("maximum");

MetricsQueryResourcesResult metric = await metricsClient.QueryResourcesAsync(resourceIds: [new(id)],
                                                                             metricNames: ["UsageNanoCores"],
                                                                             metricNamespace: "Microsoft.App/containerApps",
                                                                             options: options,
                                                                             cancellationToken: cancellationToken);

`

RequestFailedException: Detected invalid time interval input: 2024-05-23 5:43:22 AM 00:00/2024-05-30 5:43:22 AM 00:00, supported Iso 8601 time interval format: (Datetime/Datetime, Datetime/Duration, Duration/Datetime, Duration) Status: 400 (Bad Request) ErrorCode: BadRequest

Content: {"error":{"additionalInfo":[{"type":"string","info":"TraceId=xxxxxxxxxxxxxx"},{"type":"string","info":"DateTime=2024-05-30T05:43:40.8507138Z"},{"type":"string","info":"ExceptionType=Microsoft.Online.Metrics.MetricsMP.Utilities.RPRequestFormatException"}],"code":"BadRequest","message":"Detected invalid time interval input: 2024-05-23 5:43:22 AM 00:00/2024-05-30 5:43:22 AM 00:00, supported Iso 8601 time interval format: (Datetime/Datetime, Datetime/Duration, Duration/Datetime, Duration)"}}

peterbomers commented 3 months ago

This error occurs because the MetricsClient uses a ToString on the Start & End of the TimeRange property. Definitely a bug :)

As ugly work arround we created a HttpPipelinePolicy to modify the request message.

unshou commented 3 months ago

Facing the same issue, which means I cannot retrieve MetricsQueryClient data for a time range in the past.

Any update on a possible fix?