Skip to content

Commit

Permalink
check: merge each field of Alert.PagerDuty config
Browse files Browse the repository at this point in the history
  • Loading branch information
adamdecaf committed Oct 24, 2024
1 parent 06fa663 commit c997f5b
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 2 deletions.
24 changes: 22 additions & 2 deletions internal/check/checks.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func Setup(ctx context.Context, logger log.Logger, conf *config.Config) (*Instan
"check_name": log.String(check.Name),
})

client, err := provider.NewClient(checkLogger, cmp.Or(check.Alert, conf.Alert))
client, err := provider.NewClient(checkLogger, mergeAlertConfigs(check.Alert, conf.Alert))
if err != nil {
return nil, fmt.Errorf("setting up check[%d] provider: %w", idx, err)
}
Expand Down Expand Up @@ -67,7 +67,7 @@ func (xs *Instances) CheckIn(ctx context.Context, logger log.Logger, checkID str
})

// Grab the provider client for the check
client, err := provider.NewClient(logger, cmp.Or(found.Alert, xs.conf.Alert))
client, err := provider.NewClient(logger, mergeAlertConfigs(found.Alert, xs.conf.Alert))
if err != nil {
return nil, fmt.Errorf("problem getting client for check-in: %w", err)
}
Expand All @@ -82,3 +82,23 @@ func (xs *Instances) CheckIn(ctx context.Context, logger log.Logger, checkID str
NextExpectedCheckIn: checkInExpected,
}, nil
}

func mergeAlertConfigs(local, global config.Alert) config.Alert {
var out config.Alert

// PagerDuty config merging
out.PagerDuty = cmp.Or(local.PagerDuty, global.PagerDuty)

if local.PagerDuty != nil && global.PagerDuty != nil {
// Prefer the local config over the global config
out.PagerDuty = &config.PagerDuty{
ApiKey: cmp.Or(local.PagerDuty.ApiKey, global.PagerDuty.ApiKey),
EscalationPolicy: cmp.Or(local.PagerDuty.EscalationPolicy, global.PagerDuty.EscalationPolicy),
From: cmp.Or(local.PagerDuty.From, global.PagerDuty.From),
RoutingKey: cmp.Or(local.PagerDuty.RoutingKey, global.PagerDuty.RoutingKey),
Urgency: cmp.Or(local.PagerDuty.Urgency, global.PagerDuty.Urgency),
}
}

return out
}
36 changes: 36 additions & 0 deletions internal/check/checks_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package check

import (
"testing"

"github.com/adamdecaf/deadcheck/internal/config"

"github.com/stretchr/testify/require"
)

func TestMergeAlertConfigs(t *testing.T) {
t.Run("pagerduty", func(t *testing.T) {
var local config.Alert
global := config.Alert{
PagerDuty: &config.PagerDuty{
ApiKey: "api-key",
},
}
got := mergeAlertConfigs(local, global)
require.Equal(t, "api-key", got.PagerDuty.ApiKey)

local.PagerDuty = &config.PagerDuty{
ApiKey: "other-key",
}
got = mergeAlertConfigs(local, global)
require.Equal(t, "other-key", got.PagerDuty.ApiKey)

global.PagerDuty.Urgency = "high"
got = mergeAlertConfigs(local, global)
require.Equal(t, "high", got.PagerDuty.Urgency)

local.PagerDuty.Urgency = "low"
got = mergeAlertConfigs(local, global)
require.Equal(t, "low", got.PagerDuty.Urgency)
})
}

0 comments on commit c997f5b

Please sign in to comment.