Improve pika instrumentation examples (#3390)

This commit is contained in:
Andre Murbach Maidl
2025-04-02 11:17:52 -03:00
committed by GitHub
parent 3a585b4b58
commit 6bde73ce34

View File

@ -51,7 +51,6 @@ Usage
pika_instrumentation = PikaInstrumentor() pika_instrumentation = PikaInstrumentor()
pika_instrumentation.instrument_channel(channel=channel) pika_instrumentation.instrument_channel(channel=channel)
channel.basic_publish(exchange='', routing_key='hello', body=b'Hello World!') channel.basic_publish(exchange='', routing_key='hello', body=b'Hello World!')
pika_instrumentation.uninstrument_channel(channel=channel) pika_instrumentation.uninstrument_channel(channel=channel)
@ -60,8 +59,22 @@ Usage
.. code-block:: python .. code-block:: python
import pika
from opentelemetry.instrumentation.pika import PikaInstrumentor
from opentelemetry.trace import get_tracer_provider
connection = pika.BlockingConnection(pika.URLParameters('amqp://localhost'))
channel = connection.channel()
tracer_provider = get_tracer_provider()
channel.queue_declare(queue='hello')
PikaInstrumentor.instrument_channel(channel, tracer_provider=tracer_provider) PikaInstrumentor.instrument_channel(channel, tracer_provider=tracer_provider)
channel.basic_publish(exchange='', routing_key='hello', body=b'Hello World!')
PikaInstrumentor.uninstrument_channel(channel)
* PikaInstrumentor also supports instrumenting with hooks that will be called when producing or consuming a message. * PikaInstrumentor also supports instrumenting with hooks that will be called when producing or consuming a message.
The hooks should be of type "Callable[[Span, bytes, BasicProperties], None]" The hooks should be of type "Callable[[Span, bytes, BasicProperties], None]"
where the first parameter is the span, the second parameter is the message body where the first parameter is the span, the second parameter is the message body
@ -69,14 +82,27 @@ Usage
.. code-block:: python .. code-block:: python
import pika
from opentelemetry.instrumentation.pika import PikaInstrumentor
from opentelemetry.trace import Span
from pika import BasicProperties
def publish_hook(span: Span, body: bytes, properties: BasicProperties): def publish_hook(span: Span, body: bytes, properties: BasicProperties):
span.set_attribute("messaging.payload", body.decode()) span.set_attribute("messaging.payload", body.decode())
def consume_hook(span: Span, body: bytes, properties: BasicProperties): def consume_hook(span: Span, body: bytes, properties: BasicProperties):
span.set_attribute("messaging.id", properties.message_id) span.set_attribute("messaging.id", properties.message_id)
connection = pika.BlockingConnection(pika.URLParameters('amqp://localhost'))
channel = connection.channel()
channel.queue_declare(queue='hello')
PikaInstrumentor.instrument_channel(channel, publish_hook=publish_hook, consume_hook=consume_hook) PikaInstrumentor.instrument_channel(channel, publish_hook=publish_hook, consume_hook=consume_hook)
channel.basic_publish(exchange='', routing_key='hello', body=b'Hello World!')
PikaInstrumentor.uninstrument_channel(channel)
Consumer Instrumentation Consumer Instrumentation
------------------------ ------------------------
For consumer instrumentation, pika supports two consuming modes: For consumer instrumentation, pika supports two consuming modes: