78 lines
2.7 KiB
Go
78 lines
2.7 KiB
Go
package events
|
|
|
|
import (
|
|
"crypto/sha256"
|
|
"database/sql"
|
|
"encoding/hex"
|
|
"encoding/json/jsontext"
|
|
json "encoding/json/v2"
|
|
"slices"
|
|
"strings"
|
|
|
|
"github.com/google/uuid"
|
|
|
|
"github.com/samcharles93/archie-core/modules/connectors"
|
|
"github.com/samcharles93/archie-core/modules/events/internal/match"
|
|
"github.com/samcharles93/archie-core/modules/events/internal/store"
|
|
)
|
|
|
|
// defaultKeyFields name the fields that identify what an event is about, for
|
|
// a source that lists none. An object stands for its scalar members, so
|
|
// "labels" covers every label.
|
|
var defaultKeyFields = []string{"alertname", "name", "labels", "severity", "service", "host", "instance"}
|
|
|
|
// groupKey is the identity events of one kind share: source, type and the
|
|
// values at keyFields. It also returns those values as the filter of the
|
|
// trigger the group proposes.
|
|
func groupKey(source, eventType string, fields jsontext.Value, keyFields []string) (string, map[string]string) {
|
|
if len(keyFields) == 0 {
|
|
keyFields = defaultKeyFields
|
|
}
|
|
filter := match.Pick(fields, keyFields)
|
|
paths := make([]string, 0, len(filter))
|
|
for p := range filter {
|
|
paths = append(paths, p)
|
|
}
|
|
slices.Sort(paths)
|
|
var b strings.Builder
|
|
b.WriteString(source + "\x00" + eventType)
|
|
for _, p := range paths {
|
|
b.WriteString("\x00" + p + "=" + filter[p])
|
|
}
|
|
sum := sha256.Sum256([]byte(b.String()))
|
|
return hex.EncodeToString(sum[:]), filter
|
|
}
|
|
|
|
// keyPaths returns the source's key fields; none means the default list.
|
|
func keyPaths(src store.Source) []string {
|
|
var paths []string
|
|
if len(src.KeyFields) > 0 {
|
|
// Only a list that encodes is stored, so a failure reads as none.
|
|
_ = json.Unmarshal(src.KeyFields, &paths)
|
|
}
|
|
return paths
|
|
}
|
|
|
|
// newRow returns the row stored for an event a source delivered. An echo of a
|
|
// delivery Archie sent (own) is recorded with the run that sent it and gets no
|
|
// group key, so it can never form or join a group. Any other event's key is
|
|
// fixed here, once, from the source's key fields.
|
|
func newRow(orgID string, src store.Source, e connectors.Event, body []byte, own bool, runID uuid.UUID) store.Event {
|
|
row := store.Event{
|
|
ID: uuid.New(), OrgID: orgID, Source: src.Name, Type: e.Type, SourceEventID: e.SourceEventID,
|
|
Fields: e.Fields, Body: body, Own: own,
|
|
}
|
|
row.LinkKey.String, row.LinkKey.Valid = e.LinkKey, e.LinkKey != ""
|
|
row.OccurredAt.Time, row.OccurredAt.Valid = e.OccurredAt, !e.OccurredAt.IsZero()
|
|
if own {
|
|
row.OriginRunID = uuid.NullUUID{UUID: runID, Valid: runID != uuid.Nil}
|
|
return row
|
|
}
|
|
key, filter := groupKey(src.Name, e.Type, e.Fields, keyPaths(src))
|
|
// A map of strings always encodes.
|
|
raw, _ := json.Marshal(filter)
|
|
row.GroupKey = sql.NullString{String: key, Valid: true}
|
|
row.GroupFilter = raw
|
|
return row
|
|
}
|