@@ -45,6 +45,10 @@ class CloudLoggingFilter(logging.Filter):
4545 overwritten using the `extras` argument when writing logs.
4646 """
4747
48+ # The subset of http_request fields have been tested to work consistently across GCP environments
49+ # https://cloud.google.com/logging/docs/reference/v2/rest/v2/LogEntry#httprequest
50+ _supported_http_fields = ("requestMethod" , "requestUrl" , "userAgent" , "protocol" )
51+
4852 def __init__ (self , project = None , default_labels = None ):
4953 self .project = project
5054 self .default_labels = default_labels if default_labels else {}
@@ -74,8 +78,17 @@ def filter(self, record):
7478 Add new Cloud Logging data to each LogRecord as it comes in
7579 """
7680 user_labels = getattr (record , "labels" , {})
81+ # infer request data from the environment
7782 inferred_http , inferred_trace , inferred_span = get_request_data ()
83+ if inferred_http is not None :
84+ # filter inferred_http to include only well-supported fields
85+ inferred_http = {
86+ k : v
87+ for (k , v ) in inferred_http .items ()
88+ if k in self ._supported_http_fields and v is not None
89+ }
7890 if inferred_trace is not None and self .project is not None :
91+ # add full path for detected trace
7992 inferred_trace = f"projects/{ self .project } /traces/{ inferred_trace } "
8093 # set new record values
8194 record ._resource = getattr (record , "resource" , None )
@@ -84,13 +97,14 @@ def filter(self, record):
8497 record ._http_request = getattr (record , "http_request" , inferred_http )
8598 record ._source_location = CloudLoggingFilter ._infer_source_location (record )
8699 record ._labels = {** self .default_labels , ** user_labels } or None
87- # create guaranteed string representations for structured logging
88- record ._msg_str = record .msg or ""
100+ # create string representations for structured logging
89101 record ._trace_str = record ._trace or ""
90102 record ._span_id_str = record ._span_id or ""
91103 record ._http_request_str = json .dumps (record ._http_request or {})
92104 record ._source_location_str = json .dumps (record ._source_location or {})
93105 record ._labels_str = json .dumps (record ._labels or {})
106+ # break quotes for parsing through structured logging
107+ record ._msg_str = str (record .msg ).replace ('"' , '\\ "' ) if record .msg else ""
94108 return True
95109
96110
0 commit comments