deimos-ruby 2.0.15 → 2.0.17

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: d6c9d3bcbf085821dc0be156389bd223dbce36ba8c34ff452612cf82ca3b3be4
4
- data.tar.gz: fc9bf56f6ad566a9e197e50b5b167af620e9c0d6ec0fd9f50999059b7fded6ee
3
+ metadata.gz: a6b4516c780cd6c4acfe46092cf4ff7342a3db20b5d259f6fee59e85c2dd6c01
4
+ data.tar.gz: 02347f3bb35d9cd1c0435f097d453c6f7d90bccee6892d637954c862155164fd
5
5
  SHA512:
6
- metadata.gz: 88ac68371a699744e87fcc0c7e8a1294af1b02d4ff6973f95c1667c3754ac8fd0eeda65afddd966a4451058028409e3183e6bca112c553dbf7ac67f72929ffdd
7
- data.tar.gz: 30f93b2f6aefbdf3b8554bb0a6b5bac53e6638a88adc75bdded31fde88b783ca6311fb50b652f9b8085d32da6175449181dd8229a1544018e0e1c7c51ddc158f
6
+ metadata.gz: 8b1c2de2fa812f2385b07f3e9fde5135e734571f5bb34d96dd31536ebaf7576c3acdccc7df03098632d549e264d8ef1a5d8f2589995287dda4628dbd535a1840
7
+ data.tar.gz: 600268d4111cb77b2f4c06a6e07ee95d7e7e4387c298a6099c4498f247a64a443426d639c09b1e30455e3f74ebca7a3790ca5b54abcf34d3ab8918811f58a765
data/CHANGELOG.md CHANGED
@@ -7,6 +7,14 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
7
7
 
8
8
  ## UNRELEASED
9
9
 
10
+ ## 2.0.17 - 2025-07-03
11
+
12
+ - Feature: Allow producers to specify `:message` and `:key` in the `publish` methods.
13
+
14
+ ## 2.0.16 - 2025-05-30
15
+
16
+ - Fix: Send `publish` metric even if the producer was not a Deimos producer.
17
+
10
18
  ## 2.0.15 - 2025-05-29
11
19
 
12
20
  - Fix: Do not force data to JSON when logging payloads. This allows log formatters to handle the payloads on their own (e.g. adding additional fields).
data/README.md CHANGED
@@ -259,13 +259,22 @@ If you publish a payload `{ "test_id" => "123", "some_int" => 123 }`, this
259
259
  will be turned into a key that looks like `{ "test_id" => "123"}` and schema-encoded
260
260
  before being sent to Kafka.
261
261
 
262
- If you are using `plain` or `schema` as your config, you will need to have a
263
- special `payload_key` key to your payload hash. This will be extracted and
262
+ If you are using `plain` or `schema` as your config, you can specify the key in two ways:
263
+
264
+ * Add a special `payload_key` key to your payload hash. This will be extracted and
264
265
  used as the key (for `plain`, it will be used directly, while for `schema`
265
266
  it will be encoded first against the schema). So your payload would look like
266
267
  `{ "test_id" => "123", "some_int" => 123, payload_key: "some_other_key"}`.
267
268
  Remember that if you're using `schema`, the `payload_key` must be a *hash*,
268
269
  not a plain value.
