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?

thanks

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.

Thanks!

Hi @joanlopez sorry for the late reply. unfortunately I do not know how I can offer more clues to solve this issue without sharing code and samples (which I can’t do for confidentiality reasons). However your suggestion has given me a few ideas to tinker with.

thanks

Hey @PlayStay,

However your suggestion has given me a few ideas to tinker with.

Glad to read that from you! Just bear in mind that you can reach out again if you have any more specific question, we’ll try to do our best to help and support you! :bowing_man: