WARNING: THIS SITE IS A MIRROR OF GITHUB.COM / IT CANNOT LOGIN OR REGISTER ACCOUNTS / THE CONTENTS ARE PROVIDED AS-IS / THIS SITE ASSUMES NO RESPONSIBILITY FOR ANY DISPLAYED CONTENT OR LINKS / IF YOU FOUND SOMETHING MAY NOT GOOD FOR EVERYONE, CONTACT ADMIN AT ilovescratch@foxmail.com
Skip to content

Conversation

@Drowze
Copy link
Contributor

@Drowze Drowze commented Dec 8, 2025

What does this PR do?

Fixes the automatic WaterDrop integration activation (via the Karafka integration).

For context, by the time that Karafka::App#initialized! is called, Karafka.producer was already initialized.
Not only that, there's really nowhere we can hook in the Karafka initialization where we can be sure that Karafka.producer wasn't yet initiaized - that's because the Karafka initialization is NOT necessarily tied to the WaterDrop initialization. For instance, the following is a possible scenario:

$producer = WaterDrop::Producer.new { ... }

Datadog.configure { |c| c.tracing.instrument :karafka }

# note that the producer was initialized before the Karafka app
# (and in this case, even before datadog was configured)
Karafka::App.setup { |c| c.producer = $producer }

So instead of trying to hook somewhere before Karafka.producer is initialized, let's simply listen to a Karafka after-initialization event and append our middleware to the producer when that event is triggered.

Motivation:
We've noticed that WaterDrop distributed tracing was not working after configuring only the Karafka integration.

Change log entry

Additional Notes:

How to test the change?

By the time that `Karafka::App#initialized!` is called,
`Karafka.producer` was already initialized.

On top of that, there's really nowhere we can hook in the Karafka
initialization where we can be sure that `Karafka.producer` wasn't yet
initiaized - that's because the Karafka initialization is NOT
necessarily tied to the WaterDrop initialization. For instance, the
following is a possible scenario:

```
$producer = WaterDrop::Producer.new { ... }

Datadog.configure do |c|
  c.tracing.instrument :karafka
end

 # note that the producer was initialized before the Karafka app (and in
 # this case, even before datadog was configured)
Karafka::App.setup do |c|
  c.producer = $producer
end
```

So instead of trying to hook somewhere before `Karafka.producer` is
initialized, let's simply listen to a Karafka after-initialization event
and append our middleware to the producer when that event is triggered.
@Drowze Drowze requested review from a team as code owners December 8, 2025 18:04
@Drowze Drowze requested a review from vpellan December 8, 2025 18:04
@github-actions github-actions bot added integrations Involves tracing integrations tracing labels Dec 8, 2025
::Karafka::Messages::Messages.prepend(MessagesPatch)
::Karafka::App.singleton_class.prepend(AppPatch)

if Contrib::WaterDrop::Integration.compatible?
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As of now, the WaterDrop integration requires version >= 2.8.8.rc1 (as it introduced producer.configured events, which we can hook onto to add a middleware to a just-initialized instance)
Meanwhile, the lowest Karafka version we support (2.3.0) requires waterdrop >= 2.6.12.

Because of that version discrepancy, it's perfectly possible to have Karafka compatible and WaterDrop incompatible at the same time (in fact, our karafka_min gemfiles fall into exactly this)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

integrations Involves tracing integrations tracing

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant