Average response time by path in loki logs

1.9k Views Asked by At

I have stream of http logs json via Loki that look like:

2022-11-30 16:18:46 {"message":{"duration":"8.37ms","env":"dev","path":"/rest/path1","status":200}} 
2022-11-30 16:18:46 {"message":{"duration":"112.32ms","env":"dev","path":"/rest/path2","status":200}}   
2022-11-30 16:18:46 {"message":{"duration":"32.37ms","env":"dev","path":"/rest/path1","status":200}} 
2022-11-30 16:18:46 {"message":{"duration":"21.337ms","env":"dev","path":"/rest/path3","status":200}}

I'd like to display average response time by path.

I am able convert this data in to table with grafana transformation. and then looking to for average of max duration time based on path.

1

There are 1 best solutions below

0
On

have you tried looking at range queries? First you would need to extract the duration. See how I did it here using the LogQL Analyzer.

Then you can use a range query with avg_over_time.

You would end up with a query like this:

avg_over_time(
{app="your-app} 
| pattern `<date> <time> <json_content>` 
| line_format "{{.json_content}}" 
| json
| unwrap message_duration | __error__=""[1m]) 
by (message_path)

Hope this helps :).