Skip to content

Commit

Permalink
Fix indentation when emitting logs
Browse files Browse the repository at this point in the history
  • Loading branch information
harelmo-lumigo committed Apr 8, 2024
1 parent 9c55ffc commit a82fa86
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 24 deletions.
6 changes: 3 additions & 3 deletions src/lumigo_opentelemetry/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -220,9 +220,9 @@ def init() -> Dict[str, Any]:
SimpleLogRecordProcessor(
ConsoleLogExporter(
out=open(logdump_file, "w"),
# Print one log per line for ease of parsing, as the file itself
# will not be valid JSON but a sequence of JSON objects (not a valid JSON array)
formatter=lambda log_record: log_record.to_json() + "\n",
# Override the default formatter to remove indentation, so one log record will be printed per line
formatter=lambda log_record: log_record.to_json(indent=None)
+ "\n",
)
)
)
Expand Down
13 changes: 7 additions & 6 deletions src/test/integration/logging/tests/test_logging.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,13 @@


def run_logging_app():
sample_path = path.join(
app_path = path.join(
path.dirname(path.abspath(__file__)),
"../app/logging_app.py",
)
subprocess.check_output(
[sys.executable, sample_path],

subprocess.run(
[sys.executable, app_path],
env={
**os.environ,
"AUTOWRAPT_BOOTSTRAP": "lumigo_opentelemetry",
Expand All @@ -29,9 +30,9 @@ def test_logging(self):
logs_container = LogsContainer.get_logs_from_file()

self.assertEqual(len(logs_container), 1)

logs_container[0]["body"] == "Hello OTEL!"
self.assertEqual(logs_container[0]["body"], "Hello OTEL!")

# "resource" is currently not a proper dictionary, but a string from a repr(...) call over the resource attributes.
# Pending a fix in https://github.com/open-telemetry/opentelemetry-python/pull/3346
logs_container[0]["resource"]["service.name"] == "logging-app"
self.assertIn("'service.name': 'logging-app'", logs_container[0]["resource"])
# self.assertEqual(logs_container[0]["resource"]["service.name"], "logging-app")
41 changes: 26 additions & 15 deletions src/test/test_utils/logs_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,26 +13,37 @@
class LogsContainer:
logs: List[Dict[str, Any]]

def parse_logs_from_file(
path: Optional[str] = LOGS_FILE_FULL_PATH,
) -> LogsContainer:
with open(path) as file:
return LogsContainer(logs=[json.loads(line) for line in file.readlines()])

@staticmethod
def get_logs_from_file(
path: Optional[str] = LOGS_FILE_FULL_PATH,
wait_time_sec: int = 3,
timeout_sec: int = 3,
expected_log_count: int = 1,
) -> LogsContainer:
time.sleep(wait_time_sec)

try:
with open(path) as file:
logs = [json.loads(line) for line in file.readlines()]

return LogsContainer(logs=logs)

except Exception as err:
print(f"Failed to parse logs from file after {wait_time_sec}s: {err}")

return LogsContainer(logs=[])
waited_time_in_sec = 0
logs_container = LogsContainer(logs=[])

while waited_time_in_sec < timeout_sec:
try:
logs_container = LogsContainer.parse_logs_from_file(path)
if len(logs_container) >= expected_log_count:
return logs_container
except Exception as err:
print(
f"Failed to parse logs from file after {waited_time_in_sec}s: {err}"
)
time.sleep(1)
waited_time_in_sec += 1

return logs_container

def __len__(self) -> int:
return len(self.logs)

def __getitem__(self, item) -> Dict[str, Any]:
return self.logs[item]
def __getitem__(self, key: int) -> Dict[str, Any]:
return self.logs[key]

0 comments on commit a82fa86

Please sign in to comment.