mirror of
https://github.com/go-gitea/gitea.git
synced 2025-07-14 08:54:42 +02:00
add login and webhook counters
This commit is contained in:
parent
9d7283ae3c
commit
f23be74c86
@ -35,6 +35,8 @@ import (
|
|||||||
user_service "code.gitea.io/gitea/services/user"
|
user_service "code.gitea.io/gitea/services/user"
|
||||||
|
|
||||||
"github.com/markbates/goth"
|
"github.com/markbates/goth"
|
||||||
|
"github.com/prometheus/client_golang/prometheus"
|
||||||
|
"github.com/prometheus/client_golang/prometheus/promauto"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
@ -180,6 +182,8 @@ func prepareSignInPageData(ctx *context.Context) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var loginCounter = promauto.NewCounterVec(prometheus.CounterOpts{Namespace: "gitea", Subsystem: "auth", Name: "login"}, []string{"status"}) // TODO: Add source/provider in the future?
|
||||||
|
|
||||||
// SignIn render sign in page
|
// SignIn render sign in page
|
||||||
func SignIn(ctx *context.Context) {
|
func SignIn(ctx *context.Context) {
|
||||||
if CheckAutoLogin(ctx) {
|
if CheckAutoLogin(ctx) {
|
||||||
@ -217,6 +221,7 @@ func SignInPost(ctx *context.Context) {
|
|||||||
|
|
||||||
u, source, err := auth_service.UserSignIn(ctx, form.UserName, form.Password)
|
u, source, err := auth_service.UserSignIn(ctx, form.UserName, form.Password)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
loginCounter.WithLabelValues("failure").Inc()
|
||||||
if errors.Is(err, util.ErrNotExist) || errors.Is(err, util.ErrInvalidArgument) {
|
if errors.Is(err, util.ErrNotExist) || errors.Is(err, util.ErrInvalidArgument) {
|
||||||
ctx.RenderWithErr(ctx.Tr("form.username_password_incorrect"), tplSignIn, &form)
|
ctx.RenderWithErr(ctx.Tr("form.username_password_incorrect"), tplSignIn, &form)
|
||||||
log.Warn("Failed authentication attempt for %s from %s: %v", form.UserName, ctx.RemoteAddr(), err)
|
log.Warn("Failed authentication attempt for %s from %s: %v", form.UserName, ctx.RemoteAddr(), err)
|
||||||
@ -237,6 +242,7 @@ func SignInPost(ctx *context.Context) {
|
|||||||
ctx.HTML(http.StatusOK, "user/auth/prohibit_login")
|
ctx.HTML(http.StatusOK, "user/auth/prohibit_login")
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
loginCounter.WithLabelValues("success").Inc()
|
||||||
ctx.ServerError("UserSignIn", err)
|
ctx.ServerError("UserSignIn", err)
|
||||||
}
|
}
|
||||||
return
|
return
|
||||||
|
@ -32,6 +32,8 @@ import (
|
|||||||
webhook_module "code.gitea.io/gitea/modules/webhook"
|
webhook_module "code.gitea.io/gitea/modules/webhook"
|
||||||
|
|
||||||
"github.com/gobwas/glob"
|
"github.com/gobwas/glob"
|
||||||
|
"github.com/prometheus/client_golang/prometheus"
|
||||||
|
"github.com/prometheus/client_golang/prometheus/promauto"
|
||||||
)
|
)
|
||||||
|
|
||||||
func newDefaultRequest(ctx context.Context, w *webhook_model.Webhook, t *webhook_model.HookTask) (req *http.Request, body []byte, err error) {
|
func newDefaultRequest(ctx context.Context, w *webhook_model.Webhook, t *webhook_model.HookTask) (req *http.Request, body []byte, err error) {
|
||||||
@ -148,6 +150,8 @@ func addDefaultHeaders(req *http.Request, secret []byte, w *webhook_model.Webhoo
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var webhookCounter = promauto.NewCounterVec(prometheus.CounterOpts{Namespace: "gitea", Subsystem: "webhook", Name: "deliveries", Help: "Number of webhook deliveries"}, []string{"success"})
|
||||||
|
|
||||||
// Deliver creates the [http.Request] (depending on the webhook type), sends it
|
// Deliver creates the [http.Request] (depending on the webhook type), sends it
|
||||||
// and records the status and response.
|
// and records the status and response.
|
||||||
func Deliver(ctx context.Context, t *webhook_model.HookTask) error {
|
func Deliver(ctx context.Context, t *webhook_model.HookTask) error {
|
||||||
@ -265,6 +269,12 @@ func Deliver(ctx context.Context, t *webhook_model.HookTask) error {
|
|||||||
t.ResponseInfo.Headers[k] = strings.Join(vals, ",")
|
t.ResponseInfo.Headers[k] = strings.Join(vals, ",")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if t.IsSucceed {
|
||||||
|
webhookCounter.WithLabelValues("success").Inc()
|
||||||
|
} else {
|
||||||
|
webhookCounter.WithLabelValues("failure").Inc()
|
||||||
|
}
|
||||||
|
|
||||||
p, err := io.ReadAll(resp.Body)
|
p, err := io.ReadAll(resp.Body)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.ResponseInfo.Body = fmt.Sprintf("read body: %s", err)
|
t.ResponseInfo.Body = fmt.Sprintf("read body: %s", err)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user