270
+ * Specify `:message` and `:key` values when producing messages. This can be helpful when using [schema classes](#generated-schema-classes):
271
+
272
+ ```ruby
273
+ MyProducer.publish({
274
+ message: MySchema.new("test_id" => "123", "some_int" => 123),
275
+ key: MySchemaKey.new("test_id" => "123")
276
+ })
277
+ ```
269
278
 
270
279
  ## Instrumentation
271
280
 
@@ -7,11 +7,6 @@ module Deimos
7
7
  # :nodoc:
8
8
  def self.execute(producer_class:, messages:)
9
9
  Karafka.producer.produce_many_sync(messages)
10
- Deimos.config.metrics&.increment(
11
- 'publish',
12
- tags: %W(status:success topic:#{messages.first[:topic]}),
13
- by: messages.size
14
- )
15
10
  end
16
11
  end
17
12
  end
@@ -7,11 +7,6 @@ module Deimos
7
7
  # :nodoc:
8
8
  def self.execute(producer_class:, messages:)
9
9
  Karafka.producer.produce_many_async(messages)
10
- Deimos.config.metrics&.increment(
11
- 'publish',
12
- tags: %W(status:success topic:#{messages.first[:topic]}),
13
- by: messages.size
14
- )
15
10
  end
16
11
  end
17
12
  end
@@ -0,0 +1,25 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Deimos
4
+ class ProducerMetricsListener
5
+ %i[
6
+ produced_sync
7
+ produced_async
8
+ ].each do |event_scope|
9
+ define_method(:"on_message_#{event_scope}") do |event|
10
+ Deimos.config.metrics&.increment(
11
+ 'publish',
12
+ tags: %W(status:success topic:#{event[:message][:topic]})
13
+ )
14
+ end
15
+
16
+ define_method(:"on_messages_#{event_scope}") do |event|
17
+ Deimos.config.metrics&.increment(
18
+ 'publish',
19
+ tags: %W(status:success topic:#{event[:messages].first[:topic]}),
20
+ by: event[:messages].size
21
+ )
22
+ end
23
+ end
24
+ end
25
+ end
@@ -111,12 +111,17 @@ module Deimos
111
111
  backend = determine_backend_class(sync, force_send)
112
112
 
113
113
  messages = Array(payloads).map do |p|
114
- {
114
+ m = {
115
115
  payload: p&.to_h,
116
116
  headers: headers,
117
117
  topic: topic,
118
118
  partition_key: self.partition_key(p)
119
119
  }
120
+ if m.dig(:payload, :key).present? && m.dig(:payload, :message).present?
121
+ m[:key] = m[:payload][:key].to_h
122
+ m[:payload] = m[:payload][:message].to_h
123
+ end
124
+ m
120
125
  end
121
126
  self.produce(messages, backend: backend)
122
127
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Deimos
4
- VERSION = '2.0.15'
4
+ VERSION = '2.0.17'
5
5
  end
data/lib/deimos.rb CHANGED
@@ -25,6 +25,7 @@ require 'deimos/ext/schema_route'
25
25
  require 'deimos/ext/consumer_route'
26
26
  require 'deimos/ext/producer_route'
27
27
  require 'deimos/ext/producer_middleware'
28
+ require 'deimos/ext/producer_metrics_listener'
28
29
  require 'deimos/ext/routing_defaults'
29
30
 
30
31
  require 'deimos/railtie' if defined?(Rails)
@@ -146,6 +147,8 @@ module Deimos
146
147
  Karafka::Setup::AttributesMap.producer(Karafka::Setup::Config.config.kafka.dup)
147
148
  EVENT_TYPES.each { |type| Karafka.monitor.notifications_bus.register_event(type) }
148
149
 
150
+ Karafka.producer.monitor.subscribe(ProducerMetricsListener.new)
151
+
149
152
  Karafka.producer.monitor.subscribe('error.occurred') do |event|
150
153
  if event.payload.key?(:messages)
151
154
  topic = event[:messages].first[:topic]
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: deimos-ruby
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.0.15
4
+ version: 2.0.17
5
5
  platform: ruby
6
6
  authors:
7
7
  - Daniel Orner
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2025-05-29 00:00:00.000000000 Z
11
+ date: 2025-07-03 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: avro_turf
@@ -457,6 +457,7 @@ files:
457
457
  - lib/deimos/consumer.rb
458
458
  - lib/deimos/exceptions.rb
459
459
  - lib/deimos/ext/consumer_route.rb
460
+ - lib/deimos/ext/producer_metrics_listener.rb
460
461
  - lib/deimos/ext/producer_middleware.rb
461
462
  - lib/deimos/ext/producer_route.rb
462
463
  - lib/deimos/ext/routing_defaults.rb