Skip to content
This repository has been archived by the owner on Jul 22, 2020. It is now read-only.

Commit

Permalink
Allow passing query args to HTTP requests send to the Alertmanager API
Browse files Browse the repository at this point in the history
Alertmanager 0.4.x silences endpoint uses pagination, code for this was incorrectly dropped in #216, re-add it. Requires a way for mapper packages to signal the need for passing query args to HTTP requests
  • Loading branch information
prymitive committed Jan 25, 2018
1 parent 3f8368b commit 0f73b20
Show file tree
Hide file tree
Showing 8 changed files with 46 additions and 0 deletions.
11 changes: 11 additions & 0 deletions internal/alertmanager/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,11 @@ func (am *Alertmanager) pullSilences(version string) error {
log.Errorf("[%s] Failed to generate silences endpoint URL: %s", am.Name, err)
return err
}
// append query args if mapper needs those
queryArgs := mapper.QueryArgs()
if queryArgs != "" {
url = fmt.Sprintf("%s?%s", url, queryArgs)
}

start := time.Now()
// read raw body from the source
Expand Down Expand Up @@ -175,6 +180,12 @@ func (am *Alertmanager) pullAlerts(version string) error {
return err
}

// append query args if mapper needs those
queryArgs := mapper.QueryArgs()
if queryArgs != "" {
url = fmt.Sprintf("%s?%s", url, queryArgs)
}

start := time.Now()
// read raw body from the source
source, err := am.reader.Read(url)
Expand Down
1 change: 1 addition & 0 deletions internal/mapper/mapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ var (
type Mapper interface {
IsSupported(version string) bool
AbsoluteURL(baseURI string) (string, error)
QueryArgs() string
}

// AlertMapper handles mapping of Alertmanager alert information to unsee AlertGroup models
Expand Down
5 changes: 5 additions & 0 deletions internal/mapper/v04/alerts.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,11 @@ func (m AlertMapper) AbsoluteURL(baseURI string) (string, error) {
return uri.JoinURL(baseURI, "api/v1/alerts/groups")
}

// QueryArgs for HTTP requests send to the Alertmanager API endpoint
func (m AlertMapper) QueryArgs() string {
return ""
}

// IsSupported returns true if given version string is supported
func (m AlertMapper) IsSupported(version string) bool {
versionRange := semver.MustParseRange(">=0.4.0 <0.5.0")
Expand Down
9 changes: 9 additions & 0 deletions internal/mapper/v04/silences.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ package v04
import (
"encoding/json"
"errors"
"fmt"
"io"
"math"
"strconv"
"time"

Expand Down Expand Up @@ -52,6 +54,13 @@ func (m SilenceMapper) AbsoluteURL(baseURI string) (string, error) {
return uri.JoinURL(baseURI, "api/v1/silences")
}

// QueryArgs for HTTP requests send to the Alertmanager API endpoint
func (m SilenceMapper) QueryArgs() string {
// Alertmanager 0.4 uses pagination for silences, pass a huge value so that
// we get all possible silences
return fmt.Sprintf("api/v1/silences?limit=%d", math.MaxInt32)
}

// IsSupported returns true if given version string is supported
func (m SilenceMapper) IsSupported(version string) bool {
versionRange := semver.MustParseRange(">=0.4.0 <0.5.0")
Expand Down
5 changes: 5 additions & 0 deletions internal/mapper/v05/alerts.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,11 @@ func (m AlertMapper) AbsoluteURL(baseURI string) (string, error) {
return uri.JoinURL(baseURI, "api/v1/alerts/groups")
}

// QueryArgs for HTTP requests send to the Alertmanager API endpoint
func (m AlertMapper) QueryArgs() string {
return ""
}

// IsSupported returns true if given version string is supported
func (m AlertMapper) IsSupported(version string) bool {
versionRange := semver.MustParseRange(">=0.5.0 <=0.6.0")
Expand Down
5 changes: 5 additions & 0 deletions internal/mapper/v05/silences.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,11 @@ func (m SilenceMapper) AbsoluteURL(baseURI string) (string, error) {
return uri.JoinURL(baseURI, "api/v1/silences")
}

// QueryArgs for HTTP requests send to the Alertmanager API endpoint
func (m SilenceMapper) QueryArgs() string {
return ""
}

// IsSupported returns true if given version string is supported
func (m SilenceMapper) IsSupported(version string) bool {
versionRange := semver.MustParseRange(">=0.5.0")
Expand Down
5 changes: 5 additions & 0 deletions internal/mapper/v061/alerts.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,11 @@ func (m AlertMapper) AbsoluteURL(baseURI string) (string, error) {
return uri.JoinURL(baseURI, "api/v1/alerts/groups")
}

// QueryArgs for HTTP requests send to the Alertmanager API endpoint
func (m AlertMapper) QueryArgs() string {
return ""
}

// IsSupported returns true if given version string is supported
func (m AlertMapper) IsSupported(version string) bool {
versionRange := semver.MustParseRange("=0.6.1")
Expand Down
5 changes: 5 additions & 0 deletions internal/mapper/v062/alerts.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,11 @@ func (m AlertMapper) AbsoluteURL(baseURI string) (string, error) {
return uri.JoinURL(baseURI, "api/v1/alerts/groups")
}

// QueryArgs for HTTP requests send to the Alertmanager API endpoint
func (m AlertMapper) QueryArgs() string {
return ""
}

// IsSupported returns true if given version string is supported
func (m AlertMapper) IsSupported(version string) bool {
versionRange := semver.MustParseRange(">=0.6.2")
Expand Down

0 comments on commit 0f73b20

Please sign in to comment.