forked from MagicStack/uvloop
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implements a new tracing support for uvloop adding the pair methods `start_tracing` and `stop_tracing` that allows the user to start or stop the tracing. The user must provide a valid `uvloop.tracing.Tracer` implementation that would be used internally by uvloop to create spans, each span must also meet the `uvloop.tracing.Span` class contract. This POC only implements the creation of spans during underlying calls to the `getaddrinfo` function. This POC relates to the conversation in MagicStack#163.
- Loading branch information
Showing
10 changed files
with
211 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -230,3 +230,5 @@ include "request.pxd" | |
include "handles/udp.pxd" | ||
|
||
include "server.pxd" | ||
|
||
include "tracing.pxd" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
cdef class TracedContext: | ||
cdef: | ||
object _tracer | ||
object _span | ||
object _root_span | ||
|
||
cdef object start_span(self, name, tags=?) | ||
cdef object current_span(self) | ||
|
||
cdef TracedContext __traced_context() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import abc | ||
|
||
class Span(abc.ABC): | ||
|
||
@abc.abstractmethod | ||
def set_tag(self, key, value): | ||
"""Tag the span with an arbitrary key and value.""" | ||
|
||
@abc.abstractmethod | ||
def finish(self, finish_time=None): | ||
"""Indicate that the work represented by this span | ||
has been completed or terminated.""" | ||
|
||
@abc.abstractproperty | ||
def is_finished(self): | ||
"""Return True if the current span is already finished.""" | ||
|
||
|
||
class Tracer(abc.ABC): | ||
|
||
@abc.abstractmethod | ||
def start_span(self, name, parent_span): | ||
"""Start a new Span with a specific name. The parent of the span | ||
will be also passed as a paramter.""" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
from contextlib import contextmanager | ||
|
||
if PY37: | ||
import contextvars | ||
__traced_ctx = contextvars.ContextVar('__traced_ctx', default=None) | ||
else: | ||
__traced_ctx = None | ||
|
||
|
||
cdef class TracedContext: | ||
def __cinit__(self, tracer, root_span): | ||
self._tracer = tracer | ||
self._root_span = root_span | ||
self._span = None | ||
|
||
cdef object start_span(self, name, tags=None): | ||
parent_span = self._span if self._span else self._root_span | ||
span = self._tracer.start_span(name, parent_span) | ||
|
||
if tags: | ||
for key, value in tags.items(): | ||
span.set_tag(key, value) | ||
|
||
self._span = span | ||
return self._span | ||
|
||
cdef object current_span(self): | ||
return self._span | ||
|
||
|
||
cdef inline TracedContext __traced_context(): | ||
cdef: | ||
PyObject* traced_context = NULL | ||
|
||
if not PY37: | ||
return | ||
|
||
PyContextVar_Get(<PyContextVar*> __traced_ctx, None, &traced_context) | ||
|
||
if <object>traced_context is None: | ||
return | ||
return <TracedContext>traced_context | ||
|
||
|
||
def start_tracing(tracer, root_span): | ||
if not PY37: | ||
raise RuntimeError( | ||
"tracing only supported by Python 3.7 or newer versions") | ||
|
||
traced_context = __traced_ctx.get(None) | ||
if traced_context is not None: | ||
raise RuntimeError("Tracing already started") | ||
|
||
traced_context = TracedContext(tracer, root_span) | ||
__traced_ctx.set(traced_context) | ||
|
||
|
||
def stop_tracing(): | ||
if not PY37: | ||
raise RuntimeError( | ||
"tracing only supported by Python 3.7 or newer versions") | ||
|
||
traced_context = __traced_context() | ||
if traced_context is None: | ||
return | ||
|
||
span = traced_context.current_span() | ||
if span and not span.is_finished: | ||
span.finish() | ||
|
||
__traced_ctx.set(None) |