Skip to content

Commit

Permalink
Clean up ingest health endpoint call, add telemetry and error checks
Browse files Browse the repository at this point in the history
  • Loading branch information
MauAraujo committed May 13, 2024
1 parent a9f0a3c commit dfd7530
Showing 1 changed file with 26 additions and 9 deletions.
35 changes: 26 additions & 9 deletions api/server/handlers/billing/ingest.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package billing

import (
"bytes"
"context"
"encoding/json"
"fmt"
"net/http"
Expand Down Expand Up @@ -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
}

0 comments on commit dfd7530

Please sign in to comment.