1
0
Fork 0

Add metrics data to Execute response

master
Philip O'Toole 10 years ago
parent 11cd12b59a
commit 4f410113e4

@ -45,6 +45,12 @@ func isTransaction(req *http.Request) (bool, error) {
return queryParam(req, "transaction") return queryParam(req, "transaction")
} }
type WriteResponse struct {
Time string
Success int
Fail int
}
// The raftd server is a combination of the Raft server and an HTTP // The raftd server is a combination of the Raft server and an HTTP
// server which acts as the transport. // server which acts as the transport.
type Server struct { type Server struct {
@ -219,6 +225,11 @@ func (s *Server) readHandler(w http.ResponseWriter, req *http.Request) {
func (s *Server) writeHandler(w http.ResponseWriter, req *http.Request) { func (s *Server) writeHandler(w http.ResponseWriter, req *http.Request) {
log.Trace("writeHandler for URL: %s", req.URL) log.Trace("writeHandler for URL: %s", req.URL)
var nSuccess int
var nFail int
var startTime time.Time
// Read the value from the POST body. // Read the value from the POST body.
b, err := ioutil.ReadAll(req.Body) b, err := ioutil.ReadAll(req.Body)
if err != nil { if err != nil {
@ -232,6 +243,7 @@ func (s *Server) writeHandler(w http.ResponseWriter, req *http.Request) {
} }
// Execute the command against the Raft server. // Execute the command against the Raft server.
startTime = time.Now()
switch { switch {
case len(stmts) == 0: case len(stmts) == 0:
log.Trace("No database execute commands supplied") log.Trace("No database execute commands supplied")
@ -240,18 +252,39 @@ func (s *Server) writeHandler(w http.ResponseWriter, req *http.Request) {
case len(stmts) == 1: case len(stmts) == 1:
log.Trace("Single statment, implicit transaction") log.Trace("Single statment, implicit transaction")
_, err = s.raftServer.Do(command.NewWriteCommand(stmts[0])) _, err = s.raftServer.Do(command.NewWriteCommand(stmts[0]))
if err != nil {
nFail++
} else {
nSuccess++
}
case len(stmts) > 1: case len(stmts) > 1:
log.Trace("Multistatement, transaction possible") log.Trace("Multistatement, transaction possible")
transaction, _ := isTransaction(req) transaction, _ := isTransaction(req)
if transaction { if transaction {
log.Trace("Transaction requested") log.Trace("Transaction requested")
_, err = s.raftServer.Do(command.NewTransactionWriteCommandSet(stmts)) _, err = s.raftServer.Do(command.NewTransactionWriteCommandSet(stmts))
if err != nil {
nFail++
} else {
nSuccess++
}
} else { } else {
log.Trace("No transaction requested") log.Trace("No transaction requested")
// Do each individually, returning JSON respoonse // Do each individually, returning JSON respoonse
} }
} }
duration := time.Since(startTime)
wr := WriteResponse{Time: duration.String(), Success: nSuccess, Fail: nFail}
pretty, _ := isPretty(req)
if pretty {
b, err = json.MarshalIndent(wr, "", " ")
} else {
b, err = json.Marshal(wr)
}
if err != nil { if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest) http.Error(w, err.Error(), http.StatusBadRequest) // Internal error actually
} else {
w.Write([]byte(b))
} }
} }

Loading…
Cancel
Save