grafana-loki

How to sum certain values from Grafana-Loki logs?


I have this log line:

Successfully encrypted 189322 bytes for upload req_id=MediaUpload
Successfully encrypted 189322 bytes for upload req_id=MediaUpload
Successfully encrypted 492346 bytes for upload req_id=MediaUpload

There's a way to sum the bytes of the matching query log lines? Per example, by the those logs I would like to have a summed value of 870990 bytes or 0.87099 MB.

Is that possible?


Solution

  • Sure you can. Check this out.

    I've used the pattern parser to extract the bytes as a number out of your log lines.

    Then you can run a range query on top of that:

    Eg.

    sum by (app) 
    (sum_over_time(
    {app="your-app"}
    | pattern `Successfully encrypted <byte_size> bytes for upload req_id=<_>`
    | unwrap byte_size 
    | __error__="" [$__interval]
    ))
    

    you can change $__interval based on your needs.