Skip to content

Commit

Permalink
Caroline Gilbert: new structlog tests pass
Browse files Browse the repository at this point in the history
  • Loading branch information
carolinecgilbert committed Apr 17, 2024
1 parent 3a64898 commit 45aa0a3
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 101 deletions.
55 changes: 0 additions & 55 deletions handlers/opentelemetry_structlog/src/test_logging.py

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
# Imports for StructlogHandler tests
from unittest.mock import Mock
from handlers.opentelemetry_structlog.src.exporter import LogExporter
from datetime import datetime, timezone
from datetime import datetime, timezone, timedelta
from unittest.mock import MagicMock, patch


Expand Down Expand Up @@ -295,15 +295,15 @@ def test_call_method_processes_log_correctly(self):
# Assert that the logger's emit method was called with the processed event
logger.emit.assert_called_once()

# Adding new test cases

def test_log_record_translation_attributes(self):
"""Verify that event_dict translates correctly into a LogRecord with the correct attributes."""
exporter = MagicMock()
logger = MagicMock()
exporter_instance = StructlogHandler("test_service", "test_host", exporter)
exporter_instance._logger = logger

timestamp = datetime.now(timezone.utc)
timestamp = datetime.now(timezone.utc).isoformat()
event_dict = {
"level": "info",
"event": "test event",
Expand All @@ -315,7 +315,7 @@ def test_log_record_translation_attributes(self):
# Use the instance to process the event_dict.
# Mocking the internal logger's emit method to capture the log record
with patch.object(exporter_instance._logger, 'emit') as mock_emit:
exporter_instance(event_dict=event_dict, logger=None, name=None)
exporter_instance(event_dict=event_dict, logger=logger, name=None)
calls = mock_emit.call_args_list
assert len(calls) > 0, "Emit should be called"
log_record = calls[0][0][0] # First call, first arg
Expand All @@ -324,47 +324,61 @@ def test_log_record_translation_attributes(self):
# and you need to verify its contents.
# Need to adjust the assertion depending on how log records are structured.
# I am assuming log_record is a dictionary that was passed to logger.emit.
assert log_record["body"] == event_dict["event"], "LogRecord body should match event"
assert log_record["timestamp"] == timestamp, "LogRecord timestamp should match event"
assert log_record["level"] == event_dict["level"], "LogRecord level should match event"
assert log_record.body == event_dict["event"], "LogRecord body should match event"

assert log_record.attributes["level"] == event_dict["level"], "LogRecord level should match event"

def test_filtering_of_excluded_attributes(self):
"""Ensure specified attributes are not passed to the log record."""
event_dict = {
"level": "error",
"event": "Something happened!",
"timestamp": datetime.now(timezone.utc),
"exception": (ValueError, ValueError("An error occurred"), None)
}

# Get the StructlogHandler instance
exporter_instance = self.structlog_exporter()

with patch.object(exporter_instance._logger, "emit") as mocked_emit:
# Call the exporter_instance with the event_dict
exporter_instance(event_dict=event_dict, logger=None, name=None)

# Check if emit method was called
mocked_emit.assert_called_once()

# Get the log record passed to emit method
log_record = mocked_emit.call_args.args[0]

# Check if the exception attribute is not present in the log record
assert "exception" not in log_record.attributes, "Excluded attributes should not be in the log record"

# def test_filtering_of_excluded_attributes(self):
# """Ensure specified attributes are not passed to the log record."""
# event_dict = {
# "level": "error",
# "event": "Something happened!",
# "timestamp": datetime.now(timezone.utc),
# "exception": ValueError("An error occurred")
# }

# # Get the StructlogHandler instance
# exporter_instance = self.structlog_exporter()

# with patch.object(exporter_instance._logger, "_logger") as mocked_logger:
# exporter_instance(event_dict=event_dict, logger=None, name=None)
# calls = mocked_logger.emit.call_args_list
# log_record = calls[0][0][0]
# assert "exception" not in log_record.attributes, "Excluded attributes should not be in the log record"

# def test_trace_context_propogation(self):
# """Ensure trace context is correctly propagated to the log record."""
# with self.tracer.start_as_current_span("test_span") as span:
# span_id = format(span.get_span_context().span_id, "016x")
# trace_id = format(span.get_span_context().trace_id, "032x")
# trace_sampled = span.get_span_context().trace_flags.sampled
# event_dict = {
# "level": "info",
# "event": "test event",
# "timestamp": datetime.now(timezone.utc)
# }

def test_trace_context_propogation(self):
"""Ensure trace context is correctly propagated to the log record."""
with self.tracer.start_as_current_span("test_span") as span:
span_id = format(span.get_span_context().span_id, "016x")
trace_id = format(span.get_span_context().trace_id, "032x")
trace_sampled = span.get_span_context().trace_flags.sampled
event_dict = {
"level": "info",
"event": "test event",
"timestamp": datetime.now(timezone.utc)
}

# # Get the StructlogHandler instance
# exporter_instance = self.structlog_exporter()

# with patch.object(exporter_instance, "_logger") as mocked_logger:
# exporter_instance(event_dict=event_dict, logger=None, name=None)
# calls = mocked_logger.emit.call_args_list
# log_record = calls[0][0][0]
# assert log_record.attributes["otelSpanID"] == span_id, "Span ID should be propagated"
# assert log_record.attributes["otelTraceID"] == trace_id, "Trace ID should be propagated"
# assert log_record.attributes["otelTraceSampled"] == trace_sampled, ""
# Get the StructlogHandler instance
exporter_instance = self.structlog_exporter()

with patch.object(exporter_instance, "_logger") as mocked_logger:
exporter_instance(event_dict=event_dict, logger=None, name=None)
calls = mocked_logger.emit.call_args_list
log_record = calls[0][0][0]

# Assert that the log record has the correct trace context
actual_span_id = format(log_record.span_id, "016x")
assert actual_span_id == span_id, "Span ID should be propagated"

actual_trace_id = format(log_record.trace_id, "032x")
assert actual_trace_id == trace_id, "Trace ID should be propagated"

assert log_record.trace_flags == trace_sampled, "Trace flags should be propagated"

0 comments on commit 45aa0a3

Please sign in to comment.