diff --git a/lib/cache.py b/lib/cache.py index 4e4d809..e0da115 100644 --- a/lib/cache.py +++ b/lib/cache.py @@ -2,7 +2,9 @@ import json from typing import Any, Optional from lib.helpers import get_environment_setting +from lib.telemetry import OpenTelemetryExporter +OPEN_TELEMETRY_EXPORTER = OpenTelemetryExporter(service_name="QueueWorkerService", local_debug=False) REDIS_URL = get_environment_setting("REDIS_URL") DEFAULT_TTL = int(get_environment_setting("CACHE_DEFAULT_TTL") or 24*60*60) CACHE_PREFIX = "presto_media_cache:" @@ -36,7 +38,9 @@ def get_cached_result(content_hash: str, reset_ttl: bool = True, ttl: int = DEFA if cached_result is not None: if reset_ttl: client.expire(CACHE_PREFIX+content_hash, ttl) - return json.loads(cached_result) + response = json.loads(cached_result) + OPEN_TELEMETRY_EXPORTER.log_execution_status("cache_hit_response", "cache_hit_response") + return response return None @staticmethod @@ -52,3 +56,4 @@ def set_cached_result(content_hash: str, result: Any, ttl: int = DEFAULT_TTL) -> if content_hash: client = Cache.get_client() client.setex(CACHE_PREFIX+content_hash, ttl, json.dumps(result)) + OPEN_TELEMETRY_EXPORTER.log_execution_status("cache_miss_response", "cache_hit_response") diff --git a/lib/model/model.py b/lib/model/model.py index 8472193..7a0a87d 100644 --- a/lib/model/model.py +++ b/lib/model/model.py @@ -8,6 +8,7 @@ from lib.helpers import get_class from lib import schemas from lib.cache import Cache + class Model(ABC): BATCH_SIZE = 1 def __init__(self): diff --git a/lib/telemetry.py b/lib/telemetry.py index 1460082..40916fe 100644 --- a/lib/telemetry.py +++ b/lib/telemetry.py @@ -75,6 +75,16 @@ def __init__(self, service_name: str, local_debug=False) -> None: unit="s", description="Errored Message Response" ) + self.cache_hit_response = self.meter.create_counter( + name="cache_hit_response", + unit="s", + description="Returned cached response" + ) + self.cache_miss_response = self.meter.create_counter( + name="cache_miss_response", + unit="s", + description="Returned non-cached response" + ) def log_execution_time(self, func_name: str, execution_time: float): env_name = os.getenv("DEPLOY_ENV", "development")