Log timeout in API. (#4231)
* Log timeout in API. * make a reset function
This commit is contained in:
+16
-3
@@ -70,6 +70,14 @@ type twirpLogger struct {
|
||||
fieldsOrig []any
|
||||
fields []any
|
||||
startedAt time.Time
|
||||
deadline time.Time
|
||||
}
|
||||
|
||||
func (t *twirpLogger) reset() {
|
||||
t.fields = t.fieldsOrig
|
||||
t.error = nil
|
||||
t.startedAt = time.Time{}
|
||||
t.deadline = time.Time{}
|
||||
}
|
||||
|
||||
func AppendLogFields(ctx context.Context, fields ...any) {
|
||||
@@ -84,6 +92,9 @@ func AppendLogFields(ctx context.Context, fields ...any) {
|
||||
func loggerRequestReceived(ctx context.Context, twirpLoggerPool *sync.Pool) (context.Context, error) {
|
||||
r := twirpLoggerPool.Get().(*twirpLogger)
|
||||
r.startedAt = time.Now()
|
||||
if deadline, ok := ctx.Deadline(); ok {
|
||||
r.deadline = deadline
|
||||
}
|
||||
r.fields = r.fieldsOrig
|
||||
r.error = nil
|
||||
|
||||
@@ -115,6 +126,9 @@ func loggerResponseSent(ctx context.Context, twirpLoggerPool *sync.Pool) {
|
||||
}
|
||||
|
||||
r.fields = append(r.fields, "duration", time.Since(r.startedAt))
|
||||
if !r.deadline.IsZero() {
|
||||
r.fields = append(r.fields, "timeout", r.deadline.Sub(r.startedAt))
|
||||
}
|
||||
|
||||
if status, ok := twirp.StatusCode(ctx); ok {
|
||||
r.fields = append(r.fields, "status", status)
|
||||
@@ -127,9 +141,8 @@ func loggerResponseSent(ctx context.Context, twirpLoggerPool *sync.Pool) {
|
||||
serviceMethod := "API " + r.service + "." + r.method
|
||||
utils.GetLogger(ctx).WithComponent(utils.ComponentAPI).Infow(serviceMethod, r.fields...)
|
||||
|
||||
r.fields = r.fieldsOrig
|
||||
r.error = nil
|
||||
|
||||
// reset fields and return to pool
|
||||
r.reset()
|
||||
twirpLoggerPool.Put(r)
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user