Skip to content

Commit

Permalink
Merge branch 'main' into superq/v0.28.0-rc.0
Browse files Browse the repository at this point in the history
  • Loading branch information
gotjosh authored Oct 24, 2024
2 parents a472387 + 6b77acd commit 4a28c0e
Show file tree
Hide file tree
Showing 49 changed files with 1,702 additions and 204 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/container_description.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ jobs:
if: github.repository_owner == 'prometheus' || github.repository_owner == 'prometheus-community' # Don't run this workflow on forks.
steps:
- name: git checkout
uses: actions/checkout@a5ac7e51b41094c92402da3b24376905380afc29 # v4.1.6
uses: actions/checkout@d632683dd7b4114ad314bca15554477dd762a938 # v4.2.0
- name: Set docker hub repo name
run: echo "DOCKER_REPO_NAME=$(make docker-repo-name)" >> $GITHUB_ENV
- name: Push README to Dockerhub
Expand All @@ -40,7 +40,7 @@ jobs:
if: github.repository_owner == 'prometheus' || github.repository_owner == 'prometheus-community' # Don't run this workflow on forks.
steps:
- name: git checkout
uses: actions/checkout@a5ac7e51b41094c92402da3b24376905380afc29 # v4.1.6
uses: actions/checkout@d632683dd7b4114ad314bca15554477dd762a938 # v4.2.0
- name: Set quay.io org name
run: echo "DOCKER_REPO=$(echo quay.io/${GITHUB_REPOSITORY_OWNER} | tr -d '-')" >> $GITHUB_ENV
- name: Set quay.io repo name
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/golangci-lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ jobs:
runs-on: ubuntu-latest
steps:
- name: Checkout repository
uses: actions/checkout@692973e3d937129bcbf40652eb9f2f61becf3332 # v4.1.7
uses: actions/checkout@d632683dd7b4114ad314bca15554477dd762a938 # v4.2.0
- name: Install Go
uses: actions/setup-go@0a12ed9d6a96ab950c8f026ed9f722fe0da7ef32 # v5.0.2
with:
Expand Down
22 changes: 15 additions & 7 deletions api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,16 +45,20 @@ type API struct {
inFlightSem chan struct{}
}

