Skip to content

Commit 82603f0

Browse files
authored
Update partial transaction failure handling and add test (elastic#1818)
1 parent 77a2251 commit 82603f0

File tree

2 files changed

+30
-1
lines changed

2 files changed

+30
-1
lines changed

elasticapm/contrib/serverless/aws.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
import json
3535
import os
3636
import platform
37+
import re
3738
import time
3839
import urllib
3940
import warnings
@@ -453,7 +454,7 @@ def send_partial_transaction(self):
453454
},
454455
)
455456
except Exception as e:
456-
if "HTTP 404" in str(e):
457+
if re.match(r"HTTP [4,5]\d\d", str(e)):
457458
REGISTER_PARTIAL_TRANSACTIONS = False
458459
logger.info(
459460
"APM Lambda Extension does not support partial transactions. "

tests/contrib/serverless/aws_tests.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -411,6 +411,9 @@ def test_func(event, context):
411411

412412

413413
def test_partial_transaction(event_api, context, sending_elasticapm_client):
414+
import elasticapm.contrib.serverless.aws
415+
416+
elasticapm.contrib.serverless.aws.REGISTER_PARTIAL_TRANSACTIONS = True
414417
os.environ["AWS_LAMBDA_FUNCTION_NAME"] = "test_func"
415418
os.environ["ELASTIC_APM_LAMBDA_APM_SERVER"] = "http://localhost:8200"
416419

@@ -427,3 +430,28 @@ def test_func(event, context):
427430
assert b"metadata" in request.data
428431
assert b"transaction" in request.data
429432
sending_elasticapm_client.close()
433+
434+
435+
def test_partial_transaction_failure(event_api, context, sending_elasticapm_client):
436+
import elasticapm.contrib.serverless.aws
437+
438+
elasticapm.contrib.serverless.aws.REGISTER_PARTIAL_TRANSACTIONS = True
439+
os.environ["AWS_LAMBDA_FUNCTION_NAME"] = "test_func"
440+
os.environ["ELASTIC_APM_LAMBDA_APM_SERVER"] = "http://localhost:8200"
441+
sending_elasticapm_client.httpserver.code = 404
442+
sending_elasticapm_client.httpserver.content = "go away"
443+
444+
@capture_serverless
445+
def test_func(event, context):
446+
return {"statusCode": 200, "headers": {"foo": "bar"}}
447+
448+
test_func(event_api, context)
449+
test_func(event_api, context)
450+
451+
assert len(sending_elasticapm_client.httpserver.requests) == 3
452+
request = sending_elasticapm_client.httpserver.requests[0]
453+
assert request.full_path == "/register/transaction?"
454+
assert request.content_type == "application/vnd.elastic.apm.transaction+ndjson"
455+
assert b"metadata" in request.data
456+
assert b"transaction" in request.data
457+
sending_elasticapm_client.close()

0 commit comments

Comments
 (0)