Skip to content

Commit fc60578

Browse files
Log and send observability headers
i.e. The session ID and the request ID, are both included in logs from the `ProxyConnection` object and in the requests sent to the EfileProxyServer.
1 parent 27af93d commit fc60578

4 files changed

Lines changed: 359 additions & 406 deletions

File tree

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,4 @@ runtime_config.json
2020

2121
# Python Test stuff
2222
.coverage
23-
coverage_html
23+
coverage_html

docassemble/EFSPIntegration/efm_client.py

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
#!/usr/bin/env python3
2-
import json
2+
import logging
3+
import re
34
import pycountry
4-
from typing import Callable, Dict, Union
5+
from typing import Dict, Union
56

67
import requests
7-
from requests import Response
8+
from logging import LoggerAdapter
9+
from requests import Response, PreparedRequest
810
from docassemble.base.functions import all_variables, get_config
911
from docassemble.base.util import (
1012
DAObject,
@@ -23,6 +25,23 @@
2325
__all__ = ["ApiResponse", "ProxyConnection", "state_name_to_code"]
2426

2527

28+
class DALogger(LoggerAdapter):
29+
def __init__(self, logger):
30+
super().__init__(logger)
31+
32+
def log(self, level, msg, *args, **kwargs):
33+
"""
34+
Delegate a log call to Docassemble's `log` function, after adding
35+
contextual information from this adapter instance.
36+
"""
37+
kwargs
38+
if not self.isEnabledFor(level):
39+
return
40+
41+
msg = re.sub(r"\n", " ", msg)
42+
log(f"[{args}, {kwargs}] {msg}")
43+
44+
2645
def state_name_to_code(state_name: str) -> str:
2746
try:
2847
return pycountry.subdivisions.lookup(state_name).code.split("-")[-1]
@@ -147,12 +166,15 @@ def __init__(
147166
self.credentials_code_block = credentials_code_block
148167

149168
super().__init__(
150-
url=url, api_key=api_key, default_jurisdiction=default_jurisdiction
169+
url=url,
170+
api_key=api_key,
171+
default_jurisdiction=default_jurisdiction,
172+
logger=DALogger(logging.getLogger("docassemble")),
151173
)
152174

153-
def _call_proxy(self, send_func: Callable[[], Response]) -> ApiResponse:
175+
def _call_proxy(self, req: PreparedRequest) -> ApiResponse:
154176
try:
155-
resp = send_func()
177+
resp = self.proxy_client.send(req)
156178
if resp.status_code == 401 and self.credentials_code_block:
157179
reconsider(self.credentials_code_block)
158180
except requests.ConnectionError as ex:

0 commit comments

Comments
 (0)