Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement quorum reads and merging for /v1/silences and /v1/silence/id #4146

Merged
merged 1 commit into from
Apr 30, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions pkg/alertmanager/distributor.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,12 @@ func (d *Distributor) isQuorumReadPath(p string) (bool, merger.Merger) {
if strings.HasSuffix(p, "/v2/alerts/groups") {
return true, merger.V2AlertGroups{}
}
if strings.HasSuffix(p, "/v1/silences") {
return true, merger.V1Silences{}
}
if strings.HasSuffix(path.Dir(p), "/v1/silence") {
return true, merger.V1SilenceID{}
}
if strings.HasSuffix(p, "/v2/silences") {
return true, merger.V2Silences{}
}
Expand All @@ -104,9 +110,7 @@ func (d *Distributor) isQuorumReadPath(p string) (bool, merger.Merger) {
}

func (d *Distributor) isUnaryReadPath(p string) bool {
return strings.HasSuffix(p, "/v1/silences") ||
strings.HasSuffix(path.Dir(p), "/v1/silence") ||
strings.HasSuffix(p, "/status") ||
return strings.HasSuffix(p, "/status") ||
strings.HasSuffix(p, "/receivers")
}

Expand Down
19 changes: 6 additions & 13 deletions pkg/alertmanager/distributor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,14 +119,15 @@ func TestDistributor_DistributeRequest(t *testing.T) {
headersNotPreserved: true,
route: "/alerts/groups",
}, {
name: "Read /v1/silences is sent to only 1 AM",
name: "Read /v1/silences is sent to 3 AMs",
numAM: 5,
numHappyAM: 5,
replicationFactor: 3,
isRead: true,
expStatusCode: http.StatusOK,
expectedTotalCalls: 1,
expectedTotalCalls: 3,
route: "/v1/silences",
responseBody: []byte(`{"status":"success","data":[]}`),
}, {
name: "Read /v2/silences is sent to 3 AMs",
numAM: 5,
Expand All @@ -146,14 +147,15 @@ func TestDistributor_DistributeRequest(t *testing.T) {
expectedTotalCalls: 1,
route: "/silences",
}, {
name: "Read /v1/silence/id is sent to only 1 AM",
name: "Read /v1/silence/id is sent to 3 AMs",
numAM: 5,
numHappyAM: 5,
replicationFactor: 3,
isRead: true,
expStatusCode: http.StatusOK,
expectedTotalCalls: 1,
expectedTotalCalls: 3,
route: "/v1/silence/id",
responseBody: []byte(`{"status":"success","data":{"id":"aaa","updatedAt":"2020-01-01T00:00:00Z"}}`),
}, {
name: "Read /v2/silence/id is sent to 3 AMs",
numAM: 5,
Expand All @@ -174,15 +176,6 @@ func TestDistributor_DistributeRequest(t *testing.T) {
expectedTotalCalls: 0,
headersNotPreserved: true,
route: "/silence/id",
}, {
name: "Read /silence/id is sent to only 1 AM",
numAM: 5,
numHappyAM: 5,
replicationFactor: 3,
isRead: true,
expStatusCode: http.StatusOK,
expectedTotalCalls: 1,
route: "/silence/id",
}, {
name: "Delete /silence/id is sent to only 1 AM",
numAM: 5,
Expand Down
51 changes: 51 additions & 0 deletions pkg/alertmanager/merger/v1_silence_id.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package merger

import (
"encoding/json"
"errors"
"fmt"

v2_models "github.com/prometheus/alertmanager/api/v2/models"
)

// V1Silences implements the Merger interface for GET /v1/silences. This re-uses the logic for
// merging /v2/silences, with additional handling for the enclosing status/data fields. Unlike for
// alerts, the API definitions for silences are almost identical between v1 and v2. The differences
// are that the fields in the JSON output are ordered differently, and the timestamps have more
// precision in v1, but these differences should not be problematic to clients.
type V1SilenceID struct{}

func (V1SilenceID) MergeResponses(in [][]byte) ([]byte, error) {
type bodyType struct {
Status string `json:"status"`
Data *v2_models.GettableSilence `json:"data"`
}

silences := make(v2_models.GettableSilences, 0)
for _, body := range in {
parsed := bodyType{}
if err := json.Unmarshal(body, &parsed); err != nil {
return nil, err
}
if parsed.Status != statusSuccess {
return nil, fmt.Errorf("unable to merge response of status: %s", parsed.Status)
}
silences = append(silences, parsed.Data)
}

merged, err := mergeV2Silences(silences)
if err != nil {
return nil, err
}

if len(merged) != 1 {
return nil, errors.New("unexpected mismatched silence ids")
}

body := bodyType{
Status: statusSuccess,
Data: merged[0],
}

return json.Marshal(body)
}
110 changes: 110 additions & 0 deletions pkg/alertmanager/merger/v1_silence_id_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
package merger

import (
"testing"

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

func TestV1SilenceID_ReturnsNewestSilence(t *testing.T) {
in := [][]byte{
[]byte(`{"status":"success","data":{` +
`"id":"77b580dd-1d9c-4b7e-9bba-13ac173cb4e5",` +
`"matchers":[` +
`{` +
`"name":"instance",` +
`"value":"prometheus-one",` +
`"isRegex":false,` +
`"isEqual":true` +
`}` +
`],` +
`"startsAt":"2021-04-28T17:31:01.725956017Z",` +
`"endsAt":"2021-04-28T20:31:01.722829007+02:00",` +
`"updatedAt":"2021-04-28T17:32:01.725956017Z",` +
`"createdBy":"",` +
`"comment":"The newer silence",` +
`"status":{"state":"active"}` +
`}}`),
[]byte(`{"status":"success","data":{` +
`"id":"77b580dd-1d9c-4b7e-9bba-13ac173cb4e5",` +
`"matchers":[` +
`{` +
`"name":"instance",` +
`"value":"prometheus-one",` +
`"isRegex":false,` +
`"isEqual":true` +
`}` +
`],` +
`"startsAt":"2021-04-28T17:31:01.725956017Z",` +
`"endsAt":"2021-04-28T20:31:01.722829007+02:00",` +
`"updatedAt":"2021-04-28T17:31:01.725956017Z",` +
`"createdBy":"",` +
`"comment":"Silence Comment #1",` +
`"status":{"state":"active"}` +
`}}`),
}

expected := []byte(`{"status":"success","data":{` +
`"id":"77b580dd-1d9c-4b7e-9bba-13ac173cb4e5",` +
`"status":{"state":"active"},` +
`"updatedAt":"2021-04-28T17:32:01.725Z",` +
`"comment":"The newer silence",` +
`"createdBy":"",` +
`"endsAt":"2021-04-28T20:31:01.722+02:00",` +
`"matchers":[` +
`{` +
`"isEqual":true,` +
`"isRegex":false,` +
`"name":"instance",` +
`"value":"prometheus-one"` +
`}` +
`],` +
`"startsAt":"2021-04-28T17:31:01.725Z"` +
`}}`)

out, err := V1SilenceID{}.MergeResponses(in)
require.NoError(t, err)
require.Equal(t, string(expected), string(out))
}

func TestV1SilenceID_InvalidDifferentIDs(t *testing.T) {
in := [][]byte{
[]byte(`{"status":"success","data":{` +
`"id":"77b580dd-1d9c-4b7e-9bba-13ac173cb4e5",` +
`"matchers":[` +
`{` +
`"name":"instance",` +
`"value":"prometheus-one",` +
`"isRegex":false,` +
`"isEqual":true` +
`}` +
`],` +
`"startsAt":"2021-04-28T17:31:01.725956017Z",` +
`"endsAt":"2021-04-28T20:31:01.722829007+02:00",` +
`"updatedAt":"2021-04-28T17:32:01.725956017Z",` +
`"createdBy":"",` +
`"comment":"Silence Comment #1",` +
`"status":{"state":"active"}` +
`}}`),
[]byte(`{"status":"success","data":{` +
`"id":"261248d1-4ff7-4cf1-9957-850c65f4e48b",` +
`"matchers":[` +
`{` +
`"name":"instance",` +
`"value":"prometheus-one",` +
`"isRegex":false,` +
`"isEqual":true` +
`}` +
`],` +
`"startsAt":"2021-04-28T17:31:01.725956017Z",` +
`"endsAt":"2021-04-28T20:31:01.722829007+02:00",` +
`"updatedAt":"2021-04-28T17:31:01.725956017Z",` +
`"createdBy":"",` +
`"comment":"Silence Comment #2",` +
`"status":{"state":"active"}` +
`}}`),
}

_, err := V1SilenceID{}.MergeResponses(in)
require.Error(t, err)
}
45 changes: 45 additions & 0 deletions pkg/alertmanager/merger/v1_silences.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package merger

import (
"encoding/json"
"fmt"

v2_models "github.com/prometheus/alertmanager/api/v2/models"
)

// V1Silences implements the Merger interface for GET /v1/silences. Unlike for alerts, the API
// definitions for silences are almost identical between v1 and v2. The differences are that the
// fields in the JSON output are ordered differently, and the timestamps have more precision in v1,
// but these differences should not be problematic to clients. Therefore, the implementation
// re-uses the v2 types, with additional handling for the enclosing status/data fields.
type V1Silences struct{}

func (V1Silences) MergeResponses(in [][]byte) ([]byte, error) {
type bodyType struct {
Status string `json:"status"`
Data v2_models.GettableSilences `json:"data"`
}

silences := make(v2_models.GettableSilences, 0)
for _, body := range in {
parsed := bodyType{}
if err := json.Unmarshal(body, &parsed); err != nil {
return nil, err
}
if parsed.Status != statusSuccess {
return nil, fmt.Errorf("unable to merge response of status: %s", parsed.Status)
}
silences = append(silences, parsed.Data...)
}

merged, err := mergeV2Silences(silences)
if err != nil {
return nil, err
}
body := bodyType{
Status: statusSuccess,
Data: merged,
}

return json.Marshal(body)
}
Loading