Module: Kafka::Tracer

Defined in:
lib/kafka/tracer.rb,
lib/kafka/tracer/version.rb

Defined Under Namespace

Classes: IncompatibleGemVersion

Constant Summary collapse

IngoreMessage =
->(_value, _key, _headers, _topic, _partition, _partition_key) { false }
VERSION =
'0.4.0'

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.ignore_messageObject

Returns the value of attribute ignore_message.



11
12
13
# File 'lib/kafka/tracer.rb', line 11

def ignore_message
  @ignore_message
end

.tracerObject

Returns the value of attribute tracer.



11
12
13
# File 'lib/kafka/tracer.rb', line 11

def tracer
  @tracer
end

Class Method Details

.compatible_version?Boolean

Returns:

  • (Boolean)


31
32
33
34
# File 'lib/kafka/tracer.rb', line 31

def compatible_version?
  # https://github.com/zendesk/ruby-kafka/pull/604
  Gem::Version.new(Kafka::VERSION) >= Gem::Version.new("0.7.0")
end

.instrument(tracer: OpenTracing.global_tracer, ignore_message: IngoreMessage) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/kafka/tracer.rb', line 15

def instrument(tracer: OpenTracing.global_tracer, ignore_message: IngoreMessage)
  begin
    require 'kafka'
  rescue LoadError
    return
  end
  raise IncompatibleGemVersion unless compatible_version?

  @ignore_message = ignore_message
  @tracer = tracer
  patch_producer_produce
  patch_client_deliver_message
  patch_client_each_message
  patch_consumer_each_message
end

.patch_client_deliver_messageObject

used to send sync messages



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/kafka/tracer.rb', line 71

def patch_client_deliver_message
  ::Kafka::Client.class_eval do
    alias_method :deliver_message_original, :deliver_message

    def deliver_message(value, key: nil, headers: {}, topic:, partition: nil, partition_key: nil, retries: 1)
      if ::Kafka::Tracer.ignore_message.call(value, key, headers, topic, partition, partition_key)
        result = deliver_message_original(value,
                                          key: key,
                                          headers: headers,
                                          topic: topic,
                                          partition: partition,
                                          partition_key: partition_key,
                                          retries: retries)
      else
        tags = {
          'component' => 'ruby-kafka',
          'span.kind' => 'producer',
          'message_bus.partition' => partition,
          'message_bus.partition_key' => partition_key,
          'message_bus.destination' => topic,
          'message_bus.pending_message' => false
        }

        tracer = ::Kafka::Tracer.tracer

        tracer.start_active_span('kafka.producer', tags: tags) do |scope|
          OpenTracing.inject(scope.span.context, OpenTracing::FORMAT_TEXT_MAP, headers)

          begin
            result = deliver_message_original(value,
                                              key: key,
                                              headers: headers,
                                              topic: topic,
                                              partition: partition,
                                              partition_key: partition_key,
                                              retries: retries)
          rescue Kafka::Error => e
            scope.span.set_tag('error', true)
            raise
          end
        end
      end

      result
    end
  end
end

.patch_client_each_messageObject



177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
# File 'lib/kafka/tracer.rb', line 177

def patch_client_each_message
  ::Kafka::Client.class_eval do
    alias_method :each_message_original, :each_message

    def each_message(topic:, start_from_beginning: true, max_wait_time: 5, min_bytes: 1, max_bytes: 1_048_576, &block)
      tracer = ::Kafka::Tracer.tracer

      wrapped_block = lambda { |message|
        context = tracer.extract(OpenTracing::FORMAT_TEXT_MAP, message.headers)

        tags = {
          'component' => 'ruby-kafka',
          'span.kind' => 'consumer',
          'message_bus.partition' => message.partition,
          'message_bus.destination' => message.topic,
          'message_bus.pre_fetched_in_batch' => true
        }

        reference = OpenTracing::Reference.follows_from(context)

        tracer.start_active_span('kafka.consumer', references: [reference], tags: tags) do |scope|
          begin
            block.call(message)
          rescue StandardError
            scope.span.set_tag('error', true)
            raise
          end
        end
      }

      each_message_original(
        topic: topic,
        start_from_beginning: start_from_beginning,
        max_wait_time: max_wait_time,
        min_bytes: min_bytes,
        max_bytes: max_bytes,
        &wrapped_block
      )
    end
  end
