From f144f4dc6bc56d6815ba47669f623266134eea30 Mon Sep 17 00:00:00 2001
From: ale <ale@incal.net>
Date: Tue, 20 Nov 2018 10:37:06 +0000
Subject: [PATCH] Add tracing support to auth-server requests

---
 server/login.go | 27 ++++++++++++++++++++++++++-
 1 file changed, 26 insertions(+), 1 deletion(-)

diff --git a/server/login.go b/server/login.go
index e2145aa..82b86fc 100644
--- a/server/login.go
+++ b/server/login.go
@@ -16,6 +16,7 @@ import (
 	"github.com/gorilla/csrf"
 	"github.com/gorilla/sessions"
 	"github.com/tstranex/u2f"
+	"go.opencensus.io/trace"
 
 	"git.autistici.org/id/auth"
 	authclient "git.autistici.org/id/auth/client"
@@ -293,7 +294,31 @@ func (l *loginHandler) makeAuthRequest(w http.ResponseWriter, req *http.Request,
 		U2FResponse: u2fResponse,
 		U2FAppID:    appID,
 	}
-	return l.authClient.Authenticate(req.Context(), &ar)
+
+	// Trace the authentication request.
+	ctx, span := trace.StartSpan(req.Context(), "auth",
+		trace.WithSpanKind(trace.SpanKindClient))
+	span.AddAttributes(
+		trace.StringAttribute("auth.user", username),
+		trace.StringAttribute("auth.service", l.authService),
+		trace.BoolAttribute("auth.with_password", len(password) > 0),
+		trace.BoolAttribute("auth.with_otp", otp != ""),
+		trace.BoolAttribute("auth.with_u2f", u2fResponse != nil),
+	)
+	defer span.End()
+
+	resp, err := l.authClient.Authenticate(ctx, &ar)
+
+	// Record the authentication response status in the trace.
+	if err != nil {
+		span.SetStatus(trace.Status{Code: trace.StatusCodeUnknown, Message: err.Error()})
+	} else if resp.Status == auth.StatusOK {
+		span.SetStatus(trace.Status{Code: trace.StatusCodeOK, Message: "OK"})
+	} else {
+		span.SetStatus(trace.Status{Code: trace.StatusCodePermissionDenied, Message: resp.Status.String()})
+	}
+
+	return resp, err
 }
 
 // Return a (relative) URL that will redirect the user to the login
-- 
GitLab