提交 04ad8897 编写于 作者: D David Symonds

Preserve Alertmanager URLs as *url.URL.

Render a nicer link in the web UI.
上级 9eb1a5d6
......@@ -316,13 +316,13 @@ func (n *Notifier) setMore() {
}
}
// Alertmanagers returns a list Alertmanager URLs.
func (n *Notifier) Alertmanagers() []string {
// Alertmanagers returns a slice of Alertmanager URLs.
func (n *Notifier) Alertmanagers() []*url.URL {
n.mtx.RLock()
amSets := n.alertmanagers
n.mtx.RUnlock()
var res []string
var res []*url.URL
for _, ams := range amSets {
ams.mtx.RLock()
......@@ -364,7 +364,7 @@ func (n *Notifier) sendAll(alerts ...*model.Alert) bool {
defer cancel()
go func(am alertmanager) {
u := am.url()
u := am.url().String()
if err := n.sendOne(ctx, ams.client, u, b); err != nil {
log.With("alertmanager", u).With("count", len(alerts)).Errorf("Error sending alerts: %s", err)
......@@ -412,20 +412,19 @@ func (n *Notifier) Stop() {
// alertmanager holds Alertmanager endpoint information.
type alertmanager interface {
url() string
url() *url.URL
}
type alertmanagerLabels model.LabelSet
const pathLabel = "__alerts_path__"
func (a alertmanagerLabels) url() string {
u := &url.URL{
func (a alertmanagerLabels) url() *url.URL {
return &url.URL{
Scheme: string(a[model.SchemeLabel]),
Host: string(a[model.AddressLabel]),
Path: string(a[pathLabel]),
}
return u.String()
}
// alertmanagerSet contains a set of Alertmanagers discovered via a group of service
......@@ -476,7 +475,7 @@ func (s *alertmanagerSet) Sync(tgs []*config.TargetGroup) {
seen := map[string]struct{}{}
for _, am := range all {
us := am.url()
us := am.url().String()
if _, ok := seen[us]; ok {
continue
}
......
......@@ -19,6 +19,7 @@ import (
"io/ioutil"
"net/http"
"net/http/httptest"
"net/url"
"reflect"
"testing"
"time"
......@@ -415,6 +416,10 @@ type alertmanagerMock struct {
urlf func() string
}
func (a alertmanagerMock) url() string {
return a.urlf()
func (a alertmanagerMock) url() *url.URL {
u, err := url.Parse(a.urlf())
if err != nil {
panic(err)
}
return u
}
......@@ -19,6 +19,7 @@ import (
"fmt"
"math"
"net/http"
"net/url"
"sort"
"strconv"
"time"
......@@ -74,7 +75,7 @@ type targetRetriever interface {
}
type alertmanagerRetriever interface {
Alertmanagers() []string
Alertmanagers() []*url.URL
}
type response struct {
......@@ -430,8 +431,8 @@ func (api *API) alertmanagers(r *http.Request) (interface{}, *apiError) {
urls := api.alertmanagerRetriever.Alertmanagers()
ams := &AlertmanagerDiscovery{ActiveAlertmanagers: make([]*AlertmanagerTarget, len(urls))}
for i := range urls {
ams.ActiveAlertmanagers[i] = &AlertmanagerTarget{URL: urls[i]}
for i, url := range urls {
ams.ActiveAlertmanagers[i] = &AlertmanagerTarget{URL: url.String()}
}
return ams, nil
......
......@@ -39,9 +39,9 @@ func (f targetRetrieverFunc) Targets() []*retrieval.Target {
return f()
}
type alertmanagerRetrieverFunc func() []string
type alertmanagerRetrieverFunc func() []*url.URL
func (f alertmanagerRetrieverFunc) Alertmanagers() []string {
func (f alertmanagerRetrieverFunc) Alertmanagers() []*url.URL {
return f()
}
......@@ -77,8 +77,12 @@ func TestEndpoints(t *testing.T) {
}
})
ar := alertmanagerRetrieverFunc(func() []string {
return []string{"http://alertmanager.example.com:8080/api/v1/alerts"}
ar := alertmanagerRetrieverFunc(func() []*url.URL {
return []*url.URL{{
Scheme: "http",
Host: "alertmanager.example.com:8080",
Path: "/api/v1/alerts",
}}
})
api := &API{
......
此差异已折叠。
......@@ -54,7 +54,8 @@
</tr>
{{range .Alertmanagers}}
<tr>
<td>{{.}}</td>
{{/* Alertmanager URLs always have Scheme, Host and Path set */}}
<td>{{.Scheme}}://<a href="{{.Scheme}}://{{.Host}}">{{.Host}}</a>{{.Path}}</td>
</tr>
{{end}}
</tbody>
......
......@@ -347,7 +347,7 @@ func (h *Handler) status(w http.ResponseWriter, r *http.Request) {
Birth time.Time
CWD string
Version *PrometheusVersion
Alertmanagers []string
Alertmanagers []*url.URL
}{
Birth: h.birth,
CWD: h.cwd,
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册