// Options for the creation of an API object. Alerts, Silences, and StatusFunc
// are mandatory to set. The zero value for everything else is a safe default.
// Options for the creation of an API object. Alerts, Silences, AlertStatusFunc
// and GroupMutedFunc are mandatory. The zero value for everything else is a safe
// default.
type Options struct {
// Alerts to be used by the API. Mandatory.
Alerts provider.Alerts
// Silences to be used by the API. Mandatory.
Silences *silence.Silences
// StatusFunc is used be the API to retrieve the AlertStatus of an
// AlertStatusFunc is used be the API to retrieve the AlertStatus of an
// alert. Mandatory.
StatusFunc func(model.Fingerprint) types.AlertStatus
AlertStatusFunc func(model.Fingerprint) types.AlertStatus
// GroupMutedFunc is used be the API to know if an alert is muted.
// Mandatory.
GroupMutedFunc func(routeID, groupKey string) ([]string, bool)
// Peer from the gossip cluster. If nil, no clustering will be used.
Peer cluster.ClusterPeer
// Timeout for all HTTP connections. The zero value (and negative
Expand Down Expand Up @@ -83,8 +87,11 @@ func (o Options) validate() error {
if o.Silences == nil {
return errors.New("mandatory field Silences not set")
}
if o.StatusFunc == nil {
return errors.New("mandatory field StatusFunc not set")
if o.AlertStatusFunc == nil {
return errors.New("mandatory field AlertStatusFunc not set")
}
if o.GroupMutedFunc == nil {
return errors.New("mandatory field GroupMutedFunc not set")
}
if o.GroupFunc == nil {
return errors.New("mandatory field GroupFunc not set")
Expand Down Expand Up @@ -113,7 +120,8 @@ func New(opts Options) (*API, error) {
v2, err := apiv2.NewAPI(
opts.Alerts,
opts.GroupFunc,
opts.StatusFunc,
opts.AlertStatusFunc,
opts.GroupMutedFunc,
opts.Silences,
opts.Peer,
log.With(l, "version", "v2"),
Expand Down
17 changes: 13 additions & 4 deletions api/v2/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ type API struct {
alerts provider.Alerts
alertGroups groupsFn
getAlertStatus getAlertStatusFn
groupMutedFunc groupMutedFunc
uptime time.Time

// mtx protects alertmanagerConfig, setAlertStatus and route.
Expand All @@ -78,6 +79,7 @@ type API struct {

type (
groupsFn func(func(*dispatch.Route) bool, func(*types.Alert, time.Time) bool) (dispatch.AlertGroups, map[prometheus_model.Fingerprint][]string)
groupMutedFunc func(routeID, groupKey string) ([]string, bool)
getAlertStatusFn func(prometheus_model.Fingerprint) types.AlertStatus
setAlertStatusFn func(prometheus_model.LabelSet)
)
Expand All @@ -86,16 +88,18 @@ type (
func NewAPI(
alerts provider.Alerts,
gf groupsFn,
sf getAlertStatusFn,
asf getAlertStatusFn,
gmf groupMutedFunc,
silences *silence.Silences,
peer cluster.ClusterPeer,
l log.Logger,
r prometheus.Registerer,
) (*API, error) {
api := API{
alerts: alerts,
getAlertStatus: sf,
getAlertStatus: asf,
alertGroups: gf,
groupMutedFunc: gmf,
peer: peer,
silences: silences,
logger: l,
Expand Down Expand Up @@ -290,7 +294,7 @@ func (api *API) getAlertsHandler(params alert_ops.GetAlertsParams) middleware.Re
continue
}

alert := AlertToOpenAPIAlert(a, api.getAlertStatus(a.Fingerprint()), receivers)
alert := AlertToOpenAPIAlert(a, api.getAlertStatus(a.Fingerprint()), receivers, nil)

res = append(res, alert)
}
Expand Down Expand Up @@ -407,6 +411,11 @@ func (api *API) getAlertGroupsHandler(params alertgroup_ops.GetAlertGroupsParams
res := make(open_api_models.AlertGroups, 0, len(alertGroups))

for _, alertGroup := range alertGroups {
mutedBy, isMuted := api.groupMutedFunc(alertGroup.RouteID, alertGroup.GroupKey)
if !*params.Muted && isMuted {
continue
}

ag := &open_api_models.AlertGroup{
Receiver: &open_api_models.Receiver{Name: &alertGroup.Receiver},
Labels: ModelLabelSetToAPILabelSet(alertGroup.Labels),
Expand All @@ -417,7 +426,7 @@ func (api *API) getAlertGroupsHandler(params alertgroup_ops.GetAlertGroupsParams
fp := alert.Fingerprint()
receivers := allReceivers[fp]
status := api.getAlertStatus(fp)
apiAlert := AlertToOpenAPIAlert(alert, status, receivers)
apiAlert := AlertToOpenAPIAlert(alert, status, receivers, mutedBy)
ag.Alerts = append(ag.Alerts, apiAlert)
}
res = append(res, ag)
Expand Down
13 changes: 7 additions & 6 deletions api/v2/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -484,17 +484,12 @@ func TestAlertToOpenAPIAlert(t *testing.T) {
UpdatedAt: updated,
}
)
openAPIAlert := AlertToOpenAPIAlert(alert, types.AlertStatus{State: types.AlertStateActive}, receivers)
openAPIAlert := AlertToOpenAPIAlert(alert, types.AlertStatus{State: types.AlertStateActive}, receivers, nil)
require.Equal(t, &open_api_models.GettableAlert{
Annotations: open_api_models.LabelSet{},
Alert: open_api_models.Alert{
Labels: open_api_models.LabelSet{"severity": "critical", "alertname": "alert1"},
},
Status: &open_api_models.AlertStatus{
State: &active,
InhibitedBy: []string{},
SilencedBy: []string{},
},
StartsAt: convertDateTime(start),
EndsAt: convertDateTime(time.Time{}),
UpdatedAt: convertDateTime(updated),
Expand All @@ -503,6 +498,12 @@ func TestAlertToOpenAPIAlert(t *testing.T) {
{Name: &receivers[0]},
{Name: &receivers[1]},
},
Status: &open_api_models.AlertStatus{
State: &active,
InhibitedBy: []string{},
SilencedBy: []string{},
MutedBy: []string{},
},
}, openAPIAlert)
}

Expand Down
39 changes: 39 additions & 0 deletions api/v2/client/alertgroup/get_alert_groups_parameters.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 12 additions & 1 deletion api/v2/compat.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ func PostableSilenceToProto(s *open_api_models.PostableSilence) (*silencepb.Sile
}

// AlertToOpenAPIAlert converts internal alerts, alert types, and receivers to *open_api_models.GettableAlert.
func AlertToOpenAPIAlert(alert *types.Alert, status types.AlertStatus, receivers []string) *open_api_models.GettableAlert {
func AlertToOpenAPIAlert(alert *types.Alert, status types.AlertStatus, receivers, mutedBy []string) *open_api_models.GettableAlert {
startsAt := strfmt.DateTime(alert.StartsAt)
updatedAt := strfmt.DateTime(alert.UpdatedAt)
endsAt := strfmt.DateTime(alert.EndsAt)
Expand All @@ -128,7 +128,13 @@ func AlertToOpenAPIAlert(alert *types.Alert, status types.AlertStatus, receivers
}

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

state := string(status.State)
if len(mutedBy) > 0 {
// If the alert is muted, change the state to suppressed.
state = open_api_models.AlertStatusStateSuppressed
}

aa := &open_api_models.GettableAlert{
Alert: open_api_models.Alert{
GeneratorURL: strfmt.URI(alert.GeneratorURL),
Expand All @@ -144,6 +150,7 @@ func AlertToOpenAPIAlert(alert *types.Alert, status types.AlertStatus, receivers
State: &state,
SilencedBy: status.SilencedBy,
InhibitedBy: status.InhibitedBy,
MutedBy: mutedBy,
},
}

Expand All @@ -155,6 +162,10 @@ func AlertToOpenAPIAlert(alert *types.Alert, status types.AlertStatus, receivers
aa.Status.InhibitedBy = []string{}
}

if aa.Status.MutedBy == nil {
aa.Status.MutedBy = []string{}
}

return aa
}

Expand Down
17 changes: 17 additions & 0 deletions api/v2/models/alert_status.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions api/v2/openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,11 @@ paths:
type: boolean
description: Show inhibited alerts
default: true
- in: query
name: muted
type: boolean
description: Show muted alerts
default: true
- name: filter
in: query
description: A list of matchers to filter alerts by
Expand Down Expand Up @@ -501,10 +506,15 @@ definitions:
type: array
items:
type: string
mutedBy:
type: array
items:
type: string
required:
- state
- silencedBy
- inhibitedBy
- mutedBy
receiver:
type: object
properties:
Expand Down
Loading

0 comments on commit 4a28c0e

Please sign in to comment.