Skip to content

Commit

Permalink
use billing customer id for reporting (#6002)
Browse files Browse the repository at this point in the history
  • Loading branch information
pjain1 authored Oct 29, 2024
1 parent d6a7c28 commit 846ec89
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 10 deletions.
14 changes: 8 additions & 6 deletions admin/metrics/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,12 +101,14 @@ func (c *Client) AutoscalerSlotsRecommendations(ctx context.Context, limit, offs
}

type Usage struct {
OrgID string `json:"org_id"`
ProjectID string `json:"project_id"`
StartTime time.Time `json:"start_time"`
EndTime time.Time `json:"end_time"`
EventName string `json:"event_name"`
MaxValue float64 `json:"max_value"`
OrgID string `json:"org_id"`
ProjectID string `json:"project_id"`
ProjectName string `json:"project_name"`
BillingCustomerID *string `json:"billing_customer_id"`
StartTime time.Time `json:"start_time"`
EndTime time.Time `json:"end_time"`
EventName string `json:"event_name"`
MaxValue float64 `json:"max_value"`
}

func (c *Client) GetUsageMetrics(ctx context.Context, startTime, endTime, afterTime time.Time, afterOrgID, afterProjectID, afterEventName, grain string, limit int) ([]*Usage, error) {
Expand Down
15 changes: 11 additions & 4 deletions admin/worker/billing_reporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,14 +94,21 @@ func (w *Worker) reportUsage(ctx context.Context) error {
for _, m := range u {
reportedOrgs[m.OrgID] = struct{}{}

customerID := m.OrgID
if m.BillingCustomerID != nil && *m.BillingCustomerID != "" {
// org might have been deleted or recently created in both cases billing customer id will be null. If billing not initialized for the org, then it will be empty string
// in all cases just use org ID to report in hope that org ID will be set as billing customer id in the future if not reported values will be ignored
customerID = *m.BillingCustomerID
}

usage = append(usage, &billing.Usage{
CustomerID: m.OrgID,
CustomerID: customerID,
MetricName: m.EventName,
Value: m.MaxValue,
ReportingGrain: w.admin.Biller.GetReportingGranularity(),
StartTime: m.StartTime,
EndTime: m.EndTime,
Metadata: map[string]interface{}{"org_id": m.OrgID, "project_id": m.ProjectID},
Metadata: map[string]interface{}{"org_id": m.OrgID, "project_id": m.ProjectID, "project_name": m.ProjectName},
})
}

Expand Down Expand Up @@ -147,11 +154,11 @@ func (w *Worker) reportUsage(ctx context.Context) error {
// count the projects which are not hibernated and created before the given time
count, err := w.admin.DB.CountBillingProjectsForOrganization(ctx, org, endTime)
if err != nil {
w.logger.Warn("failed to validate active projects for org", zap.String("org", org), zap.Error(err))
w.logger.Warn("failed to validate active projects for org", zap.String("org_id", org), zap.Error(err))
continue
}
if count > 0 {
w.logger.Warn("skipping usage reporting for org: no usage data available", zap.String("org", org), zap.Time("start_time", startTime), zap.Time("end_time", endTime))
w.logger.Warn("skipped usage reporting for org as no usage data was available", zap.String("org_id", org), zap.Time("start_time", startTime), zap.Time("end_time", endTime))
}
}
}
Expand Down

0 comments on commit 846ec89

Please sign in to comment.