-
Notifications
You must be signed in to change notification settings - Fork 542
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
POC Tracing support #171
Open
pfreixes
wants to merge
1
commit into
MagicStack:master
Choose a base branch
from
pfreixes:POC_tracing_spans
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
POC Tracing support #171
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,7 +18,9 @@ from .includes.python cimport PY_VERSION_HEX, \ | |
PyContext, \ | ||
PyContext_CopyCurrent, \ | ||
PyContext_Enter, \ | ||
PyContext_Exit | ||
PyContext_Exit, \ | ||
PyContextVar, \ | ||
PyContextVar_Get | ||
|
||
from libc.stdint cimport uint64_t | ||
from libc.string cimport memset, strerror, memcpy | ||
|
@@ -821,13 +823,26 @@ cdef class Loop: | |
except Exception as ex: | ||
if not fut.cancelled(): | ||
fut.set_exception(ex) | ||
|
||
else: | ||
if not fut.cancelled(): | ||
fut.set_result(data) | ||
|
||
else: | ||
if not fut.cancelled(): | ||
fut.set_exception(result) | ||
|
||
traced_context = __traced_context() | ||
if traced_context: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please use explicit |
||
traced_context.current_span().finish() | ||
|
||
traced_context = __traced_context() | ||
if traced_context: | ||
traced_context.start_span( | ||
"getaddrinfo", | ||
tags={'host': host, 'port': port} | ||
) | ||
|
||
AddrInfoRequest(self, host, port, family, type, proto, flags, callback) | ||
return fut | ||
|
||
|
@@ -2976,6 +2991,8 @@ include "handles/udp.pyx" | |
|
||
include "server.pyx" | ||
|
||
include "tracing.pyx" | ||
|
||
|
||
# Used in UVProcess | ||
cdef vint __atfork_installed = 0 | ||
|
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) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
They should always be available, just raise
NotImplementedError
in 3.6/3.5.