Skip to content

Commit

Permalink
Handle both logged messages and objects
Browse files Browse the repository at this point in the history
  • Loading branch information
harelmo-lumigo committed Apr 11, 2024
1 parent f5d7051 commit 79a450a
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 8 deletions.
5 changes: 3 additions & 2 deletions src/lumigo_opentelemetry/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -217,8 +217,9 @@ def init() -> Dict[str, Any]:
def log_hook(_: Span, record: logging.LogRecord) -> None:
from lumigo_opentelemetry.libs.json_utils import dump

scrubbed = dump(record.getMessage())
record.msg = scrubbed
record.msg = dump(
record.getMessage() if isinstance(record.msg, str) else record.msg
)

# Inject the span context into logs
LoggingInstrumentor().instrument(set_logging_format=True, log_hook=log_hook)
Expand Down
15 changes: 10 additions & 5 deletions src/test/integration/logging/app/logging_app.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import json
import os
from opentelemetry import trace
from lumigo_opentelemetry import logger_provider
import logging
Expand All @@ -9,11 +10,15 @@
logger.setLevel(logging.DEBUG)

with tracer.start_as_current_span("some-span") as span:
logger.debug(
json.dumps(
{"some-thing": "Hello OTEL!", "some-super-secret-stuff": "this is a secret"}
)
)
message = {
"some-thing": "Hello OTEL!",
"some-super-secret-stuff": "this is a secret",
}

if os.environ["LOG_AS_JSON_STRING"] == "true":
logger.debug(json.dumps(message))
else:
logger.debug(message)

logger_provider.force_flush()
logger_provider.shutdown()
27 changes: 26 additions & 1 deletion src/test/integration/logging/tests/test_logging.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@
from test.test_utils.logs_parser import LogsContainer


def run_logging_app(logging_enabled: bool, log_dump_file: str = None):
def run_logging_app(
logging_enabled: bool, log_dump_file: str = None, log_as_string: bool = False
):
app_path = path.join(
path.dirname(path.abspath(__file__)),
"../app/logging_app.py",
Expand All @@ -23,6 +25,7 @@ def run_logging_app(logging_enabled: bool, log_dump_file: str = None):
"LUMIGO_DEBUG_LOGDUMP": log_dump_file
or os.environ.get("LUMIGO_DEBUG_LOGDUMP"),
"LUMIGO_SECRET_MASKING_REGEX": '[".*super-secret.*"]',
"LOG_AS_JSON_STRING": str(log_as_string).lower(),
},
capture_output=True,
)
Expand Down Expand Up @@ -71,6 +74,28 @@ def test_logging_enabled(self):
self.assertEqual(log_attributes["otelServiceName"], "logging-app")
self.assertEqual(log_attributes["otelTraceSampled"], True)

def test_secret_scrubbing_string(self):
run_logging_app(logging_enabled=True, log_as_string=True)

logs_container = LogsContainer.get_logs_from_file()

self.assertEqual(len(logs_container), 1)
self.assertEqual(
logs_container[0]["body"],
'{"some-thing": "Hello OTEL!", "some-super-secret-stuff": "****"}',
)

def test_secret_scrubbing_object(self):
run_logging_app(logging_enabled=True, log_as_string=False)

logs_container = LogsContainer.get_logs_from_file()

self.assertEqual(len(logs_container), 1)
self.assertEqual(
logs_container[0]["body"],
'{"some-thing": "Hello OTEL!", "some-super-secret-stuff": "****"}',
)

def test_logging_disabled(self):
run_logging_app(logging_enabled=False)

Expand Down

0 comments on commit 79a450a

Please sign in to comment.