Skip to content

Commit

Permalink
Add num_retries field to HTTPLog
Browse files Browse the repository at this point in the history
  • Loading branch information
rowanseymour committed Sep 21, 2021
1 parent 95b647b commit 3e3eab5
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 24 deletions.
1 change: 1 addition & 0 deletions core/handlers/airtime_transferred.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ func handleAirtimeTransferred(ctx context.Context, tx *sqlx.Tx, rp *redis.Pool,
httpLog.Response,
httpLog.Status != flows.CallStatusSuccess,
time.Duration(httpLog.ElapsedMS)*time.Millisecond,
httpLog.Retries,
httpLog.CreatedOn,
))
}
Expand Down
2 changes: 2 additions & 0 deletions core/handlers/service_called.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ func handleServiceCalled(ctx context.Context, tx *sqlx.Tx, rp *redis.Pool, oa *m
httpLog.Response,
httpLog.Status != flows.CallStatusSuccess,
time.Duration(httpLog.ElapsedMS)*time.Millisecond,
httpLog.Retries,
httpLog.CreatedOn,
)
} else if event.Service == "ticketer" {
Expand All @@ -71,6 +72,7 @@ func handleServiceCalled(ctx context.Context, tx *sqlx.Tx, rp *redis.Pool, oa *m
httpLog.Response,
httpLog.Status != flows.CallStatusSuccess,
time.Duration(httpLog.ElapsedMS)*time.Millisecond,
httpLog.Retries,
httpLog.CreatedOn,
)
}
Expand Down
22 changes: 13 additions & 9 deletions core/handlers/webhook_called.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,16 +45,20 @@ func handleWebhookCalled(ctx context.Context, tx *sqlx.Tx, rp *redis.Pool, oa *m
flow, _ := oa.Flow(run.FlowReference().UUID)

// create an HTTP log
httpLog := models.NewWebhookCalledLog(
oa.OrgID(),
flow.(*models.Flow).ID(),
event.URL, event.StatusCode, event.Request, event.Response,
event.Status != flows.CallStatusSuccess,
time.Millisecond*time.Duration(event.ElapsedMS), event.CreatedOn(),
)
scene.AppendToEventPreCommitHook(hooks.InsertHTTPLogsHook, httpLog)
if flow != nil {
httpLog := models.NewWebhookCalledLog(
oa.OrgID(),
flow.(*models.Flow).ID(),
event.URL, event.StatusCode, event.Request, event.Response,
event.Status != flows.CallStatusSuccess,
time.Millisecond*time.Duration(event.ElapsedMS),
event.Retries,
event.CreatedOn(),
)
scene.AppendToEventPreCommitHook(hooks.InsertHTTPLogsHook, httpLog)
}

// for backwards compatibility, for now also create a webhook result...
// TODO drop this once RP UI is switched to using HTTP logs
response := event.Response
if event.Status == flows.CallStatusConnectionError {
response = "connection error"
Expand Down
25 changes: 14 additions & 11 deletions core/models/http_logs.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,15 @@ type HTTPLog struct {
Response null.String `db:"response"`
IsError bool `db:"is_error"`
RequestTime int `db:"request_time"`
NumRetries int `db:"num_retries"`
CreatedOn time.Time `db:"created_on"`
FlowID FlowID `db:"flow_id"`
ClassifierID ClassifierID `db:"classifier_id"`
TicketerID TicketerID `db:"ticketer_id"`
AirtimeTransferID AirtimeTransferID `db:"airtime_transfer_id"`
}

func newHTTPLog(orgID OrgID, logType HTTPLogType, url string, statusCode int, request, response string, isError bool, elapsed time.Duration, createdOn time.Time) *HTTPLog {
func newHTTPLog(orgID OrgID, logType HTTPLogType, url string, statusCode int, request, response string, isError bool, elapsed time.Duration, retries int, createdOn time.Time) *HTTPLog {
return &HTTPLog{
OrgID: orgID,
LogType: logType,
Expand All @@ -60,34 +61,35 @@ func newHTTPLog(orgID OrgID, logType HTTPLogType, url string, statusCode int, re
Response: null.String(response),
IsError: isError,
RequestTime: int(elapsed / time.Millisecond),
NumRetries: retries,
CreatedOn: createdOn,
}
}

// NewWebhookCalledLog creates a new HTTP log for an in-flow webhook call
func NewWebhookCalledLog(orgID OrgID, fid FlowID, url string, statusCode int, request, response string, isError bool, elapsed time.Duration, createdOn time.Time) *HTTPLog {
h := newHTTPLog(orgID, LogTypeWebhookCalled, url, statusCode, request, response, isError, elapsed, createdOn)
func NewWebhookCalledLog(orgID OrgID, fid FlowID, url string, statusCode int, request, response string, isError bool, elapsed time.Duration, retries int, createdOn time.Time) *HTTPLog {
h := newHTTPLog(orgID, LogTypeWebhookCalled, url, statusCode, request, response, isError, elapsed, retries, createdOn)
h.FlowID = fid
return h
}

// NewClassifierCalledLog creates a new HTTP log for a classifier call
func NewClassifierCalledLog(orgID OrgID, cid ClassifierID, url string, statusCode int, request, response string, isError bool, elapsed time.Duration, createdOn time.Time) *HTTPLog {
h := newHTTPLog(orgID, LogTypeClassifierCalled, url, statusCode, request, response, isError, elapsed, createdOn)
func NewClassifierCalledLog(orgID OrgID, cid ClassifierID, url string, statusCode int, request, response string, isError bool, elapsed time.Duration, retries int, createdOn time.Time) *HTTPLog {
h := newHTTPLog(orgID, LogTypeClassifierCalled, url, statusCode, request, response, isError, elapsed, retries, createdOn)
h.ClassifierID = cid
return h
}

// NewTicketerCalledLog creates a new HTTP log for a ticketer call
func NewTicketerCalledLog(orgID OrgID, tid TicketerID, url string, statusCode int, request, response string, isError bool, elapsed time.Duration, createdOn time.Time) *HTTPLog {
h := newHTTPLog(orgID, LogTypeTicketerCalled, url, statusCode, request, response, isError, elapsed, createdOn)
func NewTicketerCalledLog(orgID OrgID, tid TicketerID, url string, statusCode int, request, response string, isError bool, elapsed time.Duration, retries int, createdOn time.Time) *HTTPLog {
h := newHTTPLog(orgID, LogTypeTicketerCalled, url, statusCode, request, response, isError, elapsed, retries, createdOn)
h.TicketerID = tid
return h
}

// NewAirtimeTransferredLog creates a new HTTP log for an airtime transfer
func NewAirtimeTransferredLog(orgID OrgID, url string, statusCode int, request, response string, isError bool, elapsed time.Duration, createdOn time.Time) *HTTPLog {
return newHTTPLog(orgID, LogTypeAirtimeTransferred, url, statusCode, request, response, isError, elapsed, createdOn)
func NewAirtimeTransferredLog(orgID OrgID, url string, statusCode int, request, response string, isError bool, elapsed time.Duration, retries int, createdOn time.Time) *HTTPLog {
return newHTTPLog(orgID, LogTypeAirtimeTransferred, url, statusCode, request, response, isError, elapsed, retries, createdOn)
}

// SetAirtimeTransferID called to set the transfer ID on a log after the transfer has been created
Expand All @@ -96,8 +98,8 @@ func (h *HTTPLog) SetAirtimeTransferID(tid AirtimeTransferID) {
}

const insertHTTPLogsSQL = `
INSERT INTO request_logs_httplog( log_type, org_id, url, status_code, flow_id, classifier_id, ticketer_id, airtime_transfer_id, request, response, is_error, request_time, created_on)
VALUES(:log_type, :org_id, :url, :status_code, :flow_id, :classifier_id, :ticketer_id, :airtime_transfer_id, :request, :response, :is_error, :request_time, :created_on)
INSERT INTO request_logs_httplog( log_type, org_id, url, status_code, flow_id, classifier_id, ticketer_id, airtime_transfer_id, request, response, is_error, request_time, num_retries, created_on)
VALUES(:log_type, :org_id, :url, :status_code, :flow_id, :classifier_id, :ticketer_id, :airtime_transfer_id, :request, :response, :is_error, :request_time, :num_retries, :created_on)
RETURNING id
`

Expand Down Expand Up @@ -152,6 +154,7 @@ func (h *HTTPLogger) Ticketer(t *Ticketer) flows.HTTPLogCallback {
l.Response,
l.Status != flows.CallStatusSuccess,
time.Duration(l.ElapsedMS)*time.Millisecond,
l.Retries,
l.CreatedOn,
))
}
Expand Down
10 changes: 6 additions & 4 deletions core/models/http_logs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,26 +18,28 @@ import (
func TestHTTPLogs(t *testing.T) {
ctx, _, db, _ := testsuite.Get()

defer func() { db.MustExec(`DELETE FROM request_logs_httplog`) }()

// insert a classifier log
log := models.NewClassifierCalledLog(testdata.Org1.ID, testdata.Wit.ID, "http://foo.bar", 200, "GET /", "STATUS 200", false, time.Second, time.Now())
log := models.NewClassifierCalledLog(testdata.Org1.ID, testdata.Wit.ID, "http://foo.bar", 200, "GET /", "STATUS 200", false, time.Second, 0, time.Now())
err := models.InsertHTTPLogs(ctx, db, []*models.HTTPLog{log})
assert.Nil(t, err)

testsuite.AssertQuery(t, db, `SELECT count(*) from request_logs_httplog WHERE org_id = $1 AND status_code = 200 AND classifier_id = $2 AND is_error = FALSE`, testdata.Org1.ID, testdata.Wit.ID).Returns(1)

// insert a log with nil response
log = models.NewClassifierCalledLog(testdata.Org1.ID, testdata.Wit.ID, "http://foo.bar", 0, "GET /", "", true, time.Second, time.Now())
log = models.NewClassifierCalledLog(testdata.Org1.ID, testdata.Wit.ID, "http://foo.bar", 0, "GET /", "", true, time.Second, 0, time.Now())
err = models.InsertHTTPLogs(ctx, db, []*models.HTTPLog{log})
assert.Nil(t, err)

testsuite.AssertQuery(t, db, `SELECT count(*) from request_logs_httplog WHERE org_id = $1 AND status_code = 0 AND classifier_id = $2 AND is_error = TRUE AND response IS NULL`, testdata.Org1.ID, testdata.Wit.ID).Returns(1)

// insert a webhook log
log = models.NewWebhookCalledLog(testdata.Org1.ID, testdata.Favorites.ID, "http://foo.bar", 400, "GET /", "HTTP 200", false, time.Second, time.Now())
log = models.NewWebhookCalledLog(testdata.Org1.ID, testdata.Favorites.ID, "http://foo.bar", 400, "GET /", "HTTP 200", false, time.Second, 2, time.Now())
err = models.InsertHTTPLogs(ctx, db, []*models.HTTPLog{log})
assert.Nil(t, err)

testsuite.AssertQuery(t, db, `SELECT count(*) from request_logs_httplog WHERE org_id = $1 AND status_code = 400 AND flow_id = $2`, testdata.Org1.ID, testdata.Favorites.ID).Returns(1)
testsuite.AssertQuery(t, db, `SELECT count(*) from request_logs_httplog WHERE org_id = $1 AND status_code = 400 AND flow_id = $2 AND num_retries = 2`, testdata.Org1.ID, testdata.Favorites.ID).Returns(1)
}

func TestHTTPLogger(t *testing.T) {
Expand Down
Binary file modified mailroom_test.dump
Binary file not shown.

0 comments on commit 3e3eab5

Please sign in to comment.