Skip to content

Commit

Permalink
Merge pull request #272 from cdnjs/sven/metrics
Browse files Browse the repository at this point in the history
add metrics
  • Loading branch information
xtuc authored Jan 18, 2024
2 parents dbe86eb + e8e8e1f commit 9b61bad
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 1 deletion.
6 changes: 5 additions & 1 deletion cmd/process-version-host/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -231,6 +235,6 @@ func compress(src string, buf io.Writer) error {
if err := zr.Close(); err != nil {
return err
}
//

return nil
}
4 changes: 4 additions & 0 deletions functions/check-pkg-updates/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
}
62 changes: 62 additions & 0 deletions metrics/metrics.go
Original file line number Diff line number Diff line change
@@ -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
}

0 comments on commit 9b61bad

Please sign in to comment.