2015-01-21 11:07:45 -08:00
|
|
|
// Copyright 2013 The Prometheus Authors
|
2013-02-08 05:49:55 -08:00
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
package web
|
|
|
|
|
|
|
|
import (
|
2015-11-11 07:31:09 -08:00
|
|
|
"bytes"
|
2017-10-24 21:21:42 -07:00
|
|
|
"context"
|
2015-06-16 05:57:30 -07:00
|
|
|
"encoding/json"
|
2013-04-05 04:41:52 -07:00
|
|
|
"fmt"
|
2015-06-15 03:50:53 -07:00
|
|
|
"io"
|
2014-07-29 09:28:48 -07:00
|
|
|
"io/ioutil"
|
2017-08-11 11:45:52 -07:00
|
|
|
stdlog "log"
|
2015-06-30 05:38:01 -07:00
|
|
|
"net"
|
2013-02-08 05:49:55 -08:00
|
|
|
"net/http"
|
2017-08-22 16:00:56 -07:00
|
|
|
"net/http/pprof"
|
2015-06-15 03:50:53 -07:00
|
|
|
"net/url"
|
2013-08-09 09:09:44 -07:00
|
|
|
"os"
|
2017-05-22 05:15:02 -07:00
|
|
|
"path"
|
2015-06-15 03:50:53 -07:00
|
|
|
"path/filepath"
|
2018-03-07 07:14:46 -08:00
|
|
|
"runtime"
|
2015-06-15 03:50:53 -07:00
|
|
|
"sort"
|
2015-07-08 07:14:57 -07:00
|
|
|
"strings"
|
2015-06-15 03:50:53 -07:00
|
|
|
"sync"
|
2017-07-25 17:47:45 -07:00
|
|
|
"sync/atomic"
|
2013-08-29 06:15:22 -07:00
|
|
|
"time"
|
|
|
|
|
2017-07-06 05:38:40 -07:00
|
|
|
"google.golang.org/grpc"
|
|
|
|
|
2013-08-29 06:15:22 -07:00
|
|
|
pprof_runtime "runtime/pprof"
|
2015-06-23 08:46:50 -07:00
|
|
|
template_text "text/template"
|
2013-06-28 01:19:16 -07:00
|
|
|
|
2017-07-06 05:38:40 -07:00
|
|
|
"github.com/cockroachdb/cmux"
|
2017-08-11 11:45:52 -07:00
|
|
|
"github.com/go-kit/kit/log"
|
|
|
|
"github.com/go-kit/kit/log/level"
|
2017-10-24 21:21:42 -07:00
|
|
|
"github.com/mwitkow/go-conntrack"
|
2017-05-02 16:49:29 -07:00
|
|
|
"github.com/opentracing-contrib/go-stdlib/nethttp"
|
|
|
|
"github.com/opentracing/opentracing-go"
|
2013-06-28 01:19:16 -07:00
|
|
|
"github.com/prometheus/client_golang/prometheus"
|
2015-08-20 08:18:46 -07:00
|
|
|
"github.com/prometheus/common/model"
|
2015-09-24 08:07:11 -07:00
|
|
|
"github.com/prometheus/common/route"
|
2017-07-06 05:38:40 -07:00
|
|
|
"github.com/prometheus/tsdb"
|
2016-12-06 01:45:59 -08:00
|
|
|
"golang.org/x/net/netutil"
|
2013-06-28 01:19:16 -07:00
|
|
|
|
2015-06-15 03:50:53 -07:00
|
|
|
"github.com/prometheus/prometheus/config"
|
2016-11-23 09:23:09 -08:00
|
|
|
"github.com/prometheus/prometheus/notifier"
|
2017-04-04 05:44:39 -07:00
|
|
|
"github.com/prometheus/prometheus/pkg/labels"
|
2015-06-15 03:50:53 -07:00
|
|
|
"github.com/prometheus/prometheus/promql"
|
|
|
|
"github.com/prometheus/prometheus/rules"
|
2018-02-01 01:55:07 -08:00
|
|
|
"github.com/prometheus/prometheus/scrape"
|
2017-10-24 21:21:42 -07:00
|
|
|
"github.com/prometheus/prometheus/storage"
|
2015-06-15 03:50:53 -07:00
|
|
|
"github.com/prometheus/prometheus/template"
|
2015-09-17 05:49:50 -07:00
|
|
|
"github.com/prometheus/prometheus/util/httputil"
|
2016-06-23 08:14:32 -07:00
|
|
|
api_v1 "github.com/prometheus/prometheus/web/api/v1"
|
2017-07-10 06:44:29 -07:00
|
|
|
api_v2 "github.com/prometheus/prometheus/web/api/v2"
|
2015-11-11 07:31:09 -08:00
|
|
|
"github.com/prometheus/prometheus/web/ui"
|
2013-02-08 05:49:55 -08:00
|
|
|
)
|
|
|
|
|
2015-05-18 03:16:25 -07:00
|
|
|
var localhostRepresentations = []string{"127.0.0.1", "localhost"}
|
|
|
|
|
2015-06-15 03:50:53 -07:00
|
|
|
// Handler serves various HTTP endpoints of the Prometheus server
|
|
|
|
type Handler struct {
|
2017-08-11 11:45:52 -07:00
|
|
|
logger log.Logger
|
|
|
|
|
2018-02-01 02:06:24 -08:00
|
|
|
scrapeManager *scrape.Manager
|
2016-05-13 08:59:59 -07:00
|
|
|
ruleManager *rules.Manager
|
|
|
|
queryEngine *promql.Engine
|
2016-09-15 15:58:06 -07:00
|
|
|
context context.Context
|
2017-09-18 03:32:17 -07:00
|
|
|
tsdb func() *tsdb.DB
|
2016-12-29 00:27:30 -08:00
|
|
|
storage storage.Storage
|
2018-01-30 09:45:37 -08:00
|
|
|
notifier *notifier.Manager
|
2015-06-15 03:50:53 -07:00
|
|
|
|
2016-06-23 08:14:32 -07:00
|
|
|
apiV1 *api_v1.API
|
2015-06-15 03:50:53 -07:00
|
|
|
|
2016-05-13 08:59:59 -07:00
|
|
|
router *route.Router
|
|
|
|
quitCh chan struct{}
|
2016-07-11 07:24:54 -07:00
|
|
|
reloadCh chan chan error
|
2016-05-13 08:59:59 -07:00
|
|
|
options *Options
|
2017-09-04 05:09:21 -07:00
|
|
|
config *config.Config
|
2016-05-13 08:59:59 -07:00
|
|
|
configString string
|
|
|
|
versionInfo *PrometheusVersion
|
|
|
|
birth time.Time
|
2016-12-03 15:37:59 -08:00
|
|
|
cwd string
|
2016-05-13 08:59:59 -07:00
|
|
|
flagsMap map[string]string
|
2015-06-15 03:50:53 -07:00
|
|
|
|
2015-09-29 08:51:03 -07:00
|
|
|
externalLabels model.LabelSet
|
|
|
|
mtx sync.RWMutex
|
2016-09-08 08:39:52 -07:00
|
|
|
now func() model.Time
|
2017-07-25 17:47:45 -07:00
|
|
|
|
|
|
|
ready uint32 // ready is uint32 rather than boolean to be able to use atomic functions.
|
2015-09-01 09:47:48 -07:00
|
|
|
}
|
|
|
|
|
2017-05-11 08:09:24 -07:00
|
|
|
// ApplyConfig updates the config field of the Handler struct
|
2016-07-11 07:24:54 -07:00
|
|
|
func (h *Handler) ApplyConfig(conf *config.Config) error {
|
2015-09-01 09:47:48 -07:00
|
|
|
h.mtx.Lock()
|
|
|
|
defer h.mtx.Unlock()
|
|
|
|
|
2017-05-11 08:09:24 -07:00
|
|
|
h.config = conf
|
2015-09-01 09:47:48 -07:00
|
|
|
|
2016-07-11 07:24:54 -07:00
|
|
|
return nil
|
2015-06-15 03:50:53 -07:00
|
|
|
}
|
2013-02-08 06:38:50 -08:00
|
|
|
|
2016-05-13 08:59:59 -07:00
|
|
|
// PrometheusVersion contains build information about Prometheus.
|
2016-05-05 04:46:51 -07:00
|
|
|
type PrometheusVersion struct {
|
|
|
|
Version string `json:"version"`
|
|
|
|
Revision string `json:"revision"`
|
|
|
|
Branch string `json:"branch"`
|
|
|
|
BuildUser string `json:"buildUser"`
|
|
|
|
BuildDate string `json:"buildDate"`
|
|
|
|
GoVersion string `json:"goVersion"`
|
|
|
|
}
|
|
|
|
|
2015-06-15 03:50:53 -07:00
|
|
|
// Options for the web Handler.
|
|
|
|
type Options struct {
|
2016-09-15 15:58:06 -07:00
|
|
|
Context context.Context
|
2017-09-18 03:32:17 -07:00
|
|
|
TSDB func() *tsdb.DB
|
|
|
|
Storage storage.Storage
|
2016-09-15 15:58:06 -07:00
|
|
|
QueryEngine *promql.Engine
|
2018-02-01 02:06:24 -08:00
|
|
|
ScrapeManager *scrape.Manager
|
2016-09-15 15:58:06 -07:00
|
|
|
RuleManager *rules.Manager
|
2018-01-30 09:45:37 -08:00
|
|
|
Notifier *notifier.Manager
|
2016-09-15 15:58:06 -07:00
|
|
|
Version *PrometheusVersion
|
|
|
|
Flags map[string]string
|
|
|
|
|
2015-06-15 03:50:53 -07:00
|
|
|
ListenAddress string
|
2016-12-01 05:29:45 -08:00
|
|
|
ReadTimeout time.Duration
|
2016-12-06 01:45:59 -08:00
|
|
|
MaxConnections int
|
2015-06-30 05:38:01 -07:00
|
|
|
ExternalURL *url.URL
|
2016-07-05 06:05:43 -07:00
|
|
|
RoutePrefix string
|
2017-07-06 05:38:40 -07:00
|
|
|
MetricsPath string
|
2015-06-15 03:50:53 -07:00
|
|
|
UseLocalAssets bool
|
|
|
|
UserAssetsPath string
|
|
|
|
ConsoleTemplatesPath string
|
|
|
|
ConsoleLibrariesPath string
|
2017-07-10 06:44:29 -07:00
|
|
|
EnableLifecycle bool
|
2017-07-10 00:29:41 -07:00
|
|
|
EnableAdminAPI bool
|
2015-06-15 03:50:53 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// New initializes a new web Handler.
|
2017-08-11 11:45:52 -07:00
|
|
|
func New(logger log.Logger, o *Options) *Handler {
|
2017-05-02 16:49:29 -07:00
|
|
|
router := route.New()
|
2016-12-03 15:37:59 -08:00
|
|
|
cwd, err := os.Getwd()
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
cwd = "<error retrieving current working directory>"
|
|
|
|
}
|
2017-08-11 11:45:52 -07:00
|
|
|
if logger == nil {
|
|
|
|
logger = log.NewNopLogger()
|
|
|
|
}
|
2016-12-03 15:37:59 -08:00
|
|
|
|
2015-06-15 03:50:53 -07:00
|
|
|
h := &Handler{
|
2017-08-11 11:45:52 -07:00
|
|
|
logger: logger,
|
2015-08-20 09:23:57 -07:00
|
|
|
router: router,
|
|
|
|
quitCh: make(chan struct{}),
|
2016-07-11 07:24:54 -07:00
|
|
|
reloadCh: make(chan chan error),
|
2015-08-20 09:23:57 -07:00
|
|
|
options: o,
|
2016-09-15 15:58:06 -07:00
|
|
|
versionInfo: o.Version,
|
2016-05-13 08:59:59 -07:00
|
|
|
birth: time.Now(),
|
2016-12-03 15:37:59 -08:00
|
|
|
cwd: cwd,
|
2016-09-15 15:58:06 -07:00
|
|
|
flagsMap: o.Flags,
|
2015-06-15 03:50:53 -07:00
|
|
|
|
2016-09-15 15:58:06 -07:00
|
|
|
context: o.Context,
|
2017-11-25 05:13:54 -08:00
|
|
|
scrapeManager: o.ScrapeManager,
|
2016-09-15 15:58:06 -07:00
|
|
|
ruleManager: o.RuleManager,
|
|
|
|
queryEngine: o.QueryEngine,
|
2017-09-18 03:32:17 -07:00
|
|
|
tsdb: o.TSDB,
|
|
|
|
storage: o.Storage,
|
2016-11-23 09:23:09 -08:00
|
|
|
notifier: o.Notifier,
|
2015-06-15 03:50:53 -07:00
|
|
|
|
2017-05-11 08:09:24 -07:00
|
|
|
now: model.Now,
|
2017-07-25 17:47:45 -07:00
|
|
|
|
|
|
|
ready: 0,
|
2015-06-02 04:07:46 -07:00
|
|
|
}
|
2013-05-06 00:56:32 -07:00
|
|
|
|
2017-11-25 05:13:54 -08:00
|
|
|
h.apiV1 = api_v1.NewAPI(h.queryEngine, h.storage, h.scrapeManager, h.notifier,
|
2017-05-11 08:09:24 -07:00
|
|
|
func() config.Config {
|
|
|
|
h.mtx.RLock()
|
|
|
|
defer h.mtx.RUnlock()
|
|
|
|
return *h.config
|
|
|
|
},
|
api: Added v1/status/flags endpoint. (#3864)
Endpoint URL: /api/v1/status/flags
Example Output:
```json
{
"status": "success",
"data": {
"alertmanager.notification-queue-capacity": "10000",
"alertmanager.timeout": "10s",
"completion-bash": "false",
"completion-script-bash": "false",
"completion-script-zsh": "false",
"config.file": "my_cool_prometheus.yaml",
"help": "false",
"help-long": "false",
"help-man": "false",
"log.level": "info",
"query.lookback-delta": "5m",
"query.max-concurrency": "20",
"query.timeout": "2m",
"storage.tsdb.max-block-duration": "36h",
"storage.tsdb.min-block-duration": "2h",
"storage.tsdb.no-lockfile": "false",
"storage.tsdb.path": "data/",
"storage.tsdb.retention": "15d",
"version": "false",
"web.console.libraries": "console_libraries",
"web.console.templates": "consoles",
"web.enable-admin-api": "false",
"web.enable-lifecycle": "false",
"web.external-url": "",
"web.listen-address": "0.0.0.0:9090",
"web.max-connections": "512",
"web.read-timeout": "5m",
"web.route-prefix": "/",
"web.user-assets": ""
}
}
```
Signed-off-by: Bartek Plotka <bwplotka@gmail.com>
2018-02-21 00:49:02 -08:00
|
|
|
o.Flags,
|
2017-10-06 08:20:20 -07:00
|
|
|
h.testReady,
|
2017-12-02 21:07:05 -08:00
|
|
|
h.options.TSDB,
|
|
|
|
h.options.EnableAdminAPI,
|
2017-05-11 08:09:24 -07:00
|
|
|
)
|
2017-07-06 05:38:40 -07:00
|
|
|
|
2016-07-05 06:05:43 -07:00
|
|
|
if o.RoutePrefix != "/" {
|
2015-06-15 03:50:53 -07:00
|
|
|
// If the prefix is missing for the root path, prepend it.
|
2015-06-02 23:38:50 -07:00
|
|
|
router.Get("/", func(w http.ResponseWriter, r *http.Request) {
|
2016-07-05 06:05:43 -07:00
|
|
|
http.Redirect(w, r, o.RoutePrefix, http.StatusFound)
|
2015-06-02 23:38:50 -07:00
|
|
|
})
|
2016-07-05 06:05:43 -07:00
|
|
|
router = router.WithPrefix(o.RoutePrefix)
|
2015-06-02 23:38:50 -07:00
|
|
|
}
|
|
|
|
|
2015-09-17 07:33:14 -07:00
|
|
|
instrh := prometheus.InstrumentHandler
|
|
|
|
instrf := prometheus.InstrumentHandlerFunc
|
2017-07-25 17:47:45 -07:00
|
|
|
readyf := h.testReady
|
2015-06-02 23:38:50 -07:00
|
|
|
|
2015-08-28 01:53:13 -07:00
|
|
|
router.Get("/", func(w http.ResponseWriter, r *http.Request) {
|
2017-08-19 11:44:15 -07:00
|
|
|
http.Redirect(w, r, path.Join(o.ExternalURL.Path, "/graph"), http.StatusFound)
|
2015-08-28 01:53:13 -07:00
|
|
|
})
|
|
|
|
|
2017-07-25 17:47:45 -07:00
|
|
|
router.Get("/alerts", readyf(instrf("alerts", h.alerts)))
|
|
|
|
router.Get("/graph", readyf(instrf("graph", h.graph)))
|
|
|
|
router.Get("/status", readyf(instrf("status", h.status)))
|
|
|
|
router.Get("/flags", readyf(instrf("flags", h.flags)))
|
2017-05-11 08:09:24 -07:00
|
|
|
router.Get("/config", readyf(instrf("config", h.serveConfig)))
|
2017-07-25 17:47:45 -07:00
|
|
|
router.Get("/rules", readyf(instrf("rules", h.rules)))
|
|
|
|
router.Get("/targets", readyf(instrf("targets", h.targets)))
|
|
|
|
router.Get("/version", readyf(instrf("version", h.version)))
|
2017-12-04 07:12:28 -08:00
|
|
|
router.Get("/service-discovery", readyf(instrf("servicediscovery", h.serviceDiscovery)))
|
2015-06-02 23:38:50 -07:00
|
|
|
|
2017-09-18 03:32:17 -07:00
|
|
|
router.Get("/heap", instrf("heap", h.dumpHeap))
|
2015-06-02 23:38:50 -07:00
|
|
|
|
2017-06-16 07:51:41 -07:00
|
|
|
router.Get("/metrics", prometheus.Handler().ServeHTTP)
|
2015-06-04 09:24:04 -07:00
|
|
|
|
2017-07-25 17:47:45 -07:00
|
|
|
router.Get("/federate", readyf(instrh("federate", httputil.CompressionHandler{
|
2015-09-18 07:51:53 -07:00
|
|
|
Handler: http.HandlerFunc(h.federation),
|
2017-07-25 17:47:45 -07:00
|
|
|
})))
|
2015-09-17 07:33:14 -07:00
|
|
|
|
2017-07-25 17:47:45 -07:00
|
|
|
router.Get("/consoles/*filepath", readyf(instrf("consoles", h.consoles)))
|
2015-06-02 23:38:50 -07:00
|
|
|
|
2017-09-18 03:32:17 -07:00
|
|
|
router.Get("/static/*filepath", instrf("static", h.serveStaticAsset))
|
2013-02-08 05:49:55 -08:00
|
|
|
|
2015-06-15 03:50:53 -07:00
|
|
|
if o.UserAssetsPath != "" {
|
2017-09-18 03:32:17 -07:00
|
|
|
router.Get("/user/*filepath", instrf("user", route.FileServe(o.UserAssetsPath)))
|
2013-05-23 06:47:00 -07:00
|
|
|
}
|
|
|
|
|
2017-07-10 06:44:29 -07:00
|
|
|
if o.EnableLifecycle {
|
2015-06-15 03:50:53 -07:00
|
|
|
router.Post("/-/quit", h.quit)
|
2017-07-10 06:44:29 -07:00
|
|
|
router.Post("/-/reload", h.reload)
|
|
|
|
} else {
|
2017-09-18 03:32:17 -07:00
|
|
|
router.Post("/-/quit", func(w http.ResponseWriter, _ *http.Request) {
|
2017-07-10 06:44:29 -07:00
|
|
|
w.WriteHeader(http.StatusForbidden)
|
|
|
|
w.Write([]byte("Lifecycle APIs are not enabled"))
|
2017-09-18 03:32:17 -07:00
|
|
|
})
|
|
|
|
router.Post("/-/reload", func(w http.ResponseWriter, _ *http.Request) {
|
2017-07-10 06:44:29 -07:00
|
|
|
w.WriteHeader(http.StatusForbidden)
|
|
|
|
w.Write([]byte("Lifecycle APIs are not enabled"))
|
2017-09-18 03:32:17 -07:00
|
|
|
})
|
2015-03-24 14:04:38 -07:00
|
|
|
}
|
2017-07-10 06:44:29 -07:00
|
|
|
router.Get("/-/quit", func(w http.ResponseWriter, _ *http.Request) {
|
|
|
|
w.WriteHeader(http.StatusMethodNotAllowed)
|
|
|
|
w.Write([]byte("Only POST requests allowed"))
|
|
|
|
})
|
|
|
|
router.Get("/-/reload", func(w http.ResponseWriter, _ *http.Request) {
|
2016-11-07 07:14:22 -08:00
|
|
|
w.WriteHeader(http.StatusMethodNotAllowed)
|
2017-07-10 06:44:29 -07:00
|
|
|
w.Write([]byte("Only POST requests allowed"))
|
2016-11-07 07:14:22 -08:00
|
|
|
})
|
2015-08-26 08:42:25 -07:00
|
|
|
|
2017-09-18 03:32:17 -07:00
|
|
|
router.Get("/debug/*subpath", serveDebug)
|
|
|
|
router.Post("/debug/*subpath", serveDebug)
|
2017-07-25 17:47:45 -07:00
|
|
|
|
|
|
|
router.Get("/-/healthy", func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
w.WriteHeader(http.StatusOK)
|
|
|
|
fmt.Fprintf(w, "Prometheus is Healthy.\n")
|
|
|
|
})
|
|
|
|
router.Get("/-/ready", readyf(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
w.WriteHeader(http.StatusOK)
|
|
|
|
fmt.Fprintf(w, "Prometheus is Ready.\n")
|
|
|
|
}))
|
2015-07-21 15:27:55 -07:00
|
|
|
|
2015-06-15 03:50:53 -07:00
|
|
|
return h
|
|
|
|
}
|
|
|
|
|
2017-07-06 05:38:40 -07:00
|
|
|
var corsHeaders = map[string]string{
|
|
|
|
"Access-Control-Allow-Headers": "Accept, Authorization, Content-Type, Origin",
|
|
|
|
"Access-Control-Allow-Methods": "GET, OPTIONS",
|
|
|
|
"Access-Control-Allow-Origin": "*",
|
|
|
|
"Access-Control-Expose-Headers": "Date",
|
|
|
|
}
|
|
|
|
|
|
|
|
// Enables cross-site script calls.
|
|
|
|
func setCORS(w http.ResponseWriter) {
|
|
|
|
for h, v := range corsHeaders {
|
|
|
|
w.Header().Set(h, v)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-22 16:00:56 -07:00
|
|
|
func serveDebug(w http.ResponseWriter, req *http.Request) {
|
|
|
|
ctx := req.Context()
|
|
|
|
subpath := route.Param(ctx, "subpath")
|
|
|
|
|
2017-09-01 04:12:51 -07:00
|
|
|
if subpath == "/pprof" {
|
|
|
|
http.Redirect(w, req, req.URL.Path+"/", http.StatusMovedPermanently)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-09-07 07:24:12 -07:00
|
|
|
if !strings.HasPrefix(subpath, "/pprof/") {
|
|
|
|
http.NotFound(w, req)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
subpath = strings.TrimPrefix(subpath, "/pprof/")
|
|
|
|
|
|
|
|
switch subpath {
|
|
|
|
case "cmdline":
|
2017-08-22 16:00:56 -07:00
|
|
|
pprof.Cmdline(w, req)
|
2017-09-07 07:24:12 -07:00
|
|
|
case "profile":
|
2017-08-22 16:00:56 -07:00
|
|
|
pprof.Profile(w, req)
|
2017-09-07 07:24:12 -07:00
|
|
|
case "symbol":
|
2017-08-22 16:00:56 -07:00
|
|
|
pprof.Symbol(w, req)
|
2017-09-07 07:24:12 -07:00
|
|
|
case "trace":
|
2017-08-22 16:00:56 -07:00
|
|
|
pprof.Trace(w, req)
|
2017-09-07 07:24:12 -07:00
|
|
|
default:
|
2017-09-01 04:12:51 -07:00
|
|
|
req.URL.Path = "/debug/pprof/" + subpath
|
2017-09-07 07:24:12 -07:00
|
|
|
pprof.Index(w, req)
|
2017-08-22 16:00:56 -07:00
|
|
|
}
|
|
|
|
}
|
2015-06-15 03:50:53 -07:00
|
|
|
|
2017-08-11 11:45:52 -07:00
|
|
|
func (h *Handler) serveStaticAsset(w http.ResponseWriter, req *http.Request) {
|
2017-05-02 16:49:29 -07:00
|
|
|
fp := route.Param(req.Context(), "filepath")
|
2015-11-11 07:31:09 -08:00
|
|
|
fp = filepath.Join("web/ui/static", fp)
|
|
|
|
|
|
|
|
info, err := ui.AssetInfo(fp)
|
|
|
|
if err != nil {
|
2017-08-11 11:45:52 -07:00
|
|
|
level.Warn(h.logger).Log("msg", "Could not get file info", "err", err, "file", fp)
|
2015-11-11 07:31:09 -08:00
|
|
|
w.WriteHeader(http.StatusNotFound)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
file, err := ui.Asset(fp)
|
|
|
|
if err != nil {
|
|
|
|
if err != io.EOF {
|
2017-08-11 11:45:52 -07:00
|
|
|
level.Warn(h.logger).Log("msg", "Could not get file", "err", err, "file", fp)
|
2015-11-11 07:31:09 -08:00
|
|
|
}
|
|
|
|
w.WriteHeader(http.StatusNotFound)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
http.ServeContent(w, req, info.Name(), info.ModTime(), bytes.NewReader(file))
|
|
|
|
}
|
|
|
|
|
2017-07-25 17:47:45 -07:00
|
|
|
// Ready sets Handler to be ready.
|
|
|
|
func (h *Handler) Ready() {
|
|
|
|
atomic.StoreUint32(&h.ready, 1)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Verifies whether the server is ready or not.
|
|
|
|
func (h *Handler) isReady() bool {
|
|
|
|
ready := atomic.LoadUint32(&h.ready)
|
2018-02-25 23:58:10 -08:00
|
|
|
return ready > 0
|
2017-07-25 17:47:45 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Checks if server is ready, calls f if it is, returns 503 if it is not.
|
|
|
|
func (h *Handler) testReady(f http.HandlerFunc) http.HandlerFunc {
|
|
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
if h.isReady() {
|
|
|
|
f(w, r)
|
|
|
|
} else {
|
|
|
|
w.WriteHeader(http.StatusServiceUnavailable)
|
|
|
|
fmt.Fprintf(w, "Service Unavailable")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-04 22:38:07 -07:00
|
|
|
// Checks if server is ready, calls f if it is, returns 503 if it is not.
|
|
|
|
func (h *Handler) testReadyHandler(f http.Handler) http.HandlerFunc {
|
|
|
|
return h.testReady(f.ServeHTTP)
|
|
|
|
}
|
|
|
|
|
2015-06-15 03:50:53 -07:00
|
|
|
// Quit returns the receive-only quit channel.
|
|
|
|
func (h *Handler) Quit() <-chan struct{} {
|
|
|
|
return h.quitCh
|
2015-06-02 04:07:46 -07:00
|
|
|
}
|
|
|
|
|
2015-08-20 09:23:57 -07:00
|
|
|
// Reload returns the receive-only channel that signals configuration reload requests.
|
2016-07-11 07:24:54 -07:00
|
|
|
func (h *Handler) Reload() <-chan chan error {
|
2015-08-11 00:08:17 -07:00
|
|
|
return h.reloadCh
|
|
|
|
}
|
|
|
|
|
2015-06-02 04:07:46 -07:00
|
|
|
// Run serves the HTTP endpoints.
|
2017-07-06 05:38:40 -07:00
|
|
|
func (h *Handler) Run(ctx context.Context) error {
|
2017-08-11 11:45:52 -07:00
|
|
|
level.Info(h.logger).Log("msg", "Start listening for connections", "address", h.options.ListenAddress)
|
2017-07-06 05:38:40 -07:00
|
|
|
|
2017-10-06 03:22:19 -07:00
|
|
|
listener, err := net.Listen("tcp", h.options.ListenAddress)
|
2017-07-06 05:38:40 -07:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-10-06 03:22:19 -07:00
|
|
|
listener = netutil.LimitListener(listener, h.options.MaxConnections)
|
|
|
|
|
|
|
|
// Monitor incoming connections with conntrack.
|
|
|
|
listener = conntrack.NewListener(listener,
|
|
|
|
conntrack.TrackWithName("http"),
|
|
|
|
conntrack.TrackWithTracing())
|
2017-07-06 05:38:40 -07:00
|
|
|
|
|
|
|
var (
|
2017-10-06 03:22:19 -07:00
|
|
|
m = cmux.New(listener)
|
2017-07-06 05:38:40 -07:00
|
|
|
grpcl = m.Match(cmux.HTTP2HeaderField("content-type", "application/grpc"))
|
|
|
|
httpl = m.Match(cmux.HTTP1Fast())
|
|
|
|
grpcSrv = grpc.NewServer()
|
|
|
|
)
|
2017-07-10 06:44:29 -07:00
|
|
|
av2 := api_v2.New(
|
2017-07-06 05:38:40 -07:00
|
|
|
time.Now,
|
2017-09-18 03:32:17 -07:00
|
|
|
h.options.TSDB,
|
2017-07-06 05:38:40 -07:00
|
|
|
h.options.QueryEngine,
|
2017-09-18 03:32:17 -07:00
|
|
|
h.options.Storage.Querier,
|
2018-02-01 01:55:07 -08:00
|
|
|
func() []*scrape.Target {
|
2017-11-25 05:13:54 -08:00
|
|
|
return h.options.ScrapeManager.Targets()
|
2017-07-06 05:38:40 -07:00
|
|
|
},
|
|
|
|
func() []*url.URL {
|
|
|
|
return h.options.Notifier.Alertmanagers()
|
|
|
|
},
|
2017-07-10 00:29:41 -07:00
|
|
|
h.options.EnableAdminAPI,
|
2017-07-06 05:38:40 -07:00
|
|
|
)
|
|
|
|
av2.RegisterGRPC(grpcSrv)
|
|
|
|
|
2017-12-11 05:39:58 -08:00
|
|
|
hh, err := av2.HTTPHandler(h.options.ListenAddress)
|
2017-07-06 05:38:40 -07:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-10-04 22:38:07 -07:00
|
|
|
hhFunc := h.testReadyHandler(hh)
|
|
|
|
|
2017-05-02 16:49:29 -07:00
|
|
|
operationName := nethttp.OperationNameFunc(func(r *http.Request) string {
|
|
|
|
return fmt.Sprintf("%s %s", r.Method, r.URL.Path)
|
|
|
|
})
|
2017-07-06 05:38:40 -07:00
|
|
|
mux := http.NewServeMux()
|
|
|
|
mux.Handle("/", h.router)
|
|
|
|
|
|
|
|
av1 := route.New()
|
|
|
|
h.apiV1.Register(av1)
|
2017-09-06 05:41:07 -07:00
|
|
|
apiPath := "/api"
|
|
|
|
if h.options.RoutePrefix != "/" {
|
|
|
|
apiPath = h.options.RoutePrefix + apiPath
|
|
|
|
level.Info(h.logger).Log("msg", "router prefix", "prefix", h.options.RoutePrefix)
|
|
|
|
}
|
|
|
|
|
|
|
|
mux.Handle(apiPath+"/v1/", http.StripPrefix(apiPath+"/v1", av1))
|
2017-07-06 05:38:40 -07:00
|
|
|
|
2017-10-04 04:32:32 -07:00
|
|
|
mux.Handle(apiPath+"/", http.StripPrefix(apiPath,
|
2017-07-06 05:38:40 -07:00
|
|
|
http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
setCORS(w)
|
2017-10-04 22:38:07 -07:00
|
|
|
hhFunc(w, r)
|
2017-07-06 05:38:40 -07:00
|
|
|
}),
|
|
|
|
))
|
|
|
|
|
2017-08-11 11:45:52 -07:00
|
|
|
errlog := stdlog.New(log.NewStdlibAdapter(level.Error(h.logger)), "", 0)
|
|
|
|
|
2017-07-06 05:38:40 -07:00
|
|
|
httpSrv := &http.Server{
|
|
|
|
Handler: nethttp.Middleware(opentracing.GlobalTracer(), mux, operationName),
|
2017-08-11 11:45:52 -07:00
|
|
|
ErrorLog: errlog,
|
2016-12-01 05:29:45 -08:00
|
|
|
ReadTimeout: h.options.ReadTimeout,
|
2016-05-25 04:13:22 -07:00
|
|
|
}
|
2017-07-06 05:38:40 -07:00
|
|
|
|
2017-08-10 05:48:31 -07:00
|
|
|
go func() {
|
|
|
|
if err := httpSrv.Serve(httpl); err != nil {
|
2017-08-11 11:45:52 -07:00
|
|
|
level.Warn(h.logger).Log("msg", "error serving HTTP", "err", err)
|
2017-08-10 05:48:31 -07:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
go func() {
|
|
|
|
if err := grpcSrv.Serve(grpcl); err != nil {
|
2017-08-11 11:45:52 -07:00
|
|
|
level.Warn(h.logger).Log("msg", "error serving gRPC", "err", err)
|
2017-08-10 05:48:31 -07:00
|
|
|
}
|
|
|
|
}()
|
2017-07-06 05:38:40 -07:00
|
|
|
|
2017-11-11 04:06:13 -08:00
|
|
|
errCh := make(chan error)
|
|
|
|
go func() {
|
|
|
|
errCh <- m.Serve()
|
|
|
|
}()
|
|
|
|
|
|
|
|
select {
|
|
|
|
case e := <-errCh:
|
|
|
|
return e
|
|
|
|
case <-ctx.Done():
|
|
|
|
httpSrv.Shutdown(ctx)
|
|
|
|
grpcSrv.GracefulStop()
|
|
|
|
return nil
|
|
|
|
}
|
2013-02-08 05:49:55 -08:00
|
|
|
}
|
2013-03-27 09:40:01 -07:00
|
|
|
|
2015-06-15 03:50:53 -07:00
|
|
|
func (h *Handler) alerts(w http.ResponseWriter, r *http.Request) {
|
|
|
|
alerts := h.ruleManager.AlertingRules()
|
2016-12-27 05:13:33 -08:00
|
|
|
alertsSorter := byAlertStateAndNameSorter{alerts: alerts}
|
2015-06-15 03:50:53 -07:00
|
|
|
sort.Sort(alertsSorter)
|
|
|
|
|
|
|
|
alertStatus := AlertStatus{
|
|
|
|
AlertingRules: alertsSorter.alerts,
|
|
|
|
AlertStateToRowClass: map[rules.AlertState]string{
|
|
|
|
rules.StateInactive: "success",
|
|
|
|
rules.StatePending: "warning",
|
|
|
|
rules.StateFiring: "danger",
|
|
|
|
},
|
|
|
|
}
|
2015-11-11 07:31:09 -08:00
|
|
|
h.executeTemplate(w, "alerts.html", alertStatus)
|
2015-06-15 03:50:53 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
func (h *Handler) consoles(w http.ResponseWriter, r *http.Request) {
|
2017-05-02 16:49:29 -07:00
|
|
|
ctx := r.Context()
|
2015-06-15 03:50:53 -07:00
|
|
|
name := route.Param(ctx, "filepath")
|
|
|
|
|
|
|
|
file, err := http.Dir(h.options.ConsoleTemplatesPath).Open(name)
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, err.Error(), http.StatusNotFound)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
text, err := ioutil.ReadAll(file)
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Provide URL parameters as a map for easy use. Advanced users may have need for
|
|
|
|
// parameters beyond the first, so provide RawParams.
|
|
|
|
rawParams, err := url.ParseQuery(r.URL.RawQuery)
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, err.Error(), http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
params := map[string]string{}
|
|
|
|
for k, v := range rawParams {
|
|
|
|
params[k] = v[0]
|
|
|
|
}
|
|
|
|
data := struct {
|
|
|
|
RawParams url.Values
|
|
|
|
Params map[string]string
|
|
|
|
Path string
|
|
|
|
}{
|
|
|
|
RawParams: rawParams,
|
|
|
|
Params: params,
|
2015-07-08 07:14:57 -07:00
|
|
|
Path: strings.TrimLeft(name, "/"),
|
2015-06-15 03:50:53 -07:00
|
|
|
}
|
|
|
|
|
2017-11-23 04:04:54 -08:00
|
|
|
tmpl := template.NewTemplateExpander(
|
|
|
|
h.context,
|
|
|
|
string(text),
|
|
|
|
"__console_"+name,
|
|
|
|
data,
|
|
|
|
h.now(),
|
2018-01-09 08:44:23 -08:00
|
|
|
template.QueryFunc(rules.EngineQueryFunc(h.queryEngine, h.storage)),
|
2017-11-23 04:04:54 -08:00
|
|
|
h.options.ExternalURL,
|
|
|
|
)
|
2015-06-15 03:50:53 -07:00
|
|
|
filenames, err := filepath.Glob(h.options.ConsoleLibrariesPath + "/*.lib")
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
result, err := tmpl.ExpandHTML(filenames)
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
io.WriteString(w, result)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *Handler) graph(w http.ResponseWriter, r *http.Request) {
|
2015-11-11 07:31:09 -08:00
|
|
|
h.executeTemplate(w, "graph.html", nil)
|
2015-06-15 03:50:53 -07:00
|
|
|
}
|
|
|
|
|
2015-06-16 05:57:30 -07:00
|
|
|
func (h *Handler) status(w http.ResponseWriter, r *http.Request) {
|
2015-11-11 07:31:09 -08:00
|
|
|
h.executeTemplate(w, "status.html", struct {
|
2018-03-07 07:14:46 -08:00
|
|
|
Birth time.Time
|
|
|
|
CWD string
|
|
|
|
Version *PrometheusVersion
|
|
|
|
Alertmanagers []*url.URL
|
|
|
|
GoroutineCount int
|
|
|
|
GOMAXPROCS int
|
|
|
|
GOGC string
|
2015-06-15 03:23:02 -07:00
|
|
|
}{
|
2018-03-07 07:14:46 -08:00
|
|
|
Birth: h.birth,
|
|
|
|
CWD: h.cwd,
|
|
|
|
Version: h.versionInfo,
|
|
|
|
Alertmanagers: h.notifier.Alertmanagers(),
|
|
|
|
GoroutineCount: runtime.NumGoroutine(),
|
|
|
|
GOMAXPROCS: runtime.GOMAXPROCS(0),
|
|
|
|
GOGC: os.Getenv("GOGC"),
|
2015-06-15 03:23:02 -07:00
|
|
|
})
|
2015-06-15 03:50:53 -07:00
|
|
|
}
|
2014-04-14 16:02:15 -07:00
|
|
|
|
2016-05-13 08:59:59 -07:00
|
|
|
func (h *Handler) flags(w http.ResponseWriter, r *http.Request) {
|
|
|
|
h.executeTemplate(w, "flags.html", h.flagsMap)
|
|
|
|
}
|
|
|
|
|
2017-05-11 08:09:24 -07:00
|
|
|
func (h *Handler) serveConfig(w http.ResponseWriter, r *http.Request) {
|
2016-05-13 08:59:59 -07:00
|
|
|
h.mtx.RLock()
|
|
|
|
defer h.mtx.RUnlock()
|
|
|
|
|
2017-05-11 08:09:24 -07:00
|
|
|
h.executeTemplate(w, "config.html", h.config.String())
|
2016-05-13 08:59:59 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
func (h *Handler) rules(w http.ResponseWriter, r *http.Request) {
|
|
|
|
h.executeTemplate(w, "rules.html", h.ruleManager)
|
|
|
|
}
|
|
|
|
|
2017-12-04 07:12:28 -08:00
|
|
|
func (h *Handler) serviceDiscovery(w http.ResponseWriter, r *http.Request) {
|
|
|
|
var index []string
|
2017-12-18 11:41:31 -08:00
|
|
|
targets := h.scrapeManager.TargetMap()
|
2017-12-04 07:12:28 -08:00
|
|
|
for job := range targets {
|
|
|
|
index = append(index, job)
|
|
|
|
}
|
|
|
|
sort.Strings(index)
|
|
|
|
scrapeConfigData := struct {
|
|
|
|
Index []string
|
2018-02-01 01:55:07 -08:00
|
|
|
Targets map[string][]*scrape.Target
|
2017-12-04 07:12:28 -08:00
|
|
|
}{
|
|
|
|
Index: index,
|
|
|
|
Targets: targets,
|
|
|
|
}
|
|
|
|
h.executeTemplate(w, "service-discovery.html", scrapeConfigData)
|
|
|
|
}
|
|
|
|
|
2016-05-13 08:59:59 -07:00
|
|
|
func (h *Handler) targets(w http.ResponseWriter, r *http.Request) {
|
2016-12-02 04:28:58 -08:00
|
|
|
// Bucket targets by job label
|
2018-02-01 01:55:07 -08:00
|
|
|
tps := map[string][]*scrape.Target{}
|
2017-11-25 05:13:54 -08:00
|
|
|
for _, t := range h.scrapeManager.Targets() {
|
2016-12-29 00:27:30 -08:00
|
|
|
job := t.Labels().Get(model.JobLabel)
|
2016-12-02 04:28:58 -08:00
|
|
|
tps[job] = append(tps[job], t)
|
|
|
|
}
|
|
|
|
|
2017-03-30 13:45:18 -07:00
|
|
|
for _, targets := range tps {
|
|
|
|
sort.Slice(targets, func(i, j int) bool {
|
2017-04-04 05:44:39 -07:00
|
|
|
return targets[i].Labels().Get(labels.InstanceName) < targets[j].Labels().Get(labels.InstanceName)
|
2017-03-30 13:45:18 -07:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2016-11-23 09:23:09 -08:00
|
|
|
h.executeTemplate(w, "targets.html", struct {
|
2018-02-01 01:55:07 -08:00
|
|
|
TargetPools map[string][]*scrape.Target
|
2016-11-23 09:23:09 -08:00
|
|
|
}{
|
2016-12-02 04:28:58 -08:00
|
|
|
TargetPools: tps,
|
2016-11-23 09:23:09 -08:00
|
|
|
})
|
2016-05-13 08:59:59 -07:00
|
|
|
}
|
|
|
|
|
2015-06-16 05:57:30 -07:00
|
|
|
func (h *Handler) version(w http.ResponseWriter, r *http.Request) {
|
|
|
|
dec := json.NewEncoder(w)
|
2016-05-05 04:46:51 -07:00
|
|
|
if err := dec.Encode(h.versionInfo); err != nil {
|
2015-06-16 05:57:30 -07:00
|
|
|
http.Error(w, fmt.Sprintf("error encoding JSON: %s", err), http.StatusInternalServerError)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-15 03:50:53 -07:00
|
|
|
func (h *Handler) quit(w http.ResponseWriter, r *http.Request) {
|
|
|
|
fmt.Fprintf(w, "Requesting termination... Goodbye!")
|
|
|
|
close(h.quitCh)
|
2014-04-14 16:02:15 -07:00
|
|
|
}
|
|
|
|
|
2015-08-11 00:08:17 -07:00
|
|
|
func (h *Handler) reload(w http.ResponseWriter, r *http.Request) {
|
2016-07-11 07:24:54 -07:00
|
|
|
rc := make(chan error)
|
|
|
|
h.reloadCh <- rc
|
|
|
|
if err := <-rc; err != nil {
|
|
|
|
http.Error(w, fmt.Sprintf("failed to reload config: %s", err), http.StatusInternalServerError)
|
|
|
|
}
|
2015-08-11 00:08:17 -07:00
|
|
|
}
|
|
|
|
|
2015-06-30 05:38:01 -07:00
|
|
|
func (h *Handler) consolesPath() string {
|
2015-06-15 03:50:53 -07:00
|
|
|
if _, err := os.Stat(h.options.ConsoleTemplatesPath + "/index.html"); !os.IsNotExist(err) {
|
2015-06-30 05:38:01 -07:00
|
|
|
return h.options.ExternalURL.Path + "/consoles/index.html"
|
2014-10-02 07:44:47 -07:00
|
|
|
}
|
2015-06-15 03:50:53 -07:00
|
|
|
if h.options.UserAssetsPath != "" {
|
|
|
|
if _, err := os.Stat(h.options.UserAssetsPath + "/index.html"); !os.IsNotExist(err) {
|
2015-06-30 05:38:01 -07:00
|
|
|
return h.options.ExternalURL.Path + "/user/index.html"
|
2014-10-02 07:44:47 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
2015-06-30 05:38:01 -07:00
|
|
|
func tmplFuncs(consolesPath string, opts *Options) template_text.FuncMap {
|
|
|
|
return template_text.FuncMap{
|
2016-06-08 11:23:54 -07:00
|
|
|
"since": func(t time.Time) time.Duration {
|
|
|
|
return time.Since(t) / time.Millisecond * time.Millisecond
|
|
|
|
},
|
2015-06-30 05:38:01 -07:00
|
|
|
"consolesPath": func() string { return consolesPath },
|
|
|
|
"pathPrefix": func() string { return opts.ExternalURL.Path },
|
2017-03-18 13:38:35 -07:00
|
|
|
"buildVersion": func() string { return opts.Version.Revision },
|
2017-03-20 06:07:41 -07:00
|
|
|
"stripLabels": func(lset map[string]string, labels ...string) map[string]string {
|
2015-05-18 03:16:25 -07:00
|
|
|
for _, ln := range labels {
|
|
|
|
delete(lset, ln)
|
|
|
|
}
|
|
|
|
return lset
|
|
|
|
},
|
2015-06-22 13:57:32 -07:00
|
|
|
"globalURL": func(u *url.URL) *url.URL {
|
2015-06-30 05:38:01 -07:00
|
|
|
host, port, err := net.SplitHostPort(u.Host)
|
|
|
|
if err != nil {
|
|
|
|
return u
|
|
|
|
}
|
2015-06-22 13:57:32 -07:00
|
|
|
for _, lhr := range localhostRepresentations {
|
2015-06-30 05:38:01 -07:00
|
|
|
if host == lhr {
|
|
|
|
_, ownPort, err := net.SplitHostPort(opts.ListenAddress)
|
|
|
|
if err != nil {
|
|
|
|
return u
|
|
|
|
}
|
|
|
|
|
|
|
|
if port == ownPort {
|
|
|
|
// Only in the case where the target is on localhost and its port is
|
|
|
|
// the same as the one we're listening on, we know for sure that
|
|
|
|
// we're monitoring our own process and that we need to change the
|
|
|
|
// scheme, hostname, and port to the externally reachable ones as
|
|
|
|
// well. We shouldn't need to touch the path at all, since if a
|
|
|
|
// path prefix is defined, the path under which we scrape ourselves
|
|
|
|
// should already contain the prefix.
|
|
|
|
u.Scheme = opts.ExternalURL.Scheme
|
|
|
|
u.Host = opts.ExternalURL.Host
|
|
|
|
} else {
|
|
|
|
// Otherwise, we only know that localhost is not reachable
|
|
|
|
// externally, so we replace only the hostname by the one in the
|
|
|
|
// external URL. It could be the wrong hostname for the service on
|
|
|
|
// this port, but it's still the best possible guess.
|
|
|
|
host, _, err := net.SplitHostPort(opts.ExternalURL.Host)
|
|
|
|
if err != nil {
|
|
|
|
return u
|
|
|
|
}
|
|
|
|
u.Host = host + ":" + port
|
|
|
|
}
|
|
|
|
break
|
2015-06-22 13:57:32 -07:00
|
|
|
}
|
2015-05-18 03:16:25 -07:00
|
|
|
}
|
2015-06-22 13:57:32 -07:00
|
|
|
return u
|
2015-05-18 03:16:25 -07:00
|
|
|
},
|
2018-02-01 01:55:07 -08:00
|
|
|
"numHealthy": func(pool []*scrape.Target) int {
|
2017-08-09 08:10:30 -07:00
|
|
|
alive := len(pool)
|
|
|
|
for _, p := range pool {
|
2018-02-01 01:55:07 -08:00
|
|
|
if p.Health() != scrape.HealthGood {
|
2017-08-09 08:10:30 -07:00
|
|
|
alive--
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return alive
|
|
|
|
},
|
2018-02-01 01:55:07 -08:00
|
|
|
"healthToClass": func(th scrape.TargetHealth) string {
|
2015-06-15 03:50:53 -07:00
|
|
|
switch th {
|
2018-02-01 01:55:07 -08:00
|
|
|
case scrape.HealthUnknown:
|
2015-06-15 03:50:53 -07:00
|
|
|
return "warning"
|
2018-02-01 01:55:07 -08:00
|
|
|
case scrape.HealthGood:
|
2015-06-15 03:50:53 -07:00
|
|
|
return "success"
|
|
|
|
default:
|
|
|
|
return "danger"
|
|
|
|
}
|
|
|
|
},
|
2015-06-23 08:46:50 -07:00
|
|
|
"alertStateToClass": func(as rules.AlertState) string {
|
|
|
|
switch as {
|
|
|
|
case rules.StateInactive:
|
|
|
|
return "success"
|
|
|
|
case rules.StatePending:
|
|
|
|
return "warning"
|
|
|
|
case rules.StateFiring:
|
|
|
|
return "danger"
|
|
|
|
default:
|
|
|
|
panic("unknown alert state")
|
|
|
|
}
|
|
|
|
},
|
2015-06-30 05:38:01 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-11 07:31:09 -08:00
|
|
|
func (h *Handler) getTemplate(name string) (string, error) {
|
|
|
|
baseTmpl, err := ui.Asset("web/ui/templates/_base.html")
|
|
|
|
if err != nil {
|
|
|
|
return "", fmt.Errorf("error reading base template: %s", err)
|
|
|
|
}
|
|
|
|
pageTmpl, err := ui.Asset(filepath.Join("web/ui/templates", name))
|
|
|
|
if err != nil {
|
|
|
|
return "", fmt.Errorf("error reading page template %s: %s", name, err)
|
|
|
|
}
|
|
|
|
return string(baseTmpl) + string(pageTmpl), nil
|
|
|
|
}
|
|
|
|
|
2015-06-30 05:38:01 -07:00
|
|
|
func (h *Handler) executeTemplate(w http.ResponseWriter, name string, data interface{}) {
|
|
|
|
text, err := h.getTemplate(name)
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
}
|
|
|
|
|
2017-11-23 04:04:54 -08:00
|
|
|
tmpl := template.NewTemplateExpander(
|
|
|
|
h.context,
|
|
|
|
text,
|
|
|
|
name,
|
|
|
|
data,
|
|
|
|
h.now(),
|
2018-01-09 08:44:23 -08:00
|
|
|
template.QueryFunc(rules.EngineQueryFunc(h.queryEngine, h.storage)),
|
2017-11-23 04:04:54 -08:00
|
|
|
h.options.ExternalURL,
|
|
|
|
)
|
2015-06-30 05:38:01 -07:00
|
|
|
tmpl.Funcs(tmplFuncs(h.consolesPath(), h.options))
|
2015-05-18 03:16:25 -07:00
|
|
|
|
2015-06-23 08:46:50 -07:00
|
|
|
result, err := tmpl.ExpandHTML(nil)
|
2013-03-27 09:40:01 -07:00
|
|
|
if err != nil {
|
2015-06-23 08:46:50 -07:00
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
2013-03-27 09:40:01 -07:00
|
|
|
return
|
|
|
|
}
|
2015-06-23 08:46:50 -07:00
|
|
|
io.WriteString(w, result)
|
2013-03-27 09:40:01 -07:00
|
|
|
}
|
2013-08-09 09:09:44 -07:00
|
|
|
|
2017-08-11 11:45:52 -07:00
|
|
|
func (h *Handler) dumpHeap(w http.ResponseWriter, r *http.Request) {
|
2013-08-29 06:15:22 -07:00
|
|
|
target := fmt.Sprintf("/tmp/%d.heap", time.Now().Unix())
|
|
|
|
f, err := os.Create(target)
|
|
|
|
if err != nil {
|
2017-08-11 11:45:52 -07:00
|
|
|
level.Error(h.logger).Log("msg", "Could not dump heap", "err", err)
|
2013-08-29 06:15:22 -07:00
|
|
|
}
|
|
|
|
fmt.Fprintf(w, "Writing to %s...", target)
|
|
|
|
defer f.Close()
|
|
|
|
pprof_runtime.WriteHeapProfile(f)
|
|
|
|
fmt.Fprintf(w, "Done")
|
|
|
|
}
|
|
|
|
|
2015-06-15 03:50:53 -07:00
|
|
|
// AlertStatus bundles alerting rules and the mapping of alert states to row classes.
|
|
|
|
type AlertStatus struct {
|
|
|
|
AlertingRules []*rules.AlertingRule
|
|
|
|
AlertStateToRowClass map[rules.AlertState]string
|
2013-08-09 09:09:44 -07:00
|
|
|
}
|
2015-05-22 03:29:33 -07:00
|
|
|
|
2016-12-27 05:13:33 -08:00
|
|
|
type byAlertStateAndNameSorter struct {
|
2015-06-15 03:50:53 -07:00
|
|
|
alerts []*rules.AlertingRule
|
|
|
|
}
|
|
|
|
|
2016-12-27 05:13:33 -08:00
|
|
|
func (s byAlertStateAndNameSorter) Len() int {
|
2015-06-15 03:50:53 -07:00
|
|
|
return len(s.alerts)
|
|
|
|
}
|
|
|
|
|
2016-12-27 05:13:33 -08:00
|
|
|
func (s byAlertStateAndNameSorter) Less(i, j int) bool {
|
|
|
|
return s.alerts[i].State() > s.alerts[j].State() ||
|
|
|
|
(s.alerts[i].State() == s.alerts[j].State() &&
|
|
|
|
s.alerts[i].Name() < s.alerts[j].Name())
|
2015-06-15 03:50:53 -07:00
|
|
|
}
|
|
|
|
|
2016-12-27 05:13:33 -08:00
|
|
|
func (s byAlertStateAndNameSorter) Swap(i, j int) {
|
2015-06-15 03:50:53 -07:00
|
|
|
s.alerts[i], s.alerts[j] = s.alerts[j], s.alerts[i]
|
2015-05-22 03:29:33 -07:00
|
|
|
}
|