Skip to content

Commit d613bd3

Browse files
authored
A rd 5221 alb triggered by support (#198)
* add trigger by load_balancer
1 parent 8913043 commit d613bd3

File tree

2 files changed

+94
-3
lines changed

2 files changed

+94
-3
lines changed

src/lumigo_tracer/event/event_trigger.py

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ def parse_triggered_by(event: dict):
3131
return None
3232
if _is_supported_http_method(event):
3333
return _parse_http_method(event)
34+
if _is_load_balancer_method(event):
35+
return _parse_load_balancer_method(event)
3436
elif _is_supported_sns(event):
3537
return _parse_sns(event)
3638
elif _is_supported_streams(event):
@@ -77,7 +79,20 @@ def _is_supported_http_method(event: dict):
7779
and event.get("requestContext", {}).get("stage") is not None # noqa
7880
) or ( # noqa
7981
event.get("version", "") == "2.0" and "headers" in event # noqa
80-
) # noqa # noqa
82+
) # noqa
83+
84+
85+
def _is_load_balancer_method(event: dict):
86+
return (
87+
"httpMethod" in event # noqa
88+
and "headers" in event # noqa
89+
and event["headers"].get("host") # noqa
90+
and "requestContext" in event # noqa
91+
and ( # noqa
92+
event.get("requestContext", {}).get("elb") is not None # noqa
93+
or event.get("requestContext", {}).get("alb") is not None # noqa
94+
) # noqa
95+
)
8196

8297

8398
def _parse_http_method(event: dict):
@@ -87,6 +102,16 @@ def _parse_http_method(event: dict):
87102
return _parse_http_method_v1(event)
88103

89104

105+
def _parse_load_balancer_method(event: dict):
106+
result = {
107+
"triggeredBy": "load_balancer",
108+
"httpMethod": event.get("httpMethod", ""),
109+
}
110+
if isinstance(event.get("headers"), dict):
111+
result["api"] = event["headers"].get("host")
112+
return result
113+
114+
90115
def _parse_http_method_v1(event: dict):
91116
result = {
92117
"triggeredBy": "apigw",

src/test/unit/event/test_event_trigger.py

Lines changed: 68 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -282,7 +282,7 @@
282282
"detailType": "Scheduled Event",
283283
},
284284
),
285-
( # elb example trigger
285+
( # old elb example trigger
286286
{
287287
"requestContext": {
288288
"elb": {
@@ -302,7 +302,73 @@
302302
"host": "lambd-loadb-bp68mp6nujg0-50156485.us-east-1.elb.amazonaws.com",
303303
},
304304
},
305-
{"triggeredBy": "unknown"},
305+
{
306+
"api": "lambd-loadb-bp68mp6nujg0-50156485.us-east-1.elb.amazonaws.com",
307+
"httpMethod": "POST",
308+
"triggeredBy": "load_balancer",
309+
},
310+
),
311+
( # new elb example trigger
312+
{
313+
"requestContext": {
314+
"elb": {
315+
"targetGroupArn": "arn:aws:elasticloadbalancing:region:123456789012:targetgroup/my-target-group/111"
316+
}
317+
},
318+
"httpMethod": "GET",
319+
"path": "/",
320+
"queryStringParameters": {},
321+
"headers": {
322+
"accept": "text/html,application/xhtml+xml",
323+
"accept-language": "en-US,en;q=0.8",
324+
"content-type": "text/plain",
325+
"cookie": "cookies",
326+
"host": "lambda-111-us-east-2.elb.amazonaws.com",
327+
"user-agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_6)",
328+
"x-amzn-trace-id": "Root=1-111",
329+
"x-forwarded-for": "111",
330+
"x-forwarded-port": "111",
331+
"x-forwarded-proto": "https",
332+
},
333+
"isBase64Encoded": False,
334+
"body": "request_body",
335+
},
336+
{
337+
"api": "lambda-111-us-east-2.elb.amazonaws.com",
338+
"httpMethod": "GET",
339+
"triggeredBy": "load_balancer",
340+
},
341+
),
342+
( # alb example trigger
343+
{
344+
"requestContext": {
345+
"alb": {
346+
"targetGroupArn": "arn:aws:elasticloadbalancing:region:123456789012:targetgroup/my-target-group/111"
347+
}
348+
},
349+
"httpMethod": "GET",
350+
"path": "/",
351+
"queryStringParameters": {},
352+
"headers": {
353+
"accept": "text/html,application/xhtml+xml",
354+
"accept-language": "en-US,en;q=0.8",
355+
"content-type": "text/plain",
356+
"cookie": "cookies",
357+
"host": "lambda-111-us-east-2.elb.amazonaws.com",
358+
"user-agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_6)",
359+
"x-amzn-trace-id": "Root=1-111",
360+
"x-forwarded-for": "111",
361+
"x-forwarded-port": "111",
362+
"x-forwarded-proto": "https",
363+
},
364+
"isBase64Encoded": False,
365+
"body": "request_body",
366+
},
367+
{
368+
"api": "lambda-111-us-east-2.elb.amazonaws.com",
369+
"httpMethod": "GET",
370+
"triggeredBy": "load_balancer",
371+
},
306372
),
307373
( # API GW V2
308374
{

0 commit comments

Comments
 (0)