From 549f26e13fcf2488ae999bb970f2458b9742cfbb Mon Sep 17 00:00:00 2001 From: subham sarkar Date: Wed, 11 Dec 2024 00:44:53 +0530 Subject: [PATCH] Logging and other basic improvements --- x-pack/metricbeat/module/openai/usage/usage.go | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/x-pack/metricbeat/module/openai/usage/usage.go b/x-pack/metricbeat/module/openai/usage/usage.go index 1766cfa7367..86dbe76ce81 100644 --- a/x-pack/metricbeat/module/openai/usage/usage.go +++ b/x-pack/metricbeat/module/openai/usage/usage.go @@ -94,7 +94,7 @@ func New(base mb.BaseMetricSet) (mb.MetricSet, error) { // 3. Fetches usage data for each day in the range // 4. Reports collected metrics through the mb.ReporterV2 func (m *MetricSet) Fetch(report mb.ReporterV2) error { - endDate := time.Now().UTC() + endDate := time.Now().UTC().Truncate(time.Hour * 24) // truncate to day as we only collect daily data if !m.config.Collection.Realtime { // If we're not collecting realtime data, then just pull until @@ -127,11 +127,14 @@ func (m *MetricSet) fetchDateRange(startDate, endDate time.Time, httpClient *RLH if err == nil { currentStartDate := lastProcessedDate.AddDate(0, 0, 1) if currentStartDate.After(endDate) { + m.logger.Infof("Skipping API key #%d as current start date (%s) is after end date (%s)", apiKeyIdx, currentStartDate, endDate) return nil } startDate = currentStartDate } + m.logger.Debugf("Fetching data for API key #%d from %s to %s", apiKeyIdx, startDate, endDate) + for d := startDate; !d.After(endDate); d = d.AddDate(0, 0, 1) { select { case <-ctx.Done(): @@ -139,6 +142,8 @@ func (m *MetricSet) fetchDateRange(startDate, endDate time.Time, httpClient *RLH default: dateStr := d.Format(dateFormatForStateStore) if err := m.fetchSingleDay(apiKeyIdx, dateStr, apiKey.Key, httpClient); err != nil { + // If there's an error, log it and continue to the next day. + // In this case, we are not saving the state. m.logger.Errorf("Error fetching data (api key #%d) for date %s: %v", apiKeyIdx, dateStr, err) continue }