Skip to content

feat: add additional tracing on websockets #13332

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

Open
wants to merge 22 commits into
base: main
Choose a base branch
from

Conversation

quinna-h
Copy link
Contributor

@quinna-h quinna-h commented May 5, 2025

Add websocket tracing according to RFC

Checklist

  • PR author has checked that all the criteria below are met
  • The PR description includes an overview of the change
  • The PR description articulates the motivation for the change
  • The change includes tests OR the PR description describes a testing strategy
  • The PR description notes risks associated with the change, if any
  • Newly-added code is easy to change
  • The change follows the library release note guidelines
  • The change includes or references documentation updates if necessary
  • Backport labels are set (if applicable)

Reviewer Checklist

  • Reviewer has checked that all the criteria below are met
  • Title is accurate
  • All changes are related to the pull request's stated goal
  • Avoids breaking API changes
  • Testing strategy adequately addresses listed risks
  • Newly-added code is easy to change
  • Release note makes sense to a user of the library
  • If necessary, author has acknowledged and discussed the performance implications of this PR as reported in the benchmarks PR comment
  • Backport labels are set in a manner that is consistent with the release branch maintenance policy

@quinna-h quinna-h changed the title add tracing on websockets add additional tracing on websockets May 5, 2025
Copy link
Contributor

github-actions bot commented May 5, 2025

CODEOWNERS have been resolved as:

releasenotes/notes/asgi-websocket-tracing-support-0b785b921385dfeb.yaml  @DataDog/apm-python
tests/snapshots/tests.contrib.fastapi.test_fastapi.test_long_running_websocket_session.json  @DataDog/apm-python
tests/snapshots/tests.contrib.fastapi.test_fastapi.test_websocket_sampling_not_inherited.json  @DataDog/apm-python
tests/snapshots/tests.contrib.fastapi.test_fastapi.test_websocket_tracing_not_separate_traces.json  @DataDog/apm-python
ddtrace/contrib/internal/asgi/middleware.py                             @DataDog/apm-core-python @DataDog/apm-idm-python
ddtrace/contrib/internal/django/patch.py                                @DataDog/apm-core-python @DataDog/apm-idm-python
ddtrace/contrib/internal/fastapi/patch.py                               @DataDog/apm-core-python @DataDog/apm-idm-python
ddtrace/contrib/internal/starlette/patch.py                             @DataDog/apm-core-python @DataDog/apm-idm-python
tests/contrib/fastapi/app.py                                            @DataDog/apm-core-python @DataDog/apm-idm-python
tests/contrib/fastapi/test_fastapi.py                                   @DataDog/apm-core-python @DataDog/apm-idm-python
tests/snapshots/tests.contrib.fastapi.test_fastapi.test_traced_websocket.json  @DataDog/apm-python

Copy link
Contributor

github-actions bot commented May 5, 2025

Bootstrap import analysis

Comparison of import times between this PR and base.

Summary

The average import time from this PR is: 289 ± 6 ms.

The average import time from base is: 290 ± 5 ms.

The import time difference between this PR and base is: -0.9 ± 0.2 ms.

Import time breakdown

The following import paths have shrunk:

ddtrace.auto 1.294 ms (0.45%)
ddtrace 0.684 ms (0.24%)
ddtrace.internal._unpatched 0.031 ms (0.01%)
json 0.031 ms (0.01%)
json.decoder 0.031 ms (0.01%)
re 0.031 ms (0.01%)
enum 0.031 ms (0.01%)
types 0.031 ms (0.01%)
ddtrace.bootstrap.sitecustomize 0.610 ms (0.21%)
ddtrace.bootstrap.preload 0.610 ms (0.21%)
ddtrace.internal.remoteconfig.client 0.610 ms (0.21%)

@pr-commenter
Copy link

pr-commenter bot commented May 5, 2025

Benchmarks

Benchmark execution time: 2025-06-12 21:23:50

Comparing candidate commit cdae31c in PR branch quinna.halim/websockets-tracing-asgi with baseline commit b588b17 in branch main.

Found 0 performance improvements and 1 performance regressions! Performance is the same for 463 metrics, 2 unstable metrics.

scenario:iastaspectsospath-ospathsplitext_aspect

  • 🟥 execution_time [+605.977ns; +675.077ns] or [+13.434%; +14.965%]

ws_span.set_metric("_dd.dm.inherited", 1)
parent_span = self.tracer.current_root_span()
if parent_span is not None:
ws_span.set_tag_str("_dd.dm.service", parent_span.service)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

