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

Commit 65be6d3

Browse files
authored
fix(fastapi): wrapper sync mode
- fastapi: Added wrapper sync mode - Removed aiohttp support - tornado: Fixed trace cleaning
1 parent dae687d commit 65be6d3

13 files changed

Lines changed: 268 additions & 485 deletions

File tree

README.md

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,6 @@ The following frameworks are supported by Epsagon:
195195
|[Django](#django) |`>=1.11` |<ul><li>- [x] </li></ul> |
196196
|[Flask](#flask) |`>=0.5` |<ul><li>- [x] </li></ul> |
197197
|[Tornado](#tornado) |`>=4.0` |<ul><li>- [x] </li></ul> |
198-
|[aiohttp](#aiohttp) |`>=3.0.0` |<ul><li>- [x] </li></ul> |
199198
|[fastapi](#fastapi) |`>=0.62.0` |<ul><li>- [x] </li></ul> |
200199
|[Celery](#celery) |`>=4.0.0` |<ul><li>- [x] </li></ul> |
201200
|[Azure Functions](#azure-functions) |`>=2.0.0` |<ul><li>- [ ] </li></ul> |
@@ -295,22 +294,6 @@ epsagon.init(
295294
)
296295
```
297296

298-
### aiohttp
299-
300-
Tracing aiohttp application can be done in two methods:
301-
1. [Auto-tracing](#auto-tracing) using the environment variable.
302-
2. Calling the SDK.
303-
304-
Calling the SDK is simple, and should be done in your main `py` file where the application is being initialized:
305-
```python
306-
import epsagon
307-
epsagon.init(
308-
token='<epsagon-token>',
309-
app_name='<app-name-stage>',
310-
metadata_only=False,
311-
)
312-
```
313-
314297
### fastapi
315298

316299
Tracing fastapi application can be done in two methods:

epsagon/modules/__init__.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
IGNORE_MODULES = ('__init__',)
1212
PYTHON_EXTENSIONS = ('.py', '.pyc')
1313
VERSION_DEPENDENCIES = {
14-
'aiohttp': (3, 5, 3),
1514
'fastapi': (3, 5, 3),
1615
}
1716

epsagon/modules/aiohttp.py

Lines changed: 0 additions & 42 deletions
This file was deleted.

epsagon/modules/tornado.py

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,8 @@ def after_request(cls, wrapped, instance, args, kwargs):
9494
)
9595

9696
res = wrapped(*args, **kwargs)
97+
trace = None
98+
is_trace_sent = False
9799
try:
98100
unique_id = getattr(instance, TORNADO_TRACE_ID, None)
99101
if not unique_id:
@@ -122,11 +124,11 @@ def after_request(cls, wrapped, instance, args, kwargs):
122124
if not ignored:
123125
tornado_runner.update_response(instance, response_body)
124126
epsagon.trace.trace_factory.send_traces(trace)
125-
except Exception as instrumentation_exception: # pylint: disable=W0703
126-
epsagon.trace.trace_factory.add_exception(
127-
instrumentation_exception,
128-
traceback.format_exc()
129-
)
127+
is_trace_sent = True
128+
except Exception: # pylint: disable=W0703
129+
if not is_trace_sent and trace:
130+
epsagon.trace.trace_factory.pop_trace(trace=trace)
131+
130132
return res
131133

132134
@classmethod
@@ -205,9 +207,9 @@ def thread_pool_submit(cls, func, _, args, kwargs):
205207
unique_id = None
206208

207209
try:
208-
unique_id = (
209-
epsagon.trace.trace_factory.get_or_create_trace().unique_id
210-
)
210+
trace = epsagon.trace.trace_factory.get_trace()
211+
if trace:
212+
unique_id = trace.unique_id
211213

212214
except Exception as instrumentation_exception: # pylint: disable=W0703
213215
epsagon.trace.trace_factory.add_exception(

epsagon/runners/aiohttp.py

Lines changed: 0 additions & 93 deletions
This file was deleted.

epsagon/trace.py

Lines changed: 1 addition & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
from epsagon.trace_transports import NoneTransport, HTTPTransport, LogTransport
2323
from .constants import (
2424
TIMEOUT_GRACE_TIME_MS,
25-
EPSAGON_MARKER,
2625
MAX_LABEL_SIZE,
2726
DEFAULT_SAMPLE_RATE,
2827
TRACE_URL_PREFIX,
@@ -96,7 +95,6 @@ def __init__(self):
9695
self.keys_to_ignore = None
9796
self.keys_to_allow = None
9897
self.use_single_trace = True
99-
self.use_async_tracer = False
10098
self.singleton_trace = None
10199
self.local_thread_to_unique_id = {}
102100
self.transport = NoneTransport()
@@ -209,13 +207,6 @@ def switch_to_multiple_traces(self):
209207
"""
210208
self.use_single_trace = False
211209

212-
def switch_to_async_tracer(self):
213-
"""
214-
Set the use_async_tracer flag to True.
215-
:return: None
216-
"""
217-
self.use_async_tracer = True
218-
219210
def _create_new_trace(self, unique_id=None):
220211
"""
221212
Creating new trace instance
@@ -264,66 +255,18 @@ def _get_thread_trace(self, should_create=False):
264255
self.traces[thread_id] = new_trace
265256
return self.traces[thread_id]
266257

267-
@staticmethod
268-
def _get_current_task():
269-
"""
270-
Gets the current asyncio task safely
271-
:return: The task.
272-
"""
273-
# Dynamic import since this is only valid in Python3+
274-
asyncio = __import__('asyncio')
275-
try:
276-
return asyncio.Task.current_task()
277-
except RuntimeError:
278-
return None
279-
280-
def _get_tracer_async_mode(self, should_create):
281-
"""
282-
Get trace assuming async tracer.
283-
:return: The trace.
284-
"""
285-
task = type(self)._get_current_task()
286-
if not task:
287-
return None
288-
289-
trace = getattr(task, EPSAGON_MARKER, None)
290-
if not trace and should_create:
291-
trace = self._create_new_trace()
292-
setattr(task, EPSAGON_MARKER, trace)
293-
return trace
294-
295-
def _pop_trace_async_mode(self):
296-
"""
297-
Pops the trace from the current task, assuming async tracer
298-
:return: The trace.
299-
"""
300-
task = type(self)._get_current_task()
301-
if not task:
302-
return None
303-
304-
trace = getattr(task, EPSAGON_MARKER, None)
305-
if trace: # can safely remove tracer from async task
306-
delattr(task, EPSAGON_MARKER)
307-
return trace
308-
309258
def _get_trace(self, unique_id=None, should_create=False):
310259
"""
311260
Get trace based on the tracing mode:
312261
single trace (use_single_trace=True) or
313-
multiple threads (use_single_trace=False) or
314-
async tasks (use_async_tracer=True)
262+
multiple threads (use_single_trace=False)
315263
316264
If should_create then creating a new trace if trace does not exist
317265
if use_single_trace is set to False, each thread will have
318266
it's own trace..
319-
if use_async_tracer is set to True, each asyncio.Task will be
320-
assigned with the trace.
321267
:return: The trace.
322268
"""
323269
with TraceFactory.LOCK:
324-
if self.use_async_tracer:
325-
return self._get_tracer_async_mode(should_create=should_create)
326-
327270
unique_id = self.get_thread_local_unique_id(unique_id)
328271
if unique_id:
329272
trace = (
@@ -378,11 +321,6 @@ def pop_trace(self, trace=None):
378321
:return: unique id
379322
"""
380323
with self.LOCK:
381-
if self.use_async_tracer:
382-
trace = self._pop_trace_async_mode()
383-
if trace:
384-
# async tracer found
385-
return trace
386324
if self.traces:
387325
trace = self.traces.pop(self.get_trace_identifier(trace), None)
388326
if not self.traces:

0 commit comments

Comments
 (0)