Browse Source

metrics: add initial Prometheus support (#4141)

pull/5380/merge
Unknwon 6 years ago
parent
commit
520530dfcf
No known key found for this signature in database
GPG Key ID: 25B575AE3213B2B3
  1. 19
      cmd/web.go
  2. 6
      conf/app.ini
  3. 2
      gogs.go
  4. 6
      pkg/bindata/bindata.go
  5. 18
      pkg/context/auth.go
  6. 32
      pkg/setting/setting.go
  7. 2
      templates/.VERSION

19
cmd/web.go

@ -25,6 +25,7 @@ import (
"github.com/go-macaron/session"
"github.com/go-macaron/toolbox"
"github.com/mcuadros/go-version"
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/urfave/cli"
log "gopkg.in/clog.v1"
"gopkg.in/macaron.v1"
@ -260,11 +261,11 @@ func runWeb(c *cli.Context) error {
})
// ***** END: User *****
adminReq := context.Toggle(&context.ToggleOptions{SignInRequired: true, AdminRequired: true})
reqAdmin := context.Toggle(&context.ToggleOptions{SignInRequired: true, AdminRequired: true})
// ***** START: Admin *****
m.Group("/admin", func() {
m.Get("", adminReq, admin.Dashboard)
m.Get("", admin.Dashboard)
m.Get("/config", admin.Config)
m.Post("/config/test_mail", admin.SendTestMail)
m.Get("/monitor", admin.Monitor)
@ -298,7 +299,7 @@ func runWeb(c *cli.Context) error {
m.Post("/delete", admin.DeleteNotices)
m.Get("/empty", admin.EmptyNotices)
})
}, adminReq)
}, reqAdmin)
// ***** END: Admin *****
m.Group("", func() {
@ -659,6 +660,18 @@ func runWeb(c *cli.Context) error {
apiv1.RegisterRoutes(m)
}, ignSignIn)
m.Group("/-", func() {
if setting.Prometheus.Enabled {
m.Get("/metrics", func(c *context.Context) {
if !setting.Prometheus.EnableBasicAuth {
return
}
c.RequireBasicAuth(setting.Prometheus.BasicAuthUsername, setting.Prometheus.BasicAuthPassword)
}, promhttp.Handler())
}
})
// robots.txt
m.Get("/robots.txt", func(c *context.Context) {
if setting.HasRobotsTxt {

6
conf/app.ini

@ -464,6 +464,12 @@ NEWS_FEED_PAGING_NUM = 20
; Number of commits that are showed in one page
COMMITS_PAGING_NUM = 30
[prometheus]
ENABLED = true
ENABLE_BASIC_AUTH = false
BASIC_AUTH_USERNAME =
BASIC_AUTH_PASSWORD =
[i18n]
LANGS = en-US,zh-CN,zh-HK,zh-TW,de-DE,fr-FR,nl-NL,lv-LV,ru-RU,ja-JP,es-ES,pt-BR,pl-PL,bg-BG,it-IT,fi-FI,tr-TR,cs-CZ,sr-SP,sv-SE,ko-KR,gl-ES,uk-UA,en-GB,hu-HU,sk-SK,id-ID,fa-IR,vi-VN
NAMES = English,简体中文,繁體中文(香港),繁體中文(臺灣),Deutsch,français,Nederlands,latviešu,русский,日本語,español,português do Brasil,polski,български,italiano,suomi,Türkçe,čeština,српски,svenska,한국어,galego,українська,English (United Kingdom),Magyar,Slovenčina,Indonesian,Persian,Vietnamese

2
gogs.go

@ -16,7 +16,7 @@ import (
"github.com/gogs/gogs/pkg/setting"
)
const APP_VER = "0.11.65.0914"
const APP_VER = "0.11.66.0914"
func init() {
setting.AppVer = APP_VER

6
pkg/bindata/bindata.go

File diff suppressed because one or more lines are too long

18
pkg/context/auth.go

@ -5,13 +5,16 @@
package context
import (
"net/http"
"net/url"
"strings"
"github.com/go-macaron/csrf"
"gopkg.in/macaron.v1"
"github.com/gogs/gogs/pkg/auth"
"github.com/gogs/gogs/pkg/setting"
"github.com/gogs/gogs/pkg/tool"
)
type ToggleOptions struct {
@ -92,3 +95,18 @@ func Toggle(options *ToggleOptions) macaron.Handler {
}
}
}
// RequireBasicAuth verifies HTTP Basic Authentication header with given credentials
func (c *Context) RequireBasicAuth(username, password string) {
fields := strings.Fields(c.Req.Header.Get("Authorization"))
if len(fields) != 2 || fields[0] != "Basic" {
c.Status(http.StatusUnauthorized)
return
}
uname, passwd, _ := tool.BasicAuthDecode(fields[1])
if uname != username || passwd != password {
c.Status(http.StatusForbidden)
return
}
}

32
pkg/setting/setting.go

@ -294,6 +294,14 @@ var (
} `ini:"ui.user"`
}
// Prometheus settings
Prometheus struct {
Enabled bool
EnableBasicAuth bool
BasicAuthUsername string
BasicAuthPassword string
}
// I18n settings
Langs []string
Names []string
@ -652,27 +660,29 @@ func NewContext() {
}
if err = Cfg.Section("http").MapTo(&HTTP); err != nil {
log.Fatal(2, "Fail to map HTTP settings: %v", err)
log.Fatal(2, "Failed to map HTTP settings: %v", err)
} else if err = Cfg.Section("webhook").MapTo(&Webhook); err != nil {
log.Fatal(2, "Fail to map Webhook settings: %v", err)
log.Fatal(2, "Failed to map Webhook settings: %v", err)
} else if err = Cfg.Section("release.attachment").MapTo(&Release.Attachment); err != nil {
log.Fatal(2, "Fail to map Release.Attachment settings: %v", err)
log.Fatal(2, "Failed to map Release.Attachment settings: %v", err)
} else if err = Cfg.Section("markdown").MapTo(&Markdown); err != nil {
log.Fatal(2, "Fail to map Markdown settings: %v", err)
log.Fatal(2, "Failed to map Markdown settings: %v", err)
} else if err = Cfg.Section("smartypants").MapTo(&Smartypants); err != nil {
log.Fatal(2, "Fail to map Smartypants settings: %v", err)
log.Fatal(2, "Failed to map Smartypants settings: %v", err)
} else if err = Cfg.Section("admin").MapTo(&Admin); err != nil {
log.Fatal(2, "Fail to map Admin settings: %v", err)
log.Fatal(2, "Failed to map Admin settings: %v", err)
} else if err = Cfg.Section("cron").MapTo(&Cron); err != nil {
log.Fatal(2, "Fail to map Cron settings: %v", err)
log.Fatal(2, "Failed to map Cron settings: %v", err)
} else if err = Cfg.Section("git").MapTo(&Git); err != nil {
log.Fatal(2, "Fail to map Git settings: %v", err)
log.Fatal(2, "Failed to map Git settings: %v", err)
} else if err = Cfg.Section("mirror").MapTo(&Mirror); err != nil {
log.Fatal(2, "Fail to map Mirror settings: %v", err)
log.Fatal(2, "Failed to map Mirror settings: %v", err)
} else if err = Cfg.Section("api").MapTo(&API); err != nil {
log.Fatal(2, "Fail to map API settings: %v", err)
log.Fatal(2, "Failed to map API settings: %v", err)
} else if err = Cfg.Section("ui").MapTo(&UI); err != nil {
log.Fatal(2, "Fail to map UI settings: %v", err)
log.Fatal(2, "Failed to map UI settings: %v", err)
} else if err = Cfg.Section("prometheus").MapTo(&Prometheus); err != nil {
log.Fatal(2, "Failed to map Prometheus settings: %v", err)
}
if Mirror.DefaultInterval <= 0 {

2
templates/.VERSION

@ -1 +1 @@
0.11.65.0914
0.11.66.0914

Loading…
Cancel
Save