Skip to content

Commit c23ba45

Browse files
committed
fix: AWS Lambda collector.
Fix initialization of spans and metrics collectors for the AWS Lambda Collector. Signed-off-by: Paulo Vital <paulo.vital@ibm.com>
1 parent 5680356 commit c23ba45

4 files changed

Lines changed: 85 additions & 3 deletions

File tree

‎src/instana/agent/serverless.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ def _prepare_payload(self, payload: dict[str, Any]) -> dict[str, Any]:
153153
Returns:
154154
Modified payload with filtered spans
155155
"""
156-
spans = payload.get("spans", [])
156+
spans = payload.get("spans") or []
157157
filtered_spans = self.filter_spans(spans)
158158

159159
if len(filtered_spans) > 0:

‎src/instana/collector/aws_lambda.py‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,8 @@ def should_send_snapshot_data(self):
4646

4747
def prepare_payload(self):
4848
payload = DictionaryOfStan()
49-
payload["spans"] = None
50-
payload["metrics"] = None
49+
payload["spans"] = []
50+
payload["metrics"]["plugins"] = []
5151

5252
if not self.span_queue.empty():
5353
payload["spans"] = format_span(self.queued_spans())

‎tests/agent/test_serverless_agent.py‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,18 @@ def test_prepare_payload_with_no_spans(self) -> None:
205205
assert "metrics" in result
206206
assert "spans" not in result or len(result.get("spans", [])) == 0
207207

208+
def test_prepare_payload_with_none_spans(self) -> None:
209+
"""Test payload preparation when spans are explicitly None."""
210+
agent = ConcreteServerlessAgent()
211+
212+
payload = {"spans": None, "metrics": {"test": "data"}}
213+
214+
result = agent._prepare_payload(payload)
215+
216+
assert "metrics" in result
217+
# Should handle None spans without raising a TypeError
218+
assert result["spans"] is None
219+
208220
def test_build_headers(self) -> None:
209221
"""Test that headers are built correctly."""
210222
agent = ConcreteServerlessAgent()
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
# (c) Copyright IBM Corp. 2026
2+
3+
"""
4+
Unit tests for AWSLambdaCollector.
5+
This test suite validates the payload preparation and metric reporting
6+
for the AWS Lambda instrumentation collector.
7+
"""
8+
9+
from collections import defaultdict
10+
from typing import Generator
11+
from unittest.mock import MagicMock, patch
12+
13+
import pytest
14+
15+
from instana.collector.aws_lambda import AWSLambdaCollector
16+
17+
18+
class TestAWSLambdaCollector:
19+
"""Test suite for AWSLambdaCollector base class."""
20+
21+
@pytest.fixture(autouse=True)
22+
def _resource(
23+
self,
24+
caplog: pytest.LogCaptureFixture,
25+
) -> Generator[None, None, None]:
26+
27+
self.agent = MagicMock()
28+
self.collector = AWSLambdaCollector(self.agent)
29+
yield
30+
31+
def test_prepare_payload_empty_span_queue(self) -> None:
32+
"""Test that prepare_payload returns an empty list for spans when queue is empty."""
33+
payload = self.collector.prepare_payload()
34+
35+
assert isinstance(payload, defaultdict)
36+
37+
assert "spans" in payload
38+
assert payload["spans"] == []
39+
40+
assert "metrics" in payload
41+
assert "plugins" in payload["metrics"]
42+
43+
def test_prepare_payload_with_spans(self) -> None:
44+
"""Test that prepare_payload formats queued spans and includes them in the payload."""
45+
mock_span = MagicMock()
46+
self.collector.span_queue.put(mock_span)
47+
48+
# Mock queued_spans and format_span
49+
self.collector.queued_spans = MagicMock(return_value=[mock_span])
50+
51+
with patch("instana.collector.aws_lambda.format_span") as mock_format:
52+
mock_format.return_value = [{"n": "test_span"}]
53+
payload = self.collector.prepare_payload()
54+
55+
assert payload["spans"] == [{"n": "test_span"}]
56+
assert payload["metrics"]["plugins"] == []
57+
58+
def test_prepare_payload_with_snapshot_data(self) -> None:
59+
"""Test that prepare_payload includes snapshot data when it should be sent."""
60+
self.collector.snapshot_data = {
61+
"plugins": [{"name": "com.instana.plugin.aws.lambda"}]
62+
}
63+
self.collector.snapshot_data_sent = False
64+
65+
payload = self.collector.prepare_payload()
66+
67+
assert payload["metrics"] == {
68+
"plugins": [{"name": "com.instana.plugin.aws.lambda"}]
69+
}
70+
assert self.collector.snapshot_data_sent is True

0 commit comments

Comments
 (0)