-
Notifications
You must be signed in to change notification settings - Fork 3
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
feat: Add middleware DatadogDiagnosticMiddleware
#735
Merged
Merged
Changes from 3 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
""" | ||
Diagnostic middleware for Datadog. | ||
""" | ||
|
||
import logging | ||
|
||
from django.core.exceptions import MiddlewareNotUsed | ||
from edx_toggles.toggles import WaffleFlag | ||
|
||
log = logging.getLogger(__name__) | ||
|
||
# .. toggle_name: datadog.diagnostics.log_root_span | ||
# .. toggle_implementation: WaffleFlag | ||
# .. toggle_default: False | ||
# .. toggle_description: Enables logging of Datadog root span IDs for web requests. | ||
# .. toggle_warning: This is a noisy feature and so it should only be enabled | ||
# for a percentage of requests. | ||
# .. toggle_use_cases: temporary | ||
# .. toggle_creation_date: 2024-07-24 | ||
# .. toggle_target_removal_date: 2024-10-01 | ||
# .. toggle_tickets: https://github.com/edx/edx-arch-experiments/issues/692 | ||
LOG_ROOT_SPAN = WaffleFlag('datadog.diagnostics.log_root_span', module_name=__name__) | ||
|
||
|
||
# pylint: disable=missing-function-docstring | ||
class DatadogDiagnosticMiddleware: | ||
""" | ||
Middleware to add diagnostic logging for Datadog. | ||
|
||
Best added early in the middleware stack. | ||
|
||
Only activates if ``ddtrace`` package is installed and | ||
``datadog.diagnostics.log_root_span`` Waffle flag is enabled. | ||
""" | ||
def __init__(self, get_response): | ||
self.get_response = get_response | ||
self.error = False | ||
|
||
try: | ||
from ddtrace import tracer # pylint: disable=import-outside-toplevel | ||
self.dd_tracer = tracer | ||
except ImportError: | ||
# If import fails, don't even load this middleware. | ||
raise MiddlewareNotUsed # pylint: disable=raise-missing-from | ||
|
||
def __call__(self, request): | ||
return self.get_response(request) | ||
|
||
def process_view(self, request, _view_func, _view_args, _view_kwargs): | ||
try: | ||
self.log_diagnostics(request) | ||
except BaseException as e: | ||
# If there's an error, it will probably hit every request, | ||
# so let's just log it once. | ||
if not self.error: | ||
self.error = True | ||
log.error( | ||
"Encountered error in DatadogDiagnosticMiddleware " | ||
f"(suppressing further errors): {e!r}" | ||
) | ||
|
||
def log_diagnostics(self, request): | ||
""" | ||
Contains all the actual logging logic. | ||
""" | ||
if LOG_ROOT_SPAN.is_enabled(): | ||
route_pattern = getattr(request.resolver_match, 'route', None) | ||
local_root_span = self.dd_tracer.current_root_span() | ||
current_span = self.dd_tracer.current_span() | ||
# pylint: disable=protected-access | ||
log.info( | ||
f"Datadog span diagnostics: Route = {route_pattern}; " | ||
f"local root span = {local_root_span._pprint()}; " | ||
f"current span = {current_span._pprint()}" | ||
) |
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.
Right now we are dealing with both Splunk and Datadog, so this makes sense. Also, our Log Ingest was high (over-budget), but SRE may have taken care of that for us? Otherwise, in the future, we could instead control this via Log Index rules that could be tuned based on noisiness. Although the log messages might still affect disk usage, if it really is high.