Skip to content

Commit e50cb32

Browse files
authored
Improve kafka-python instrumentation examples (#3388)
* Improve kafka-python instrumentation example * Improve kafka-python instrumentation examples
1 parent 5a2cfb3 commit e50cb32

File tree

1 file changed

+14
-1
lines changed
  • instrumentation/opentelemetry-instrumentation-kafka-python/src/opentelemetry/instrumentation/kafka

1 file changed

+14
-1
lines changed

instrumentation/opentelemetry-instrumentation-kafka-python/src/opentelemetry/instrumentation/kafka/__init__.py

+14-1
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,14 @@
3030
producer = KafkaProducer(bootstrap_servers=['localhost:9092'])
3131
producer.send('my-topic', b'raw_bytes')
3232
33+
def process_msg(message):
34+
print(message)
35+
3336
# report a span of type consumer with the default settings
3437
consumer = KafkaConsumer('my-topic', group_id='my-group', bootstrap_servers=['localhost:9092'])
3538
for message in consumer:
36-
# process message
39+
# process message
40+
process_msg(message)
3741
3842
The _instrument() method accepts the following keyword args:
3943
tracer_provider (TracerProvider) - an optional tracer provider
@@ -53,6 +57,7 @@ def consume_hook(span: Span, record: kafka.record.ABCRecord, args, kwargs)
5357
def produce_hook(span, args, kwargs):
5458
if span and span.is_recording():
5559
span.set_attribute("custom_user_attribute_from_produce_hook", "some-value")
60+
5661
def consume_hook(span, record, args, kwargs):
5762
if span and span.is_recording():
5863
span.set_attribute("custom_user_attribute_from_consume_hook", "some-value")
@@ -65,6 +70,14 @@ def consume_hook(span, record, args, kwargs):
6570
producer = KafkaProducer(bootstrap_servers=['localhost:9092'])
6671
producer.send('my-topic', b'raw_bytes')
6772
73+
def process_msg(message):
74+
print(message)
75+
76+
consumer = KafkaConsumer('my-topic', group_id='my-group', bootstrap_servers=['localhost:9092'])
77+
for message in consumer:
78+
# process message
79+
process_msg(message)
80+
6881
API
6982
___
7083
"""

0 commit comments

Comments
 (0)