Skip to content

Commit

Permalink
Enable Sentry Caches and Queries pages
Browse files Browse the repository at this point in the history
  • Loading branch information
Ouziel committed Jul 29, 2024
1 parent b67e9ca commit 3f037a6
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 8 deletions.
17 changes: 13 additions & 4 deletions counterparty-core/counterpartycore/lib/api/api_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
from flask_httpauth import HTTPBasicAuth
from sentry_sdk import capture_exception
from sentry_sdk import configure_scope as configure_sentry_scope
from sentry_sdk import start_span as start_sentry_span
from werkzeug.serving import make_server

multiprocessing.set_start_method("spawn", force=True)
Expand Down Expand Up @@ -223,9 +224,16 @@ def execute_api_function(db, rule, route, function_args):
) and not request.path.startswith("/v2/blocks/last"):
cache_key = request.url

if cache_key in BLOCK_CACHE:
result = BLOCK_CACHE[cache_key]
else:
with start_sentry_span(op="cache.get") as sentry_get_span:
sentry_get_span.set_data("cache.key", cache_key)
if cache_key in BLOCK_CACHE:
result = BLOCK_CACHE[cache_key]
sentry_get_span.set_data("cache.hit", True)
return result
else:
sentry_get_span.set_data("cache.hit", False)

with start_sentry_span(op="cache.put") as sentry_put_span:
if function_needs_db(route["function"]):
result = route["function"](db, **function_args)
else:
Expand All @@ -236,11 +244,12 @@ def execute_api_function(db, rule, route, function_args):
and route["function"].__name__ != "redirect_to_api_v1"
and not request.path.startswith("/v2/mempool/")
):
sentry_put_span.set_data("cache.key", cache_key)
BLOCK_CACHE[cache_key] = result
if len(BLOCK_CACHE) > MAX_BLOCK_CACHE_SIZE:
BLOCK_CACHE.popitem(last=False)

return result
return result


def get_transaction_name(rule):
Expand Down
13 changes: 9 additions & 4 deletions counterparty-core/counterpartycore/lib/api/queries.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from counterpartycore.lib import config
from counterpartycore.lib.api.util import divide
from flask import request
from sentry_sdk import start_span as start_sentry_span

OrderStatus = Literal["all", "open", "expired", "filled", "cancelled"]
OrderMatchesStatus = Literal["all", "pending", "completed", "expired"]
Expand Down Expand Up @@ -255,11 +256,15 @@ def select_rows(
query = f"{query} OFFSET ?"
bindings.append(offset)

cursor.execute(query, bindings)
result = cursor.fetchall()
with start_sentry_span(op="db.sql.execute", description=query) as sql_span:
sql_span.set_tag("db.system", "sqlite3")
cursor.execute(query, bindings)
result = cursor.fetchall()

cursor.execute(query_count, bindings_count)
result_count = cursor.fetchone()["count"]
with start_sentry_span(op="db.sql.execute", description=query_count) as sql_span:
sql_span.set_tag("db.system", "sqlite3")
cursor.execute(query_count, bindings_count)
result_count = cursor.fetchone()["count"]

if result and len(result) > limit:
next_cursor = result[-1][cursor_field]
Expand Down

0 comments on commit 3f037a6

Please sign in to comment.