K6 metrics from xk6 Go extensions?

Is it possible to send a variable initialized with a k6/metrics (i.e. Counter) to a Go extension and add metrics from there?

My goal is to increment metrics from the Go extension too.

Yes, for example, GitHub - grafana/xk6-client-prometheus-remote: A k6 extension for testing Prometheus Remote Write endpoints does just that. The metrics are defined here and used here. Though @dgzlopes, you should probably also specify the time :stuck_out_tongue_winking_eye:

If you want to create the metric from the k6/metrics and pass it to your extension as in:

import { Counter } from "k6/metrics";
import { customFunc } from "k6/x/myExtension";
var  c = new Counter ("coolCounter");

export default function(){
    customFunc(c); // this to then be able to call `c.Add`

In this case, you are hitting an issue with k6 use of common.Bind which I try to explain here. So you can’t … easily.

I haven’t tried it but what you will get instead of the original object will be a map[string]interface{} where the keys should be add and the value func(call goja.FunctionCall) goja.Value(for this particular case) so you technically should be able to call that … just with some more hoops to jump through.

Hopefully, we will drop the whole common.Bind thing at some point (unfortunately breaking your code ;() and have it be way easier … but no ETAs, sorry.