Skip to content

Commit

Permalink
Consolidate external reachability flags into one.
Browse files Browse the repository at this point in the history
Besides fixing prometheus#805 by
making the entire externally reachable server URL configurable, this
adds tests for the "globalURL" template function and makes it easier to
test other such functions in the future.

This breaks the `web.Hostname` flag (and introduces `web.external-url`).
This flag is likely only used by few users, so I hope that's
justifiable.

Fixes prometheus#805
  • Loading branch information
juliusv committed Jul 3, 2015
1 parent 8510076 commit fcff35b
Show file tree
Hide file tree
Showing 6 changed files with 122 additions and 90 deletions.
36 changes: 19 additions & 17 deletions cmd/prometheus/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"flag"
"fmt"
"net"
"net/url"
"os"
"strings"
"text/template"
Expand Down Expand Up @@ -68,17 +69,13 @@ func init() {
)

// Web.
cfg.fs.StringVar(
&cfg.web.PathPrefix, "web.path-prefix", "",
"Prefix for all web paths.",
)
cfg.fs.StringVar(
&cfg.web.ListenAddress, "web.listen-address", ":9090",
"Address to listen on for the web interface, API, and telemetry.",
)
cfg.fs.StringVar(
&cfg.web.Hostname, "web.hostname", "",
"Hostname on which the server is available.",
&cfg.prometheusURL, "web.external-url", "",
"The URL under which Prometheus is externally reachable (for example, if Prometheus is served via a reverse proxy). Used for generating relative and absolute links back to Prometheus itself. If omitted, relevant URL components will be derived automatically.",
)
cfg.fs.StringVar(
&cfg.web.MetricsPath, "web.telemetry-path", "/metrics",
Expand All @@ -98,7 +95,7 @@ func init() {
)
cfg.fs.StringVar(
&cfg.web.ConsoleTemplatesPath, "web.console.templates", "consoles",
"Path to the console template directory, available at /console.",
"Path to the console template directory, available at /consoles.",
)
cfg.fs.StringVar(
&cfg.web.ConsoleLibrariesPath, "web.console.libraries", "console_libraries",
Expand Down Expand Up @@ -224,24 +221,29 @@ func parse(args []string) error {
return err
}

ppref := strings.TrimRight(cfg.web.PathPrefix, "/")
if ppref != "" && !strings.HasPrefix(ppref, "/") {
ppref = "/" + ppref
}
cfg.web.PathPrefix = ppref

if cfg.web.Hostname == "" {
cfg.web.Hostname, err = os.Hostname()
if cfg.prometheusURL == "" {
hostname, err := os.Hostname()
if err != nil {
return err
}
_, port, err := net.SplitHostPort(cfg.web.ListenAddress)
if err != nil {
return err
}
cfg.prometheusURL = fmt.Sprintf("http://%s:%s/", hostname, port)
}

_, port, err := net.SplitHostPort(cfg.web.ListenAddress)
promURL, err := url.Parse(cfg.prometheusURL)
if err != nil {
return err
}
cfg.prometheusURL = fmt.Sprintf("http://%s:%s%s/", cfg.web.Hostname, port, cfg.web.PathPrefix)
cfg.web.ExternalURL = promURL

ppref := strings.TrimRight(cfg.web.ExternalURL.Path, "/")
if ppref != "" && !strings.HasPrefix(ppref, "/") {
ppref = "/" + ppref
}
cfg.web.ExternalURL.Path = ppref

return nil
}
Expand Down
3 changes: 1 addition & 2 deletions cmd/prometheus/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,7 @@ func Main() int {
SampleAppender: sampleAppender,
NotificationHandler: notificationHandler,
QueryEngine: queryEngine,
PrometheusURL: cfg.prometheusURL,
PathPrefix: cfg.web.PathPrefix,
ExternalURL: cfg.web.ExternalURL,
})

flags := map[string]string{}
Expand Down
14 changes: 6 additions & 8 deletions rules/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ package rules
import (
"fmt"
"io/ioutil"
"net/url"
"path/filepath"
"strings"
"sync"
"time"

Expand Down Expand Up @@ -103,8 +103,7 @@ type Manager struct {
sampleAppender storage.SampleAppender
notificationHandler *notification.NotificationHandler

prometheusURL string
pathPrefix string
externalURL *url.URL
}

// ManagerOptions bundles options for the Manager.
Expand All @@ -115,8 +114,7 @@ type ManagerOptions struct {
NotificationHandler *notification.NotificationHandler
SampleAppender storage.SampleAppender

PrometheusURL string
PathPrefix string
ExternalURL *url.URL
}

// NewManager returns an implementation of Manager, ready to be started
Expand All @@ -130,7 +128,7 @@ func NewManager(o *ManagerOptions) *Manager {
sampleAppender: o.SampleAppender,
queryEngine: o.QueryEngine,
notificationHandler: o.NotificationHandler,
prometheusURL: o.PrometheusURL,
externalURL: o.ExternalURL,
}
return manager
}
Expand Down Expand Up @@ -211,7 +209,7 @@ func (m *Manager) queueAlertNotifications(rule *AlertingRule, timestamp clientmo
defs := "{{$labels := .Labels}}{{$value := .Value}}"

expand := func(text string) string {
tmpl := template.NewTemplateExpander(defs+text, "__alert_"+rule.Name(), tmplData, timestamp, m.queryEngine, m.pathPrefix)
tmpl := template.NewTemplateExpander(defs+text, "__alert_"+rule.Name(), tmplData, timestamp, m.queryEngine, m.externalURL.Path)
result, err := tmpl.Expand()
if err != nil {
result = err.Error()
Expand All @@ -230,7 +228,7 @@ func (m *Manager) queueAlertNotifications(rule *AlertingRule, timestamp clientmo
Value: aa.Value,
ActiveSince: aa.ActiveSince.Time(),
RuleString: rule.String(),
GeneratorURL: m.prometheusURL + strings.TrimLeft(strutil.GraphLinkForExpression(rule.vector.String()), "/"),
GeneratorURL: m.externalURL.String() + strutil.GraphLinkForExpression(rule.vector.String()),
})
}
m.notificationHandler.SubmitReqs(notifications)
Expand Down
Loading

0 comments on commit fcff35b

Please sign in to comment.