Skip to content

Add Prometheus metrics instrumentation to WikiStreamService - #68

Open
GergesShamon wants to merge 3 commits into
mainfrom
feature/prometheus-metrics-wikistream
Open

Add Prometheus metrics instrumentation to WikiStreamService#68
GergesShamon wants to merge 3 commits into
mainfrom
feature/prometheus-metrics-wikistream

Conversation

@GergesShamon

@GergesShamon GergesShamon commented Jul 27, 2026

Copy link
Copy Markdown
Contributor

Summary

Adds Micrometer/Prometheus metrics instrumentation to WikiStreamService to enable monitoring and diagnostics of the SSE event pipeline (stream ingestion, caching, and broadcast stages).

Changes

  • Added micrometer-registry-prometheus dependency to pom.xml
  • Injected MeterRegistry into WikiStreamService and registered the following metrics:
    • wiki.events.received (Counter) — total events received from the Wikimedia stream
    • wiki.events.matched (Counter) — events matching at least one user filter
    • wiki.event.process.duration (Timer) — time from parse to broadcast dispatch
    • wiki.cache.write.duration (Timer) — Redis cache write latency
    • wiki.broadcast.duration (Timer) — per-emitter SSE send duration
    • wiki.stream.reconnects (Counter) — stream reconnect attempts
    • wiki.replay.size (DistributionSummary) — number of events replayed per reconnecting client
    • wiki.emitters.active (Gauge) — currently connected SSE clients
  • Exposed prometheus, health, and metrics actuator endpoints in application.properties
  • Enabled percentile histograms for the three latency-critical timers (event.process, broadcast, cache.write) to support bottleneck diagnosis
  • Updated WikiStreamServiceTest to inject a SimpleMeterRegistry for unit tests

Notes

  • No behavioral changes to the streaming/broadcast logic itself — this is purely additive instrumentation.

Testing

  • Existing unit tests updated and passing with SimpleMeterRegistry.
  • Manually verified /actuator/prometheus exposes the new metrics.

Summary by CodeRabbit

  • New Features
    • Added Prometheus monitoring for wiki event streams, including event processing, caching, broadcasting, reconnections, filtering, and replay activity.
    • Added health, metrics, and Prometheus monitoring endpoints.
    • Enabled percentile and histogram tracking for key stream performance metrics.

@coderabbitai

coderabbitai Bot commented Jul 27, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

Warning

Review limit reached

@GergesShamon, you've reached your PR review limit, so we couldn't start this review.

Next review available in: 27 minutes

Enable usage-based reviews in Billing to review now. Otherwise, wait until the next included review is available.
You're only billed for reviews past your plan's rate limits ($0.25/file).

How can I continue?

After more reviews become available, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

To avoid repeated limits, reduce automatic review volume by pausing incremental auto-reviews earlier, using label-based review opt-in, excluding WIP or generated PR titles, or requesting reviews manually when the PR is ready. If your team needs uninterrupted high-volume reviews, an organization admin can enable usage-based reviews.

How do review limits work?

CodeRabbit enforces per-developer PR review limits for each organization. Most developers receive the normal plan review availability.

For paid Pro and Pro+ PR reviews, CodeRabbit uses adaptive limits for sustained high-volume activity. When a developer's recent PR review activity reaches the 95th percentile or higher among CodeRabbit users, additional reviews become available more gradually as earlier reviews age out of the rolling window.

Please refer docs for additional details.

Review details
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro Plus

Run ID: c497deaa-b1b4-4a16-a517-ff154d617ec5

📥 Commits

Reviewing files that changed from the base of the PR and between 9c3a9c5 and cb81aac.

📒 Files selected for processing (5)
  • pom.xml
  • src/main/java/org/qrdlife/wikiconnect/wikimonitor/config/SecurityConfig.java
  • src/main/java/org/qrdlife/wikiconnect/wikimonitor/service/WikiStreamService.java
  • src/main/resources/application.properties
  • src/test/java/org/qrdlife/wikiconnect/wikimonitor/service/WikiStreamServiceTest.java
📝 Walkthrough

Walkthrough

Adds Micrometer instrumentation to the SSE stream service, exposes Prometheus and Actuator metrics, records event-processing measurements, and updates production and test constructor wiring for MeterRegistry.

Changes

SSE metrics instrumentation

Layer / File(s) Summary
Metric registry and exposure
pom.xml, src/main/java/.../WikiStreamService.java, src/main/resources/application.properties
Adds the Prometheus registry, defines counters, timers, replay summaries, and an active-emitter gauge, and exposes the related Actuator endpoints.
SSE processing measurements
src/main/java/.../WikiStreamService.java
Records event receipt, matching, reconnect, processing, cache, broadcast, and replay metrics across the SSE flow.
Test registry wiring
src/test/java/.../WikiStreamServiceTest.java
Creates a SimpleMeterRegistry and supplies it to WikiStreamService during test setup.

Estimated code review effort: 3 (Moderate) | ~20 minutes

Possibly related PRs

Suggested reviewers: praffq-dev

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 11.11% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately summarizes the main change: adding Prometheus metrics instrumentation to WikiStreamService.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch feature/prometheus-metrics-wikistream

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Actionable comments posted: 3

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (1)
src/main/java/org/qrdlife/wikiconnect/wikimonitor/service/WikiStreamService.java (1)

