Add rate-limit observability events#3030
Open
Eministar wants to merge 2 commits intodiscord-jda:masterfrom
Open
Add rate-limit observability events#3030Eministar wants to merge 2 commits intodiscord-jda:masterfrom
Eministar wants to merge 2 commits intodiscord-jda:masterfrom
Conversation
This change introduces a neutral metrics integration point for JDA REST
processing and wires it into both request execution and rate-limit handling.
Motivation
- Large bots need stable operational visibility for REST throughput,
failures, retries, and rate-limit pressure.
- Existing logging is useful for debugging, but not ideal for metrics
backends and alerting pipelines.
- A lightweight, backend-agnostic collector API provides a clean bridge
to Micrometer/OpenTelemetry adapters without introducing hard dependencies.
What was added
- New public API type: RestMetricsCollector
- onRequest(RequestMetric): request execution metrics
- onRateLimit(RateLimitEvent): rate-limit and backoff metrics
- New request metric model:
- JDA instance
- compiled route
- HTTP status code (-1 when no HTTP response exists)
- attempt count
- duration in milliseconds
- success flag
- queued flag
- execution error (nullable)
- RestConfig support:
- setMetricsCollector(RestMetricsCollector)
- getMetricsCollector()
- RateLimitConfig support:
- optional metricsCollector field and accessor
- constructor wiring for metrics collector propagation
- Runtime wiring in JDAImpl:
- configured RestConfig metrics collector is passed into RateLimitConfig
- SequentialRestRateLimiter integration:
- existing rate-limit events are now forwarded to
RestMetricsCollector#onRateLimit when configured
- callback failures are isolated and logged (non-fatal)
- Requester integration:
- emits RequestMetric for successful responses, retry-exhausted paths,
skipped requests, and transport exceptions
- tracks attempts and end-to-end duration
- callback failures are isolated and logged (non-fatal)
Documentation
- Added README “Metrics Bridge” section with usage example.
- Added/updated Javadocs for all new public API methods and types.
Compatibility and behavior
- Backward compatible.
- No default runtime behavior change when no collector is configured.
- No backend dependency added (adapter responsibility remains external).
- Collector callbacks run on request/rate-limit worker threads and should be
non-blocking.
Validation
- Ran ./gradlew format
- Ran ./gradlew build
- Build and tests passed successfully.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Pull Request Etiquette
./gradlew formatChanges
Closes Issue: NaN
Description
This PR adds a structured observability hook for JDA rate-limit processing, focused on the default
SequentialRestRateLimiter.What was added
RestRateLimiter.RateLimitEventwith typed event categories:REQUEST_QUEUEDREQUEST_REQUEUEDBUCKET_UPDATEDRATE_LIMITEDBACKOFFRestConfigsetRateLimitEventConsumer(...)getRateLimitEventConsumer()JDAImplnow passes the configured event consumer intoRestRateLimiter.RateLimitConfig.SequentialRestRateLimiternow emits structured events during queueing, retries, bucket updates, 429 handling, and backoff windows.Rate-Limit Observability).Usage example
Scope
This is one logical feature: observability for REST rate-limit handling.
No unrelated refactors are included.
Validation