Recommended way to emit custom metrics in workflow/activity

What is the recommended way to emit custom metrics in workflow/activity?
I can imagine it should be a side-effect, so should we use a local activity to emit metric? Or is there a better way to do that?

Asking this, because we don’t want to send duplicate metrics on recovery.

Workflow.getMetricsScope() returns metrics scope instance that automatically dedupes metrics on replay.

Thanks for the info! (btw, it should be Workflow.getMetricsScope())

1 Like

I notices that the scope.timer(METRIC_NAME).start() returns a Stopwatch.
Is it safe to use this:

Stopwatch watch = metricScope.timer(METRIC_NAME).start();
operation();
watch.stop();

Or using Workflow.currentTimeMillis only:

long startTime = Workflow.currentTimeMillis();
operation();
long duration = Workflow.currentTimeMillis() - startTime;
metricScope.timer(METRIC_NAME).record(com.uber.m3.util.Duration.ofMillis(duration));

It is safe to use

Stopwatch watch = metricScope.timer(METRIC_NAME).start();
operation();
watch.stop();

Here is the code that initializes the scope to use the workflow clock if you are interested in implementation details.

1 Like