web/outpost: make metrics compliant with Prometheus best-practices Today, all NewHistogramVec store values in nanoseconds without changing the default histogram bucket, which are made for seconds, making them a bit useless. In addition, some metrics names are not self-explanatoryand and do not comply with Prometheus best practices. This commit tries to fix all of this "issues". NOTE: I kept old metrics in order to avoid breaking changes with existing dashboards and metrics. Signed-off-by: Alexandre NICOLAIE <xunleii@users.noreply.github.com>
		
			
				
	
	
		
			67 lines
		
	
	
		
			1.9 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
			
		
		
	
	
			67 lines
		
	
	
		
			1.9 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
package ldap
 | 
						|
 | 
						|
import (
 | 
						|
	"net"
 | 
						|
	"time"
 | 
						|
 | 
						|
	"beryju.io/ldap"
 | 
						|
	"github.com/getsentry/sentry-go"
 | 
						|
	"github.com/prometheus/client_golang/prometheus"
 | 
						|
	log "github.com/sirupsen/logrus"
 | 
						|
	"goauthentik.io/internal/outpost/ldap/bind"
 | 
						|
	"goauthentik.io/internal/outpost/ldap/metrics"
 | 
						|
)
 | 
						|
 | 
						|
func (ls *LDAPServer) Bind(bindDN string, bindPW string, conn net.Conn) (ldap.LDAPResultCode, error) {
 | 
						|
	req, span := bind.NewRequest(bindDN, bindPW, conn)
 | 
						|
	selectedApp := ""
 | 
						|
	defer func() {
 | 
						|
		span.Finish()
 | 
						|
		metrics.Requests.With(prometheus.Labels{
 | 
						|
			"outpost_name": ls.ac.Outpost.Name,
 | 
						|
			"type":         "bind",
 | 
						|
			"app":          selectedApp,
 | 
						|
		}).Observe(float64(span.EndTime.Sub(span.StartTime)) / float64(time.Second))
 | 
						|
		metrics.RequestsLegacy.With(prometheus.Labels{
 | 
						|
			"outpost_name": ls.ac.Outpost.Name,
 | 
						|
			"type":         "bind",
 | 
						|
			"app":          selectedApp,
 | 
						|
		}).Observe(float64(span.EndTime.Sub(span.StartTime)))
 | 
						|
		req.Log().WithField("took-ms", span.EndTime.Sub(span.StartTime).Milliseconds()).Info("Bind request")
 | 
						|
	}()
 | 
						|
 | 
						|
	defer func() {
 | 
						|
		err := recover()
 | 
						|
		if err == nil {
 | 
						|
			return
 | 
						|
		}
 | 
						|
		log.WithError(err.(error)).Error("recover in bind request")
 | 
						|
		sentry.CaptureException(err.(error))
 | 
						|
	}()
 | 
						|
 | 
						|
	for _, instance := range ls.providers {
 | 
						|
		username, err := instance.binder.GetUsername(bindDN)
 | 
						|
		if err == nil {
 | 
						|
			selectedApp = instance.GetAppSlug()
 | 
						|
			return instance.binder.Bind(username, req)
 | 
						|
		} else {
 | 
						|
			req.Log().WithError(err).Debug("Username not for instance")
 | 
						|
		}
 | 
						|
	}
 | 
						|
	req.Log().WithField("request", "bind").Warning("No provider found for request")
 | 
						|
	metrics.RequestsRejected.With(prometheus.Labels{
 | 
						|
		"outpost_name": ls.ac.Outpost.Name,
 | 
						|
		"type":         "bind",
 | 
						|
		"reason":       "no_provider",
 | 
						|
		"app":          "",
 | 
						|
	}).Inc()
 | 
						|
	metrics.RequestsRejectedLegacy.With(prometheus.Labels{
 | 
						|
		"outpost_name": ls.ac.Outpost.Name,
 | 
						|
		"type":         "bind",
 | 
						|
		"reason":       "no_provider",
 | 
						|
		"app":          "",
 | 
						|
	}).Inc()
 | 
						|
 | 
						|
	return ldap.LDAPResultInsufficientAccessRights, nil
 | 
						|
}
 |