mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
fixed py code indentation (#80966)
* fixed py code indentation * pretty
This commit is contained in:
parent
0c9265f59b
commit
a7e9408433
@ -201,23 +201,22 @@ This optional step uses a python script to generate the sample logs used in this
|
|||||||
|
|
||||||
```
|
```
|
||||||
#!/bin/env python3
|
#!/bin/env python3
|
||||||
|
```
|
||||||
|
|
||||||
import datetime
|
import datetime
|
||||||
import math
|
import math
|
||||||
import random
|
import random
|
||||||
import sys
|
import sys
|
||||||
import time
|
import time
|
||||||
|
|
||||||
|
|
||||||
# Simulation parameters
|
|
||||||
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)
|
||||||
@ -234,6 +233,7 @@ This optional step uses a python script to generate the sample logs used in this
|
|||||||
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()
|
||||||
|
|
||||||
```
|
```
|
||||||
|
|
||||||
1. Give the script executable permissions.
|
1. Give the script executable permissions.
|
||||||
@ -241,8 +241,10 @@ This optional step uses a python script to generate the sample logs used in this
|
|||||||
In a terminal window on linux-based systems run the command:
|
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.
|
||||||
|
|
||||||
@ -290,23 +292,29 @@ 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
|
||||||
|
|
||||||
```
|
```
|
||||||
|
|
||||||
**Windows Powershell**
|
**Windows Powershell**
|
||||||
|
|
||||||
```
|
```
|
||||||
|
|
||||||
$client = new-object System.Net.WebClient
|
$client = new-object System.Net.WebClient
|
||||||
$client.DownloadFile("https://raw.githubusercontent.com/grafana/loki/v2.8.0/production/docker-compose.yaml",
|
$client.DownloadFile("https://raw.githubusercontent.com/grafana/loki/v2.8.0/production/docker-compose.yaml",
|
||||||
"C:\Users\$Env:UserName\Desktop\docker-compose.yaml")
|
"C:\Users\$Env:UserName\Desktop\docker-compose.yaml")
|
||||||
#downloads the file to the Desktop
|
#downloads the file to the Desktop
|
||||||
|
|
||||||
```
|
```
|
||||||
|
|
||||||
1. Run the container
|
1. Run the container
|
||||||
|
|
||||||
```
|
```
|
||||||
|
|
||||||
docker compose up -d
|
docker compose up -d
|
||||||
|
|
||||||
```
|
```
|
||||||
|
|
||||||
1. Create and edit a python file that will generate logs.
|
1. Create and edit a python file that will generate logs.
|
||||||
@ -314,18 +322,23 @@ that generates the sample logs used in this tutorial to create alerts.
|
|||||||
**Bash**
|
**Bash**
|
||||||
|
|
||||||
```
|
```
|
||||||
|
|
||||||
touch web-server-logs-simulator.py && nano web-server-logs-simulator.py
|
touch web-server-logs-simulator.py && nano web-server-logs-simulator.py
|
||||||
|
|
||||||
```
|
```
|
||||||
|
|
||||||
**Windows Powershell**
|
**Windows Powershell**
|
||||||
|
|
||||||
```
|
```
|
||||||
|
|
||||||
New-Item web-server-logs-simulator.py ; notepad web-server-logs-simulator.py
|
New-Item web-server-logs-simulator.py ; notepad web-server-logs-simulator.py
|
||||||
|
|
||||||
```
|
```
|
||||||
|
|
||||||
1. Paste the following code into the file
|
1. Paste the following code into the file
|
||||||
|
|
||||||
```
|
```
|
||||||
|
|
||||||
#!/bin/env python3
|
#!/bin/env python3
|
||||||
|
|
||||||
import datetime
|
import datetime
|
||||||
@ -334,17 +347,16 @@ that generates the sample logs used in this tutorial to create alerts.
|
|||||||
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:
|
||||||
@ -360,6 +372,7 @@ that generates the sample logs used in this tutorial to create alerts.
|
|||||||
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()
|
||||||
|
|
||||||
```
|
```
|
||||||
|
|
||||||
1. Execute the log-generating python script.
|
1. Execute the log-generating python script.
|
||||||
@ -367,7 +380,9 @@ that generates the sample logs used in this tutorial to create alerts.
|
|||||||
In a terminal window on linux-based systems run the command:
|
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
|
||||||
|
|
||||||
```
|
```
|
||||||
|
|
||||||
- Use `tee` to direct the script output to the console and the specified file path. For example, if promtail is
|
- Use `tee` to direct the script output to the console and the specified file path. For example, if promtail is
|
||||||
@ -376,9 +391,11 @@ that generates the sample logs used in this tutorial to create alerts.
|
|||||||
- To avoid running the script with elevated permissions, create the log file manually and change the permissions for the output file only.
|
- To avoid running the script with elevated permissions, create the log file manually and change the permissions for the output file only.
|
||||||
|
|
||||||
```
|
```
|
||||||
|
|
||||||
sudo touch /var/log/web_requests.log
|
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**
|
||||||
@ -386,7 +403,9 @@ that generates the sample logs used in this tutorial to create alerts.
|
|||||||
Run Powershell as administrator
|
Run Powershell as administrator
|
||||||
|
|
||||||
```
|
```
|
||||||
|
|
||||||
python ./web-server-logs-simulator.py | Tee-Object "C:\ProgramFiles\GrafanaLabs\grafana\var\log\web_requests.log"
|
python ./web-server-logs-simulator.py | Tee-Object "C:\ProgramFiles\GrafanaLabs\grafana\var\log\web_requests.log"
|
||||||
|
|
||||||
```
|
```
|
||||||
|
|
||||||
1. Verify that the logs are showing up in Grafana’s Explore view:
|
1. Verify that the logs are showing up in Grafana’s Explore view:
|
||||||
@ -406,3 +425,4 @@ 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…
Reference in New Issue
Block a user