From 7fd27bf1f6c4ea243697f00eb24b1ded857bebc6 Mon Sep 17 00:00:00 2001 From: muthu-mps <101238137+muthu-mps@users.noreply.github.com> Date: Thu, 27 Jul 2023 14:38:57 +0000 Subject: [PATCH] [Azure Billing] Update forecast api metrics interval (#36142) * Update forecast API metrics interval --- CHANGELOG.next.asciidoc | 1 + x-pack/metricbeat/module/azure/billing/billing.go | 11 ++++++----- .../metricbeat/module/azure/billing/billing_test.go | 4 ++-- 3 files changed, 9 insertions(+), 7 deletions(-) diff --git a/CHANGELOG.next.asciidoc b/CHANGELOG.next.asciidoc index 59d25cec04a..66c3113db86 100644 --- a/CHANGELOG.next.asciidoc +++ b/CHANGELOG.next.asciidoc @@ -231,6 +231,7 @@ automatic splitting at root level, if root level element is an array. {pull}3415 - Improve documentation for ActiveMQ module {issue}35113[35113] {pull}35558[35558] - Fix EC2 host.cpu.usage {pull}35717[35717] - Resolve statsd module's prematurely halting of metrics parsing upon encountering an invalid packet. {pull}35075[35075] +- Fix the gap in fetching forecast API metrics at the end of each month for Azure billing module {pull}36142[36142] *Osquerybeat* diff --git a/x-pack/metricbeat/module/azure/billing/billing.go b/x-pack/metricbeat/module/azure/billing/billing.go index a2968d71a6d..4777df5f217 100644 --- a/x-pack/metricbeat/module/azure/billing/billing.go +++ b/x-pack/metricbeat/module/azure/billing/billing.go @@ -127,11 +127,12 @@ func usageIntervalFrom(reference time.Time) (time.Time, time.Time) { // reference time. // // For example, if the reference time is 2007-01-09 09:41:00Z, the forecast period is: +// The forecast data is fetched from current day - 2 and for next 30 days. // -// 2007-01-01T00:00:00Z -> 2007-01-31:59:59Z +// 2007-01-07T00:00:00Z -> 2007-02-05:59:59Z func forecastIntervalFrom(reference time.Time) (time.Time, time.Time) { - referenceUTC := reference.UTC() - beginningOfMonth := time.Date(referenceUTC.Year(), referenceUTC.Month(), 1, 0, 0, 0, 0, time.UTC) - endOfMonth := beginningOfMonth.AddDate(0, 1, 0).Add(-1 * time.Second) - return beginningOfMonth, endOfMonth + referenceUTC := reference.UTC().Truncate(24 * time.Hour).Add((-48) * time.Hour) + forecastStartDate := time.Date(referenceUTC.Year(), referenceUTC.Month(), referenceUTC.Day(), 0, 0, 0, 0, time.UTC) + forecastEndDate := forecastStartDate.AddDate(0, 0, 0).Add(-1*time.Second).AddDate(0, 0, 30) + return forecastStartDate, forecastEndDate } diff --git a/x-pack/metricbeat/module/azure/billing/billing_test.go b/x-pack/metricbeat/module/azure/billing/billing_test.go index 65f4482cc5b..e8dc95119b4 100644 --- a/x-pack/metricbeat/module/azure/billing/billing_test.go +++ b/x-pack/metricbeat/module/azure/billing/billing_test.go @@ -32,9 +32,9 @@ func TestForecastPeriodFrom(t *testing.T) { referenceTime, err := time.Parse("2006-01-02 15:04:05", "2007-01-09 09:41:00") assert.NoError(t, err) - expectedStartTime, err := time.Parse("2006-01-02 15:04:05", "2007-01-01 00:00:00") + expectedStartTime, err := time.Parse("2006-01-02 15:04:05", "2007-01-07 00:00:00") assert.NoError(t, err) - expectedEndTime, err := time.Parse("2006-01-02 15:04:05", "2007-01-31 23:59:59") + expectedEndTime, err := time.Parse("2006-01-02 15:04:05", "2007-02-05 23:59:59") assert.NoError(t, err) actualStartTime, actualEndTime := forecastIntervalFrom(referenceTime)