Skip to content

Commit 27f63e2

Browse files
authored
Merge branch 'main' into fix-metric-attributes-missing-tracing-disabled-httpx-instrumentation
2 parents 0682ab0 + 80c357b commit 27f63e2

File tree

4 files changed

+138
-32
lines changed

4 files changed

+138
-32
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1111
1212
## Unreleased
1313

14+
15+
### Added
16+
17+
- `opentelemetry-instrumentation-pika` Added instrumentation for All `SelectConnection` adapters
18+
([#3584](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/3584))
19+
1420
### Fixed
1521

1622
- `opentelemetry-instrumentation-httpx`: fix missing metric response attributes when tracing is disabled

instrumentation/opentelemetry-instrumentation-mysql/tests/test_mysql_integration.py

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,9 @@
2020
from opentelemetry import trace as trace_api
2121
from opentelemetry.instrumentation.mysql import MySQLInstrumentor
2222
from opentelemetry.sdk import resources
23-
from opentelemetry.semconv.trace import SpanAttributes
23+
from opentelemetry.semconv._incubating.attributes.db_attributes import (
24+
DB_STATEMENT,
25+
)
2426
from opentelemetry.test.test_base import TestBase
2527

2628

@@ -157,7 +159,7 @@ def test_instrument_connection_with_dbapi_sqlcomment_enabled(self):
157159
f"Select 1 /*db_driver='mysql.connector%%3Afoobar',dbapi_level='123',dbapi_threadsafety='123',driver_paramstyle='test',mysql_client_version='foobaz',traceparent='00-{trace_id}-{span_id}-01'*/;",
158160
)
159161
self.assertEqual(
160-
span.attributes[SpanAttributes.DB_STATEMENT],
162+
span.attributes[DB_STATEMENT],
161163
"Select 1;",
162164
)
163165

@@ -196,7 +198,7 @@ def test_instrument_connection_with_dbapi_sqlcomment_enabled_stmt_enabled(
196198
f"Select 1 /*db_driver='mysql.connector%%3Afoobar',dbapi_level='123',dbapi_threadsafety='123',driver_paramstyle='test',mysql_client_version='foobaz',traceparent='00-{trace_id}-{span_id}-01'*/;",
197199
)
198200
self.assertEqual(
199-
span.attributes[SpanAttributes.DB_STATEMENT],
201+
span.attributes[DB_STATEMENT],
200202
f"Select 1 /*db_driver='mysql.connector%%3Afoobar',dbapi_level='123',dbapi_threadsafety='123',driver_paramstyle='test',mysql_client_version='foobaz',traceparent='00-{trace_id}-{span_id}-01'*/;",
201203
)
202204

@@ -239,7 +241,7 @@ def test_instrument_connection_with_dbapi_sqlcomment_enabled_with_options(
239241
f"Select 1 /*db_driver='mysql.connector%%3Afoobar',dbapi_threadsafety='123',mysql_client_version='foobaz',traceparent='00-{trace_id}-{span_id}-01'*/;",
240242
)
241243
self.assertEqual(
242-
span.attributes[SpanAttributes.DB_STATEMENT],
244+
span.attributes[DB_STATEMENT],
243245
"Select 1;",
244246
)
245247

@@ -274,7 +276,7 @@ def test_instrument_connection_with_dbapi_sqlcomment_not_enabled_default(
274276
spans_list = self.memory_exporter.get_finished_spans()
275277
span = spans_list[0]
276278
self.assertEqual(
277-
span.attributes[SpanAttributes.DB_STATEMENT],
279+
span.attributes[DB_STATEMENT],
278280
"Select 1;",
279281
)
280282

@@ -332,7 +334,7 @@ def test_instrument_with_dbapi_sqlcomment_enabled(
332334
f"Select 1 /*db_driver='mysql.connector%%3Afoobar',dbapi_level='123',dbapi_threadsafety='123',driver_paramstyle='test',mysql_client_version='foobaz',traceparent='00-{trace_id}-{span_id}-01'*/;",
333335
)
334336
self.assertEqual(
335-
span.attributes[SpanAttributes.DB_STATEMENT],
337+
span.attributes[DB_STATEMENT],
336338
"Select 1;",
337339
)
338340

@@ -373,7 +375,7 @@ def test_instrument_with_dbapi_sqlcomment_enabled_stmt_enabled(
373375
f"Select 1 /*db_driver='mysql.connector%%3Afoobar',dbapi_level='123',dbapi_threadsafety='123',driver_paramstyle='test',mysql_client_version='foobaz',traceparent='00-{trace_id}-{span_id}-01'*/;",
374376
)
375377
self.assertEqual(
376-
span.attributes[SpanAttributes.DB_STATEMENT],
378+
span.attributes[DB_STATEMENT],
377379
f"Select 1 /*db_driver='mysql.connector%%3Afoobar',dbapi_level='123',dbapi_threadsafety='123',driver_paramstyle='test',mysql_client_version='foobaz',traceparent='00-{trace_id}-{span_id}-01'*/;",
378380
)
379381

@@ -418,7 +420,7 @@ def test_instrument_with_dbapi_sqlcomment_enabled_with_options(
418420
f"Select 1 /*db_driver='mysql.connector%%3Afoobar',dbapi_threadsafety='123',mysql_client_version='foobaz',traceparent='00-{trace_id}-{span_id}-01'*/;",
419421
)
420422
self.assertEqual(
421-
span.attributes[SpanAttributes.DB_STATEMENT],
423+
span.attributes[DB_STATEMENT],
422424
"Select 1;",
423425
)
424426

@@ -453,7 +455,7 @@ def test_instrument_with_dbapi_sqlcomment_not_enabled_default(
453455
spans_list = self.memory_exporter.get_finished_spans()
454456
span = spans_list[0]
455457
self.assertEqual(
456-
span.attributes[SpanAttributes.DB_STATEMENT],
458+
span.attributes[DB_STATEMENT],
457459
"Select 1;",
458460
)
459461

instrumentation/opentelemetry-instrumentation-pika/src/opentelemetry/instrumentation/pika/pika_instrumentor.py

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
# pylint: disable=unnecessary-dunder-call
1515

1616
from logging import getLogger
17-
from typing import Any, Collection, Dict, Optional
17+
from typing import Any, Collection, Dict, Optional, Union
1818

1919
import pika
2020
import wrapt
@@ -24,6 +24,8 @@
2424
BlockingChannel,
2525
_QueueConsumerGeneratorInfo,
2626
)
27+
from pika.channel import Channel
28+
from pika.connection import Connection
2729

2830
from opentelemetry import trace
2931
from opentelemetry.instrumentation.instrumentor import BaseInstrumentor
@@ -53,12 +55,16 @@ class PikaInstrumentor(BaseInstrumentor): # type: ignore
5355

5456
# pylint: disable=attribute-defined-outside-init
5557
@staticmethod
56-
def _instrument_blocking_channel_consumers(
57-
channel: BlockingChannel,
58+
def _instrument_channel_consumers(
59+
channel: Union[BlockingChannel, Channel],
5860
tracer: Tracer,
5961
consume_hook: utils.HookT = utils.dummy_callback,
6062
) -> Any:
61-
for consumer_tag, consumer_info in channel._consumer_infos.items():
63+
if isinstance(channel, BlockingChannel):
64+
consumer_infos = channel._consumer_infos
65+
elif isinstance(channel, Channel):
66+
consumer_infos = channel._consumers
67+
for consumer_tag, consumer_info in consumer_infos.items():
6268
callback_attr = PikaInstrumentor.CONSUMER_CALLBACK_ATTR
6369
consumer_callback = getattr(consumer_info, callback_attr, None)
6470
if consumer_callback is None:
@@ -79,7 +85,7 @@ def _instrument_blocking_channel_consumers(
7985

8086
@staticmethod
8187
def _instrument_basic_publish(
82-
channel: BlockingChannel,
88+
channel: Union[BlockingChannel, Channel],
8389
tracer: Tracer,
8490
publish_hook: utils.HookT = utils.dummy_callback,
8591
) -> None:
@@ -93,7 +99,7 @@ def _instrument_basic_publish(
9399

94100
@staticmethod
95101
def _instrument_channel_functions(
96-
channel: BlockingChannel,
102+
channel: Union[BlockingChannel, Channel],
97103
tracer: Tracer,
98104
publish_hook: utils.HookT = utils.dummy_callback,
99105
) -> None:
@@ -103,7 +109,9 @@ def _instrument_channel_functions(
103109
)
104110

105111
@staticmethod
106-
def _uninstrument_channel_functions(channel: BlockingChannel) -> None:
112+
def _uninstrument_channel_functions(
113+
channel: Union[BlockingChannel, Channel],
114+
) -> None:
107115
for function_name in _FUNCTIONS_TO_UNINSTRUMENT:
108116
if not hasattr(channel, function_name):
109117
continue
@@ -115,7 +123,7 @@ def _uninstrument_channel_functions(channel: BlockingChannel) -> None:
115123
@staticmethod
116124
# Make sure that the spans are created inside hash them set as parent and not as brothers
117125
def instrument_channel(
118-
channel: BlockingChannel,
126+
channel: Union[BlockingChannel, Channel],
119127
tracer_provider: Optional[TracerProvider] = None,
120128
publish_hook: utils.HookT = utils.dummy_callback,
121129
consume_hook: utils.HookT = utils.dummy_callback,
@@ -133,7 +141,7 @@ def instrument_channel(
133141
tracer_provider,
134142
schema_url="https://opentelemetry.io/schemas/1.11.0",
135143
)
136-
PikaInstrumentor._instrument_blocking_channel_consumers(
144+
PikaInstrumentor._instrument_channel_consumers(
137145
channel, tracer, consume_hook
138146
)
139147
PikaInstrumentor._decorate_basic_consume(channel, tracer, consume_hook)
@@ -178,16 +186,17 @@ def wrapper(wrapped, instance, args, kwargs):
178186
return channel
179187

180188
wrapt.wrap_function_wrapper(BlockingConnection, "channel", wrapper)
189+
wrapt.wrap_function_wrapper(Connection, "channel", wrapper)
181190

182191
@staticmethod
183192
def _decorate_basic_consume(
184-
channel: BlockingChannel,
193+
channel: Union[BlockingChannel, Channel],
185194
tracer: Optional[Tracer],
186195
consume_hook: utils.HookT = utils.dummy_callback,
187196
) -> None:
188197
def wrapper(wrapped, instance, args, kwargs):
189198
return_value = wrapped(*args, **kwargs)
190-
PikaInstrumentor._instrument_blocking_channel_consumers(
199+
PikaInstrumentor._instrument_channel_consumers(
191200
channel, tracer, consume_hook
192201
)
193202
return return_value
@@ -236,6 +245,7 @@ def _uninstrument(self, **kwargs: Dict[str, Any]) -> None:
236245
if hasattr(self, "__opentelemetry_tracer_provider"):
237246
delattr(self, "__opentelemetry_tracer_provider")
238247
unwrap(BlockingConnection, "channel")
248+
unwrap(Connection, "channel")
239249
unwrap(_QueueConsumerGeneratorInfo, "__init__")
240250

241251
def instrumentation_dependencies(self) -> Collection[str]:

0 commit comments

Comments
 (0)