diff --git a/api/server/handlers/billing/ingest.go b/api/server/handlers/billing/ingest.go index aed066bc7c..329ea8b560 100644 --- a/api/server/handlers/billing/ingest.go +++ b/api/server/handlers/billing/ingest.go @@ -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"` }{} diff --git a/internal/billing/usage.go b/internal/billing/usage.go index e8cc0eef76..3d07c0541a 100644 --- a/internal/billing/usage.go +++ b/internal/billing/usage.go @@ -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 { @@ -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++ }