Skip to content

Commit c8314a8

Browse files
author
CircleCI
committed
sqs-messaging-system from current lumigo's main
1 parent c8020cb commit c8314a8

File tree

2 files changed

+165
-0
lines changed
  • instrumentation/opentelemetry-instrumentation-botocore

2 files changed

+165
-0
lines changed

Diff for: instrumentation/opentelemetry-instrumentation-botocore/src/opentelemetry/instrumentation/botocore/extensions/sqs.py

+29
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,12 @@
1515
from opentelemetry.instrumentation.botocore.extensions.types import (
1616
_AttributeMapT,
1717
_AwsSdkExtension,
18+
_BotoResultT,
1819
)
20+
from opentelemetry.semconv.trace import SpanAttributes
21+
from opentelemetry.trace.span import Span
22+
23+
_SUPPORTED_OPERATIONS = ["SendMessage", "SendMessageBatch", "ReceiveMessage"]
1924

2025

2126
class _SqsExtension(_AwsSdkExtension):
@@ -24,3 +29,27 @@ def extract_attributes(self, attributes: _AttributeMapT):
2429
if queue_url:
2530
# TODO: update when semantic conventions exist
2631
attributes["aws.queue_url"] = queue_url
32+
attributes[SpanAttributes.MESSAGING_SYSTEM] = "aws.sqs"
33+
attributes[SpanAttributes.MESSAGING_URL] = queue_url
34+
attributes[SpanAttributes.MESSAGING_DESTINATION] = queue_url.split(
35+
"/"
36+
)[-1]
37+
38+
def on_success(self, span: Span, result: _BotoResultT):
39+
operation = self._call_context.operation
40+
if operation in _SUPPORTED_OPERATIONS:
41+
if operation == "SendMessage":
42+
span.set_attribute(
43+
SpanAttributes.MESSAGING_MESSAGE_ID,
44+
result.get("MessageId"),
45+
)
46+
elif operation == "SendMessageBatch" and result.get("Successful"):
47+
span.set_attribute(
48+
SpanAttributes.MESSAGING_MESSAGE_ID,
49+
result["Successful"][0]["MessageId"],
50+
)
51+
elif operation == "ReceiveMessage" and result.get("Messages"):
52+
span.set_attribute(
53+
SpanAttributes.MESSAGING_MESSAGE_ID,
54+
result["Messages"][0]["MessageId"],
55+
)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
import botocore.session
2+
from moto import mock_sqs
3+
4+
from opentelemetry.instrumentation.botocore import BotocoreInstrumentor
5+
from opentelemetry.semconv.trace import SpanAttributes
6+
from opentelemetry.test.test_base import TestBase
7+
8+
9+
class TestDynamoDbExtension(TestBase):
10+
def setUp(self):
11+
super().setUp()
12+
BotocoreInstrumentor().instrument()
13+
14+
session = botocore.session.get_session()
15+
session.set_credentials(
16+
access_key="access-key", secret_key="secret-key"
17+
)
18+
self.region = "us-west-2"
19+
self.client = session.create_client("sqs", region_name=self.region)
20+
21+
def tearDown(self):
22+
super().tearDown()
23+
BotocoreInstrumentor().uninstrument()
24+
25+
@mock_sqs
26+
def test_sqs_messaging_send_message(self):
27+
create_queue_result = self.client.create_queue(
28+
QueueName="test_queue_name"
29+
)
30+
queue_url = create_queue_result["QueueUrl"]
31+
response = self.client.send_message(
32+
QueueUrl=queue_url, MessageBody="content"
33+
)
34+
35+
spans = self.memory_exporter.get_finished_spans()
36+
assert spans
37+
self.assertEqual(len(spans), 2)
38+
span = spans[1]
39+
self.assertEqual(
40+
span.attributes[SpanAttributes.MESSAGING_SYSTEM], "aws.sqs"
41+
)
42+
self.assertEqual(
43+
span.attributes[SpanAttributes.MESSAGING_URL], queue_url
44+
)
45+
self.assertEqual(
46+
span.attributes[SpanAttributes.MESSAGING_DESTINATION],
47+
"test_queue_name",
48+
)
49+
self.assertEqual(
50+
span.attributes[SpanAttributes.MESSAGING_MESSAGE_ID],
51+
response["MessageId"],
52+
)
53+
54+
@mock_sqs
55+
def test_sqs_messaging_send_message_batch(self):
56+
create_queue_result = self.client.create_queue(
57+
QueueName="test_queue_name"
58+
)
59+
queue_url = create_queue_result["QueueUrl"]
60+
response = self.client.send_message_batch(
61+
QueueUrl=queue_url,
62+
Entries=[
63+
{"Id": "1", "MessageBody": "content"},
64+
{"Id": "2", "MessageBody": "content2"},
65+
],
66+
)
67+
68+
spans = self.memory_exporter.get_finished_spans()
69+
assert spans
70+
self.assertEqual(len(spans), 2)
71+
span = spans[1]
72+
self.assertEqual(span.attributes["rpc.method"], "SendMessageBatch")
73+
self.assertEqual(
74+
span.attributes[SpanAttributes.MESSAGING_SYSTEM], "aws.sqs"
75+
)
76+
self.assertEqual(
77+
span.attributes[SpanAttributes.MESSAGING_URL], queue_url
78+
)
79+
self.assertEqual(
80+
span.attributes[SpanAttributes.MESSAGING_DESTINATION],
81+
"test_queue_name",
82+
)
83+
self.assertEqual(
84+
span.attributes[SpanAttributes.MESSAGING_MESSAGE_ID],
85+
response["Successful"][0]["MessageId"],
86+
)
87+
88+
@mock_sqs
89+
def test_sqs_messaging_receive_message(self):
90+
create_queue_result = self.client.create_queue(
91+
QueueName="test_queue_name"
92+
)
93+
queue_url = create_queue_result["QueueUrl"]
94+
self.client.send_message(QueueUrl=queue_url, MessageBody="content")
95+
message_result = self.client.receive_message(
96+
QueueUrl=create_queue_result["QueueUrl"]
97+
)
98+
99+
spans = self.memory_exporter.get_finished_spans()
100+
assert spans
101+
self.assertEqual(len(spans), 3)
102+
span = spans[-1]
103+
self.assertEqual(span.attributes["rpc.method"], "ReceiveMessage")
104+
self.assertEqual(
105+
span.attributes[SpanAttributes.MESSAGING_SYSTEM], "aws.sqs"
106+
)
107+
self.assertEqual(
108+
span.attributes[SpanAttributes.MESSAGING_URL], queue_url
109+
)
110+
self.assertEqual(
111+
span.attributes[SpanAttributes.MESSAGING_DESTINATION],
112+
"test_queue_name",
113+
)
114+
self.assertEqual(
115+
span.attributes[SpanAttributes.MESSAGING_MESSAGE_ID],
116+
message_result["Messages"][0]["MessageId"],
117+
)
118+
119+
@mock_sqs
120+
def test_sqs_messaging_failed_operation(self):
121+
with self.assertRaises(Exception):
122+
self.client.send_message(
123+
QueueUrl="non-existing", MessageBody="content"
124+
)
125+
126+
spans = self.memory_exporter.get_finished_spans()
127+
assert spans
128+
self.assertEqual(len(spans), 1)
129+
span = spans[0]
130+
self.assertEqual(span.attributes["rpc.method"], "SendMessage")
131+
self.assertEqual(
132+
span.attributes[SpanAttributes.MESSAGING_SYSTEM], "aws.sqs"
133+
)
134+
self.assertEqual(
135+
span.attributes[SpanAttributes.MESSAGING_URL], "non-existing"
136+
)

0 commit comments

Comments
 (0)