end

.patch_consumer_each_messageObject



219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
# File 'lib/kafka/tracer.rb', line 219

def patch_consumer_each_message
  ::Kafka::Consumer.class_eval do
    alias_method :each_message_original, :each_message

    def each_message(min_bytes: 1, max_bytes: 10485760, max_wait_time: 1, automatically_mark_as_processed: true)
      tracer = ::Kafka::Tracer.tracer

      wrapped_block = lambda { |message|
        context = tracer.extract(OpenTracing::FORMAT_TEXT_MAP, message.headers)

        tags = {
          'component' => 'ruby-kafka',
          'span.kind' => 'consumer',
          'message_bus.partition' => message.partition,
          'message_bus.destination' => message.topic,
          'message_bus.pre_fetched_in_batch' => true
        }

        reference = OpenTracing::Reference.follows_from(context)

        tracer.start_active_span('kafka.consumer', references: [reference], tags: tags) do |scope|
          begin
            yield message
          rescue StandardError
            scope.span.set_tag('error', true)
            raise
          end
        end
      }

      each_message_original(
        min_bytes: min_bytes,
        max_bytes: max_bytes,
        max_wait_time: max_wait_time,
        automatically_mark_as_processed: automatically_mark_as_processed,
        &wrapped_block
      )
    end
  end
end

.patch_deliver_messagesObject

TODO



175
# File 'lib/kafka/tracer.rb', line 175

def patch_deliver_messages; end

.patch_producer_produceObject

used to send a batch of messages



120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
# File 'lib/kafka/tracer.rb', line 120

def patch_producer_produce
  ::Kafka::Producer.class_eval do
    alias_method :produce_original, :produce

    def produce(value, key: nil, headers: {}, topic:, partition: nil, partition_key: nil, create_time: Time.now)

      if ::Kafka::Tracer.ignore_message.call(value, key, headers, topic, partition, partition_key)
        result = produce_original(
          value,
          key: key,
          headers: headers,
          topic: topic,
          partition: partition,
          partition_key: partition_key,
          create_time: create_time
        )

      else
        tags = {
          'component' => 'ruby-kafka',
          'span.kind' => 'producer',
          'message_bus.partition' => partition,
          'message_bus.partition_key' => partition_key,
          'message_bus.destination' => topic,
          'message_bus.pending_message' => true
        }

        tracer = ::Kafka::Tracer.tracer

        tracer.start_active_span('kafka.producer', tags: tags) do |scope|
          OpenTracing.inject(scope.span.context, OpenTracing::FORMAT_TEXT_MAP, headers)

          begin
            result = produce_original(
              value,
              key: key,
              headers: headers,
              topic: topic,
              partition: partition,
              partition_key: partition_key,
              create_time: create_time
            )
          rescue Kafka::Error => e
            scope.span.set_tag('error', true)
            raise
          end
        end
      end

      result
    end
  end
end

.removeObject



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/kafka/tracer.rb', line 36

def remove
  if ::Kafka::Producer.method_defined?(:produce_original)
    ::Kafka::Producer.class_eval do
      remove_method :produce
      alias_method :produce, :produce_original
      remove_method :produce_original
    end
  end

  if ::Kafka::Client.method_defined?(:deliver_message_original)
    ::Kafka::Client.class_eval do
      remove_method :deliver_message
      alias_method :deliver_message, :deliver_message_original
      remove_method :deliver_message_original
    end
  end

  if ::Kafka::Client.method_defined?(:each_message_original)
    ::Kafka::Client.class_eval do
      remove_method :each_message
      alias_method :each_message, :each_message_original
      remove_method :each_message_original
    end
  end

  if ::Kafka::Consumer.method_defined?(:each_message_original)
    ::Kafka::Consumer.class_eval do
      remove_method :each_message
      alias_method :each_message, :each_message_original
      remove_method :each_message_original
    end
  end
end