diff --git a/counterparty-core/counterpartycore/lib/api/api_server.py b/counterparty-core/counterpartycore/lib/api/api_server.py index bad557ee4..593f3e80d 100644 --- a/counterparty-core/counterpartycore/lib/api/api_server.py +++ b/counterparty-core/counterpartycore/lib/api/api_server.py @@ -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) @@ -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: @@ -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): diff --git a/counterparty-core/counterpartycore/lib/api/queries.py b/counterparty-core/counterpartycore/lib/api/queries.py index 2281a14b9..653310b73 100644 --- a/counterparty-core/counterpartycore/lib/api/queries.py +++ b/counterparty-core/counterpartycore/lib/api/queries.py @@ -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"] @@ -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]