Skip to content

Commit 5734cd0

Browse files
authored
Merge branch 'main' into add_redis_client_only_instrumentation
2 parents 9b77998 + 45797ec commit 5734cd0

File tree

4 files changed

+46
-19
lines changed
  • instrumentation
    • opentelemetry-instrumentation-aiokafka/src/opentelemetry/instrumentation/aiokafka
    • opentelemetry-instrumentation-mysqlclient/src/opentelemetry/instrumentation/mysqlclient
    • opentelemetry-instrumentation-pymssql/src/opentelemetry/instrumentation/pymssql
    • opentelemetry-instrumentation-pymysql/src/opentelemetry/instrumentation/pymysql

4 files changed

+46
-19
lines changed

instrumentation/opentelemetry-instrumentation-aiokafka/src/opentelemetry/instrumentation/aiokafka/__init__.py

+32-8
Original file line numberDiff line numberDiff line change
@@ -20,20 +20,35 @@
2020
2121
.. code:: python
2222
23+
import asyncio
2324
from opentelemetry.instrumentation.aiokafka import AIOKafkaInstrumentor
2425
from aiokafka import AIOKafkaProducer, AIOKafkaConsumer
2526
2627
# Instrument kafka
2728
AIOKafkaInstrumentor().instrument()
2829
2930
# report a span of type producer with the default settings
30-
producer = AIOKafkaProducer(bootstrap_servers=['localhost:9092'])
31-
await producer.send('my-topic', b'raw_bytes')
31+
async def produce():
32+
producer = AIOKafkaProducer(bootstrap_servers=['localhost:9092'])
33+
await producer.start()
34+
try:
35+
await producer.send_and_wait('my-topic', b'raw_bytes')
36+
finally:
37+
await producer.stop()
3238
3339
# report a span of type consumer with the default settings
34-
consumer = AIOKafkaConsumer('my-topic', group_id='my-group', bootstrap_servers=['localhost:9092'])
35-
async for message in consumer:
36-
# process message
40+
async def consume():
41+
consumer = AIOKafkaConsumer('my-topic', group_id='my-group', bootstrap_servers=['localhost:9092'])
42+
await consumer.start()
43+
try:
44+
async for message in consumer:
45+
# process message
46+
print(message)
47+
finally:
48+
await consumer.stop()
49+
50+
asyncio.run(produce())
51+
asyncio.run(consume())
3752
3853
The _instrument() method accepts the following keyword args:
3954
tracer_provider (TracerProvider) - an optional tracer provider
@@ -47,12 +62,14 @@ def async_consume_hook(span: Span, record: kafka.record.ABCRecord, args, kwargs)
4762
4863
.. code:: python
4964
50-
from opentelemetry.instrumentation.kafka import AIOKafkaInstrumentor
65+
import asyncio
66+
from opentelemetry.instrumentation.aiokafka import AIOKafkaInstrumentor
5167
from aiokafka import AIOKafkaProducer, AIOKafkaConsumer
5268
5369
async def async_produce_hook(span, args, kwargs):
5470
if span and span.is_recording():
5571
span.set_attribute("custom_user_attribute_from_async_response_hook", "some-value")
72+
5673
async def async_consume_hook(span, record, args, kwargs):
5774
if span and span.is_recording():
5875
span.set_attribute("custom_user_attribute_from_consume_hook", "some-value")
@@ -62,8 +79,15 @@ async def async_consume_hook(span, record, args, kwargs):
6279
6380
# Using kafka as normal now will automatically generate spans,
6481
# including user custom attributes added from the hooks
65-
producer = AIOKafkaProducer(bootstrap_servers=['localhost:9092'])
66-
await producer.send('my-topic', b'raw_bytes')
82+
async def produce():
83+
producer = AIOKafkaProducer(bootstrap_servers=['localhost:9092'])
84+
await producer.start()
85+
try:
86+
await producer.send_and_wait('my-topic', b'raw_bytes')
87+
finally:
88+
await producer.stop()
89+
90+
asyncio.run(produce())
6791
6892
API
6993
___

