|
| 1 | +import json |
| 2 | + |
| 3 | +from google.cloud.logging_v2.handlers import CloudLoggingFilter, CloudLoggingHandler |
| 4 | +from google.cloud.logging_v2.handlers._helpers import _parse_xcloud_trace |
| 5 | +from google.cloud.logging_v2.handlers.handlers import DEFAULT_LOGGER_NAME |
| 6 | +from google.cloud.logging_v2.handlers.transports import BackgroundThreadTransport |
| 7 | + |
| 8 | +from .request_logging_middleware import _FASTAPI_REQUEST_CONTEXT |
| 9 | + |
| 10 | + |
| 11 | +class FastAPILoggingFilter(CloudLoggingFilter): |
| 12 | + """ |
| 13 | + This LoggingFilter is extended for logging a request on FastAPI. |
| 14 | + This data can be manually overwritten using the `extras` argument when writing logs. |
| 15 | + """ |
| 16 | + |
| 17 | + def filter(self, record): |
| 18 | + """ |
| 19 | + Add new Cloud Logging data to each LogRecord as it comes in |
| 20 | + """ |
| 21 | + user_labels = getattr(record, "labels", {}) |
| 22 | + # infer request data from context_vars |
| 23 | + ( |
| 24 | + inferred_http, |
| 25 | + inferred_trace, |
| 26 | + inferred_span, |
| 27 | + inferred_sampled, |
| 28 | + ) = self.get_request_data() |
| 29 | + if inferred_trace is not None and self.project is not None: |
| 30 | + # add full path for detected trace |
| 31 | + inferred_trace = f"projects/{self.project}/traces/{inferred_trace}" |
| 32 | + # set new record values |
| 33 | + record._resource = getattr(record, "resource", None) |
| 34 | + record._trace = getattr(record, "trace", inferred_trace) or None |
| 35 | + record._span_id = getattr(record, "span_id", inferred_span) or None |
| 36 | + record._trace_sampled = bool(getattr(record, "trace_sampled", inferred_sampled)) |
| 37 | + record._http_request = getattr(record, "http_request", inferred_http) |
| 38 | + record._source_location = CloudLoggingFilter._infer_source_location(record) |
| 39 | + # add logger name as a label if possible |
| 40 | + logger_label = {"python_logger": record.name} if record.name else {} |
| 41 | + record._labels = {**logger_label, **self.default_labels, **user_labels} or None |
| 42 | + # create string representations for structured logging |
| 43 | + record._trace_str = record._trace or "" |
| 44 | + record._span_id_str = record._span_id or "" |
| 45 | + record._trace_sampled_str = "true" if record._trace_sampled else "false" |
| 46 | + record._http_request_str = json.dumps( |
| 47 | + record._http_request or {}, ensure_ascii=False |
| 48 | + ) |
| 49 | + record._source_location_str = json.dumps( |
| 50 | + record._source_location or {}, ensure_ascii=False |
| 51 | + ) |
| 52 | + record._labels_str = json.dumps(record._labels or {}, ensure_ascii=False) |
| 53 | + return True |
| 54 | + |
| 55 | + def get_request_data(self): |
| 56 | + request = _FASTAPI_REQUEST_CONTEXT.get() |
| 57 | + if request is None: |
| 58 | + return None, None, None, False |
| 59 | + |
| 60 | + # build up http request data |
| 61 | + http_request = { |
| 62 | + "requestMethod": request.request_method, |
| 63 | + "requestUrl": request.request_url, |
| 64 | + "requestSize": request.content_length, |
| 65 | + "userAgent": request.user_agent, |
| 66 | + "remoteIp": request.remote_ip, |
| 67 | + "referer": request.referer, |
| 68 | + "protocol": request.protocol, |
| 69 | + } |
| 70 | + |
| 71 | + return http_request, *_parse_xcloud_trace(request.cloud_trace_content) |
| 72 | + |
| 73 | + |
| 74 | +class FastAPILoggingHandler(CloudLoggingHandler): |
| 75 | + """ |
| 76 | + This LoggingHandler is extended for logging a request on FastAPI. |
| 77 | + Usage of this LoggingHandler is the same as CloudLoggingHandler. |
| 78 | + """ |
| 79 | + |
| 80 | + def __init__( |
| 81 | + self, |
| 82 | + client, |
| 83 | + *, |
| 84 | + name=DEFAULT_LOGGER_NAME, |
| 85 | + transport=BackgroundThreadTransport, |
| 86 | + resource=None, |
| 87 | + labels=None, |
| 88 | + stream=None, |
| 89 | + ): |
| 90 | + """ |
| 91 | + Args: |
| 92 | + client (~logging_v2.client.Client): |
| 93 | + The authenticated Google Cloud Logging client for this |
| 94 | + handler to use. |
| 95 | + name (str): the name of the custom log in Cloud Logging. |
| 96 | + Defaults to 'python'. The name of the Python logger will be represented |
| 97 | + in the ``python_logger`` field. |
| 98 | + transport (~logging_v2.transports.Transport): |
| 99 | + Class for creating new transport objects. It should |
| 100 | + extend from the base :class:`.Transport` type and |
| 101 | + implement :meth`.Transport.send`. Defaults to |
| 102 | + :class:`.BackgroundThreadTransport`. The other |
| 103 | + option is :class:`.SyncTransport`. |
| 104 | + resource (~logging_v2.resource.Resource): |
| 105 | + Resource for this Handler. If not given, will be inferred from the environment. |
| 106 | + labels (Optional[dict]): Additional labels to attach to logs. |
| 107 | + stream (Optional[IO]): Stream to be used by the handler. |
| 108 | + """ |
| 109 | + super(FastAPILoggingHandler, self).__init__( |
| 110 | + client, |
| 111 | + name=name, |
| 112 | + transport=transport, |
| 113 | + resource=resource, |
| 114 | + labels=labels, |
| 115 | + stream=stream, |
| 116 | + ) |
| 117 | + |
| 118 | + # replace default cloud logging filter |
| 119 | + for default_filter in self.filters: |
| 120 | + if default_filter.isinstance(CloudLoggingFilter): |
| 121 | + self.removeFilter(default_filter) |
| 122 | + |
| 123 | + log_filter = FastAPILoggingFilter( |
| 124 | + project=self.project_id, default_labels=labels |
| 125 | + ) |
| 126 | + self.addFilter(log_filter) |
0 commit comments