Skip to content
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

chore(propagation): only call extract once per request for flask and django #11871

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 1 commit
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: 8 additions & 4 deletions ddtrace/_trace/trace_handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,14 +106,18 @@ def _get_parameters_for_new_span_directly_from_context(ctx: core.ExecutionContex
return span_kwargs


def _start_span(ctx: core.ExecutionContext, call_trace: bool = True, **kwargs) -> "Span":
def _start_span(ctx: core.ExecutionContext, call_trace: bool = True, activate_distributed_headers, **kwargs) -> "Span":
span_kwargs = _get_parameters_for_new_span_directly_from_context(ctx)
call_trace = ctx.get_item("call_trace", call_trace)
tracer = (ctx.get_item("middleware") or ctx["pin"]).tracer
distributed_headers_config = ctx.get_item("distributed_headers_config")
if distributed_headers_config:
import pdb; pdb.set_trace()
# this returns ddtrace.settings.integration.IntegrationConfig, we should change the naming to reflect this
# from distributed_headers_config to integration_config. Need to do this for calls of method too
integration_config = ctx.get_item("distributed_headers_config")
activate_distributed_headers = ctx.get_item("activate_distributed_headers")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟠 Code Quality Violation

variable name is the same as a function parameter (...read more)

A function parameter should only be read and not be modified. If your intent is to modify the value of the parameter, return the value in the function and handle the new value in the caller of the function.

View in Datadog  Leave us feedback  Documentation

if integration_config and activate_distributed_headers:
trace_utils.activate_distributed_headers(
tracer, int_config=distributed_headers_config, request_headers=ctx["distributed_headers"]
tracer, int_config=integration_config, request_headers=ctx["distributed_headers"]
)
distributed_context = ctx.get_item("distributed_context")
if distributed_context and not call_trace:
Expand Down
1 change: 1 addition & 0 deletions ddtrace/contrib/internal/flask/patch.py
Original file line number Diff line number Diff line change
Expand Up @@ -532,6 +532,7 @@ def _patched_request(pin, wrapped, instance, args, kwargs):
block_request_callable=_block_request_callable,
ignored_exception_type=NotFound,
tags={COMPONENT: config.flask.integration_name},
activate_distributed_headers=True,
) as ctx, ctx.span:
core.dispatch("flask._patched_request", (ctx,))
return wrapped(*args, **kwargs)
Expand Down
6 changes: 5 additions & 1 deletion ddtrace/contrib/internal/wsgi/wsgi.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,9 @@ def __call__(self, environ: Iterable, start_response: Callable) -> wrapt.ObjectP
headers = get_request_headers(environ)
closing_iterable = ()
not_blocked = True
# this would be the fix, we'd basically just add a kwarg activate_distributed_headers, and then
# when we call _start_span in core, we check if it's set to true or not, if it's not
# (for every non local root span) we don't call activate_distributed_headers
with core.context_with_data(
"wsgi.__call__",
remote_addr=environ.get("REMOTE_ADDR"),
Expand All @@ -104,8 +107,9 @@ def __call__(self, environ: Iterable, start_response: Callable) -> wrapt.ObjectP
span_type=SpanTypes.WEB,
span_name=(self._request_call_name if hasattr(self, "_request_call_name") else self._request_span_name),
middleware_config=self._config,
distributed_headers_config=self._config,
integration_config=self._config,
distributed_headers=environ,
activate_distributed_headers=True,
environ=environ,
middleware=self,
span_key="req_span",
Expand Down
2 changes: 2 additions & 0 deletions ddtrace/contrib/trace_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -576,7 +576,9 @@ def activate_distributed_headers(tracer, int_config=None, request_headers=None,
#
# app = Flask(__name__) # Traced via Flask instrumentation
# app = DDWSGIMiddleware(app) # Extra layer on top for WSGI
# this is what's saving us from resetting context over and over again
current_context = tracer.current_trace_context()
# we'll error here because we don't have a trace_id etc.
if current_context and current_context.trace_id == context.trace_id:
log.debug(
"will not activate extracted Context(trace_id=%r, span_id=%r), a context with that trace id is already active", # noqa: E501
Expand Down
2 changes: 2 additions & 0 deletions ddtrace/internal/core/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,9 @@ def __init__(
def __enter__(self) -> "ExecutionContext":
if self._span is None and "_CURRENT_CONTEXT" in globals():
self._token: contextvars.Token["ExecutionContext"] = _CURRENT_CONTEXT.set(self)
# context.started.flask._patched_request
dispatch("context.started.%s" % self.identifier, (self,))
# context.started.start_span.flask._patched_request
dispatch("context.started.start_span.%s" % self.identifier, (self,))
return self

Expand Down
Loading