From dfd75301438d38af2709c31030f4a9fa097f6e38 Mon Sep 17 00:00:00 2001 From: Mauricio Araujo Date: Mon, 13 May 2024 12:54:16 -0400 Subject: [PATCH] Clean up ingest health endpoint call, add telemetry and error checks --- api/server/handlers/billing/ingest.go | 35 ++++++++++++++++++++------- 1 file changed, 26 insertions(+), 9 deletions(-) diff --git a/api/server/handlers/billing/ingest.go b/api/server/handlers/billing/ingest.go index 31e8c5010c..697984c152 100644 --- a/api/server/handlers/billing/ingest.go +++ b/api/server/handlers/billing/ingest.go @@ -3,6 +3,7 @@ package billing import ( "bytes" + "context" "encoding/json" "fmt" "net/http" @@ -82,28 +83,44 @@ func (c *IngestEventsHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) return } + // Call the ingest health endpoint + err = c.postIngestHealthEndpoint(ctx, proj.ID) + if err != nil { + err := telemetry.Error(ctx, span, err, "error calling ingest health endpoint") + c.HandleAPIError(w, r, apierrors.NewErrInternal(err)) + return + } + + c.WriteResult(w, r, "") +} + +func (c *IngestEventsHandler) postIngestHealthEndpoint(ctx context.Context, projectID uint) (err error) { + ctx, span := telemetry.NewSpan(ctx, "post-ingest-health-endpoint") + defer span.End() + // Call the ingest check webhook webhookUrl := c.Config().ServerConf.IngestStatusWebhookUrl + telemetry.WithAttributes(span, telemetry.AttributeKV{Key: "ingest-status-webhook-url", Value: webhookUrl}) + + if webhookUrl == "" { + return nil + } + req := struct { ProjectID uint `json:"project_id"` }{ - ProjectID: proj.ID, + ProjectID: projectID, } reqBody, err := json.Marshal(req) if err != nil { - err := telemetry.Error(ctx, span, err, "error marshalling ingest status webhook request") - c.HandleAPIError(w, r, apierrors.NewErrInternal(err)) - return + return telemetry.Error(ctx, span, err, "error marshalling ingest status webhook request") } client := &http.Client{} resp, err := client.Post(webhookUrl, "application/json", bytes.NewBuffer(reqBody)) if err != nil || resp.StatusCode != http.StatusOK { - err := telemetry.Error(ctx, span, err, "error sending ingest status webhook request") - c.HandleAPIError(w, r, apierrors.NewErrInternal(err)) - return + return telemetry.Error(ctx, span, err, "error sending ingest status webhook request") } - - c.WriteResult(w, r, "") + return nil }