Skip to content

Commit b7515d6

Browse files
authored
add connection id (#209)
1 parent 1b78e86 commit b7515d6

File tree

7 files changed

+48
-2
lines changed

7 files changed

+48
-2
lines changed

.circleci/config.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ jobs:
5353
- run: echo "export AWS_DEFAULT_REGION=us-west-2" >> $BASH_ENV
5454
- run: mkdir -p ~/.aws
5555
- run: echo ${KEY} | gpg --batch -d --passphrase-fd 0 ../common-resources/encrypted_files/credentials_integration.enc > ~/.aws/credentials
56+
- run: . venv/bin/activate && pip uninstall lumigo_tracer -y && python setup.py develop
5657
- run: . venv/bin/activate && ./scripts/checks.sh
5758
- run: ../utils/common_bash/defaults/code_cov.sh
5859

requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,4 @@ pymongo==3.11.0
1010
redispy==3.0.0
1111
sqlalchemy==1.3.20
1212
moto==1.3.16
13+
urllib3==1.26.6

src/lumigo_tracer/spans_container.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737

3838

3939
class SpansContainer:
40+
lambda_container_id = str(uuid.uuid4())
4041
is_cold = True
4142
_span: Optional["SpansContainer"] = None
4243

@@ -73,6 +74,7 @@ def __init__(
7374
self.transaction_id = transaction_id
7475
self.max_finish_time = max_finish_time
7576
self.base_msg = {
77+
"lambda_container_id": SpansContainer.lambda_container_id,
7678
"started": started,
7779
"transactionId": transaction_id,
7880
"account": account,

src/lumigo_tracer/wrappers/http/http_data_classes.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,15 @@ class HttpRequest:
88
uri: str
99
headers: dict
1010
body: bytes
11+
instance_id: Optional[bytes]
1112

1213
def __init__(self, **kwargs):
1314
self.host = kwargs["host"]
1415
self.method = kwargs["method"]
1516
self.uri = kwargs["uri"]
1617
self.headers = {k.lower(): v for k, v in (kwargs.get("headers") or {}).items()}
1718
self.body = kwargs.get("body")
19+
self.instance_id = kwargs.get("instance_id")
1820

1921
def clone(self, **kwargs):
2022
clone_obj = deepcopy(self)

src/lumigo_tracer/wrappers/http/http_parser.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,11 +51,13 @@ def parse_request(self, parse_params: HttpRequest) -> dict:
5151
else "",
5252
"method": parse_params.method,
5353
"uri": parse_params.uri,
54+
"instance_id": parse_params.instance_id,
5455
}
5556
else:
5657
additional_info = {
5758
"method": parse_params.method if parse_params else "",
5859
"body": "The data is not available",
60+
"instance_id": parse_params.instance_id,
5961
}
6062

6163
return {

src/lumigo_tracer/wrappers/http/sync_http_wrappers.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -175,10 +175,19 @@ def _http_send_wrapper(func, instance, args, kwargs):
175175
with lumigo_safe_execute("add request event"):
176176
if headers:
177177
add_request_event(
178-
HttpRequest(host=host, method=method, uri=uri, headers=headers, body=body)
178+
HttpRequest(
179+
host=host,
180+
method=method,
181+
uri=uri,
182+
headers=headers,
183+
body=body,
184+
instance_id=id(instance),
185+
)
179186
)
180187
else:
181-
add_unparsed_request(HttpRequest(host=host, method=method, uri=uri, body=data))
188+
add_unparsed_request(
189+
HttpRequest(host=host, method=method, uri=uri, body=data, instance_id=id(instance))
190+
)
182191

183192
ret_val = func(*args, **kwargs)
184193
with lumigo_safe_execute("add response event"):
@@ -224,6 +233,7 @@ def _requests_wrapper(func, instance, args, kwargs):
224233
uri=url,
225234
body=kwargs.get("data"),
226235
headers=kwargs.get("headers"),
236+
instance_id=id(instance),
227237
)
228238
)
229239
SpansContainer.add_exception_to_span(span, exception, [])

src/test/unit/wrappers/http/test_sync_http_wrappers.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import pytest
1313
import urllib3
1414
import requests
15+
from urllib3 import HTTPConnectionPool
1516

1617
import lumigo_tracer
1718
from lumigo_tracer import lumigo_utils
@@ -43,6 +44,7 @@ def lambda_test_function(event, context):
4344
assert http_spans[0]["started"] > SpansContainer.get_span().function_span["started"]
4445
assert "ended" in http_spans[0]
4546
assert "content-length" in http_spans[0]["info"]["httpInfo"]["request"]["headers"]
47+
assert http_spans[0]["info"]["httpInfo"]["request"].get("instance_id") is not None
4648

4749

4850
def test_lambda_wrapper_query_with_http_params(context):
@@ -55,6 +57,7 @@ def lambda_test_function(event, context):
5557

5658
assert http_spans
5759
assert http_spans[0]["info"]["httpInfo"]["request"]["uri"] == "www.google.com/?q=123"
60+
assert http_spans[0]["info"]["httpInfo"]["request"].get("instance_id") is not None
5861

5962

6063
def test_uri_requests(context):
@@ -69,6 +72,7 @@ def lambda_test_function(event, context):
6972

7073
assert http_spans
7174
assert http_spans[0]["info"]["httpInfo"]["request"]["uri"] == "www.google.com/?q=123"
75+
assert http_spans[0]["info"]["httpInfo"]["request"].get("instance_id") is not None
7276

7377

7478
def test_lambda_wrapper_get_response(context):
@@ -83,6 +87,7 @@ def lambda_test_function(event, context):
8387

8488
assert http_spans
8589
assert http_spans[0]["info"]["httpInfo"]["response"]["statusCode"] == 200
90+
assert http_spans[0]["info"]["httpInfo"]["request"].get("instance_id") is not None
8691

8792

8893
def test_lambda_wrapper_http_splitted_send(context):
@@ -102,6 +107,7 @@ def lambda_test_function(event, context):
102107
assert http_spans
103108
assert http_spans[0]["info"]["httpInfo"]["request"]["body"] == '"123456"'
104109
assert "content-length" in http_spans[0]["info"]["httpInfo"]["request"]["headers"]
110+
assert http_spans[0]["info"]["httpInfo"]["request"].get("instance_id") is not None
105111

106112

107113
def test_lambda_wrapper_no_headers(context):
@@ -152,6 +158,7 @@ def lambda_test_function(event, context):
152158
assert len(http_events) == 1
153159
span = SpansContainer.get_span().spans[0]
154160
assert span["info"]["httpInfo"]["request"]["body"] == '"body"'
161+
assert span["info"]["httpInfo"]["request"].get("instance_id") is not None
155162

156163

157164
def test_bad_domains_scrubber(monkeypatch, context):
@@ -176,6 +183,7 @@ def lambda_test_function(event, context):
176183
assert http_events[0].get("info", {}).get("httpInfo", {}).get("host") == "www.google.com"
177184
assert "headers" not in http_events[0]["info"]["httpInfo"]["request"]
178185
assert http_events[0]["info"]["httpInfo"]["request"]["body"] == "The data is not available"
186+
assert http_events[0]["info"]["httpInfo"]["request"].get("instance_id") is not None
179187

180188

181189
def test_domains_scrubber_override_allows_default_domains(monkeypatch, context):
@@ -193,6 +201,7 @@ def lambda_test_function(event, context):
193201
assert len(http_events) == 1
194202
assert http_events[0].get("info", {}).get("httpInfo", {}).get("host") == ssm_url
195203
assert http_events[0]["info"]["httpInfo"]["request"]["headers"]
204+
assert http_events[0]["info"]["httpInfo"]["request"].get("instance_id") is not None
196205

197206

198207
def test_wrapping_json_request(context):
@@ -241,6 +250,7 @@ def lambda_test_function(event, context):
241250
assert event["info"]["httpInfo"]["response"]["body"]
242251
assert event["info"]["httpInfo"]["response"]["statusCode"] == 200
243252
assert event["info"]["httpInfo"]["host"] == "www.google.com"
253+
assert event["info"]["httpInfo"]["request"].get("instance_id") is not None
244254

245255

246256
def test_wrapping_requests_times(monkeypatch, context):
@@ -292,6 +302,7 @@ def lambda_test_function(event, context):
292302
assert span["info"]["httpInfo"]["request"]["method"] == "POST"
293303
assert span["info"]["httpInfo"]["request"]["body"] == '"123"'
294304
assert span["info"]["httpInfo"]["request"]["headers"]
305+
assert span["info"]["httpInfo"]["request"].get("instance_id") is not None
295306

296307

297308
def test_requests_failure_with_kwargs(monkeypatch, context):
@@ -342,6 +353,8 @@ def lambda_test_function(event, context):
342353
spans = SpansContainer.get_span().spans
343354
assert spans[0]["info"]["httpInfo"]["request"]["headers"] == json.dumps({"a": "b"})
344355
assert spans[1]["info"]["httpInfo"]["request"]["headers"] == json.dumps({"c": "d"})
356+
assert spans[0]["info"]["httpInfo"]["request"].get("instance_id") is not None
357+
assert spans[1]["info"]["httpInfo"]["request"].get("instance_id") is not None
345358

346359

347360
def api_gw_event() -> Dict:
@@ -558,3 +571,18 @@ def lambda_test_function(event, context):
558571
def test_is_lumigo_edge(host, is_lumigo, monkeypatch):
559572
monkeypatch.setattr(Configuration, "host", "lumigo.io")
560573
assert is_lumigo_edge(host) == is_lumigo
574+
575+
576+
def test_same_connection_id_for_same_connection(context):
577+
@lumigo_tracer.lumigo_tracer(token=TOKEN)
578+
def lambda_test_function(event, context):
579+
pool = HTTPConnectionPool("www.google.com", maxsize=1)
580+
pool.request("GET", "/?q=123")
581+
pool.request("GET", "/?q=1234")
582+
583+
lambda_test_function({}, context)
584+
http_spans = SpansContainer.get_span().spans
585+
586+
instance_id_1 = http_spans[0]["info"]["httpInfo"]["request"].get("instance_id")
587+
instance_id_2 = http_spans[1]["info"]["httpInfo"]["request"].get("instance_id")
588+
assert instance_id_1 == instance_id_2

0 commit comments

Comments
 (0)