-
Notifications
You must be signed in to change notification settings - Fork 785
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
151 additions
and
88 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
# this file is for debugging purposes | ||
# its code written to profile the SQL queries | ||
|
||
import inspect | ||
import logging | ||
|
||
# it basically log the SQL queries and the time it took to execute them | ||
# in a file called sqlalchemy_queries.log | ||
import os | ||
import threading | ||
import time | ||
from logging.handlers import RotatingFileHandler | ||
|
||
from sqlalchemy import event | ||
from sqlalchemy.engine import Engine | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
if os.environ.get("DEBUG_SQL", False): | ||
logger.setLevel(logging.DEBUG) | ||
# Configure logging | ||
logger = logging.getLogger("profiler") | ||
logger.setLevel(logging.INFO) | ||
file_handler = RotatingFileHandler( | ||
"sqlalchemy_queries.log", maxBytes=1024 * 1024 * 5, backupCount=5, mode="a" | ||
) | ||
file_handler.setLevel(logging.INFO) | ||
# Create a formatter and set it to the handler | ||
formatter = logging.Formatter("%(asctime)s - %(message)s") | ||
file_handler.setFormatter(formatter) | ||
# Add the handler to the logger | ||
logger.addHandler(file_handler) | ||
# Prevent the logger from propagating messages to the root logger | ||
logger.propagate = False | ||
|
||
def get_callee(): | ||
try: | ||
# Inspect the stack and find the callee outside of this module | ||
stack = inspect.stack() | ||
for frame_info in stack: | ||
# Inspecting the stack frame to find the first caller outside of this script | ||
# You might need to adjust the conditions based on your project structure | ||
if ( | ||
frame_info.function | ||
not in [ | ||
"get_callee", | ||
"after_cursor_execute", | ||
"before_cursor_execute", | ||
] | ||
and __file__ in frame_info.filename | ||
): | ||
return f"{frame_info.function} in {frame_info.filename}:{frame_info.lineno}" | ||
except Exception: | ||
return "Unknown callee" | ||
return "Callee not found" | ||
|
||
# Function to track query start time | ||
@event.listens_for(Engine, "before_cursor_execute") | ||
def before_cursor_execute( | ||
conn, cursor, statement, parameters, context, executemany | ||
): | ||
context._query_start_time = time.time() | ||
|
||
# Function to log the query and execution time | ||
@event.listens_for(Engine, "after_cursor_execute") | ||
def after_cursor_execute(conn, cursor, statement, parameters, context, executemany): | ||
total = time.time() - context._query_start_time | ||
callee = get_callee() | ||
thread_name = threading.current_thread().name | ||
logger.critical( | ||
f"Thread: {thread_name}, Callee: {callee}, Query: {statement}, Parameters: {parameters}, Time: {total:.5f} seconds" | ||
) |
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
"{""sql"": ""((source = :source_1 and annotations.summary like :annotations.summary_1) and (source = :source_2 and labels.monitor = :labels.monitor_1))"", ""params"": {""source_1"": ""prometheus"", ""annotations.summary_1"": ""%latency%"", ""source_2"": ""grafana"", ""labels.monitor_1"": ""server1""}}",[],f9e5ffd0cfc14757a8e566f9fe7cbd82,keep,"Rule Name","(source == ""prometheus"" && annotations.summary.contains(""latency"")) && (source == ""grafana"" && labels.monitor == ""server1"")",600,keep,"2024-02-01 14:18:20.002416",,,, | ||
"{}","[""labels.instance""]",3d8e3773ab224604bfa58c930bcc5e5a,keep,"CPU (group by labels.instance)","(labels.alertname.contains(""cpu""))",600,keep,"2024-02-01 14:30:13.830655",,,"CPU usage exceeded on {{ group_attributes.num_of_alerts }} pods of {{ labels.instance }} || {{ group_attributes.start_time }} | {{ group_attributes.last_update_time }}", | ||
"{}",[],f4e9ffacc26c4a079afec0e0e149bbbe,keep,"CPU (no grouping)","(labels.alertname.contains(""cpu""))",600,keep,"2024-02-01 14:30:13.837035",,,, | ||
"{}","[""labels.queue""]",632fae98b12c4f9da996dedb3abd0b92,keep,"MQ (group by labels.queue)","(name == ""mq_third_full"")",600,keep,"2024-02-01 14:30:13.839077",,,"The {{ labels.queue }} is more than third full on {{ group_attributes.num_of_alerts }} queue managers | {{ group_attributes.start_time }} || {{ group_attributes.last_update_time }}", | ||
"{}","[""labels.instance""]",5c043777828c42af97697bbaac135c81,keep,"CPU (group by labels.instance)","(labels.alertname.contains(""cpu""))",600,keep,"2024-02-01 14:32:54.560574",,,"CPU usage exceeded on {{ group_attributes.num_of_alerts }} pods of {{ labels.instance }} || {{ group_attributes.start_time }} | {{ group_attributes.last_update_time }}", | ||
"{}",[],923d63ba385a4f719c5789087eb4a921,keep,"CPU (no grouping)","(labels.alertname.contains(""cpu""))",600,keep,"2024-02-01 14:32:54.567197",,,, | ||
"{}","[""labels.queue""]",dd38939c47764a97ac420a71f1287585,keep,"MQ (group by labels.queue)","(name == ""mq_third_full"")",600,keep,"2024-02-01 14:32:54.569420",,,"The {{ labels.queue }} is more than third full on {{ group_attributes.num_of_alerts }} queue managers | {{ group_attributes.start_time }} || {{ group_attributes.last_update_time }}", |