@@ -18,10 +18,6 @@ module Monitor
1818 message.produced_sync
1919 ] . freeze
2020
21- def configuration
22- Datadog . configuration . tracing [ :waterdrop ]
23- end
24-
2521 def instrument ( event_id , payload = { } , &block )
2622 return super unless TRACEABLE_EVENTS . include? ( event_id )
2723
@@ -40,15 +36,15 @@ def instrument(event_id, payload = {}, &block)
4036
4137 span . set_tag ( Contrib ::Karafka ::Ext ::TAG_MESSAGE_COUNT , payload [ :messages ] . size )
4238
43- payload [ :messages ] . each { |message | inject ( trace_digest , message ) } if configuration [ :distributed_tracing ]
39+ payload [ :messages ] . each { |message | inject ( trace_digest , message ) }
4440 else
4541 action = event_id . sub ( 'message.produced' , 'produce' )
4642
4743 span . set_tag ( Contrib ::Ext ::Messaging ::TAG_DESTINATION , payload [ :message ] [ :topic ] )
4844 span . set_tag ( Contrib ::Karafka ::Ext ::TAG_PARTITION , payload [ :message ] [ :partition ] )
4945 span . set_tag ( Contrib ::Karafka ::Ext ::TAG_MESSAGE_COUNT , 1 )
5046
51- inject ( trace_digest , payload [ :message ] ) if configuration [ :distributed_tracing ]
47+ inject ( trace_digest , payload [ :message ] )
5248 end
5349
5450 span . resource = "waterdrop.#{ action } "
@@ -63,9 +59,17 @@ def instrument(event_id, payload = {}, &block)
6359 private
6460
6561 def inject ( trace_digest , message )
62+ return unless datadog_configuration ( message [ :topic ] ) [ :distributed_tracing ]
63+
6664 message [ :headers ] ||= { }
6765 WaterDrop . inject ( trace_digest , message [ :headers ] )
6866 end
67+
68+ # cache the configuration resolution per topic to avoid repeated lookups in message batches
69+ def datadog_configuration ( topic )
70+ @datadog_configuration ||= { }
71+ @datadog_configuration [ topic ] ||= Datadog . configuration . tracing [ :waterdrop , topic ]
72+ end
6973 end
7074 end
7175 end
0 commit comments