-
Notifications
You must be signed in to change notification settings - Fork 423
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
munir: move telemetry logger to a handler
munir: add telemetry log handler
- Loading branch information
Showing
3 changed files
with
77 additions
and
109 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
import logging | ||
import os | ||
import traceback | ||
|
||
from ddtrace.internal.telemetry.constants import TELEMETRY_LOG_LEVEL | ||
|
||
|
||
class DDTelemetryLogHandler(logging.StreamHandler): | ||
CWD = os.getcwd() | ||
|
||
def __init__(self, telemetry_writer): | ||
self.telemetry_writer = telemetry_writer | ||
super().__init__() | ||
|
||
def emit(self, record): | ||
# type: (logging.LogRecord) -> None | ||
if record.levelno >= logging.ERROR: | ||
# Capture start up errors | ||
full_file_name = os.path.join(record.pathname, record.filename) | ||
self.telemetry_writer.add_error(1, record.msg % record.args, full_file_name, record.lineno) | ||
|
||
# Capture errors logged in the ddtrace integrations | ||
if record.name.startswith("ddtrace.contrib"): | ||
telemetry_level = ( | ||
TELEMETRY_LOG_LEVEL.ERROR | ||
if record.levelno >= logging.ERROR | ||
else TELEMETRY_LOG_LEVEL.WARNING | ||
if record.levelno == logging.WARNING | ||
else TELEMETRY_LOG_LEVEL.DEBUG | ||
) | ||
stack_trace = self._format_stack_trace(record.exc_info) if record.exc_info is not None else None | ||
# The majority of ddtrace logs are called with exc_info=None and we won't collect telemetry information for them | ||
if stack_trace is not None: | ||
# Report only exceptions with a stack trace | ||
self.telemetry_writer.add_log( | ||
telemetry_level, | ||
record.msg, | ||
# Do we need to set this tag? Should we allow telemetry intake to infer this value? | ||
tags={"lib_language": "python"}, | ||
stack_trace=stack_trace, | ||
) | ||
|
||
def _format_stack_trace(self, exc_info): | ||
exc_type, exc_value, exc_traceback = exc_info | ||
if exc_traceback: | ||
tb = traceback.extract_tb(exc_traceback) | ||
formatted_tb = ["Traceback (most recent call last):"] | ||
for filename, lineno, funcname, srcline in tb: | ||
if self._should_redact(filename): | ||
formatted_tb.append(" <REDACTED>") | ||
else: | ||
relative_filename = self._format_file_path(filename) | ||
formatted_line = f' File "{relative_filename}", line {lineno}, in {funcname}\n {srcline}' | ||
formatted_tb.append(formatted_line) | ||
formatted_tb.append(f"{exc_type.__module__}.{exc_type.__name__}: {exc_value}") | ||
return "\n".join(formatted_tb) | ||
return None | ||
|
||
def _should_redact(self, filename): | ||
return "ddtrace" not in filename | ||
|
||
def _format_file_path(self, filename): | ||
try: | ||
return os.path.relpath(filename, start=self.CWD) | ||
except ValueError: | ||
return filename |
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