-
Notifications
You must be signed in to change notification settings - Fork 457
feat(tracing): resource renaming #14580
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
florentinl
wants to merge
11
commits into
main
Choose a base branch
from
florentin.labelle/APPSEC-58892/resource-renaming
base: main
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
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
2fe991f
feat(tracing): resource renaming
florentinl 8639946
fix env var
florentinl 50fe0a0
default to /
florentinl 0d13846
fixes
florentinl e858ef7
release note
florentinl 0ee3655
fix tests
florentinl cf32e1a
fix: do not tag http.endpoint when route is available
florentinl c4144d2
update system-tests
florentinl c6b6788
improve release note
florentinl fcbcee9
add benchmarking scenarios
florentinl e61c3ad
use urllib to extract the path
florentinl 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,77 @@ | ||
import re | ||
from typing import List | ||
from typing import Optional | ||
from urllib.parse import urlparse | ||
|
||
from ddtrace._trace.processor import SpanProcessor | ||
from ddtrace.ext import SpanTypes | ||
from ddtrace.ext import http | ||
from ddtrace.internal.logger import get_logger | ||
from ddtrace.settings._config import config | ||
|
||
|
||
log = get_logger(__name__) | ||
|
||
|
||
class ResourceRenamingProcessor(SpanProcessor): | ||
def __init__(self): | ||
self._INT_RE = re.compile(r"^[1-9][0-9]+$") | ||
self._INT_ID_RE = re.compile(r"^(?=.*[0-9].*)[0-9._-]{3,}$") | ||
self._HEX_RE = re.compile(r"^(?=.*[0-9].*)[A-Fa-f0-9]{6,}$") | ||
self._HEX_ID_RE = re.compile(r"^(?=.*[0-9].*)[A-Fa-f0-9._-]{6,}$") | ||
self._STR_RE = re.compile(r"^(.{20,}|.*[%&'()*+,:=@].*)$") | ||
|
||
def _compute_simplified_endpoint_path_element(self, elem: str) -> str: | ||
"""Applies the parameter replacement rules to a single path element.""" | ||
if self._INT_RE.fullmatch(elem): | ||
return "{param:int}" | ||
if self._INT_ID_RE.fullmatch(elem): | ||
return "{param:int_id}" | ||
if self._HEX_RE.fullmatch(elem): | ||
return "{param:hex}" | ||
if self._HEX_ID_RE.fullmatch(elem): | ||
return "{param:hex_id}" | ||
if self._STR_RE.fullmatch(elem): | ||
return "{param:str}" | ||
return elem | ||
|
||
def _compute_simplified_endpoint(self, url: Optional[str]) -> str: | ||
"""Extracts and simplifies the path from an HTTP URL.""" | ||
if not url: | ||
return "/" | ||
|
||
try: | ||
parsed_url = urlparse(url) | ||
except ValueError as e: | ||
log.error("Failed to parse http.url tag when processing span for resource renaming: %s", e) | ||
return "/" | ||
path = parsed_url.path | ||
if not path or path == "/": | ||
return "/" | ||
|
||
elements: List[str] = [] | ||
for part in path.split("/"): | ||
if part: | ||
elements.append(part) | ||
if len(elements) >= 8: | ||
break | ||
|
||
if not elements: | ||
return "/" | ||
|
||
elements = [self._compute_simplified_endpoint_path_element(elem) for elem in elements] | ||
return "/" + "/".join(elements) | ||
|
||
def on_span_start(self, span): | ||
pass | ||
|
||
def on_span_finish(self, span): | ||
if not span._is_top_level or span.span_type not in (SpanTypes.WEB, SpanTypes.HTTP, SpanTypes.SERVERLESS): | ||
return | ||
|
||
route = span.get_tag(http.ROUTE) | ||
|
||
if not route or config._trace_resource_renaming_always_simplified_endpoint: | ||
url = span.get_tag(http.URL) | ||
endpoint = self._compute_simplified_endpoint(url) | ||
span.set_tag_str(http.ENDPOINT, endpoint) |
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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
4 changes: 4 additions & 0 deletions
4
releasenotes/notes/feat-resource-renaming-e5911d3975c220e3.yaml
This file contains hidden or 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,4 @@ | ||
--- | ||
features: | ||
- | | ||
tracing: Added support for resource renaming, a experimental feature that enables automatic renaming of the resource field of spans for web requests from the Datadog user interface when the resource name does not correctly represent an endpoint. | ||
This file contains hidden or 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
Oops, something went wrong.
Oops, something went wrong.
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.
we should put the env var here to show how to enable.
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.
it doesn't seem like this feature actually renames the resource, right? it looks like it just optionally adds an additional tag
http.endpoint
?maybe I don't understand that tag very well