Skip to content

Commit

Permalink
add cherrypy and bottle
Browse files Browse the repository at this point in the history
  • Loading branch information
wconti27 committed Jan 22, 2025
1 parent a189c45 commit 7a1fb99
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 48 deletions.
7 changes: 4 additions & 3 deletions ddtrace/_trace/trace_handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ def _start_span(ctx: core.ExecutionContext, call_trace: bool = True, **kwargs) -
tracer,
int_config=distributed_headers_config,
request_headers=ctx["distributed_headers"],
override=ctx.get_item("distributed_headers_config_override")
override=ctx.get_item("distributed_headers_config_override"),
)
distributed_context = ctx.get_item("distributed_context")
if distributed_context and not call_trace:
Expand All @@ -134,8 +134,8 @@ def _set_web_frameworks_tags(ctx, span, int_config):
span.set_tag_str(SPAN_KIND, SpanKind.SERVER)
span.set_tag(SPAN_MEASURED_KEY)

anayltics_enabled = ctx.get_item('analytics_enabled')
analytics_sample_rate = ctx.get_item('analytics_sample_rate', True)
anayltics_enabled = ctx.get_item("analytics_enabled")
analytics_sample_rate = ctx.get_item("analytics_sample_rate", True)

# Configure trace search sample rate
if (config._analytics_enabled and anayltics_enabled is not False) or anayltics_enabled is True:
Expand Down Expand Up @@ -800,6 +800,7 @@ def listen():

