sbmt-kafka_consumer 3.0.0 → 3.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 7af7925acd4a61d131a66dd3cd290064ba11aac749ee64ca00841912bd038adc
4
- data.tar.gz: 9cd1e1f789c6e4252adedf3dd3115f512c62240b75edadc38d1590e4a85b6000
3
+ metadata.gz: 884f55960d86ac5b756f5b7defc30298e6918a543a168f69613972b6f4dd1282
4
+ data.tar.gz: 4521497f45d7c2bdf7fb78de36ddba7b077a51aee92c5c7aab41057e9b0e1f5a
5
5
  SHA512:
6
- metadata.gz: 60dbd904786ddc419e9efbb0f02904c6035089a3b23fcb4035d41c85c8db303ae807138fa1e26c042688521c34a355f0314799e82addc73ef6308fc3fe6cad15
7
- data.tar.gz: ca1d3af3c319695cbb33e11f9f66a384efb2c1884a387a233d5b197ae9ed5d3a3852c49662586a67ba15eb497ed325b92363cfe65ce8e086c6319b48dfb0569e
6
+ metadata.gz: d25efefade51458b46dd3f060983143f9de35d71d7e007dd082daf5f4a6d8a8a635daff400337d38a8621cdda1ad694e115f4816594cb9f94bd408ec8ddd2b9a
7
+ data.tar.gz: 7fcf377249867d6694515bf006af17de6ade409ddf813bffc63471d437cd1752f659c72d210f071fcf937f6be99f75bf4028e25a3b803d7abfd04e8fd464fdb3
data/CHANGELOG.md CHANGED
@@ -13,6 +13,18 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
13
13
 
14
14
  ### Fixed
15
15
 
16
+ ## [3.2.0] - 2024-09-17
17
+
18
+ ### Added
19
+
20
+ - For synchronous messages and errors, we place logs in tags
21
+
22
+ ## [3.1.0] - 2024-09-09
23
+
24
+ ### Fixed
25
+
26
+ - Refactor consumer class initialization
27
+
16
28
  ## [3.0.0] - 2024-09-04
17
29
 
18
30
  ## BREAKING
@@ -25,6 +37,12 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
25
37
 
26
38
  - Support consumer group mappers to support backward compatibility of consumer group naming
27
39
 
40
+ ## [2.8.0] - 2024-09-09
41
+
42
+ ### Fixed
43
+
44
+ - Refactor consumer class initialization
45
+
28
46
  ## [2.7.1] - 2024-08-01
29
47
 
30
48
  ### Fixed
@@ -3,15 +3,20 @@
3
3
  module Sbmt
4
4
  module KafkaConsumer
5
5
  class BaseConsumer < Karafka::BaseConsumer
6
- def self.consumer_klass(skip_on_error: false, middlewares: [])
7
- Class.new(self) do
8
- const_set(:SKIP_ON_ERROR, skip_on_error)
9
- const_set(:MIDDLEWARES, middlewares.map(&:constantize))
6
+ class_attribute :skip_on_error, instance_writer: false, default: false
7
+ class_attribute :middlewares, instance_writer: false, default: []
10
8
 
9
+ def self.consumer_klass(skip_on_error: nil, middlewares: nil)
10
+ klass = Class.new(self) do
11
11
  def self.name
12
12
  superclass.name
13
13
  end
14
14
  end
15
+
16
+ # defaults are set in class_attribute definition
17
+ klass.skip_on_error = skip_on_error if skip_on_error
18
+ klass.middlewares = middlewares.map(&:constantize) if middlewares
19
+ klass
15
20
  end
16
21
 
17
22
  def consume
@@ -40,9 +45,7 @@ module Sbmt
40
45
 
41
46
  def with_instrumentation(message)
42
47
  logger.tagged(
43
- trace_id: trace_id,
44
- topic: message.metadata.topic, partition: message.metadata.partition,
45
- key: message.metadata.key, offset: message.metadata.offset
48
+ trace_id: trace_id
46
49
  ) do
