Skip to content

Commit 7a2d23b

Browse files
scripts: add set log level script
1 parent a109ad6 commit 7a2d23b

File tree

1 file changed

+132
-0
lines changed

1 file changed

+132
-0
lines changed

scripts/set_log_level.py

Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
import argparse
2+
import sys
3+
from typing import List
4+
import requests
5+
import subprocess, signal, socket
6+
7+
8+
def parse_args(args: List[str]) -> argparse.Namespace:
9+
parser = argparse.ArgumentParser(description="Set the log level for a crate")
10+
parser.add_argument(
11+
"--crate_name", type=str, help="The name of the crate to set the log level for"
12+
)
13+
parser.add_argument(
14+
"--log_level", type=str, help="The log level to set for the crate"
15+
)
16+
parser.add_argument(
17+
"--pod_name",
18+
type=str,
19+
default="",
20+
help="Optional Kubernetes pod name to port-forward to",
21+
)
22+
23+
parser.add_argument(
24+
"--local_port",
25+
type=int,
26+
default=8082,
27+
help="Local port to bind the port-forward to (defaults to 8082)",
28+
)
29+
30+
parser.add_argument(
31+
"--monitoring_port",
32+
type=int,
33+
default=8082,
34+
help="Monitoring port exposed by the pod (defaults to 8082)",
35+
)
36+
37+
parser.add_argument(
38+
"--method",
39+
type=str,
40+
choices=["get", "post"],
41+
default="post",
42+
help="HTTP method to use: 'get' to read current log level, 'post' to set a log level",
43+
)
44+
return parser.parse_args(args)
45+
46+
47+
def main():
48+
49+
args = parse_args(sys.argv[1:])
50+
51+
# If a pod name is supplied, establish a port-forward before making the request
52+
port_forward_proc = None
53+
54+
target_port = args.monitoring_port
55+
base_port = args.local_port if args.pod_name else target_port
56+
57+
if args.pod_name:
58+
cmd = [
59+
"kubectl",
60+
"port-forward",
61+
args.pod_name,
62+
f"{args.local_port}:{args.monitoring_port}",
63+
]
64+
65+
print("Starting port-forward:", " ".join(cmd))
66+
port_forward_proc = subprocess.Popen(
67+
cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE
68+
)
69+
70+
try:
71+
with socket.create_connection(("localhost", args.local_port), timeout=1):
72+
print(
73+
f"Port-forward to {args.pod_name}:{args.monitoring_port} is ready on localhost:{args.local_port}"
74+
)
75+
except OSError as e:
76+
print(
77+
f"Unexpected error: port-forward appears up but connection failed. Details: {e}"
78+
)
79+
80+
try:
81+
if args.method == "get":
82+
full_url = f"http://localhost:{base_port}/monitoring/logLevel"
83+
print(f"Fetching current log level from {full_url}")
84+
response = requests.get(full_url, timeout=5)
85+
86+
if response.status_code != 200:
87+
print(
88+
f"Failed to fetch log level: {response.status_code} {response.text}"
89+
)
90+
sys.exit(1)
91+
92+
print("Current log level response:\n", response.text)
93+
elif args.method == "post":
94+
# Validate required arguments
95+
if not args.crate_name or not args.log_level:
96+
print("--crate_name and --log_level are required when --method=post")
97+
sys.exit(1)
98+
99+
base_url = f"http://localhost:{base_port}/monitoring/setLogLevel"
100+
full_url = f"{base_url}/{args.crate_name}/{args.log_level}"
101+
102+
print(
103+
f"Setting log level for {args.crate_name} to {args.log_level} at {full_url}"
104+
)
105+
106+
response = requests.post(full_url, timeout=5)
107+
108+
if response.status_code != 200:
109+
print(
110+
f"Failed to set log level for {args.crate_name} to {args.log_level}: {response.text}"
111+
)
112+
sys.exit(1)
113+
114+
print(
115+
f"Successfully set log level for {args.crate_name} to {args.log_level}"
116+
)
117+
else:
118+
print(f"Unsupported method {args.method}. Use 'get' or 'post'.")
119+
sys.exit(1)
120+
finally:
121+
# Clean up the port-forward process if we started one
122+
if port_forward_proc:
123+
port_forward_proc.send_signal(signal.SIGINT)
124+
try:
125+
port_forward_proc.wait(timeout=5)
126+
except subprocess.TimeoutExpired:
127+
port_forward_proc.kill()
128+
port_forward_proc.wait()
129+
130+
131+
if __name__ == "__main__":
132+
main()

0 commit comments

Comments
 (0)