Skip to content

Commit dd0ca94

Browse files
Merge pull request #6566 from devtron-labs/resp-time-audit-log
misc: append response time in audit log
2 parents 819743a + aeee946 commit dd0ca94

File tree

1 file changed

+15
-9
lines changed

1 file changed

+15
-9
lines changed

api/util/logger.go

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,14 @@ import (
2828
)
2929

3030
type AuditLoggerDTO struct {
31-
UrlPath string `json:"urlPath"`
32-
UserEmail string `json:"userEmail"`
33-
UpdatedOn time.Time `json:"updatedOn"`
34-
QueryParams string `json:"queryParams"`
35-
ApiResponseCode int `json:"apiResponseCode"`
36-
RequestPayload []byte `json:"requestPayload"`
37-
RequestMethod string `json:"requestMethod"`
31+
UrlPath string `json:"urlPath"`
32+
UserEmail string `json:"userEmail"`
33+
UpdatedOn time.Time `json:"updatedOn"`
34+
QueryParams string `json:"queryParams"`
35+
ApiResponseCode int `json:"apiResponseCode"`
36+
RequestPayload []byte `json:"requestPayload"`
37+
RequestMethod string `json:"requestMethod"`
38+
ResponseTime time.Duration `json:"responseTime"`
3839
}
3940

4041
type LoggingMiddlewareImpl struct {
@@ -72,22 +73,27 @@ func (impl LoggingMiddlewareImpl) LoggingMiddleware(next http.Handler) http.Hand
7273
// Restore the request body for downstream handlers
7374
r.Body = io.NopCloser(&bodyBuffer)
7475

76+
// Record start time for calculating response time
77+
startTime := time.Now()
78+
7579
auditLogDto := &AuditLoggerDTO{
7680
UrlPath: r.URL.Path,
7781
UserEmail: userEmail,
78-
UpdatedOn: time.Now(),
82+
UpdatedOn: startTime,
7983
QueryParams: r.URL.Query().Encode(),
8084
RequestPayload: bodyBuffer.Bytes(),
8185
RequestMethod: r.Method,
8286
}
8387
// Call the next handler in the chain.
8488
next.ServeHTTP(d, r)
8589

90+
// Calculate response time
91+
auditLogDto.ResponseTime = time.Since(startTime)
8692
auditLogDto.ApiResponseCode = d.Status()
8793
LogRequest(auditLogDto)
8894
})
8995
}
9096

9197
func LogRequest(auditLogDto *AuditLoggerDTO) {
92-
log.Printf("AUDIT_LOG: requestMethod: %s, urlPath: %s, queryParams: %s, updatedBy: %s, updatedOn: %s, apiResponseCode: %d, requestPayload: %s", auditLogDto.RequestMethod, auditLogDto.UrlPath, auditLogDto.QueryParams, auditLogDto.UserEmail, auditLogDto.UpdatedOn, auditLogDto.ApiResponseCode, auditLogDto.RequestPayload)
98+
log.Printf("AUDIT_LOG: requestMethod: %s, urlPath: %s, queryParams: %s, updatedBy: %s, updatedOn: %s, apiResponseCode: %d, responseTime: %s, requestPayload: %s", auditLogDto.RequestMethod, auditLogDto.UrlPath, auditLogDto.QueryParams, auditLogDto.UserEmail, auditLogDto.UpdatedOn, auditLogDto.ApiResponseCode, auditLogDto.ResponseTime, auditLogDto.RequestPayload)
9399
}

0 commit comments

Comments
 (0)