Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Prometheus with initial metrics #452

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 15 additions & 3 deletions api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import (
"github.com/hound-search/hound/config"
"github.com/hound-search/hound/index"
"github.com/hound-search/hound/searcher"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
)

const (
Expand Down Expand Up @@ -89,7 +91,7 @@ func searchAll(
*filesOpened += r.res.FilesOpened
}

*duration = int(time.Now().Sub(startedAt).Seconds() * 1000) //nolint
*duration = int(time.Now().Sub(startedAt).Seconds() * 1000) //nolint

return res, nil
}
Expand Down Expand Up @@ -159,6 +161,13 @@ func parseRangeValue(rv string) (int, int) {
return b, e
}

var (
searchDuration = promauto.NewHistogram(prometheus.HistogramOpts{
Name: "search_duration_seconds",
Help: "Histogram for the runtime of the search endpoint",
})
)

func Setup(m *http.ServeMux, idx map[string]*searcher.Searcher) {

m.HandleFunc("/api/v1/repos", func(w http.ResponseWriter, r *http.Request) {
Expand All @@ -171,6 +180,9 @@ func Setup(m *http.ServeMux, idx map[string]*searcher.Searcher) {
})

m.HandleFunc("/api/v1/search", func(w http.ResponseWriter, r *http.Request) {
timer := prometheus.NewTimer(searchDuration)
defer timer.ObserveDuration()

var opt index.SearchOptions

stats := parseAsBool(r.FormValue("stats"))
Expand Down Expand Up @@ -262,7 +274,7 @@ func Setup(m *http.ServeMux, idx map[string]*searcher.Searcher) {

type Webhook struct {
Repository struct {
Name string
Name string
Full_name string
}
}
Expand All @@ -272,7 +284,7 @@ func Setup(m *http.ServeMux, idx map[string]*searcher.Searcher) {
err := json.NewDecoder(r.Body).Decode(&h)

if err != nil {
writeError(w,
writeError(w,
errors.New(http.StatusText(http.StatusBadRequest)),
http.StatusBadRequest)
return
Expand Down
7 changes: 7 additions & 0 deletions cmds/houndd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"github.com/hound-search/hound/searcher"
"github.com/hound-search/hound/ui"
"github.com/hound-search/hound/web"
"github.com/prometheus/client_golang/prometheus/promhttp"
)

const gracefulShutdownSignal = syscall.SIGTERM
Expand Down Expand Up @@ -129,6 +130,12 @@ func main() {
info_log = log.New(os.Stdout, "", log.LstdFlags)
error_log = log.New(os.Stderr, "", log.LstdFlags)

// prometheus metrics endpoint
http.Handle("/metrics", promhttp.Handler())
go func() {
http.ListenAndServe(":2112", nil)
}()

flagConf := flag.String("conf", "config.json", "")
flagAddr := flag.String("addr", ":6080", "")
flagDev := flag.Bool("dev", false, "")
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ go 1.13
require (
github.com/blang/semver v3.5.1+incompatible
github.com/go-bindata/go-bindata v3.1.2+incompatible // indirect
github.com/prometheus/client_golang v1.14.0 // indirect
)
Loading