instrumentation/opentelemetry-instrumentation-mysqlclient/src/opentelemetry/instrumentation/mysqlclient/__init__.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,12 @@
2626
import MySQLdb
2727
from opentelemetry.instrumentation.mysqlclient import MySQLClientInstrumentor
2828
29-
3029
MySQLClientInstrumentor().instrument()
3130
3231
cnx = MySQLdb.connect(database="MySQL_Database")
3332
cursor = cnx.cursor()
34-
cursor.execute("INSERT INTO test (testField) VALUES (123)"
33+
cursor.execute("CREATE TABLE IF NOT EXISTS test (testField INTEGER)")
34+
cursor.execute("INSERT INTO test (testField) VALUES (123)")
3535
cnx.commit()
3636
cursor.close()
3737
cnx.close()
@@ -52,7 +52,7 @@
5252
cnx = MySQLdb.connect(database="MySQL_Database")
5353
cursor = cnx.cursor()
5454
cursor.execute("CREATE TABLE IF NOT EXISTS test (testField INTEGER)")
55-
cursor.execute("INSERT INTO test (testField) VALUES (123)"
55+
cursor.execute("INSERT INTO test (testField) VALUES (123)")
5656
cnx.commit()
5757
cursor.close()
5858
cnx.close()
@@ -75,7 +75,7 @@
7575
)
7676
cursor = instrumented_cnx.cursor()
7777
cursor.execute("CREATE TABLE IF NOT EXISTS test (testField INTEGER)")
78-
cursor.execute("INSERT INTO test (testField) VALUES (123)"
78+
cursor.execute("INSERT INTO test (testField) VALUES (123)")
7979
instrumented_cnx.commit()
8080
cursor.close()
8181
instrumented_cnx.close()

instrumentation/opentelemetry-instrumentation-pymssql/src/opentelemetry/instrumentation/pymssql/__init__.py

+4-2
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,8 @@
3030
3131
cnx = pymssql.connect(database="MSSQL_Database")
3232
cursor = cnx.cursor()
33-
cursor.execute("INSERT INTO test (testField) VALUES (123)"
33+
cursor.execute("CREATE TABLE IF NOT EXISTS test (testField INTEGER)")
34+
cursor.execute("INSERT INTO test (testField) VALUES (123)")
3435
cnx.commit()
3536
cursor.close()
3637
cnx.close()
@@ -44,7 +45,8 @@
4445
cnx = pymssql.connect(database="MSSQL_Database")
4546
instrumented_cnx = PyMSSQLInstrumentor().instrument_connection(cnx)
4647
cursor = instrumented_cnx.cursor()
47-
cursor.execute("INSERT INTO test (testField) VALUES (123)"
48+
cursor.execute("CREATE TABLE IF NOT EXISTS test (testField INTEGER)")
49+
cursor.execute("INSERT INTO test (testField) VALUES (123)")
4850
instrumented_cnx.commit()
4951
cursor.close()
5052
instrumented_cnx.close()

instrumentation/opentelemetry-instrumentation-pymysql/src/opentelemetry/instrumentation/pymysql/__init__.py

+6-5
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,12 @@
3131
3232
cnx = pymysql.connect(database="MySQL_Database")
3333
cursor = cnx.cursor()
34-
cursor.execute("INSERT INTO test (testField) VALUES (123)"
34+
cursor.execute("CREATE TABLE IF NOT EXISTS test (testField INTEGER)")
35+
cursor.execute("INSERT INTO test (testField) VALUES (123)")
3536
cnx.commit()
3637
cursor.close()
3738
cnx.close()
3839
39-
4040
.. code:: python
4141
4242
import pymysql
@@ -53,7 +53,8 @@
5353
}
5454
)
5555
cursor = instrumented_cnx.cursor()
56-
cursor.execute("INSERT INTO test (testField) VALUES (123)"
56+
cursor.execute("CREATE TABLE IF NOT EXISTS test (testField INTEGER)")
57+
cursor.execute("INSERT INTO test (testField) VALUES (123)")
5758
instrumented_cnx.commit()
5859
cursor.close()
5960
instrumented_cnx.close()
@@ -75,12 +76,12 @@
7576
7677
cnx = pymysql.connect(database="MySQL_Database")
7778
cursor = cnx.cursor()
78-
cursor.execute("INSERT INTO test (testField) VALUES (123)"
79+
cursor.execute("CREATE TABLE IF NOT EXISTS test (testField INTEGER)")
80+
cursor.execute("INSERT INTO test (testField) VALUES (123)")
7981
cnx.commit()
8082
cursor.close()
8183
cnx.close()
8284
83-
8485
For example,
8586
::
8687

0 commit comments

Comments
 (0)