MyUnisoft / loki

Node.js Loki SDK
MIT License
15 stars 3 forks source link

Query returns only values without labels/fields #166

Closed sempex closed 4 months ago

sempex commented 4 months ago

Hi everyone,

I want to display how many users per httpUserAgent visit a distinct URL based on Kubernetes ingress nginx logs. This would work with the following query:

const getBrowsers = `sum by(httpUserAgent) (count_over_time({namespace="ingress-nginx", job!~"integrations/kubernetes/eventhandler|infra-monitoring/eventrouter"} | json | vhost=~"${vHostquery}"| __error__=""[${time}]))`
const browsers = await api.Loki.queryRange(getBrowsers, {
      start: "0h"
    })

Here is the query in Grafana:

Screenshot 2024-06-30 at 21 46 05

When I execute the query above with the myunisoft/loki library I get the following response:

    "values": [
        "27170",
        "2",
        "244"
    ]

which indeed would be correct. But I found no way to get the field/label names that belong to those values, so I won't be able to display them properly. Is there a way to get those values?

fraxken commented 4 months ago

I haven't had the time to properly implement Matrix support yet (right now we mostly use Stream). I would like to implement a Unified API, but it's challenging to satisfy TypeScript with the types.

const { logs } = await api.Loki.queryRangeMatrix(
  `count_over_time({env="production", app="auth"} |= "request an authentication on"[5m])`,
  {
    limit: 400
  }
);
console.log(logs);

Which should provide you with what you want (at least, I hope). Here is an example from my own Grafana setup:

image

I will soon release a new major version with queryRangeMatrix.

fraxken commented 4 months ago

https://github.com/MyUnisoft/loki/releases/tag/v4.0.0

sempex commented 4 months ago

@fraxken Thank you so much for the new Version! We are currently building shiper a PaaS platform to deploy stuff and are using your library to display analytics. So if you ever feel the need to deploy something hit me up on discord and I will provide you with a free subscription. Thanks a lot again!