Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: collecting messaging.* span attributes in botocore instrumentation #1

Closed
wants to merge 8 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
([#1242](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/1242))
- `opentelemetry-util-http` Add support for sanitizing HTTP header values.
([#1253](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/1253))
- `opentelemetry-instrumentation-botocore` Support messaging system in the sqs extension.
([#1346](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/1346))

### Fixed

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,12 @@
from opentelemetry.instrumentation.botocore.extensions.types import (
_AttributeMapT,
_AwsSdkExtension,
_BotoResultT,
)
from opentelemetry.semconv.trace import SpanAttributes
from opentelemetry.trace.span import Span

SUPPORTED_OPERATIONS = ["SendMessage", "SendMessageBatch", "ReceiveMessage"]


class _SqsExtension(_AwsSdkExtension):
Expand All @@ -24,3 +29,27 @@ def extract_attributes(self, attributes: _AttributeMapT):
if queue_url:
# TODO: update when semantic conventions exist
attributes["aws.queue_url"] = queue_url
attributes[SpanAttributes.MESSAGING_SYSTEM] = "aws.sqs"
attributes[SpanAttributes.MESSAGING_URL] = queue_url
attributes[SpanAttributes.MESSAGING_DESTINATION] = queue_url.split(
"/"
)[-1]

def on_success(self, span: Span, result: _BotoResultT):
operation = self._call_context.operation
if operation in SUPPORTED_OPERATIONS:
saartochner-lumigo marked this conversation as resolved.
Show resolved Hide resolved
if operation == "SendMessage":
span.set_attribute(
SpanAttributes.MESSAGING_MESSAGE_ID,
result.get("MessageId"),
)
elif operation == "SendMessageBatch" and result.get("Successful"):
span.set_attribute(
SpanAttributes.MESSAGING_MESSAGE_ID,
result["Successful"][0]["MessageId"],
)
elif operation == "ReceiveMessage" and result.get("Messages"):
span.set_attribute(
SpanAttributes.MESSAGING_MESSAGE_ID,
result["Messages"][0]["MessageId"],
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
import botocore.session
from moto import mock_sqs

from opentelemetry.instrumentation.botocore import BotocoreInstrumentor
from opentelemetry.semconv.trace import SpanAttributes
from opentelemetry.test.test_base import TestBase


class TestDynamoDbExtension(TestBase):
def setUp(self):
super().setUp()
BotocoreInstrumentor().instrument()

session = botocore.session.get_session()
session.set_credentials(
access_key="access-key", secret_key="secret-key"
)
self.region = "us-west-2"
self.client = session.create_client("sqs", region_name=self.region)

def tearDown(self):
super().tearDown()
BotocoreInstrumentor().uninstrument()

@mock_sqs
def test_sqs_messaging_send_message(self):
create_queue_result = self.client.create_queue(
QueueName="test_queue_name"
)
queue_url = create_queue_result["QueueUrl"]
response = self.client.send_message(
QueueUrl=queue_url, MessageBody="content"
)

spans = self.memory_exporter.get_finished_spans()
assert spans
self.assertEqual(len(spans), 2)
span = spans[1]
self.assertEqual(
span.attributes[SpanAttributes.MESSAGING_SYSTEM], "aws.sqs"
)
self.assertEqual(
span.attributes[SpanAttributes.MESSAGING_URL], queue_url
)
self.assertEqual(
span.attributes[SpanAttributes.MESSAGING_DESTINATION],
"test_queue_name",
)
self.assertEqual(
span.attributes[SpanAttributes.MESSAGING_MESSAGE_ID],
response["MessageId"],
)

@mock_sqs
def test_sqs_messaging_send_message_batch(self):
create_queue_result = self.client.create_queue(
QueueName="test_queue_name"
)
queue_url = create_queue_result["QueueUrl"]
response = self.client.send_message_batch(
QueueUrl=queue_url,
Entries=[
{"Id": "1", "MessageBody": "content"},
{"Id": "2", "MessageBody": "content2"},
],
)

spans = self.memory_exporter.get_finished_spans()
assert spans
self.assertEqual(len(spans), 2)
span = spans[1]
self.assertEqual(
span.attributes[SpanAttributes.MESSAGING_SYSTEM], "aws.sqs"
)
self.assertEqual(
span.attributes[SpanAttributes.MESSAGING_URL], queue_url
)
self.assertEqual(
span.attributes[SpanAttributes.MESSAGING_DESTINATION],
"test_queue_name",
)
self.assertEqual(
span.attributes[SpanAttributes.MESSAGING_MESSAGE_ID],
response["Successful"][0]["MessageId"],
)

@mock_sqs
def test_sqs_messaging_receive_message(self):
create_queue_result = self.client.create_queue(
QueueName="test_queue_name"
)
queue_url = create_queue_result["QueueUrl"]
self.client.send_message(QueueUrl=queue_url, MessageBody="content")
message_result = self.client.receive_message(
QueueUrl=create_queue_result["QueueUrl"]
)

spans = self.memory_exporter.get_finished_spans()
assert spans
self.assertEqual(len(spans), 3)
span = spans[-1]
self.assertEqual(
span.attributes[SpanAttributes.MESSAGING_SYSTEM], "aws.sqs"
)
self.assertEqual(
span.attributes[SpanAttributes.MESSAGING_URL], queue_url
)
self.assertEqual(
span.attributes[SpanAttributes.MESSAGING_DESTINATION],
"test_queue_name",
)
self.assertEqual(
span.attributes[SpanAttributes.MESSAGING_MESSAGE_ID],
message_result["Messages"][0]["MessageId"],
)

@mock_sqs
def test_sqs_messaging_failed_operation(self):
try:
self.client.send_message(
QueueUrl="non-existing", MessageBody="content"
)
except Exception:
pass

spans = self.memory_exporter.get_finished_spans()
assert spans
self.assertEqual(len(spans), 1)
span = spans[0]
self.assertEqual(
span.attributes[SpanAttributes.MESSAGING_SYSTEM], "aws.sqs"
)
self.assertEqual(
span.attributes[SpanAttributes.MESSAGING_URL], "non-existing"
)