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 e2cdc77
Showing 1 changed file with 7 additions and 0 deletions.
7 changes: 7 additions & 0 deletions x-pack/metricbeat/module/openai/usage/usage.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,8 @@ func (m *MetricSet) Fetch(report mb.ReporterV2) error {
func (m *MetricSet) fetchDateRange(startDate, endDate time.Time, httpClient *RLHTTPClient) error {
g, ctx := errgroup.WithContext(context.TODO())

endDate = endDate.Truncate(time.Hour * 24) // truncate to day as we only collect daily data

for i := range m.config.APIKeys {
apiKey := m.config.APIKeys[i]
apiKeyIdx := i + 1
Expand All @@ -127,18 +129,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 e2cdc77

Please sign in to comment.