|
|
|
@ -107,42 +107,49 @@ func FallbackUsed(ctx context.Context) bool { |
|
|
|
|
func (f *AuthenticatorWithFallback) Authenticate(ctx context.Context) (context.Context, error) { |
|
|
|
|
ctx, span := f.tracer.Start(ctx, "grpcutils.AuthenticatorWithFallback.Authenticate") |
|
|
|
|
defer span.End() |
|
|
|
|
span.SetAttributes(attribute.Bool("fallback_used", false)) |
|
|
|
|
|
|
|
|
|
// Try to authenticate with the new authenticator first
|
|
|
|
|
span.SetAttributes(attribute.Bool("fallback_used", false)) |
|
|
|
|
newCtx, err := f.authenticator.Authenticate(ctx) |
|
|
|
|
if err != nil { |
|
|
|
|
// In case of error, fallback to the legacy authenticator
|
|
|
|
|
newCtx, err = f.fallback.Authenticate(ctx) |
|
|
|
|
f.metrics.fallbackCounter.WithLabelValues(fmt.Sprintf("%t", err == nil)).Inc() |
|
|
|
|
span.SetAttributes(attribute.Bool("fallback_used", true)) |
|
|
|
|
if err == nil { |
|
|
|
|
// fallback not used, authentication successful
|
|
|
|
|
f.metrics.requestsTotal.WithLabelValues("false", "true").Inc() |
|
|
|
|
return newCtx, nil |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// In case of error, fallback to the legacy authenticator
|
|
|
|
|
span.SetAttributes(attribute.Bool("fallback_used", true)) |
|
|
|
|
newCtx, err = f.fallback.Authenticate(ctx) |
|
|
|
|
if newCtx != nil { |
|
|
|
|
newCtx = context.WithValue(newCtx, contextFallbackKey{}, true) |
|
|
|
|
} |
|
|
|
|
f.metrics.requestsTotal.WithLabelValues("true", fmt.Sprintf("%t", err == nil)).Inc() |
|
|
|
|
return newCtx, err |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
const ( |
|
|
|
|
metricsNamespace = "grafana" |
|
|
|
|
metricsSubSystem = "grpc_authenticator" |
|
|
|
|
metricsSubSystem = "grpc_authenticator_with_fallback" |
|
|
|
|
) |
|
|
|
|
|
|
|
|
|
type metrics struct { |
|
|
|
|
fallbackCounter *prometheus.CounterVec |
|
|
|
|
requestsTotal *prometheus.CounterVec |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func newMetrics(reg prometheus.Registerer) *metrics { |
|
|
|
|
m := &metrics{ |
|
|
|
|
fallbackCounter: prometheus.NewCounterVec( |
|
|
|
|
requestsTotal: prometheus.NewCounterVec( |
|
|
|
|
prometheus.CounterOpts{ |
|
|
|
|
Namespace: metricsNamespace, |
|
|
|
|
Subsystem: metricsSubSystem, |
|
|
|
|
Name: "fallback_total", |
|
|
|
|
Help: "Number of times the fallback authenticator was used", |
|
|
|
|
}, []string{"result"}), |
|
|
|
|
Name: "requests_total", |
|
|
|
|
Help: "Number requests using the authenticator with fallback", |
|
|
|
|
}, []string{"fallback_used", "result"}), |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
if reg != nil { |
|
|
|
|
once.Do(func() { |
|
|
|
|
reg.MustRegister(m.fallbackCounter) |
|
|
|
|
reg.MustRegister(m.requestsTotal) |
|
|
|
|
}) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|