Parcourir la source

Add error handling

Mauricio Araujo il y a 2 ans
Parent
commit
fdb36e5f04
2 fichiers modifiés avec 12 ajouts et 12 suppressions
  1. 6 10
      api/server/handlers/billing/ingest.go
  2. 6 2
      internal/billing/usage.go

+ 6 - 10
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"`
 	}{}

+ 6 - 2
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++
 		}