for context_name in (
"aiohttp.request",
"bottle.request",
"flask.call",
"flask.jsonify",
"flask.render_template",
Expand Down
9 changes: 2 additions & 7 deletions ddtrace/contrib/internal/aiohttp/middlewares.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,11 @@
from aiohttp.web_urldispatcher import SystemRoute

from ddtrace import config
from ddtrace.constants import _ANALYTICS_SAMPLE_RATE_KEY
from ddtrace.constants import SPAN_KIND
from ddtrace.constants import SPAN_MEASURED_KEY
from ddtrace.contrib import trace_utils
from ddtrace.contrib.asyncio import context_provider
from ddtrace.ext import SpanKind
from ddtrace.ext import SpanTypes
from ddtrace.ext import http
from ddtrace.internal import core
from ddtrace.internal.constants import COMPONENT
from ddtrace.internal.schema import schematize_url_operation
from ddtrace.internal.schema.span_attribute_schema import SpanDirection

Expand Down Expand Up @@ -39,7 +34,7 @@ async def attach_context(request):
# DEV: aiohttp is special case maintains separate configuration from config api
analytics_enabled = app[CONFIG_KEY]["analytics_enabled"]
# Create a new context based on the propagated information.

with core.context_with_data(
"aiohttp.request",
span_name=schematize_url_operation("aiohttp.request", protocol="http", direction=SpanDirection.INBOUND),
Expand All @@ -52,7 +47,7 @@ async def attach_context(request):
distributed_headers_config_override=app[CONFIG_KEY]["distributed_tracing_enabled"],
headers_case_sensitive=True,
analytics_enabled=analytics_enabled,
analytics_sample_rate=app[CONFIG_KEY].get("analytics_sample_rate", True)
analytics_sample_rate=app[CONFIG_KEY].get("analytics_sample_rate", True),
) as ctx, ctx.span as req_span:
ctx.set_item("req_span", req_span)
core.dispatch("web.request", (ctx, config.aiohttp))
Expand Down
37 changes: 15 additions & 22 deletions ddtrace/contrib/internal/bottle/trace.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,9 @@

import ddtrace
from ddtrace import config
from ddtrace.constants import _ANALYTICS_SAMPLE_RATE_KEY
from ddtrace.constants import SPAN_KIND
from ddtrace.constants import SPAN_MEASURED_KEY
from ddtrace.contrib import trace_utils
from ddtrace.ext import SpanKind
from ddtrace.ext import SpanTypes
from ddtrace.internal.constants import COMPONENT
from ddtrace.internal import core
from ddtrace.internal.schema import schematize_url_operation
from ddtrace.internal.schema.span_attribute_schema import SpanDirection
from ddtrace.internal.utils.formats import asbool
Expand Down Expand Up @@ -42,24 +38,21 @@ def wrapped(*args, **kwargs):

resource = "{} {}".format(request.method, route.rule)

trace_utils.activate_distributed_headers(
self.tracer, int_config=config.bottle, request_headers=request.headers
)

with self.tracer.trace(
schematize_url_operation("bottle.request", protocol="http", direction=SpanDirection.INBOUND),
with core.context_with_data(
"bottle.request",
span_name=schematize_url_operation("bottle.request", protocol="http", direction=SpanDirection.INBOUND),
span_type=SpanTypes.WEB,
service=self.service,
resource=resource,
span_type=SpanTypes.WEB,
) as s:
s.set_tag_str(COMPONENT, config.bottle.integration_name)

# set span.kind to the type of request being performed
s.set_tag_str(SPAN_KIND, SpanKind.SERVER)

s.set_tag(SPAN_MEASURED_KEY)
# set analytics sample rate with global config enabled
s.set_tag(_ANALYTICS_SAMPLE_RATE_KEY, config.bottle.get_analytics_sample_rate(use_global_config=True))
tags={},
tracer=self.tracer,
distributed_headers=request.headers,
distributed_headers_config=config.bottle,
headers_case_sensitive=True,
analytics_sample_rate=config.bottle.get_analytics_sample_rate(use_global_config=True),
) as ctx, ctx.span as req_span:
ctx.set_item("req_span", req_span)
core.dispatch("web.request", (ctx, config.bottle))

code = None
result = None
Expand Down Expand Up @@ -92,7 +85,7 @@ def wrapped(*args, **kwargs):
url = request.urlparts._replace(query="").geturl()
full_route = "/".join([request.script_name.rstrip("/"), route.rule.lstrip("/")])
trace_utils.set_http_meta(
s,
req_span,
config.bottle,
method=method,
url=url,
Expand Down
31 changes: 15 additions & 16 deletions ddtrace/contrib/internal/cherrypy/middleware.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,10 @@
from ddtrace.constants import ERROR_MSG
from ddtrace.constants import ERROR_STACK
from ddtrace.constants import ERROR_TYPE
from ddtrace.constants import SPAN_KIND
from ddtrace.contrib import trace_utils
from ddtrace.ext import SpanKind
from ddtrace.ext import SpanTypes
from ddtrace.internal import compat
from ddtrace.internal.constants import COMPONENT
from ddtrace.internal import core
from ddtrace.internal.schema import SpanDirection
from ddtrace.internal.schema import schematize_service_name
from ddtrace.internal.schema import schematize_url_operation
Expand Down Expand Up @@ -77,20 +75,21 @@ def _setup(self):
cherrypy.request.hooks.attach("after_error_response", self._after_error_response, priority=5)

def _on_start_resource(self):
trace_utils.activate_distributed_headers(
self._tracer, int_config=config.cherrypy, request_headers=cherrypy.request.headers
)

cherrypy.request._datadog_span = self._tracer.trace(
SPAN_NAME,
service=trace_utils.int_service(None, config.cherrypy, default="cherrypy"),
with core.context_with_data(
"cherrypy.request",
span_name=SPAN_NAME,
span_type=SpanTypes.WEB,
)

cherrypy.request._datadog_span.set_tag_str(COMPONENT, config.cherrypy.integration_name)

# set span.kind to the type of request being performed
cherrypy.request._datadog_span.set_tag_str(SPAN_KIND, SpanKind.SERVER)
service=trace_utils.int_service(None, config.cherrypy, default="cherrypy"),
tags={},
tracer=self._tracer,
distributed_headers=cherrypy.request.headers,
distributed_headers_config=config.cherrypy,
headers_case_sensitive=True,
) as ctx, ctx.span as req_span:
ctx.set_item("req_span", req_span)
core.dispatch("web.request", (ctx, config.cherrypy))

cherrypy.request._datadog_span = req_span

def _after_error_response(self):
span = getattr(cherrypy.request, "_datadog_span", None)
Expand Down

0 comments on commit 7a1fb99

Please sign in to comment.