rules: load rule files relative to a base dir

This commit is contained in:
Fabian Reinartz 2015-07-03 14:48:22 +02:00
parent fd7b4cae43
commit feb8a03503
2 changed files with 10 additions and 0 deletions

View file

@ -21,6 +21,7 @@ import (
_ "net/http/pprof" // Comment this line to disable pprof endpoint. _ "net/http/pprof" // Comment this line to disable pprof endpoint.
"os" "os"
"os/signal" "os/signal"
"path/filepath"
"strings" "strings"
"syscall" "syscall"
"text/template" "text/template"
@ -76,6 +77,7 @@ func Main() int {
NotificationHandler: notificationHandler, NotificationHandler: notificationHandler,
QueryEngine: queryEngine, QueryEngine: queryEngine,
ExternalURL: cfg.web.ExternalURL, ExternalURL: cfg.web.ExternalURL,
BaseDir: filepath.Dir(cfg.configFile),
}) })
flags := map[string]string{} flags := map[string]string{}

View file

@ -104,6 +104,7 @@ type Manager struct {
notificationHandler *notification.NotificationHandler notificationHandler *notification.NotificationHandler
externalURL *url.URL externalURL *url.URL
baseDir string
} }
// ManagerOptions bundles options for the Manager. // ManagerOptions bundles options for the Manager.
@ -115,6 +116,7 @@ type ManagerOptions struct {
SampleAppender storage.SampleAppender SampleAppender storage.SampleAppender
ExternalURL *url.URL ExternalURL *url.URL
BaseDir string
} }
// NewManager returns an implementation of Manager, ready to be started // NewManager returns an implementation of Manager, ready to be started
@ -129,6 +131,7 @@ func NewManager(o *ManagerOptions) *Manager {
queryEngine: o.QueryEngine, queryEngine: o.QueryEngine,
notificationHandler: o.NotificationHandler, notificationHandler: o.NotificationHandler,
externalURL: o.ExternalURL, externalURL: o.ExternalURL,
baseDir: o.BaseDir,
} }
return manager return manager
} }
@ -327,6 +330,10 @@ func (m *Manager) ApplyConfig(conf *config.Config) bool {
var files []string var files []string
for _, pat := range conf.RuleFiles { for _, pat := range conf.RuleFiles {
if !filepath.IsAbs(pat) {
pat = filepath.Join(m.baseDir, pat)
}
fs, err := filepath.Glob(pat) fs, err := filepath.Glob(pat)
if err != nil { if err != nil {
// The only error can be a bad pattern. // The only error can be a bad pattern.
@ -356,6 +363,7 @@ func (m *Manager) loadRuleFiles(filenames ...string) error {
if err != nil { if err != nil {
return fmt.Errorf("error parsing %s: %s", fn, err) return fmt.Errorf("error parsing %s: %s", fn, err)
} }
for _, stmt := range stmts { for _, stmt := range stmts {
switch r := stmt.(type) { switch r := stmt.(type) {
case *promql.AlertStmt: case *promql.AlertStmt: