Skip to content

Commit

Permalink
double checking
Browse files Browse the repository at this point in the history
  • Loading branch information
tshyun24 committed Jan 31, 2024
1 parent a85ad04 commit 184175d
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 16 deletions.
21 changes: 20 additions & 1 deletion jobs/email-reminder/poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 8 additions & 6 deletions jobs/email-reminder/services/gcp_queue.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,9 @@ def init_app(self, app: Flask):
self.credentials_pub = credentials.with_claims(
audience=publisher_audience)
except Exception as error: # noqa: B902
raise Exception("Unable to create a connection", error) \
from error # pylint: disable=W0719
raise Exception( # pylint: disable=W0719
"Unable to create a connection",
error) from error

@property
def publisher(self):
Expand Down Expand Up @@ -125,8 +126,9 @@ def get_envelope(request: LocalProxy) -> Optional[dict]:
return None

@staticmethod
def get_simple_cloud_event(request: LocalProxy, return_raw: bool = False) \
-> type[SimpleCloudEvent | dict | None]:
def get_simple_cloud_event(request: LocalProxy,
return_raw: bool = False) -> \
type[SimpleCloudEvent | dict | None]:
"""Return a SimpleCloudEvent if one is in session from the PubSub call.
Parameters
Expand Down Expand Up @@ -179,8 +181,8 @@ def publish(self, topic: str, payload: bytes):

return future.result()
except (CancelledError, TimeoutError) as error:
raise Exception("Unable to post to queue", error) \
from error # pylint: disable=W0719
raise Exception("Unable to post to queue", # pylint: disable=W0719
error) from error

@staticmethod
def to_queue_message(ce: SimpleCloudEvent):
Expand Down
19 changes: 10 additions & 9 deletions jobs/email-reminder/services/logging.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,30 +39,31 @@
from werkzeug.local import LocalProxy


def structured_log(request: LocalProxy, severity: str = "NOTICE", message: str = None):
def structured_log(request: LocalProxy,
severity: str = "NOTICE", message: str = None):
"""Function structured logging."""
frm = inspect.stack()[1]
mod = inspect.getmodule(frm[0])

# Build structured log messages as an object.
global_log_fields = {}

if PROJECT := os.environ.get("GOOGLE_CLOUD_PROJECT"):
if project := os.environ.get("GOOGLE_CLOUD_PROJECT"):
# Add log correlation to nest all log messages.
trace_header = request.headers.get("X-Cloud-Trace-Context")

if trace_header and PROJECT:
if trace_header and project:
trace = trace_header.split("/")
global_log_fields["logging.googleapis.com/trace"] = \
f"projects/{PROJECT}/traces/{trace[0]}"
f"projects/{project}/traces/{trace[0]}"

# Complete a structured log entry.
entry = dict(
severity=severity,
message=message,
entry = {
"severity": severity,
"message": message,
# Log viewer accesses 'component' as jsonPayload.component'.
component=f"{mod.__name__}.{frm.function}",
"component": f'{mod.__name__}.{frm.function}',
**global_log_fields,
)
}

print(json.dumps(entry))

0 comments on commit 184175d

Please sign in to comment.