Sumarize only uniques

Hi, i try create a metric in New Realic One, but my metric sometimes is repeat for the id.
I need sumarize numbers.
I post metric in my code by Telemetry:

Attributes attributes = new Attributes().put("destination", destinationId);
attributes.put("mi_metrica_id", metricaId);     
metricBuffer.addMetric(new Gauge("metrica.mi_metrica.ok", register.get(true), now, attributes));
client.sendBatch(metricBuffer.createBatch());

Where register.get(true) is a Long value.

In New Relic I try sumarize all values to register.get(true), but I don`t sure not repeat the message.

This is my NRQL:
SELECT sum(metrica.mi_metrica.ok) as 'Metrica ok' FROM Metric FACET destination, mi_metrica_id, dateOf(timestamp), hourOf(timestamp) SINCE 1 hour ago

How can I do so that if there is a repeated id it does not double in the sum?

Thx

Welcome. I’m not entirely sure I understand, but are you getting the same mi_metrica_id showing under more than one destination and that is your repeated value?

hi Eric, thanks for responding. My post in the metric is in a queue, so I cannot guarantee uniqueness. What can be repeated is mi_metrica_id. I only need to select the first of the values of each id

I may still understand really, but if you need the first value of something you can try using earliest() instead of sum.

Thanks for your answer, for further explanation I am going to share the data set that I am obtaining in the metric:

  • id: 20000000003359, {“type”:“gauge”,“count”:2,“sum”:6122.0,“min”:3061.0,“max”:3061.0,“latest”:3061.0}
  • id: 20000000003295, {“type”:“gauge”,“count”:1,“sum”:3381.0,“min”:3381.0,“max”:3381.0,“latest”:3381.0}
  • id: 20000000003260, {“type”:“gauge”,“count”:1,“sum”:1855.0,“min”:1855.0,“max”:1855.0,“latest”:1855.0}

where what I need is the sum of all the “sum”, but if you see in the record the id 20000000003359 has a count of 2, so I recorded that value twice and if I do the sum it summarizes it twice.
Using the earliest () as you mention brings me the value of only the first time for each id, but it does not allow me to summarize and have the value of the sum of the three records.

The value that I need to show in my metric is the sum of the three that it would give in this case = 8297, but if I use the sum () function, it returns 11419 since it adds twice that of the id 20000000003359.

I see only one of these in your record set. Is there another that looks like this?

  • id: 20000000003359, {“type”:“gauge”,“count”:1,“sum”:6122.0,“min”:3061.0,“max”:3061.0,“latest”:3061.0}
  • id: 20000000003295,

Those values were obtained using a count:
SELECT count(metrica.mi_metrica.ok) as ‘ok’ FROM Metric FACET destination, id, dateOf(timestamp), hourOf(timestamp)

In the count of the register it is seen that it is 2, but I would need it to be added only once for each id

If you are sending counts twice I guess maybe not do that? :slight_smile:

Are you sure you have duplicates?

Can you show the raw events with all attributes for ID 20000000003359?

Also, try throwing in the RAW keyword.

I validated by log in my code and indeed the metric is posted twice, and I have no possibility to avoid this.

SELECT * FROM Metric

  • id: 20000000003359, {“type”:“gauge”,“count”:2,“sum”:6122.0,“min”:3061.0,“max”:3061.0,“latest”:3061.0}
  • id: 20000000003295, {“type”:“gauge”,“count”:1,“sum”:3381.0,“min”:3381.0,“max”:3381.0,“latest”:3381.0}
  • id: 20000000003260, {“type”:“gauge”,“count”:1,“sum”:1855.0,“min”:1855.0,“max”:1855.0,“latest”:1855.0}

and the result is like the one mentioned above
In the logs of my code I could see that the difference between one and the other is a few seconds
In the logs of my code I could see that the difference between one and the other is a few seconds, maybe that is why it shows it in a single line with a count of 2, or it may be because it has the same id.
sorry but I did not understand where to put the RAW

Ok, let me try to ask again. For ID 20000000003359 do you show more than one event in New Relic or only the one you have above that shows this:

id: 20000000003359, {“type”:“gauge”,“count”:2,“sum”:6122.0,“min”:3061.0,“max”:3061.0,“latest”:3061.0}

If you only have the one event perhaps you can sum(max) or sum(min) etc since I think you only have one event per ID? Your min/max/latest are all the same.

I only see a single event with count 2.
However, in my code that I put a log every time I post the metric to make sure how many times it is done, I see two logs with differences of a few seconds.

As you say, it would help me to add the min, but I tried the sum () function of min and it does not allow it

You may be better off sending this as an event and not a metric.

Sorry Eric but I never worked with events in New Relic, what benefit would it have compared to how I am doing now?

Metrics are aggregated. If you want to treat your custom data in the way you are (you need to get down to the record or event level) where you need to do sums and avoid aggregation events get you there. You may want to have a look at the documentation.

Well, thank you very much for your help, I’ll see if that way I can solve my problem.
Regards

1 Like

@ulises.pereyra Were you able to solve your problem? Thank you @6MM for your help on this one!

Hi Joi, I still have not been able to solve my problem, since with what Eric raised about events my data persists for only two days, I continue investigating how to solve this.

@ulises.pereyra That’s too bad… I’ll reach out to some of our SMEs to see if they can provide additional insights.

1 Like