Sum total number of items returned by a LogQL qery

Hi, with following query i get total number of logs by label “SipSessionId”.
That’s ok, but i would like to have in my stat graph just total number of “unique” SipSessionId labels.
Is this possible?

QUERY:

sum by (SipSessionId)(count_over_time({origin=“MYORIGIN”, SipSessionId=~“.+”} [$__range]))

Could you show what you expect to have?

I would like to have a simple graph, like shown, that rapresents total number of “unique” sipsessionid labels.

Can you try the query:

count(count_over_time({origin="MYORIGIN", SipSessionId=~".+"}[$__range]) by (SipSessionId))

Your code give me an error.
I have to adjust like this:

count(count_over_time({origin=“MYORIGIN”, SipSessionId=~“.+”} [$__range])) by (SipSessionId)

but I already tested and I realized that i get same identical result of using “sum by”

Okay, what else did you already try?
What’s the error?

Played around “count”… count by, sum, etc but cannot get what i need.
If you meant error related to your preview code, this is error:

{
“request”: {
“url”: “api/ds/query?ds_type=loki&requestId=Q936_1”,
“method”: “POST”,
“data”: {
“queries”: [
{
“datasource”: {
“type”: “loki”,
“uid”: “advz300kmjgg0e”
},
“editorMode”: “code”,
“expr”: “count(count_over_time({origin="MYORIGIN", SipSessionId=~".+"}[$__range]) by (SipSessionId))”,
“queryType”: “range”,
“refId”: “A”,
“legendFormat”: “”,
“maxLines”: 1000,
“datasourceId”: 58,
“intervalMs”: 60000,
“maxDataPoints”: 1179
}
],
“from”: “1725191357852”,
“to”: “1725277757852”
},
“hideFromInspector”: false
},
“response”: {
“results”: {
“A”: {
“error”: “invalid request”,
“errorSource”: “downstream”,
“status”: 500,
“refId”: “A”
}
}
}
}

Seems similar to this issue, can you take a look at it?

Tried but not luck.
See error. Always it returns “invalid request” and cannot understand why

QUERY:

count by (SipSessionId)(sum by (SipSessionId)(count_over_time({origin=“MYORIGIN”, SipSessionId=~“.+”} [$__range])))

{
“request”: {
“url”: “api/ds/query?ds_type=loki&requestId=Q956_1”,
“method”: “POST”,
“data”: {
“queries”: [
{
“datasource”: {
“type”: “loki”,
“uid”: “advz300kmjgg0e”
},
“editorMode”: “code”,
“expr”: “count by (SipSessionId)(sum by (SipSessionId)(count_over_time({origin="MYORIGIN", SipSessionId=~".+"} [$__range])))”,
“queryType”: “instant”,
“refId”: “A”,
“legendFormat”: “”,
“maxLines”: 1000,
“datasourceId”: 58,
“intervalMs”: 86400000,
“maxDataPoints”: 1
}
],
“from”: “1725192327381”,
“to”: “1725278727381”
},
“hideFromInspector”: false
},
“response”: {
“results”: {
“A”: {
“error”: “invalid request”,
“errorSource”: “downstream”,
“status”: 500,
“refId”: “A”
}
}
}
}

Ok, I’m not so good with Loki, sorry…

Looks like Loki’s having issue to count a sum (which makes sense…).

Why the result of your query:

sum by(SipSessionId)(count_over_time({origin="MYORIGIN", SipSessionId=~".+"} [$__range]))

doesn’t suit you? Looks like the total of unique labels to me…

This is what i need at the end.
Sound strange grafana cannot do this simple achivment.

Okk! Make sense.
Can you show me what you get using:

count(count_over_time({origin=“MYORIGIN”, SipSessionId=~“.+”} [$__range])) by (SipSessionId)

Also, I quite sure that keeping your actually query and adding a new transformation you can achieve the sum of each statement and get 6…

Also can you take a look at this?

Using following query give me same identical result.

count(count_over_time({origin=“MYORIGIN”, SipSessionId=~“.+”} [$__range])) by (SipSessionId)

Trying query of this link give me error on query. Alway same error of “ivalid request” :frowning:

count(count(count_over_time({origin=“MYORIGIN”, SipSessionId=~“.+”} [$__range])) by (SipSessionId))

Well… I’m sorry but I’ll not be able to help you furthermore…

1 Like

Finally!!! Found solution with transformation!!

QUERY

count_over_time({origin=“MYORIGIN”, SipSessionId=~“.+”} [$__range])

2 Likes