47
50
  ::Sbmt::KafkaConsumer.monitor.instrument(
48
51
  "consumer.consumed_one",
@@ -112,14 +115,6 @@ module Sbmt
112
115
  end
113
116
  end
114
117
 
115
- def skip_on_error
116
- self.class::SKIP_ON_ERROR
117
- end
118
-
119
- def middlewares
120
- self.class::MIDDLEWARES
121
- end
122
-
123
118
  # can be overridden in consumer to enable message logging
124
119
  def log_payload?
125
120
  false
@@ -6,17 +6,20 @@ module Sbmt
6
6
  IDEMPOTENCY_HEADER_NAME = "Idempotency-Key"
7
7
  DEFAULT_SOURCE = "KAFKA"
8
8
 
9
- def self.consumer_klass(inbox_item:, event_name: nil, skip_on_error: false, name: nil, middlewares: [])
10
- Class.new(self) do
11
- const_set(:INBOX_ITEM_CLASS_NAME, inbox_item)
12
- const_set(:EVENT_NAME, event_name)
13
- const_set(:SKIP_ON_ERROR, skip_on_error)
14
- const_set(:MIDDLEWARES, middlewares.map(&:constantize))
15
-
16
- def self.name
17
- superclass.name
18
- end
19
- end
9
+ class_attribute :inbox_item_class, instance_writer: false, default: nil
10
+ class_attribute :event_name, instance_writer: false, default: nil
11
+
12
+ def self.consumer_klass(inbox_item:, event_name: nil, skip_on_error: nil, name: nil, middlewares: nil)
13
+ # defaults are set in class_attribute definition
14
+ klass = super(skip_on_error: skip_on_error, middlewares: middlewares)
15
+ klass.inbox_item_class = inbox_item.constantize
16
+ klass.event_name = event_name if event_name
17
+ klass
18
+ end
19
+
20
+ def initialize
21
+ raise Sbmt::KafkaConsumer::Error, "inbox_item param is not set" if inbox_item_class.blank?
22
+ super
20
23
  end
21
24
 
22
25
  def extra_message_attrs(_message)
@@ -101,14 +104,6 @@ module Sbmt
101
104
  message.metadata.headers.fetch(IDEMPOTENCY_HEADER_NAME, nil).presence
102
105
  end
103
106
 
104
- def inbox_item_class
105
- @inbox_item_class ||= self.class::INBOX_ITEM_CLASS_NAME.constantize
106
- end
107
-
108
- def event_name
109
- @event_name ||= self.class::EVENT_NAME
110
- end
111
-
112
107
  def inbox_name
113
108
  inbox_item_class.box_name
114
109
  end
@@ -20,30 +20,56 @@ module Sbmt
20
20
 
21
21
  logger.tagged(
22
22
  type: type,
23
+ stacktrace: log_backtrace(error),
23
24
  **tags
24
25
  ) do
25
26
  logger.error(error_message(error))
26
- log_backtrace(error)
27
27
  end
28
28
  end
29
29
 
30
30
  # BaseConsumer events
31
31
  def on_consumer_consumed_one(event)
32
- logger.info("Successfully consumed message in #{event.payload[:time]} ms")
32
+ log_with_tags(log_tags(event), "Successfully consumed message")
33
33
  end
34
34
 
35
35
  def on_consumer_mark_as_consumed(event)
36
- logger.info("Processing message in #{event.payload[:time]} ms")
36
+ log_with_tags(log_tags(event), "Processing message")
37
37
  end
38
38
 
39
39
  def on_consumer_process_message(event)
40
- logger.info("Commit offset in #{event.payload[:time]} ms")
40
+ log_with_tags(log_tags(event), "Commit offset")
41
41
  end
42
42
 
43
43
  # InboxConsumer events
44
44
  def on_consumer_inbox_consumed_one(event)
45
- logger.tagged(status: event[:status]) do
46
- logger.info("Successfully consumed message with uuid: #{event[:message_uuid]} in #{event.payload[:time]} ms")
45
+ log_tags = log_tags(event).merge!(status: event[:status])
46
+ msg = "Successfully consumed message with uuid: #{event[:message_uuid]}"
47
+
48
+ log_with_tags(log_tags, msg)
49
+ end
50
+
51
+ private
52
+
53
+ def log_tags(event)
54
+ metadata = event.payload[:message].metadata
55
+
56
+ {
57
+ kafka: {
58
+ topic: metadata.topic,
59
+ partition: metadata.partition,
60
+ key: metadata.key,
61
+ offset: metadata.offset,
62
+ consumer_group: event.payload[:caller].topic.consumer_group.id,
63
+ consume_duration_ms: event.payload[:time]
64
+ }
65
+ }
66
+ end
67
+
68
+ def log_with_tags(log_tags, msg)
69
+ return unless logger.respond_to?(:tagged)
70
+
71
+ logger.tagged(log_tags) do
72
+ logger.send(:info, msg)
47
73
  end
48
74
  end
49
75
  end
@@ -16,9 +16,8 @@ RSpec.shared_context "with sbmt karafka consumer" do
16
16
  let(:kafka_client) { instance_double(Karafka::Connection::Client) }
17
17
  let(:null_deserializer) { Sbmt::KafkaConsumer::Serialization::NullDeserializer.new }
18
18
 
19
- let(:consumer) {
20
- build_consumer(described_class.new)
21
- }
19
+ let(:consumer_class) { described_class.consumer_klass }
20
+ let(:consumer) { build_consumer(consumer_class.new) }
22
21
 
23
22
  before {
24
23
  Sbmt::KafkaConsumer::ClientConfigurer.configure!
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Sbmt
4
4
  module KafkaConsumer
5
- VERSION = "3.0.0"
5
+ VERSION = "3.2.0"
6
6
  end
7
7
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sbmt-kafka_consumer
3
3
  version: !ruby/object:Gem::Version
4
- version: 3.0.0
4
+ version: 3.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Kuper Ruby-Platform Team
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2024-09-05 00:00:00.000000000 Z
11
+ date: 2024-09-18 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -565,7 +565,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
565
565
  - !ruby/object:Gem::Version
566
566
  version: '0'
567
567
  requirements: []
568
- rubygems_version: 3.3.7
568
+ rubygems_version: 3.1.6
569
569
  signing_key:
570
570
  specification_version: 4
571
571
  summary: Ruby gem for consuming Kafka messages