Skip to content

[Telemetry] Add Rage::Telemetry.every for periodic block scheduling - #381

Merged
Abishekcs merged 1 commit into
rage-rb:mainfrom
Abishekcs:telemetry/every-periodic-scheduler
Aug 9, 2026
Merged

[Telemetry] Add Rage::Telemetry.every for periodic block scheduling#381
Abishekcs merged 1 commit into
rage-rb:mainfrom
Abishekcs:telemetry/every-periodic-scheduler

Conversation

@Abishekcs

@Abishekcs Abishekcs commented Aug 4, 2026

Copy link
Copy Markdown
Member

What this PR does?

  • Rage::Telemetry.every now schedules each tick via Fiber.schedule instead of calling the block inline in the root fiber

Why this design?

  • Iodine timers fire in the root fiber, which blocks the reactor thread
  • Inside a scheduler fiber, the block's I/O hooks into the fiber scheduler and yields to the reactor, keeping the server responsive

@Abishekcs
Abishekcs force-pushed the telemetry/every-periodic-scheduler branch from c21a6e4 to 3f62ca9 Compare August 4, 2026 17:50
@Abishekcs Abishekcs changed the title [Telemetry] Add Rage::Telemetry.every for periodic block scheduling [WIP] [Telemetry] Add Rage::Telemetry.every for periodic block scheduling Aug 4, 2026
@Abishekcs
Abishekcs marked this pull request as draft August 4, 2026 17:53
@Abishekcs
Abishekcs force-pushed the telemetry/every-periodic-scheduler branch 2 times, most recently from 6791e3a to 0ae3770 Compare August 5, 2026 10:04
@Abishekcs Abishekcs changed the title [WIP] [Telemetry] Add Rage::Telemetry.every for periodic block scheduling [Telemetry] Add Rage::Telemetry.every for periodic block scheduling Aug 5, 2026
@Abishekcs
Abishekcs marked this pull request as ready for review August 5, 2026 10:09
@Abishekcs

Abishekcs commented Aug 5, 2026

Copy link
Copy Markdown
Member Author

@rsamoilov PR is ready for review.

Just a short summary:

I added a block to Rage::Telemetry.every(ms, &block) so that opentelemetry-instrumentation can use this to call the respective block of code they want to run at their respective interval. This block will also serve to calculate the delay in event loop lag.

Also, I wrote a test code which passes locally but kept failing in the CI I have removed it for now


  context "within the reactor" do
      before do
        Fiber.set_scheduler(Rage::FiberScheduler.new)
      end
      after do
        Fiber.set_scheduler(nil)
      end

      it "periodically executes the block" do
        counter = 0
        within_reactor do
          described_class.every(50) { counter += 1 }
          deadline = Process.clock_gettime(Process::CLOCK_MONOTONIC) + 2
          sleep 0.05 while counter < 3 && Process.clock_gettime(Process::CLOCK_MONOTONIC) < deadline
          -> { expect(counter).to be >= 3 }
        end
      end
...


@rsamoilov

Copy link
Copy Markdown
Member

Sorry for the confusion here.

We need to distinguish between Rage::Telemetry.every and event loop lag. every should be a generic method akin to setInterval in JS. It can be use for anything recurring - sampling values, batching and flushing metrics, heartbeats, aggregations, event loop lag. Basically, it should only be wrapping Iodine.run_every, exposing its functionality as a public API.

@Abishekcs

Abishekcs commented Aug 6, 2026

Copy link
Copy Markdown
Member Author

@rsamoilov made the required changes. please have another look whenever you are free. thank you!

rsamoilov
rsamoilov previously approved these changes Aug 7, 2026

@rsamoilov rsamoilov left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Great start!

There are several comments, but I've approved the PR. Feel free to either continue here or merge it and tackle those in another one.

Comment thread lib/rage/telemetry/telemetry.rb Outdated
Comment thread lib/rage/telemetry/telemetry.rb
Timer blocks currently execute directly on the reactor thread in the root fiber. Any blocking I/O in a block - e.g. flushing metrics to a collector
via HTTP - stalls the entire reactor and freezes the server.

Wrap each execution in Fiber.schedule so blocking operations that go through the fiber scheduler (socket I/O, sleep, DNS) yield to the event
loop instead of blocking it.
@Abishekcs
Abishekcs force-pushed the telemetry/every-periodic-scheduler branch from ab4aab2 to 5bbb9a9 Compare August 9, 2026 08:53
@Abishekcs

Copy link
Copy Markdown
Member Author

@rsamoilov made the required changes in this PR itself. Please have another look whenever you are free. Thank you!

@rsamoilov rsamoilov left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Beautiful!

@Abishekcs
Abishekcs merged commit 419d434 into rage-rb:main Aug 9, 2026
11 checks passed
@Abishekcs
Abishekcs deleted the telemetry/every-periodic-scheduler branch August 9, 2026 10:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants