How do I create a Delta metric?

Hey folks - I have an event driven API, where the API returns immediately with a token, and then the user polls a status API to see whether the task is complete. A different debugging API returns the total available results at a given time. For example:
Call 1: 201 results
Call 2: 201 results
Call 3: 206 results
Call 4: 218 results

I’d like to see the same stats that the Trend metric creates, but on the Deltas - [0, 0, 5, 218]. Knowing that the VUs can’t guarantee order, it would be completely fine to save the [itr #, total #] and on teardown sort the iterations and batch add the trend metrics (which would work in a distributed world). That said, when I create a local map/array to collect all the results for the teardown, they are only accessible during the iterations, but not the teardown phase.

How would I go about getting this time of metric set up? Thanks!

Every VU in k6 is a completely different JS runtime, with its own local variables and everything. setup() and teardown() are also executed in separate runtimes, so they don’t have access to the variables of VUs.

I am not sure exactly what you are trying to do, but maybe some combination of custom metrics and handleSummary() would be enough?

If not, you’d have to do it outside of k6, by piping the raw metrics data to a JSON file or to something like InfluxDB, see k6 external outputs: Results output