Import code from internal repository (#1)

Import code from internal repository
This commit is contained in:
Łukasz Mierzwa
2017-03-23 16:58:04 -07:00
committed by GitHub
parent 42a6268135
commit e239fd05fd
126 changed files with 11959 additions and 0 deletions
+40
View File
@@ -0,0 +1,40 @@
package alertmanager
import (
"errors"
"time"
"github.com/cloudflare/unsee/config"
"github.com/cloudflare/unsee/models"
log "github.com/Sirupsen/logrus"
)
// AlertGroupsAPIResponse is the schema of API response for /api/v1/alerts/groups
type AlertGroupsAPIResponse struct {
Status string `json:"status"`
Groups []models.AlertManagerAlertGroup `json:"data"`
ErrorType string `json:"errorType"`
Error string `json:"error"`
}
// Get response from AlertManager /api/v1/alerts/groups
func (response *AlertGroupsAPIResponse) Get() error {
start := time.Now()
url, err := joinURL(config.Config.AlertManagerURL, "api/v1/alerts/groups")
if err != nil {
return err
}
err = getJSONFromURL(url, config.Config.AlertManagerTimeout, response)
if err != nil {
return err
}
if response.Status != "success" {
return errors.New(response.Error)
}
log.Infof("Got %d alert group(s) in %s", len(response.Groups), time.Since(start))
return nil
}
+66
View File
@@ -0,0 +1,66 @@
package alertmanager
import (
"compress/gzip"
"encoding/json"
"fmt"
"io"
"net/http"
"net/url"
"path"
"time"
log "github.com/Sirupsen/logrus"
)
// joinURL can be used to join a base url (http(s)://domain.com) and a path (/my/path)
// it will return a joined string or an error (if you supply invalid url)
func joinURL(base string, sub string) (string, error) {
u, err := url.Parse(base)
if err != nil {
return "", err
}
u.Path = path.Join(u.Path, sub)
return u.String(), nil
}
// getJSONFromURL is a helper function that takesan URL, request timeout
// and target structure, it will make a HTTP request and decode JSON response
// onto the structure provided
func getJSONFromURL(url string, timeout time.Duration, target interface{}) error {
log.Infof("GET %s", url)
c := &http.Client{
Timeout: timeout,
}
req, err := http.NewRequest("GET", url, nil)
if err != nil {
return err
}
req.Header.Add("Accept-Encoding", "gzip")
resp, err := c.Do(req)
if err != nil {
return err
}
if resp.StatusCode != http.StatusOK {
return fmt.Errorf("Request to AlertManager failed with %s", resp.Status)
}
defer resp.Body.Close()
var reader io.ReadCloser
switch resp.Header.Get("Content-Encoding") {
case "gzip":
reader, err = gzip.NewReader(resp.Body)
if err != nil {
return fmt.Errorf("Failed to decode gzipped content: %s", err.Error())
}
defer reader.Close()
default:
reader = resp.Body
}
return json.NewDecoder(reader).Decode(target)
}
+48
View File
@@ -0,0 +1,48 @@
package alertmanager
import (
"errors"
"fmt"
"math"
"time"
"github.com/cloudflare/unsee/config"
"github.com/cloudflare/unsee/models"
log "github.com/Sirupsen/logrus"
)
type silencesData struct {
Silences []models.AlertManagerSilence `json:"silences"`
TotalSilences int `json:"totalSilences"`
}
// SilenceAPIResponse is what AlertManager API returns
type SilenceAPIResponse struct {
Status string `json:"status"`
Data silencesData `json:"data"`
ErrorType string `json:"errorType"`
Error string `json:"error"`
}
// Get will return fresh data from AlertManager API
func (response *SilenceAPIResponse) Get() error {
start := time.Now()
url, err := joinURL(config.Config.AlertManagerURL, "api/v1/silences")
if err != nil {
return err
}
url = fmt.Sprintf("%s?limit=%d", url, math.MaxUint32)
err = getJSONFromURL(url, config.Config.AlertManagerTimeout, response)
if err != nil {
return err
}
if response.Status != "success" {
return errors.New(response.Error)
}
log.Infof("Got %d silences(s) in %s", len(response.Data.Silences), time.Since(start))
return nil
}