Skip to content

Commit 964bcb9

Browse files
committed
WIP
1 parent 528b958 commit 964bcb9

File tree

10 files changed

+425
-56
lines changed

10 files changed

+425
-56
lines changed

api/api.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ import (
3030
"github.com/prometheus/alertmanager/cluster"
3131
"github.com/prometheus/alertmanager/config"
3232
"github.com/prometheus/alertmanager/dispatch"
33+
"github.com/prometheus/alertmanager/notify"
3334
"github.com/prometheus/alertmanager/provider"
3435
"github.com/prometheus/alertmanager/silence"
3536
"github.com/prometheus/alertmanager/types"
@@ -195,9 +196,9 @@ func (api *API) Register(r *route.Router, routePrefix string) *http.ServeMux {
195196

196197
// Update config and resolve timeout of each API. APIv2 also needs
197198
// setAlertStatus to be updated.
198-
func (api *API) Update(cfg *config.Config, setAlertStatus func(model.LabelSet)) {
199+
func (api *API) Update(cfg *config.Config, receivers []*notify.Receiver, setAlertStatus func(model.LabelSet)) {
199200
api.v1.Update(cfg)
200-
api.v2.Update(cfg, setAlertStatus)
201+
api.v2.Update(cfg, setAlertStatus, receivers)
201202
}
202203

203204
func (api *API) limitHandler(h http.Handler) http.Handler {

api/v2/api.go

Lines changed: 39 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ import (
3131
"github.com/prometheus/common/version"
3232
"github.com/rs/cors"
3333

34-
"github.com/prometheus/alertmanager/api/metrics"
3534
open_api_models "github.com/prometheus/alertmanager/api/v2/models"
3635
"github.com/prometheus/alertmanager/api/v2/restapi"
3736
"github.com/prometheus/alertmanager/api/v2/restapi/operations"
@@ -40,9 +39,12 @@ import (
4039
general_ops "github.com/prometheus/alertmanager/api/v2/restapi/operations/general"
4140
receiver_ops "github.com/prometheus/alertmanager/api/v2/restapi/operations/receiver"
4241
silence_ops "github.com/prometheus/alertmanager/api/v2/restapi/operations/silence"
42+
43+
"github.com/prometheus/alertmanager/api/metrics"
4344
"github.com/prometheus/alertmanager/cluster"
4445
"github.com/prometheus/alertmanager/config"
4546
"github.com/prometheus/alertmanager/dispatch"
47+
"github.com/prometheus/alertmanager/notify"
4648
"github.com/prometheus/alertmanager/pkg/labels"
4749
"github.com/prometheus/alertmanager/provider"
4850
"github.com/prometheus/alertmanager/silence"
@@ -70,7 +72,8 @@ type API struct {
7072
logger log.Logger
7173
m *metrics.Alerts
7274

73-
Handler http.Handler
75+
Handler http.Handler
76+
receivers []*notify.Receiver
7477
}
7578

7679
type (
@@ -137,13 +140,14 @@ func (api *API) requestLogger(req *http.Request) log.Logger {
137140
}
138141

139142
// Update sets the API struct members that may change between reloads of alertmanager.
140-
func (api *API) Update(cfg *config.Config, setAlertStatus setAlertStatusFn) {
143+
func (api *API) Update(cfg *config.Config, setAlertStatus setAlertStatusFn, receivers []*notify.Receiver) {
141144
api.mtx.Lock()
142145
defer api.mtx.Unlock()
143146

144147
api.alertmanagerConfig = cfg
145148
api.route = dispatch.NewRoute(cfg.Route, nil)
146149
api.setAlertStatus = setAlertStatus
150+
api.receivers = receivers
147151
}
148152

149153
func (api *API) getStatusHandler(params general_ops.GetStatusParams) middleware.Responder {
@@ -204,11 +208,38 @@ func (api *API) getStatusHandler(params general_ops.GetStatusParams) middleware.
204208

205209
func (api *API) getReceiversHandler(params receiver_ops.GetReceiversParams) middleware.Responder {
206210
api.mtx.RLock()
207-
defer api.mtx.RUnlock()
211+
configReceivers := api.receivers
212+
api.mtx.RUnlock()
213+
214+
receivers := make([]*open_api_models.Receiver, 0, len(configReceivers))
215+
for _, r := range configReceivers {
216+
integrations := make([]*open_api_models.Integration, 0, len(r.Integrations()))
217+
218+
for _, integration := range r.Integrations() {
219+
notify, duration, err := integration.GetReport()
220+
iname := integration.Name()
221+
integrations = append(integrations, &open_api_models.Integration{
222+
Name: &iname,
223+
SendResolve: integration.SendResolved(),
224+
LastNotify: notify.UTC().String(),
225+
LastNotifyDuration: duration.String(),
226+
//TODO: Last error should be optional.
227+
LastError: func() string {
228+
if err != nil {
229+
return err.Error()
230+
}
231+
return ""
232+
}(),
233+
})
234+
}
235+
236+
model := &open_api_models.Receiver{
237+
Name: r.Name(),
238+
Active: r.Active(),
239+
Integrations: integrations,
240+
}
208241

209-
receivers := make([]*open_api_models.Receiver, 0, len(api.alertmanagerConfig.Receivers))
210-
for _, r := range api.alertmanagerConfig.Receivers {
211-
receivers = append(receivers, &open_api_models.Receiver{Name: &r.Name})
242+
receivers = append(receivers, model)
212243
}
213244

214245
return receiver_ops.NewGetReceiversOK().WithPayload(receivers)
@@ -390,7 +421,7 @@ func (api *API) getAlertGroupsHandler(params alertgroup_ops.GetAlertGroupsParams
390421

391422
for _, alertGroup := range alertGroups {
392423
ag := &open_api_models.AlertGroup{
393-
Receiver: &open_api_models.Receiver{Name: &alertGroup.Receiver},
424+
Receiver: &open_api_models.Receiver{Name: alertGroup.Receiver},
394425
Labels: ModelLabelSetToAPILabelSet(alertGroup.Labels),
395426
Alerts: make([]*open_api_models.GettableAlert, 0, len(alertGroup.Alerts)),
396427
}

api/v2/compat.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,8 @@ func AlertToOpenAPIAlert(alert *types.Alert, status types.AlertStatus, receivers
124124

125125
apiReceivers := make([]*open_api_models.Receiver, 0, len(receivers))
126126
for i := range receivers {
127-
apiReceivers = append(apiReceivers, &open_api_models.Receiver{Name: &receivers[i]})
127+
//TODO: Why have this changed? It is no longer a pointer for some reason?
128+
apiReceivers = append(apiReceivers, &open_api_models.Receiver{Name: receivers[i]})
128129
}
129130

130131
fp := alert.Fingerprint().String()

api/v2/models/integration.go

Lines changed: 90 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

api/v2/models/receiver.go

Lines changed: 29 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

api/v2/openapi.yaml

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -506,6 +506,25 @@ definitions:
506506
properties:
507507
name:
508508
type: string
509+
active:
510+
type: boolean
511+
integrations:
512+
type: array
513+
items:
514+
$ref: '#/definitions/integration'
515+
integration:
516+
type: object
517+
properties:
518+
name:
519+
type: string
520+
sendResolve:
521+
type: boolean
522+
lastNotify:
523+
type: string
524+
lastNotifyDuration:
525+
type: string
526+
lastError:
527+
type: string
509528
required:
510529
- name
511530
labelSet:

0 commit comments

Comments
 (0)