Custom Counter metric from unique JSON array/response

Hello folks. I’m having a tough time creating a customer counter in a routine that returns unique values. the code block below works fine to produce a custom metrics named frameName and returns a counted value from the message JSON response. What I’m trying to do is create a custom metric for every response parsed from the JSON file. e.g. const msg = JSON.parse(message); is is possible or even “wise” to do something like.

const msg.frameName = new Counter(framename_${msg.frameName});

const received_messages = new Counter('frameName');
 socket.on('message', function (message) {
      console.log(`Received message: ${message}`);
      const msg = JSON.parse(message);

      received_messages.add(1, { frameName: msg.frameName});

any ideas, examples?


Hello @PlayStay,

In order to help you more accurately, I’d need to understand better what would you like to measure, and what’s the shape of that data.

But in general terms, I’d recommend you to either:

  • Define all the metrics (i.e. counters) for all the values you expect, in advance
    • because it’s not possible to define them on the fly
  • Use tags, so you can group your metrics by tag value.

For instance, if you’re trying to count how many messages you receive of each type (or similar), you can use a message_type tag, for instance (as you’re already doing in the example above).

In such case, the only downside is that as far as I know, for now, there’s no way to display the tagged results in the summary, unless you define a threshold according to that, as discussed here.

Please, let me know if that would help in your case. Also, feel free to bring as much context as possible, in order to be able to help you more accurately.

For instance, when you say “I’m trying to do is create a custom metric for every response parsed from the JSON file”, what would be the reason/purpose to do so? Tell me what you need from the user perspective, and I’ll try to tell you how to get it with k6, or suggest a feature request if not possible.