Skip to content
This repository was archived by the owner on Jun 13, 2023. It is now read-only.

Commit dbaf1aa

Browse files
author
Gal Bashan
authored
fix(tornado.py): fix "tornado mis-handle of 404s (#314)" (#316)
This reverts commit 5b300ab.
1 parent 5b300ab commit dbaf1aa

7 files changed

Lines changed: 21 additions & 75 deletions

File tree

epsagon/events/redis.py

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@
88

99
from ..event import BaseEvent
1010
from ..trace import trace_factory
11-
from ..utils import add_data_if_needed
12-
1311

1412
MAX_VALUE_SIZE = 1024
1513
MAX_CMD_PIPELINE = 10
@@ -112,13 +110,6 @@ def __init__(self, wrapped, instance, args, kwargs, start_time, response,
112110
self.resource['operation'] = operation
113111
self.resource['metadata']['Redis Key'] = key
114112

115-
if response:
116-
add_data_if_needed(
117-
self.resource['metadata'],
118-
'redis.response',
119-
response
120-
)
121-
122113

123114
class RedisMultiExecutionEvent(BaseRedisEvent):
124115
"""

epsagon/events/urllib3.py

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -113,20 +113,16 @@ def update_response(self, response):
113113
'response_headers',
114114
headers
115115
)
116-
response_body = getattr(response, 'data', None)
117-
if not response_body and getattr(response, 'peek', None):
118-
response_body = response.peek()
116+
response_body = response.peek()
119117
if isinstance(response_body, bytes):
120118
try:
121119
response_body = response_body.decode('utf-8')
122120
except UnicodeDecodeError:
123121
response_body = str(response_body)
124-
if response_body:
125-
add_data_if_needed(
126-
self.resource['metadata'],
127-
'response_body',
128-
response_body
129-
)
122+
add_data_if_needed(
123+
self.resource['metadata'],
124+
'response_body',
125+
response_body)
130126

131127
# Detect errors based on status code
132128
if response.status >= HTTP_ERR_CODE:

epsagon/modules/logging.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
import wrapt
1212

1313
from ..trace import trace_factory
14-
from ..utils import print_debug, get_trace_log_config
14+
from ..utils import print_debug
1515

1616
LOGGING_FUNCTIONS = (
1717
'info',
@@ -99,7 +99,7 @@ def patch():
9999
wrapt.wrap_function_wrapper('logging', 'Logger.exception', _wrapper)
100100

101101
# Instrument logging with Epsagon trace ID
102-
if get_trace_log_config():
102+
if trace_factory.is_logging_tracing_enabled():
103103
wrapt.wrap_function_wrapper(
104104
'logging',
105105
'Logger.log',

epsagon/modules/redis.py

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -44,12 +44,6 @@ def patch():
4444
'Redis.execute_command',
4545
_single_wrapper
4646
)
47-
# Version < 3.0
48-
wrapt.wrap_function_wrapper(
49-
'redis',
50-
'StrictRedis.execute_command',
51-
_single_wrapper
52-
)
5347
wrapt.wrap_function_wrapper(
5448
'redis.client',
5549
'Pipeline.immediate_execute_command',

epsagon/modules/tornado.py

Lines changed: 4 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
import epsagon.trace
1212
from epsagon.runners.tornado import TornadoRunner
1313
from epsagon.http_filters import ignore_request, is_ignored_endpoint
14-
from epsagon.utils import collect_container_metadata, print_debug
14+
from epsagon.utils import collect_container_metadata
1515

1616
TORNADO_TRACE_ID = 'epsagon_tornado_trace_key'
1717

@@ -31,7 +31,6 @@ def before_request(cls, wrapped, instance, args, kwargs):
3131
:param args: wrapt's args
3232
:param kwargs: wrapt's kwargs
3333
"""
34-
print_debug('before_request Tornado request')
3534
try:
3635
ignored = ignore_request('', instance.request.path)
3736
if not ignored and not is_ignored_endpoint(instance.request.path):
@@ -49,7 +48,6 @@ def before_request(cls, wrapped, instance, args, kwargs):
4948
)
5049

5150
trace.set_runner(cls.RUNNERS[unique_id])
52-
print_debug('Created Tornado Runner')
5351

5452
# Collect metadata in case this is a container.
5553
collect_container_metadata(
@@ -71,18 +69,9 @@ def after_request(cls, wrapped, instance, args, kwargs):
7169
:param args: wrapt's args
7270
:param kwargs: wrapt's kwargs
7371
"""
74-
print_debug('after_request Tornado request')
75-
response_body = None
76-
try:
77-
response_body = getattr(instance, '_write_buffer', None)
78-
if response_body and isinstance(response_body, list):
79-
response_body = b''.join(response_body)
80-
except Exception as instrumentation_exception: # pylint: disable=W0703
81-
epsagon.trace.trace_factory.add_exception(
82-
instrumentation_exception,
83-
traceback.format_exc()
84-
)
85-
72+
response_body = getattr(instance, '_write_buffer', None)
73+
if response_body and isinstance(response_body, list):
74+
response_body = b''.join(response_body)
8675
res = wrapped(*args, **kwargs)
8776
try:
8877
unique_id = getattr(instance, TORNADO_TRACE_ID, None)
@@ -91,11 +80,6 @@ def after_request(cls, wrapped, instance, args, kwargs):
9180

9281
tornado_runner = cls.RUNNERS.pop(unique_id)
9382

94-
# Ignoring 404s
95-
if getattr(instance, '_status_code', None) == 404:
96-
print_debug('Ignoring 404 Tornado request')
97-
return res
98-
9983
trace = epsagon.trace.trace_factory.switch_active_trace(
10084
unique_id
10185
)
@@ -127,7 +111,6 @@ def collect_exception(cls, wrapped, instance, args, kwargs):
127111
:param args: wrapt's args
128112
:param kwargs: wrapt's kwargs
129113
"""
130-
print_debug('collect_exception Tornado request')
131114
try:
132115
unique_id = getattr(instance, TORNADO_TRACE_ID, None)
133116

epsagon/runners/tornado.py

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
from __future__ import absolute_import
77
import uuid
88
from ..event import BaseEvent
9-
from ..utils import add_data_if_needed, print_debug
9+
from ..utils import add_data_if_needed
1010
from ..constants import EPSAGON_HEADER_TITLE
1111

1212
MAX_PAYLOAD_BYTES = 2000
@@ -69,19 +69,6 @@ def __init__(self, start_time, request):
6969
request_headers
7070
)
7171

72-
try:
73-
if request.body:
74-
body = request.body
75-
if isinstance(body, bytes):
76-
body = body.decode('utf-8')
77-
add_data_if_needed(
78-
self.resource['metadata'],
79-
'Request Data',
80-
body
81-
)
82-
except Exception as exception: # pylint: disable=broad-except
83-
print_debug('Could not extract body: {}'.format(exception))
84-
8572
def update_response(self, response, response_body=None):
8673
"""
8774
Adds response data to event.

epsagon/utils.py

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -95,19 +95,6 @@ def get_tc_url(use_ssl):
9595
return TRACE_COLLECTOR_URL.format(protocol=protocol, region=REGION)
9696

9797

98-
def get_trace_log_config():
99-
"""Returns the trace log correlation configuration"""
100-
# In case we're running on AWS Lambda, logging correlation is disabled
101-
if is_lambda_env():
102-
return False
103-
104-
# If EPSAGON_LOGGING_TRACING_ENABLED exists as an env var - use it
105-
if os.getenv('EPSAGON_LOGGING_TRACING_ENABLED'):
106-
return os.getenv('EPSAGON_LOGGING_TRACING_ENABLED').upper() == 'TRUE'
107-
108-
return True
109-
110-
11198
def init(
11299
token='',
113100
app_name='Application',
@@ -187,7 +174,15 @@ def init(
187174
if os.getenv('EPSAGON_METADATA'):
188175
metadata_only = (os.getenv('EPSAGON_METADATA') or '').upper() == 'TRUE'
189176

190-
logging_tracing_enabled = get_trace_log_config()
177+
# If EPSAGON_LOGGING_TRACING_ENABLED exists as an env var - use it
178+
if os.getenv('EPSAGON_LOGGING_TRACING_ENABLED'):
179+
logging_tracing_enabled = (
180+
os.getenv('EPSAGON_LOGGING_TRACING_ENABLED') or ''
181+
).upper() == 'TRUE'
182+
183+
# In case we're running on AWS Lambda, logging correlation is disabled
184+
if is_lambda_env():
185+
logging_tracing_enabled = False
191186

192187
if os.getenv('EPSAGON_SAMPLE_RATE'):
193188
sample_rate = float(os.getenv('EPSAGON_SAMPLE_RATE'))

0 commit comments

Comments
 (0)