serve

package
v0.2.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 24, 2021 License: Apache-2.0 Imports: 7 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var Command = &cli.Command{
	Name:  "serve",
	Usage: "Launch a http server that serves queries in prometheus format",
	Flags: []cli.Flag{
		&cli.StringFlag{
			Name:  "listen-addr",
			Usage: "server listen `HOST:ADDR`",
			Value: ":16480",
		},
	},
	Action: func(c *cli.Context) error {
		mux := http.NewServeMux()

		fetcher := fetch.NewCachedFetcher(fetch.Direct{})
		mux.Handle("/probe", exporter.Handler{Fetcher: fetcher})

		prometheus.MustRegister(prometheus.NewBuildInfoCollector())
		mux.Handle("/metrics", promhttp.Handler())

		mux.HandleFunc("/healthz", func(w http.ResponseWriter, req *http.Request) {
			w.Header().Set("content-type", "text/plain")
			if _, err := w.Write([]byte("ok\n")); err != nil {
				zap.S().Warnf("Failed to write ok: %v", err)
			}
		})

		mux.HandleFunc("/", func(w http.ResponseWriter, req *http.Request) {
			w.Header().Set("content-type", "text/html; charset=UTF-8")
			if _, err := w.Write(indexhtml); err != nil {
				zap.S().Warnf("Failed to write index.html: %v", err)

			}
		})

		listenAddr := c.String("listen-addr")
		zap.S().Infof("About to start listening on %s", listenAddr)
		if err := http.ListenAndServe(listenAddr, mux); err != nil {
			return err
		}

		return nil
	},
}

Functions

This section is empty.

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL