Skip to content

Commit

Permalink
Add logging for cache hit/miss
Browse files Browse the repository at this point in the history
  • Loading branch information
DGaffney committed Jun 24, 2024
1 parent b18e080 commit 3bb86db
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 1 deletion.
7 changes: 6 additions & 1 deletion lib/cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:"
Expand Down Expand Up @@ -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
Expand All @@ -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")
1 change: 1 addition & 0 deletions lib/model/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
10 changes: 10 additions & 0 deletions lib/telemetry.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down

0 comments on commit 3bb86db

Please sign in to comment.