Skip to content

Commit

Permalink
feat: add response.latency (#28)
Browse files Browse the repository at this point in the history
  • Loading branch information
vm-001 authored Sep 22, 2024
1 parent be0e502 commit 3206f99
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 35 deletions.
1 change: 1 addition & 0 deletions db/entities/attempt.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ func (m AttemptRequest) Value() (driver.Value, error) {

type AttemptResponse struct {
Status int `json:"status"`
Latency int64 `json:"latency"`
Headers map[string]string `json:"headers"`
Body *string `json:"body"`
}
Expand Down
3 changes: 3 additions & 0 deletions openapi.yml
Original file line number Diff line number Diff line change
Expand Up @@ -612,6 +612,9 @@ components:
properties:
status:
type: number
latency:
type: integer
description: "Latancy of response in milliseconds"
header:
type: object
body:
Expand Down
1 change: 1 addition & 0 deletions test/delivery/delivery_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ var _ = Describe("delivery", Ordered, func() {
Body: utils.Pointer(`{"key": "value"}`),
}, attempt.Request)
assert.Equal(GinkgoT(), 200, attempt.Response.Status)
assert.True(GinkgoT(), attempt.Response.Latency > 0)
})
})

Expand Down
77 changes: 42 additions & 35 deletions worker/worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -185,47 +185,19 @@ func (w *Worker) handleTask(ctx context.Context, task *queue.TaskMessage) error
Timeout: time.Duration(endpoint.Request.Timeout) * time.Millisecond,
}

result := &dao.AttemptResult{
AttemptedAt: types.NewTime(time.Now()),
}

// deliver the request
startAt := time.Now()
response := w.deliverer.Deliver(request)
finishAt := time.Now()

if response.Error != nil {
if errors.Is(response.Error, context.DeadlineExceeded) {
result.ErrorCode = utils.Pointer(entities.AttemptErrorCodeTimeout)
} else {
result.ErrorCode = utils.Pointer(entities.AttemptErrorCodeUnknown)
}
w.log.Infof("[worker] failed to delivery event: %v", response.Error)
}

finishAt := time.Now()

w.log.Debugf("[worker] delivery response: %v", response)

if response.Is2xx() {
result.Status = entities.AttemptStatusSuccess
} else {
result.Status = entities.AttemptStatusFailure
}

result.Request = &entities.AttemptRequest{
URL: request.URL,
Method: request.Method,
Headers: utils.HeaderMap(request.Request.Header),
Body: utils.Pointer(string(request.Payload)),
}
if response.StatusCode != 0 {
result.Response = &entities.AttemptResponse{
Status: response.StatusCode,
Headers: utils.HeaderMap(response.Header),
Body: utils.Pointer(string(response.ResponseBody)),
}
}

if data.Attempt >= len(endpoint.Retry.Config.Attempts) {
result.Exhausted = true
}
result := buildAttemptResult(request, response)
result.AttemptedAt = types.NewTime(startAt)
result.Exhausted = data.Attempt >= len(endpoint.Retry.Config.Attempts)

err = w.DB.Attempts.UpdateDelivery(ctx, task.ID, result)
if err != nil {
Expand Down Expand Up @@ -277,3 +249,38 @@ func (w *Worker) handleTask(ctx context.Context, task *queue.TaskMessage) error
}
return nil
}

func buildAttemptResult(request *deliverer.Request, response *deliverer.Response) *dao.AttemptResult {
result := &dao.AttemptResult{
Request: &entities.AttemptRequest{
URL: request.URL,
Method: request.Method,
Headers: utils.HeaderMap(request.Request.Header),
Body: utils.Pointer(string(request.Payload)),
},
Status: entities.AttemptStatusSuccess,
}

if response.Error != nil {
if errors.Is(response.Error, context.DeadlineExceeded) {
result.ErrorCode = utils.Pointer(entities.AttemptErrorCodeTimeout)
} else {
result.ErrorCode = utils.Pointer(entities.AttemptErrorCodeUnknown)
}
}

if !response.Is2xx() {
result.Status = entities.AttemptStatusFailure
}

if response.StatusCode != 0 {
result.Response = &entities.AttemptResponse{
Status: response.StatusCode,
Latency: response.Latancy.Milliseconds(),
Headers: utils.HeaderMap(response.Header),
Body: utils.Pointer(string(response.ResponseBody)),
}
}

return result
}

0 comments on commit 3206f99

Please sign in to comment.