Last active
January 19, 2021 13:28
-
-
Save imposibrus/3aa1c4e36f08b0a8d37473d0b9fa0abd to your computer and use it in GitHub Desktop.
Parse Traefik logs, extract response time and calc percentiles
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export REQUEST_NAME='getMessages'; \ | |
docker-compose logs traefik | grep $REQUEST_NAME | grep 'HTTP/1' | \ | |
awk -F ' - - ' '{print $2}' | awk '{print $13}' > "/tmp/logs_$REQUEST_NAME.log" && \ | |
sort -V "/tmp/logs_$REQUEST_NAME.log" | python3 /tmp/percentiles.py | |
# exmaple output from `docker-compose logs`: | |
# traefik | 192.168.1.100 - - [19/Jan/2021:08:30:56 +0000] "POST /chat/getUsersByIds HTTP/1.0" 200 60 "-" "-" 170636628 "middleware@docker" "h2c://10.27.3.25:80" 27ms | |
# |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sys, re, math; | |
def percentile(data, percentile): | |
size = len(data) | |
return sorted(data)[int(math.ceil((size * percentile) / 100)) - 1] | |
lines = [] | |
for line in sys.stdin: | |
lines.append(int(re.sub("\D", "", line))) | |
p5 = percentile(lines, 5) | |
p25 = percentile(lines, 25) | |
p50 = percentile(lines, 50) | |
p75 = percentile(lines, 75) | |
p95 = percentile(lines, 95) | |
print('p5 = {0}, p25 = {1}, p50 = {2}, p75 = {3}, p95 = {4}'.format(p5, p25, p50, p75, p95)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment