Skip to content

Commit

Permalink
Add error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
MauAraujo committed May 14, 2024
1 parent b76d0b5 commit 962177f
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 12 deletions.
16 changes: 6 additions & 10 deletions api/server/handlers/billing/ingest.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,21 +39,17 @@ func (c *IngestEventsHandler) ServeHTTP(w http.ResponseWriter, r *http.Request)

proj, _ := ctx.Value(types.ProjectScope).(*models.Project)

telemetry.WithAttributes(span,
telemetry.AttributeKV{Key: "lago-config-exists", Value: c.Config().BillingManager.LagoConfigLoaded},
telemetry.AttributeKV{Key: "lago-enabled", Value: proj.GetFeatureFlag(models.LagoEnabled, c.Config().LaunchDarklyClient)},
telemetry.AttributeKV{Key: "porter-cloud-enabled", Value: proj.EnableSandbox},
)

if !c.Config().BillingManager.LagoConfigLoaded || !proj.GetFeatureFlag(models.LagoEnabled, c.Config().LaunchDarklyClient) {
c.WriteResult(w, r, "")

telemetry.WithAttributes(span,
telemetry.AttributeKV{Key: "lago-config-exists", Value: c.Config().BillingManager.LagoConfigLoaded},
telemetry.AttributeKV{Key: "lago-enabled", Value: proj.GetFeatureFlag(models.LagoEnabled, c.Config().LaunchDarklyClient)},
telemetry.AttributeKV{Key: "porter-cloud-enabled", Value: proj.EnableSandbox},
)
return
}

telemetry.WithAttributes(span,
telemetry.AttributeKV{Key: "lago-enabled", Value: true},
)

ingestEventsRequest := struct {
Events []types.BillingEvent `json:"billing_events"`
}{}
Expand Down
8 changes: 6 additions & 2 deletions internal/billing/usage.go
Original file line number Diff line number Diff line change
Expand Up @@ -336,7 +336,7 @@ func (m LagoClient) ListCustomerUsage(ctx context.Context, customerID string, su

// IngestEvents sends a list of billing events to Lago's ingest endpoint
func (m LagoClient) IngestEvents(ctx context.Context, subscriptionID string, events []types.BillingEvent, enableSandbox bool) (err error) {
ctx, span := telemetry.NewSpan(ctx, "ingets-billing-events")
ctx, span := telemetry.NewSpan(ctx, "ingest-billing-events")
defer span.End()

if len(events) == 0 {
Expand Down Expand Up @@ -376,7 +376,11 @@ func (m LagoClient) IngestEvents(ctx context.Context, subscriptionID string, eve
// Retry each batch to make sure all events are ingested
var currentAttempts int
for currentAttempts < defaultMaxRetries {
m.client.Event().Batch(ctx, &batchInput)

_, lagoErr := m.client.Event().Batch(ctx, &batchInput)
if lagoErr == nil {
return telemetry.Error(ctx, span, fmt.Errorf(lagoErr.ErrorCode), "error sending ingest events to Lago")
}
currentAttempts++
}

Expand Down

0 comments on commit 962177f

Please sign in to comment.