Improve tracing

This commit is contained in:
Torjus Håkestad 2023-10-27 07:11:02 +02:00
parent ccd77cdbdc
commit 06dc50682c

View File

@ -72,7 +72,9 @@ func NewServer(config *Config) (*UserServer, error) {
Username: "admin", Username: "admin",
Role: "admin", Role: "admin",
} }
password := uuid.Must(uuid.NewRandom()).String() // TODO: Use random pw
// password := uuid.Must(uuid.NewRandom()).String()
password := "admin"
_ = u.SetPassword(password) _ = u.SetPassword(password)
_ = srv.store.AddUser(u) _ = srv.store.AddUser(u)
@ -297,11 +299,15 @@ func (s *UserServer) SetPasswordHandler(w http.ResponseWriter, r *http.Request)
} }
func (s *UserServer) VerifyHandler(w http.ResponseWriter, r *http.Request) { func (s *UserServer) VerifyHandler(w http.ResponseWriter, r *http.Request) {
_, span := otel.GetTracerProvider().Tracer("").Start(r.Context(), "Verify password")
defer span.End()
decoder := json.NewDecoder(r.Body) decoder := json.NewDecoder(r.Body)
defer r.Body.Close() defer r.Body.Close()
var request users.VerifyRequest var request users.VerifyRequest
span.AddEvent("Start decoding request")
if err := decoder.Decode(&request); err != nil { if err := decoder.Decode(&request); err != nil {
WriteError(w, users.ErrorResponse{ WriteError(w, users.ErrorResponse{
Status: http.StatusBadRequest, Status: http.StatusBadRequest,
@ -319,6 +325,7 @@ func (s *UserServer) VerifyHandler(w http.ResponseWriter, r *http.Request) {
return return
} }
span.AddEvent("Fetch user")
u, err := s.store.GetUser(id) u, err := s.store.GetUser(id)
if err != nil { if err != nil {
msg := fmt.Sprintf("Server error: %s", err) msg := fmt.Sprintf("Server error: %s", err)
@ -337,6 +344,7 @@ func (s *UserServer) VerifyHandler(w http.ResponseWriter, r *http.Request) {
return return
} }
span.AddEvent("Verify password")
err = u.ComparePassword(request.Password) err = u.ComparePassword(request.Password)
if err != nil { if err != nil {
WriteError(w, users.ErrorResponse{ WriteError(w, users.ErrorResponse{
@ -345,5 +353,6 @@ func (s *UserServer) VerifyHandler(w http.ResponseWriter, r *http.Request) {
}) })
return return
} }
span.AddEvent("Write response")
w.WriteHeader(http.StatusOK) w.WriteHeader(http.StatusOK)
} }