From e8e8e1f16260c6d5f241507019c1823a189da2cf Mon Sep 17 00:00:00 2001 From: Sven Sauleau Date: Thu, 18 Jan 2024 11:44:59 +0100 Subject: [PATCH] add metrics --- cmd/process-version-host/main.go | 6 ++- functions/check-pkg-updates/update.go | 4 ++ metrics/metrics.go | 62 +++++++++++++++++++++++++++ 3 files changed, 71 insertions(+), 1 deletion(-) create mode 100644 metrics/metrics.go diff --git a/cmd/process-version-host/main.go b/cmd/process-version-host/main.go index 75207d6..d3ff6ef 100644 --- a/cmd/process-version-host/main.go +++ b/cmd/process-version-host/main.go @@ -19,6 +19,7 @@ import ( "strings" "github.com/cdnjs/tools/audit" + "github.com/cdnjs/tools/metrics" "github.com/cdnjs/tools/sandbox" "github.com/cdnjs/tools/sentry" @@ -112,6 +113,9 @@ func processMessage(ctx context.Context, data []byte) error { if err := audit.ProcessedVersion(ctx, message.Pkg, message.Version, logs); err != nil { return errors.Wrap(err, "could not post audit") } + if err := metrics.NewUpdateProccessed(); err != nil { + return errors.Wrap(err, "could not report metrics") + } log.Printf("compressing %s\n", outDir) var buff bytes.Buffer @@ -231,6 +235,6 @@ func compress(src string, buf io.Writer) error { if err := zr.Close(); err != nil { return err } - // + return nil } diff --git a/functions/check-pkg-updates/update.go b/functions/check-pkg-updates/update.go index 0b422d9..7e2aacb 100644 --- a/functions/check-pkg-updates/update.go +++ b/functions/check-pkg-updates/update.go @@ -10,6 +10,7 @@ import ( "github.com/cdnjs/tools/audit" "github.com/cdnjs/tools/gcp" "github.com/cdnjs/tools/git" + "github.com/cdnjs/tools/metrics" "github.com/cdnjs/tools/npm" "github.com/cdnjs/tools/packages" "github.com/cdnjs/tools/util" @@ -106,6 +107,9 @@ func DoUpdate(ctx context.Context, pkg *packages.Package, versions []version.Ver if err := audit.NewVersionDetected(ctx, *pkg.Name, v.Version); err != nil { return errors.Wrap(err, "could not audit") } + if err := metrics.NewUpdateDetected(); err != nil { + return errors.Wrap(err, "could not report metrics") + } return nil } diff --git a/metrics/metrics.go b/metrics/metrics.go new file mode 100644 index 0000000..c659890 --- /dev/null +++ b/metrics/metrics.go @@ -0,0 +1,62 @@ +package metrics + +import ( + "bytes" + "encoding/json" + "fmt" + "net/http" + "os" + + "github.com/pkg/errors" +) + +const ( + METRICS_ENDPOINT = "https://metrics-worker.cloudflare-cdnjs.workers.dev" +) + +var ( + METRICS_TOKEN = os.Getenv("METRICS_TOKEN") +) + +type IncMetricPayload struct { + Name string `json:"name"` +} + +func NewUpdateDetected() error { + return sendMetrics(&IncMetricPayload{ + Name: "new_update_detected", + }) +} + +func NewUpdateProccessed() error { + return sendMetrics(&IncMetricPayload{ + Name: "new_update_processed", + }) +} + +func sendMetrics(payload *IncMetricPayload) error { + json, err := json.Marshal(payload) + if err != nil { + return errors.Wrap(err, "failed to marshall payload") + } + + req, err := http.NewRequest("POST", METRICS_ENDPOINT, bytes.NewBuffer(json)) + if err != nil { + return errors.Wrap(err, "failed to build request") + } + + req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", METRICS_TOKEN)) + req.Header.Set("Content-Type", "application/json") + + client := &http.Client{} + resp, err := client.Do(req) + if err != nil { + return errors.Wrap(err, "failed to send request") + } + defer resp.Body.Close() + + if resp.StatusCode != 201 { + return errors.Errorf("metrics endpoint returned %s", resp.Status) + } + return nil +}