Skip to content

Commit 80a79e6

Browse files
saartochner-lumigoCircleCI
andauthored
feat: add flag to avoid collecting the HTTP's request and response body (#247)
* feat: add flag to avoid collecting the HTTP's request and response body Co-authored-by: CircleCI <[email protected]>
1 parent 9f4611c commit 80a79e6

File tree

4 files changed

+46
-6
lines changed

4 files changed

+46
-6
lines changed

src/lumigo_tracer/lumigo_utils.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@
8383
STACKTRACE_LINE_TO_DROP = "lumigo_tracer/tracer.py"
8484
Container = TypeVar("Container", dict, list)
8585
DEFAULT_AUTO_TAG_KEY = "LUMIGO_AUTO_TAG"
86+
SKIP_COLLECTING_HTTP_BODY_KEY = "LUMIGO_SKIP_COLLECTING_HTTP_BODY"
8687

8788
_logger: Dict[str, logging.Logger] = {}
8889

@@ -141,6 +142,7 @@ class Configuration:
141142
should_scrub_known_services: bool = False
142143
is_sync_tracer: bool = False
143144
auto_tag: List[str] = []
145+
skip_collecting_http_body: bool = False
144146

145147
@staticmethod
146148
def get_max_entry_size(has_error: bool = False) -> int:
@@ -165,6 +167,7 @@ def config(
165167
edge_kinesis_aws_access_key_id: Optional[str] = None,
166168
edge_kinesis_aws_secret_access_key: Optional[str] = None,
167169
auto_tag: Optional[List[str]] = None,
170+
skip_collecting_http_body: bool = False,
168171
) -> None:
169172
"""
170173
This function configure the lumigo wrapper.
@@ -185,6 +188,7 @@ def config(
185188
:param edge_kinesis_aws_access_key_id: The credentials to push to the Kinesis in China region
186189
:param edge_kinesis_aws_secret_access_key: The credentials to push to the Kinesis in China region
187190
:param auto_tag: The keys from the event that should be used as execution tags.
191+
:param skip_collecting_http_body: Should we not collect the HTTP request and response bodies.
188192
"""
189193