should be the handshake span's service and resource

@quinna-h quinna-h force-pushed the quinna.halim/websockets-tracing-asgi branch from 47131b0 to 2cb44a0 Compare May 8, 2025 18:58
ipaddress.ip_address(client_ip) # validate ip address
span.set_tag_str("network.client.ip", client_ip)
except ValueError:
pass
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

silent exception (...read more)

Using the pass statement in an exception block ignores the exception. Exceptions should never be ignored. Instead, the user must add code to notify an exception occurred and attempt to handle it or recover from it.

The exception to this rule is the use of StopIteration or StopAsyncIteration when implementing a custom iterator (as those errors are used to acknowledge the end of a successful iteration).

View in Datadog  Leave us feedback  Documentation

ipaddress.ip_address(client_ip) # validate ip address
span.set_tag_str("network.client.ip", client_ip)
except ValueError:
pass
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

silent exception (...read more)

Using the pass statement in an exception block ignores the exception. Exceptions should never be ignored. Instead, the user must add code to notify an exception occurred and attempt to handle it or recover from it.

The exception to this rule is the use of StopIteration or StopAsyncIteration when implementing a custom iterator (as those errors are used to acknowledge the end of a successful iteration).

View in Datadog  Leave us feedback  Documentation

@quinna-h quinna-h changed the title add additional tracing on websockets feat: add increased tracing on websockets Jun 2, 2025
@quinna-h quinna-h changed the title feat: add increased tracing on websockets feat: add additional tracing on websockets Jun 2, 2025
@quinna-h quinna-h marked this pull request as ready for review June 2, 2025 20:49
@quinna-h quinna-h requested review from a team as code owners June 2, 2025 20:49
@quinna-h quinna-h requested review from wantsui, Yun-Kim and juanjux June 2, 2025 20:49
ipaddress.ip_address(client_ip) # validate ip address
span.set_tag_str("network.client.ip", client_ip)
except ValueError:
pass
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

silent exception (...read more)

Using the pass statement in an exception block ignores the exception. Exceptions should never be ignored. Instead, the user must add code to notify an exception occurred and attempt to handle it or recover from it.

The exception to this rule is the use of StopIteration or StopAsyncIteration when implementing a custom iterator (as those errors are used to acknowledge the end of a successful iteration).

View in Datadog  Leave us feedback  Documentation

"resource": "websocket /ws",
"trace_id": 2,
"span_id": 1,
"parent_id": 0,
"type": "web",
"error": 0,
"meta": {
"_dd.base_service": "tests.contrib.fastapi",
"_dd.p.dm": "-0",
Copy link
Contributor Author

Choose a reason for hiding this comment

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

should the fastapi.request span also have the sampling decision maker set here?

Choose a reason for hiding this comment

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

0 means default (unset). Why it differs from the value that has the websocket.receive above? If you want to set it either it has to be manually kept in the test either you can set a rule for it

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Choose a reason for hiding this comment

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

You should do in the test and not in the instrumentation code. Otherwise you will change the sampling decision.

@quinna-h quinna-h force-pushed the quinna.halim/websockets-tracing-asgi branch from d02b4a1 to ccdb2bf Compare June 4, 2025 17:14
@quinna-h quinna-h force-pushed the quinna.halim/websockets-tracing-asgi branch from 16d0a8b to dee2fd3 Compare June 5, 2025 14:43
update sampling on root span

wip

wip: style
@quinna-h quinna-h force-pushed the quinna.halim/websockets-tracing-asgi branch from dee2fd3 to ac4d82e Compare June 5, 2025 14:55
quinna-h added 2 commits June 6, 2025 13:24
change to False by default
@quinna-h quinna-h force-pushed the quinna.halim/websockets-tracing-asgi branch from a052ecf to f428bcb Compare June 6, 2025 18:00
@quinna-h quinna-h force-pushed the quinna.halim/websockets-tracing-asgi branch from 309f38d to ebd86fb Compare June 10, 2025 16:15
@quinna-h quinna-h force-pushed the quinna.halim/websockets-tracing-asgi branch from e1f8525 to b4c016f Compare June 10, 2025 17:01
@quinna-h quinna-h requested a review from amarziali June 10, 2025 17:35
@quinna-h quinna-h force-pushed the quinna.halim/websockets-tracing-asgi branch from 41fa395 to cdae31c Compare June 12, 2025 20:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants