From a9f0a3cc16a55d6153e813cfb58bba44ba54db53 Mon Sep 17 00:00:00 2001 From: Mauricio Araujo Date: Fri, 10 May 2024 19:16:27 -0400 Subject: [PATCH] Call ingest status endpoint after sending billing events --- api/server/handlers/billing/ingest.go | 25 ++++++++++++++++++++++++ api/server/shared/config/env/envconfs.go | 3 +++ 2 files changed, 28 insertions(+) diff --git a/api/server/handlers/billing/ingest.go b/api/server/handlers/billing/ingest.go index 4a476e963f..31e8c5010c 100644 --- a/api/server/handlers/billing/ingest.go +++ b/api/server/handlers/billing/ingest.go @@ -2,6 +2,8 @@ package billing import ( + "bytes" + "encoding/json" "fmt" "net/http" @@ -80,5 +82,28 @@ func (c *IngestEventsHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) return } + // Call the ingest check webhook + webhookUrl := c.Config().ServerConf.IngestStatusWebhookUrl + req := struct { + ProjectID uint `json:"project_id"` + }{ + ProjectID: proj.ID, + } + + 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 + } + + 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 + } + c.WriteResult(w, r, "") } diff --git a/api/server/shared/config/env/envconfs.go b/api/server/shared/config/env/envconfs.go index 3003a4accc..683fdc1f03 100644 --- a/api/server/shared/config/env/envconfs.go +++ b/api/server/shared/config/env/envconfs.go @@ -75,6 +75,9 @@ type ServerConf struct { PorterCloudPlanID string `env:"PORTER_CLOUD_PLAN_ID"` PorterStandardPlanID string `env:"PORTER_STANDARD_PLAN_ID"` + // The URL of the webhook to verify ingesting events works + IngestStatusWebhookUrl string `env:"INGEST_STATUS_WEBHOOK_URL"` + // This endpoint will be passed to the porter-agent so that // the billing manager can query Prometheus. PrometheusUrl string `env:"PROMETHEUS_URL"`