From 2309d71cb15a7fb653be9ef98769ee4bb8341663 Mon Sep 17 00:00:00 2001 From: Jack Zheng Date: Sun, 6 Dec 2020 10:01:26 +1100 Subject: [PATCH] Capture err value when activity returns Currently the recordActivityEnd function evaluates the value of the err parameter when the defer statement is called, rather than when the activity returns. This means that any errors produced by the activity won't be correctly recorded. For a simple example demonstrating the current behavior, please refer to https://play.golang.org/p/7gVYcXJla8E --- metrics/workflow.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/metrics/workflow.go b/metrics/workflow.go index e21ec7cd..d384018e 100644 --- a/metrics/workflow.go +++ b/metrics/workflow.go @@ -36,7 +36,7 @@ func Activity(ctx context.Context, scheduledTimeNanos int64) error { var err error metricsScope := activity.GetMetricsScope(ctx) metricsScope, sw := recordActivityStart(metricsScope, "metrics.Activity", scheduledTimeNanos) - defer recordActivityEnd(metricsScope, sw, err) + defer func() { recordActivityEnd(metricsScope, sw, err) }() time.Sleep(time.Second) logger.Info("Metrics reported.")