Skip to content

Commit

Permalink
add moar metrics
Browse files Browse the repository at this point in the history
  • Loading branch information
ps-spb committed Sep 12, 2024
1 parent 251be90 commit 491b925
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 4 deletions.
27 changes: 27 additions & 0 deletions metrics/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,30 @@ var (
},
[]string{"status"}, // Labels: success or error
)

EventProcessedCount = prometheus.NewCounterVec(
prometheus.CounterOpts{
Name: "moroz_santa_event_processed_total",
Help: "Total number of individual events processed in event uploads.",
},
[]string{"machineID"}, // Labels: machine ID
)

EventMarshalingErrors = prometheus.NewCounterVec(
prometheus.CounterOpts{
Name: "moroz_santa_event_marshaling_errors_total",
Help: "Total number of errors encountered while marshaling events to JSON.",
},
[]string{"machineID"}, // Labels: machine ID
)

DecisionOutcomes = prometheus.NewCounterVec(
prometheus.CounterOpts{
Name: "moroz_santa_decision_outcomes_total",
Help: "Total number of decisions made during event uploads.",
},
[]string{"decision"}, // Label by decision outcome
)
)

func Init() {
Expand All @@ -76,4 +100,7 @@ func Init() {
prometheus.MustRegister(PostflightRequests)
prometheus.MustRegister(RuleDownloadRequestDuration)
prometheus.MustRegister(EventUploadRequestDuration)
prometheus.MustRegister(EventProcessedCount)
prometheus.MustRegister(EventMarshalingErrors)
prometheus.MustRegister(DecisionOutcomes)
}
66 changes: 62 additions & 4 deletions moroz/svc_upload_event.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,7 @@ import (
"compress/zlib"
"context"
"encoding/json"
"fmt"
"net/http"
"os"
"path/filepath"
"time"

"github.com/go-kit/kit/endpoint"
"github.com/pkg/errors"
Expand All @@ -17,6 +13,7 @@ import (
"github.com/groob/moroz/santa"
)

/*
func (svc *SantaService) UploadEvent(ctx context.Context, machineID string, events []santa.EventPayload) error {
// TODO
if !svc.flPersistEvents {
Expand Down Expand Up @@ -50,6 +47,37 @@ func (svc *SantaService) UploadEvent(ctx context.Context, machineID string, even
}
return nil
}
*/

func (svc *SantaService) UploadEvent(ctx context.Context, machineID string, events []santa.EventPayload) error {
// Increment the counter for the number of events processed
metrics.EventProcessedCount.WithLabelValues(machineID).Add(float64(len(events)))

for _, ev := range events {
ev.EventInfo.MachineID = machineID

// Track the decision outcome for each event
metrics.DecisionOutcomes.WithLabelValues(ev.EventInfo.Decision).Inc()

// Marshal the event info to JSON for logging purposes
eventInfoJSON, err := json.Marshal(ev.EventInfo)
if err != nil {
svc.logger.Log("level", "error", "msg", "Failed to marshal event info to JSON", "err", err)
// Increment the error counter for marshaling errors
metrics.EventMarshalingErrors.WithLabelValues(machineID).Inc()
return errors.Wrap(err, "marshal event info to json")
}

// Log the event information instead of writing it to a file
svc.logger.Log(
"event", "UploadEvent",
"machineID", machineID,
"eventInfo", string(eventInfoJSON),
)
}

return nil
}

type eventRequest struct {
MachineID string
Expand Down Expand Up @@ -103,6 +131,7 @@ func decodeEventUpload(ctx context.Context, r *http.Request) (interface{}, error
return req, nil
}

/*
func (mw logmw) UploadEvent(ctx context.Context, machineID string, events []santa.EventPayload) (err error) {
defer func(begin time.Time) {
status := "success"
Expand Down Expand Up @@ -130,3 +159,32 @@ func (mw logmw) UploadEvent(ctx context.Context, machineID string, events []sant
err = mw.next.UploadEvent(ctx, machineID, events)
return
}
*/

func (svc *SantaService) UploadEvent(ctx context.Context, machineID string, events []santa.EventPayload) error {
// If event persistence is disabled, just return
if !svc.flPersistEvents {
return nil
}

// Process each event without writing to disk
for _, ev := range events {
ev.EventInfo.MachineID = machineID

// Marshal the event info to JSON for logging purposes (but don't write it to disk)
eventInfoJSON, err := json.Marshal(ev.EventInfo)
if err != nil {
svc.logger.Log("level", "error", "msg", "Failed to marshal event info to JSON", "err", err)
return errors.Wrap(err, "marshal event info to json")
}

// Log the event information instead of writing it to a file
svc.logger.Log(
"event", "UploadEvent",
"machineID", machineID,
"eventInfo", string(eventInfoJSON),
)
}

return nil
}

0 comments on commit 491b925

Please sign in to comment.