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