Skip to content

Commit

Permalink
*: add victoriametrics API
Browse files Browse the repository at this point in the history
Signed-off-by: Shuning Chen <[email protected]>
  • Loading branch information
Shuning Chen authored and Shuning Chen committed Jun 20, 2024
1 parent 1f1d885 commit d3f9e41
Showing 1 changed file with 44 additions and 1 deletion.
45 changes: 44 additions & 1 deletion service/http/http.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
package http

import (
"fmt"
"net"
"net/http"
"os"
"path"
"time"

"github.com/VictoriaMetrics/VictoriaMetrics/app/vminsert"
"github.com/VictoriaMetrics/VictoriaMetrics/app/vmselect"
"github.com/VictoriaMetrics/VictoriaMetrics/app/vmstorage"
"github.com/VictoriaMetrics/VictoriaMetrics/lib/httpserver"
conprofhttp "github.com/pingcap/ng-monitoring/component/conprof/http"
"github.com/pingcap/ng-monitoring/component/topsql"
"github.com/pingcap/ng-monitoring/config"
Expand Down Expand Up @@ -63,15 +68,53 @@ func ServeHTTP(l *config.Log, listener net.Listener) {
promHandler.ServeHTTP(c.Writer, c.Request)
})

wh := &wrapHeander{ngHanlder: ng}
httpServer = &http.Server{
Handler: ng,
Handler: wh,
ReadHeaderTimeout: 5 * time.Second,
}
if err = httpServer.Serve(listener); err != nil && err != http.ErrServerClosed {
log.Warn("failed to serve http service", zap.Error(err))
}
}

type wrapHeander struct {
ngHanlder http.Handler
}

func (wrap *wrapHeander) ServeHTTP(w http.ResponseWriter, r *http.Request) {
if r.URL.Path == "/help" {
if r.Method != "GET" {
return
}
w.Header().Add("Content-Type", "text/html; charset=utf-8")
fmt.Fprintf(w, "<h2>Single-node VictoriaMetrics</h2></br>")
fmt.Fprintf(w, "See docs at <a href='https://docs.victoriametrics.com/'>https://docs.victoriametrics.com/</a></br>")
fmt.Fprintf(w, "Useful endpoints:</br>")
httpserver.WriteAPIHelp(w, [][2]string{
{"vmui", "Web UI"},
{"targets", "discovered targets list"},
{"api/v1/targets", "advanced information about discovered targets in JSON format"},
{"config", "-promscrape.config contents"},
{"metrics", "available service metrics"},
{"flags", "command-line flags"},
{"api/v1/status/tsdb", "tsdb status page"},
{"api/v1/status/top_queries", "top queries"},
{"api/v1/status/active_queries", "active queries"},
})
}
if vminsert.RequestHandler(w, r) {
return
}
if vmselect.RequestHandler(w, r) {
return
}
if vmstorage.RequestHandler(w, r) {
return
}
wrap.ngHanlder.ServeHTTP(w, r)
}

type Status struct {
Health bool `json:"health"`
}
Expand Down

0 comments on commit d3f9e41

Please sign in to comment.