Skip to content

Commit

Permalink
Logging and other basic improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
shmsr committed Dec 10, 2024
1 parent aff36b5 commit 549f26e
Showing 1 changed file with 6 additions and 1 deletion.
7 changes: 6 additions & 1 deletion x-pack/metricbeat/module/openai/usage/usage.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -127,18 +127,23 @@ 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():
return ctx.Err()
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
}
Expand Down

0 comments on commit 549f26e

Please sign in to comment.