diff --git a/metrics/metrics.go b/metrics/metrics.go index 83a2cf3..4535e01 100644 --- a/metrics/metrics.go +++ b/metrics/metrics.go @@ -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() { @@ -76,4 +100,7 @@ func Init() { prometheus.MustRegister(PostflightRequests) prometheus.MustRegister(RuleDownloadRequestDuration) prometheus.MustRegister(EventUploadRequestDuration) + prometheus.MustRegister(EventProcessedCount) + prometheus.MustRegister(EventMarshalingErrors) + prometheus.MustRegister(DecisionOutcomes) } diff --git a/moroz/svc_upload_event.go b/moroz/svc_upload_event.go index 25ae805..4a9b86a 100644 --- a/moroz/svc_upload_event.go +++ b/moroz/svc_upload_event.go @@ -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" @@ -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 { @@ -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 @@ -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" @@ -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 +}