303-324: 🎯 Functional Correctness | 🟡 Minor | ⚡ Quick win

Do not record queued replay work as send duration.

When context.replaying is true, this task only queues a pending event and returns, but wiki.broadcast.duration still records a sample. Move the timer around the actual sendIfMatched call or rename the metric description to reflect dispatch/queue processing.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In
`@src/main/java/org/qrdlife/wikiconnect/wikimonitor/service/WikiStreamService.java`
around lines 303 - 324, The broadcast duration timer in the event handling block
should measure only actual dispatch through sendIfMatched, not events queued
during context.replaying. Move Timer.Sample creation to immediately before
sendIfMatched, or otherwise stop recording it for the queue-and-return path
while preserving existing exception handling and metric behavior for sends.
🧹 Nitpick comments (1)
src/test/java/org/qrdlife/wikiconnect/wikimonitor/service/WikiStreamServiceTest.java (1)

66-70: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick win

Add assertions for the new meters.

This setup only proves that construction accepts a registry; no test verifies metric registration or values. Add focused assertions for counters, replay size, timers, and the active-emitter gauge so instrumentation regressions are detected.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In
`@src/test/java/org/qrdlife/wikiconnect/wikimonitor/service/WikiStreamServiceTest.java`
around lines 66 - 70, Add focused assertions in WikiStreamServiceTest for the
meters registered by WikiStreamService: verify counter registration and values,
replay-size metrics, timer presence or recordings, and the active-emitter gauge.
Exercise the relevant service behavior as needed, and use the existing
meterRegistry to locate metrics by their established names and tags.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In
`@src/main/java/org/qrdlife/wikiconnect/wikimonitor/service/WikiStreamService.java`:
- Around line 448-454: Update the replay handling flow in WikiStreamService so
an empty entries collection records replay size 0 before returning. Preserve the
existing replaySize.record(replayed) behavior for non-empty and processed
replays, ensuring every reconnecting client contributes one metric sample.
- Around line 339-340: Update the matched-event accounting in sendIfMatched so
wiki.events.matched is incremented once per incoming event rather than once per
emitter; alternatively, rename the metric to explicitly represent matched
deliveries. Preserve the existing StreamEventDTO conversion and delivery
behavior.
- Around line 123-125: Update the active-emitter metric near
Gauge.builder("wiki.emitters.active", emitters, Map::size) so its measured value
matches the description: either include anonymous and unresolved-user
subscriptions in the tracked collection before they return, or revise the
description to explicitly state that it counts only authenticated registered
emitters. Keep the chosen metric source and description consistent.

---

Outside diff comments:
In
`@src/main/java/org/qrdlife/wikiconnect/wikimonitor/service/WikiStreamService.java`:
- Around line 303-324: The broadcast duration timer in the event handling block
should measure only actual dispatch through sendIfMatched, not events queued
during context.replaying. Move Timer.Sample creation to immediately before
sendIfMatched, or otherwise stop recording it for the queue-and-return path
while preserving existing exception handling and metric behavior for sends.

---

Nitpick comments:
In
`@src/test/java/org/qrdlife/wikiconnect/wikimonitor/service/WikiStreamServiceTest.java`:
- Around line 66-70: Add focused assertions in WikiStreamServiceTest for the
meters registered by WikiStreamService: verify counter registration and values,
replay-size metrics, timer presence or recordings, and the active-emitter gauge.
Exercise the relevant service behavior as needed, and use the existing
meterRegistry to locate metrics by their established names and tags.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro Plus

Run ID: 068b7b21-bb72-49f8-83e3-c2f9f6be6e1e

📥 Commits

Reviewing files that changed from the base of the PR and between 1302fc1 and 9c3a9c5.

📒 Files selected for processing (4)
  • pom.xml
  • src/main/java/org/qrdlife/wikiconnect/wikimonitor/service/WikiStreamService.java
  • src/main/resources/application.properties
  • src/test/java/org/qrdlife/wikiconnect/wikimonitor/service/WikiStreamServiceTest.java

Comment on lines +339 to 340
eventsMatched.increment();
StreamEventDTO dto = StreamEventDTO.fromRecentChange(rc, matchedFilters);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🎯 Functional Correctness | 🟡 Minor | ⚡ Quick win

Count matched events at the intended unit.

sendIfMatched runs once per emitter, so one incoming event matching three clients increments wiki.events.matched three times. That contradicts the event-level description; either rename it to represent matched deliveries or aggregate it once per incoming event.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In
`@src/main/java/org/qrdlife/wikiconnect/wikimonitor/service/WikiStreamService.java`
around lines 339 - 340, Update the matched-event accounting in sendIfMatched so
wiki.events.matched is incremented once per incoming event rather than once per
emitter; alternatively, rename the metric to explicitly represent matched
deliveries. Preserve the existing StreamEventDTO conversion and delivery
behavior.

@GergesShamon
GergesShamon force-pushed the feature/prometheus-metrics-wikistream branch from 9c3a9c5 to 3d0f680 Compare July 27, 2026 11:48
…avior

- Update description of wiki.emitters.active gauge to explicitly state that it counts authenticated registered emitters.
- Rename wiki.events.matched metric to wiki.events.matched.deliveries to reflect per-emitter delivery accounting.
- Record replay size sample of 0 when client reconnects and event cache is empty in replayMissedEvents.
- Add unit test verifying empty replay metric recording.
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.

1 participant