Skip to content

Commit faf1f7b

Browse files
committed
getApp() was called twice when entering from REST API
1 parent 01e9794 commit faf1f7b

File tree

2 files changed

+23
-16
lines changed

2 files changed

+23
-16
lines changed

cmd/alertmanager/deadmansswitches.go

+14-8
Original file line numberDiff line numberDiff line change
@@ -51,13 +51,19 @@ func deadMansSwitchEntry() *cobra.Command {
5151
Short: "Make a checkin",
5252
Args: cobra.ExactArgs(2),
5353
Run: func(cmd *cobra.Command, args []string) {
54+
ctx := ossignal.InterruptOrTerminateBackgroundCtx(nil)
55+
5456
ttl, err := parseTtlSpec(args[1], time.Now())
5557
exitIfError(err)
5658

59+
app, err := getApp(ctx)
60+
exitIfError(err)
61+
5762
_, err = deadmansswitchCheckin(
58-
ossignal.InterruptOrTerminateBackgroundCtx(nil),
63+
ctx,
5964
args[0],
60-
ttl)
65+
ttl,
66+
app)
6167
exitIfError(err)
6268
},
6369
})
@@ -105,12 +111,12 @@ func deadmansswitchRemove(ctx context.Context, subject string) error {
105111
})
106112
}
107113

108-
func deadmansswitchCheckin(ctx context.Context, subject string, ttl time.Time) (bool, error) {
109-
app, err := getApp(ctx)
110-
if err != nil {
111-
return false, err
112-
}
113-
114+
func deadmansswitchCheckin(
115+
ctx context.Context,
116+
subject string,
117+
ttl time.Time,
118+
app *amstate.App,
119+
) (bool, error) {
114120
now := time.Now()
115121

116122
alertAcked := false

cmd/alertmanager/restapi.go

+9-8
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ func newRestApi(ctx context.Context) http.Handler {
7676
handleDeadMansSwitchCheckin(w, r, alertmanagertypes.DeadMansSwitchCheckinRequest{
7777
Subject: r.URL.Query().Get("subject"),
7878
TTL: r.URL.Query().Get("ttl"),
79-
})
79+
}, app)
8080
})
8181

8282
mux.POST.HandleFunc("/deadmansswitch/checkin", func(w http.ResponseWriter, r *http.Request) {
@@ -87,7 +87,7 @@ func newRestApi(ctx context.Context) http.Handler {
8787
}
8888

8989
// handles validation
90-
handleDeadMansSwitchCheckin(w, r, checkin)
90+
handleDeadMansSwitchCheckin(w, r, checkin, app)
9191
})
9292

9393
mux.POST.HandleFunc("/prometheus-alertmanager/api/v1/alerts", func(w http.ResponseWriter, r *http.Request) {
@@ -97,11 +97,12 @@ func newRestApi(ctx context.Context) http.Handler {
9797
return mux
9898
}
9999

100-
func handleDeadMansSwitchCheckin(
101-
w http.ResponseWriter,
102-
r *http.Request,
103-
raw alertmanagertypes.DeadMansSwitchCheckinRequest,
104-
) {
100+
func handleDeadMansSwitchCheckin(
101+
w http.ResponseWriter,
102+
r *http.Request,
103+
raw alertmanagertypes.DeadMansSwitchCheckinRequest,
104+
app *amstate.App,
105+
) {
105106
if raw.Subject == "" || raw.TTL == "" {
106107
http.Error(w, "subject or ttl empty", http.StatusBadRequest)
107108
return
@@ -115,7 +116,7 @@ func newRestApi(ctx context.Context) http.Handler {
115116
return
116117
}
117118

118-
alertAcked, err := deadmansswitchCheckin(r.Context(), raw.Subject, ttl)
119+
alertAcked, err := deadmansswitchCheckin(r.Context(), raw.Subject, ttl, app)
119120
if err != nil {
120121
http.Error(w, err.Error(), http.StatusInternalServerError)
121122
return

0 commit comments

Comments
 (0)