Skip to content

Commit

Permalink
feat: record source locations (#254)
Browse files Browse the repository at this point in the history
  • Loading branch information
daniel-sanche committed Apr 16, 2021
1 parent a78f577 commit a5c2f8e
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 11 deletions.
24 changes: 16 additions & 8 deletions google/cloud/logging_v2/handlers/handlers.py
Expand Up @@ -126,6 +126,8 @@ def __init__(
self.project_id = client.project
self.resource = resource
self.labels = labels
# add extra keys to log record
self.addFilter(CloudLoggingFilter(self.project_id))

def emit(self, record):
"""Actually log the specified logging record.
Expand All @@ -138,25 +140,31 @@ def emit(self, record):
record (logging.LogRecord): The record to be logged.
"""
message = super(CloudLoggingHandler, self).format(record)
trace_id = getattr(record, "trace", None)
span_id = getattr(record, "span_id", None)
http_request = getattr(record, "http_request", None)
resource = getattr(record, "resource", self.resource)
user_labels = getattr(record, "labels", {})
# merge labels
total_labels = self.labels if self.labels is not None else {}
total_labels.update(user_labels)
if len(total_labels) == 0:
total_labels = None
# create source location object
if record.lineno and record.funcName and record.pathname:
source_location = {
"file": record.pathname,
"line": str(record.lineno),
"function": record.funcName,
}
else:
source_location = None
# send off request
self.transport.send(
record,
message,
resource=resource,
resource=getattr(record, "resource", self.resource),
labels=(total_labels if total_labels else None),
trace=trace_id,
span_id=span_id,
http_request=http_request,
trace=(record.trace if record.trace else None),
span_id=getattr(record, "span_id", None),
http_request=(record.http_request if record.http_request else None),
source_location=source_location,
)


Expand Down
16 changes: 14 additions & 2 deletions tests/unit/handlers/test_handlers.py
Expand Up @@ -257,11 +257,11 @@ def test_emit(self):
logname = "loggername"
message = "hello world"
record = logging.LogRecord(logname, logging, None, None, message, None, None)
handler.filter(record)
handler.emit(record)

self.assertEqual(
handler.transport.send_called_with,
(record, message, _GLOBAL_RESOURCE, None, None, None, None),
(record, message, _GLOBAL_RESOURCE, None, None, None, None, None),
)

def test_emit_manual_field_override(self):
Expand All @@ -286,6 +286,15 @@ def test_emit_manual_field_override(self):
setattr(record, "resource", expected_resource)
expected_labels = {"test-label": "manual"}
setattr(record, "labels", expected_labels)
expected_source = {
"file": "test-file",
"line": str(1),
"function": "test-func",
}
setattr(record, "lineno", int(expected_source["line"]))
setattr(record, "funcName", expected_source["function"])
setattr(record, "pathname", expected_source["file"])
handler.filter(record)
handler.emit(record)

self.assertEqual(
Expand All @@ -298,6 +307,7 @@ def test_emit_manual_field_override(self):
expected_trace,
expected_span,
expected_http,
expected_source,
),
)

Expand Down Expand Up @@ -413,6 +423,7 @@ def send(
trace=None,
span_id=None,
http_request=None,
source_location=None,
):
self.send_called_with = (
record,
Expand All @@ -422,4 +433,5 @@ def send(
trace,
span_id,
http_request,
source_location,
)

0 comments on commit a5c2f8e

Please sign in to comment.