Skip to content

Cache obfuscated SQL on Python side #20499

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions datadog_checks_base/datadog_checks/base/utils/db/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import socket
import threading
import time
from hashlib import md5
from concurrent.futures.thread import ThreadPoolExecutor
from enum import Enum, auto
from ipaddress import IPv4Address
Expand Down Expand Up @@ -218,6 +219,7 @@ def default_json_event_encoding(o):
return o.decode('utf-8')
raise TypeError

obfuscate_cache = {}

def obfuscate_sql_with_metadata(query, options=None, replace_null_character=False):
"""
Expand All @@ -238,6 +240,14 @@ def obfuscate_sql_with_metadata(query, options=None, replace_null_character=Fals
# replace embedded null characters \x00 before obfuscating
query = query.replace('\x00', '')

cache_key = md5((query + options).encode('utf-8')).hexdigest()
# print(query)
if cache_key in obfuscate_cache:
# print("Cache hit")
# Return cached result if available
return obfuscate_cache[cache_key]

# print("Cache miss")
statement = datadog_agent.obfuscate_sql(query, options)
# The `obfuscate_sql` testing stub returns bytes, so we have to handle that here.
# The actual `obfuscate_sql` method in the agent's Go code returns a JSON string.
Expand All @@ -256,6 +266,8 @@ def obfuscate_sql_with_metadata(query, options=None, replace_null_character=Fals
tables = metadata.pop('tables_csv', None)
tables = [table.strip() for table in tables.split(',') if table != ''] if tables else None
statement_with_metadata['metadata']['tables'] = tables

obfuscate_cache[cache_key] = statement_with_metadata
return statement_with_metadata


Expand Down
7 changes: 4 additions & 3 deletions postgres/tests/test_statements.py
Original file line number Diff line number Diff line change
Expand Up @@ -968,10 +968,11 @@ def test_statement_metadata(
# Metrics will match to the normalized query signature
normalized_query_signature = 'ca85e8d659051b3a'

def obfuscate_sql(query, options=None):
if query.startswith('SELECT city FROM persons WHERE city'):
def obfuscate_sql(query_in, options=None):
if query == query_in:
print("test query found")
return json.dumps({'query': normalized_query, 'metadata': metadata})
return json.dumps({'query': query, 'metadata': metadata})
return json.dumps({'query': query_in, 'metadata': metadata})

check = integration_check(dbm_instance)
check._connect()
Expand Down
Loading