fixed py code indentation pt.2 (#80975)

* fixed py code indentation

* pretty

* 2nd code snippet update

* pretty

* pretty
pull/80862/head^2
tonypowa 2 years ago committed by GitHub
parent ef50fe9ebb
commit 3a7b745278
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
  1. 51
      docs/sources/tutorials/create-alerts-with-logs/index.md

@ -199,9 +199,9 @@ This optional step uses a python script to generate the sample logs used in this
1. Install Python3 on your local machine if needed. 1. Install Python3 on your local machine if needed.
1. Copy the python script below and paste it into a new file on your local machine. 1. Copy the python script below and paste it into a new file on your local machine.
``` ```
#!/bin/env python3
``` #!/bin/env python3
import datetime import datetime
import math import math
@ -209,12 +209,14 @@ import random
import sys import sys
import time import time
requests_per_second = 2 requests_per_second = 2
failure_rate = 0.05 failure_rate = 0.05
get_post_ratio = 0.9 get_post_ratio = 0.9
get_average_duration_ms = 500 get_average_duration_ms = 500
post_average_duration_ms = 2000 post_average_duration_ms = 2000
while True: while True:
# Exponential distribution random value of average 1/lines_per_second. # Exponential distribution random value of average 1/lines_per_second.
@ -244,7 +246,8 @@ In a terminal window on linux-based systems run the command:
chmod 755 ./web-server-logs-simulator.py chmod 755 ./web-server-logs-simulator.py
````
```
1. Run the script. 1. Run the script.
@ -292,7 +295,7 @@ that generates the sample logs used in this tutorial to create alerts.
**Bash** **Bash**
```` ```
wget https://raw.githubusercontent.com/grafana/loki/v2.8.0/production/docker-compose.yaml -O docker-compose.yaml wget https://raw.githubusercontent.com/grafana/loki/v2.8.0/production/docker-compose.yaml -O docker-compose.yaml
@ -347,31 +350,33 @@ import random
import sys import sys
import time import time
requests_per_second = 2 requests_per_second = 2
failure_rate = 0.05 failure_rate = 0.05
get_post_ratio = 0.9 get_post_ratio = 0.9
get_average_duration_ms = 500 get_average_duration_ms = 500
post_average_duration_ms = 2000 post_average_duration_ms = 2000
while True: while True:
# Exponential distribution random value of average 1/lines_per_second. # Exponential distribution random value of average 1/lines_per_second.
d = random.expovariate(requests_per_second)
d = random.expovariate(requests_per_second) time.sleep(d)
time.sleep(d) if random.random() < failure_rate:
if random.random() < failure_rate: status = "500"
status = "500" else:
else: status = "200"
status = "200" if random.random() < get_post_ratio:
if random.random() < get_post_ratio: method = "GET"
method = "GET" duration_ms = math.floor(random.expovariate(1/get_average_duration_ms))
duration_ms = math.floor(random.expovariate(1/get_average_duration_ms)) else:
else: method = "POST"
method = "POST" duration_ms = math.floor(random.expovariate(1/post_average_duration_ms))
duration_ms = math.floor(random.expovariate(1/post_average_duration_ms)) timestamp = datetime.datetime.now(tz=datetime.timezone.utc).isoformat()
timestamp = datetime.datetime.now(tz=datetime.timezone.utc).isoformat() print(f"{timestamp} level=info method={method} url=/ status={status} duration={duration_ms}ms")
print(f"{timestamp} level=info method={method} url=/ status={status} duration={duration_ms}ms") sys.stdout.flush()
sys.stdout.flush()
``` ```
@ -396,6 +401,7 @@ sudo touch /var/log/web_requests.log
chmod 755 /var/log/web_requests.log chmod 755 /var/log/web_requests.log
python3 ./web-server-logs-simulator.py | tee -a /var/log/web_requests.log python3 ./web-server-logs-simulator.py | tee -a /var/log/web_requests.log
``` ```
**Running on Windows** **Running on Windows**
@ -425,4 +431,3 @@ If you don't see the logs in Explore, check these things:
- If the file is empty, check that you followed the steps above to create the file and change the permissions. - If the file is empty, check that you followed the steps above to create the file and change the permissions.
- If the file exists, verify that promtail is running and check that it is configured correctly. - If the file exists, verify that promtail is running and check that it is configured correctly.
- In Grafana Explore, check that the time range is only for the last 5 minutes. - In Grafana Explore, check that the time range is only for the last 5 minutes.
```

Loading…
Cancel
Save