Skip to content

Commit

Permalink
Add optional options field to enrichments
Browse files Browse the repository at this point in the history
  • Loading branch information
stevesg committed Dec 5, 2024
1 parent efd1b1e commit 8e9eb2d
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 2 deletions.
3 changes: 3 additions & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,9 @@ type Enrichment struct {
// URL to send POST request to.
URL SecretURL `yaml:"url" json:"url"`

// Options is optional arbitrary content to add to the request.
Options map[string]string `yaml:"options,omitempty" json:"options,omitempty"`

// Timeout is the maximum length of time an enrichment can take.
Timeout time.Duration `yaml:"timeout" json:"timeout"`
}
Expand Down
17 changes: 15 additions & 2 deletions enrichment/enrichment.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,14 @@ func (e *Enrichments) Apply(ctx context.Context, l log.Logger, alerts []*types.A
failed = 0
)

if receiver, ok := notify.ReceiverName(ctx); ok {
l = log.With(l, "receiver", receiver)
}

if groupKey, ok := notify.GroupKey(ctx); ok {
l = log.With(l, "aggrGroup", groupKey)
}

// TODO: These could/should be done async. Need to decide if to allow dependent enrichments.
for i, enr := range e.enrichments {
newAlerts, err := enr.Apply(ctx, l, alerts)
Expand Down Expand Up @@ -100,9 +108,11 @@ type Data struct {
Alerts []*types.Alert `json:"alerts"`

GroupLabels model.LabelSet `json:"groupLabels"`

Options map[string]string `json:"options"`
}

func GetData(ctx context.Context, l log.Logger, alerts []*types.Alert) *Data {
func (e *Enrichment) GetData(ctx context.Context, l log.Logger, alerts []*types.Alert) *Data {
recv, ok := notify.ReceiverName(ctx)
if !ok {
level.Error(l).Log("msg", "Missing receiver")
Expand All @@ -117,11 +127,12 @@ func GetData(ctx context.Context, l log.Logger, alerts []*types.Alert) *Data {
Status: string(types.Alerts(alerts...).Status()),
Alerts: alerts,
GroupLabels: groupLabels,
Options: e.conf.Options,
}
}

func (e *Enrichment) Apply(ctx context.Context, l log.Logger, alerts []*types.Alert) ([]*types.Alert, error) {
data := GetData(ctx, l, alerts)
data := e.GetData(ctx, l, alerts)

var reqBuf bytes.Buffer
if err := json.NewEncoder(&reqBuf).Encode(data); err != nil {
Expand All @@ -136,6 +147,8 @@ func (e *Enrichment) Apply(ctx context.Context, l log.Logger, alerts []*types.Al
ctx = postCtx
}

level.Info(l).Log("msg", "Enrichment started", "url", url, "request", reqBuf.String())

resp, err := notify.PostJSON(ctx, e.client, url, &reqBuf)
if err != nil {
if ctx.Err() != nil {
Expand Down
3 changes: 3 additions & 0 deletions test/with_api_v2/acceptance/send_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -478,6 +478,9 @@ route:
repeat_interval: 3s
enrichments:
- url: 'http://%s'
options:
opt1: val1
opt2: val2
receivers:
- name: "default"
Expand Down

0 comments on commit 8e9eb2d

Please sign in to comment.