190194
Configuration.token = token or os.environ.get(LUMIGO_TOKEN_KEY, "")
@@ -232,7 +236,7 @@ def config(
232236
Configuration.max_entry_size = int(os.environ.get("LUMIGO_MAX_ENTRY_SIZE", max_entry_size))
233237
Configuration.edge_kinesis_stream_name = (
234238
edge_kinesis_stream_name
235-
or os.environ.get("LUMIGO_EDGE_KINESIS_STREAM_NAME") # noqa`
239+
or os.environ.get("LUMIGO_EDGE_KINESIS_STREAM_NAME") # noqa
236240
or EDGE_KINESIS_STREAM_NAME # noqa
237241
)
238242
Configuration.edge_kinesis_aws_access_key_id = edge_kinesis_aws_access_key_id or os.environ.get(
@@ -249,6 +253,11 @@ def config(
249253
Configuration.auto_tag = auto_tag or os.environ.get(
250254
"LUMIGO_AUTO_TAG", DEFAULT_AUTO_TAG_KEY
251255
).split(",")
256+
Configuration.skip_collecting_http_body = (
257+
not Configuration.verbose
258+
or skip_collecting_http_body # noqa: W503
259+
or os.environ.get(SKIP_COLLECTING_HTTP_BODY_KEY, "false").lower() == "true" # noqa: W503
260+
)
252261

253262

254263
def _is_span_has_error(span: dict) -> bool:

src/lumigo_tracer/wrappers/http/http_parser.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ def parse_request(self, parse_params: HttpRequest) -> dict:
4848
additional_info = {
4949
"headers": lumigo_dumps(parse_params.headers),
5050
"body": lumigo_dumps(parse_params.body, omit_skip_path=HttpState.omit_skip_path)
51-
if parse_params.body
51+
if parse_params.body and not Configuration.skip_collecting_http_body
5252
else "",
5353
"method": parse_params.method,
5454
"uri": parse_params.uri,
@@ -78,7 +78,9 @@ def parse_response(self, url: str, status_code: int, headers: dict, body: bytes)
7878
if Configuration.verbose and not should_scrub_domain(url):
7979
additional_info = {
8080
"headers": lumigo_dumps(headers, max_size),
81-
"body": lumigo_dumps(body, max_size) if body else "",
81+
"body": lumigo_dumps(body, max_size)
82+
if body and not Configuration.skip_collecting_http_body
83+
else "",
8284
"statusCode": status_code,
8385
}
8486
else:

src/lumigo_tracer/wrappers/http/sync_http_wrappers.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,12 @@ def add_unparsed_request(span_id: Optional[str], parse_params: HttpRequest) -> O
7474
if http_info.get("host") == parse_params.host:
7575
if "response" not in http_info:
7676
SpansContainer.get_span().get_span_by_id(span_id)
77-
http_info["request"]["body"] = concat_old_body_to_new(
78-
http_info.get("request", {}).get("body"), parse_params.body
77+
http_info["request"]["body"] = (
78+
concat_old_body_to_new(
79+
http_info.get("request", {}).get("body"), parse_params.body
80+
)
81+
if not Configuration.skip_collecting_http_body
82+
else ""
7983
)
8084
if HttpState.previous_span_id == span_id and HttpState.previous_request:
8185
HttpState.previous_request.body += parse_params.body
@@ -130,7 +134,7 @@ def _update_request_data_increased_size_limit(http_info: dict, max_size: int) ->
130134
max_size,
131135
omit_skip_path=HttpState.omit_skip_path,
132136
)
133-
if HttpState.previous_request.body
137+
if HttpState.previous_request.body and not Configuration.skip_collecting_http_body
134138
else "",
135139
"headers": lumigo_dumps(HttpState.previous_request.headers, max_size),
136140
}

src/test/unit/test_tracer.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
EXECUTION_TAGS_KEY,
2323
report_json,
2424
EDGE_KINESIS_STREAM_NAME,
25+
SKIP_COLLECTING_HTTP_BODY_KEY,
2526
)
2627

2728
from lumigo_tracer.spans_container import SpansContainer
@@ -203,6 +204,30 @@ def lambda_test_function(event, context):
203204
assert any(line == "hello" for line in capturer.get_lines())
204205

205206

207+
@pytest.mark.parametrize("is_verbose", [True, False])
208+
def test_skip_collecting_http_parts(monkeypatch, context, is_verbose):
209+
if is_verbose:
210+
monkeypatch.setenv("LUMIGO_VERBOSE", "false")
211+
else:
212+
monkeypatch.setenv(SKIP_COLLECTING_HTTP_BODY_KEY, "true")
213+
214+
@lumigo_tracer()
215+
def lambda_test_function(event, context):
216+
conn = http.client.HTTPConnection("www.google.com")
217+
conn.request("POST", "/", json.dumps({"a": "b"}))
218+
return {"hello": "world"}
219+
220+
lambda_test_function({}, context)
221+
http_spans = list(SpansContainer.get_span().spans.values())
222+
assert http_spans[0]["info"]["httpInfo"]["request"]["body"] == ""
223+
if is_verbose:
224+
assert "uri" not in http_spans[0]["info"]["httpInfo"]["request"]
225+
assert "headers" not in http_spans[0]["info"]["httpInfo"]["request"]
226+
else:
227+
assert http_spans[0]["info"]["httpInfo"]["request"]["uri"] == "www.google.com/"
228+
assert http_spans[0]["info"]["httpInfo"]["request"]["headers"]
229+
230+
206231
def test_lumigo_chalice(context):
207232
class App:
208233
@property

0 commit comments

Comments
 (0)