| 
51 | 51 |     pika_instrumentation = PikaInstrumentor()  | 
52 | 52 |     pika_instrumentation.instrument_channel(channel=channel)  | 
53 | 53 | 
  | 
54 |  | -
  | 
55 | 54 |     channel.basic_publish(exchange='', routing_key='hello', body=b'Hello World!')  | 
56 | 55 | 
  | 
57 | 56 |     pika_instrumentation.uninstrument_channel(channel=channel)  | 
 | 
60 | 59 | 
  | 
61 | 60 | .. code-block:: python  | 
62 | 61 | 
  | 
 | 62 | +    import pika  | 
 | 63 | +    from opentelemetry.instrumentation.pika import PikaInstrumentor  | 
 | 64 | +    from opentelemetry.trace import get_tracer_provider  | 
 | 65 | +
  | 
 | 66 | +    connection = pika.BlockingConnection(pika.URLParameters('amqp://localhost'))  | 
 | 67 | +    channel = connection.channel()  | 
 | 68 | +    tracer_provider = get_tracer_provider()  | 
 | 69 | +
  | 
 | 70 | +    channel.queue_declare(queue='hello')  | 
 | 71 | +
  | 
63 | 72 |     PikaInstrumentor.instrument_channel(channel, tracer_provider=tracer_provider)  | 
64 | 73 | 
  | 
 | 74 | +    channel.basic_publish(exchange='', routing_key='hello', body=b'Hello World!')  | 
 | 75 | +
  | 
 | 76 | +    PikaInstrumentor.uninstrument_channel(channel)  | 
 | 77 | +
  | 
65 | 78 | * PikaInstrumentor also supports instrumenting with hooks that will be called when producing or consuming a message.  | 
66 | 79 |   The hooks should be of type "Callable[[Span, bytes, BasicProperties], None]"  | 
67 | 80 |   where the first parameter is the span, the second parameter is the message body  | 
68 | 81 |   and the third parameter is the message properties  | 
69 | 82 | 
  | 
70 | 83 | .. code-block:: python  | 
71 | 84 | 
  | 
 | 85 | +    import pika  | 
 | 86 | +    from opentelemetry.instrumentation.pika import PikaInstrumentor  | 
 | 87 | +    from opentelemetry.trace import Span  | 
 | 88 | +    from pika import BasicProperties  | 
 | 89 | +
  | 
72 | 90 |     def publish_hook(span: Span, body: bytes, properties: BasicProperties):  | 
73 | 91 |         span.set_attribute("messaging.payload", body.decode())  | 
74 | 92 | 
  | 
75 | 93 |     def consume_hook(span: Span, body: bytes, properties: BasicProperties):  | 
76 | 94 |         span.set_attribute("messaging.id", properties.message_id)  | 
77 | 95 | 
  | 
 | 96 | +    connection = pika.BlockingConnection(pika.URLParameters('amqp://localhost'))  | 
 | 97 | +    channel = connection.channel()  | 
 | 98 | +    channel.queue_declare(queue='hello')  | 
 | 99 | +
  | 
78 | 100 |     PikaInstrumentor.instrument_channel(channel, publish_hook=publish_hook, consume_hook=consume_hook)  | 
79 | 101 | 
  | 
 | 102 | +    channel.basic_publish(exchange='', routing_key='hello', body=b'Hello World!')  | 
 | 103 | +
  | 
 | 104 | +    PikaInstrumentor.uninstrument_channel(channel)  | 
 | 105 | +
  | 
80 | 106 | Consumer Instrumentation  | 
81 | 107 | ------------------------  | 
82 | 108 | For consumer instrumentation, pika supports two consuming modes:  | 
 | 
0 commit comments