Repository navigation
fix(metrics): engine metrics from PROMETHEUS_MULTIPROC_DIR + graceful bind #1075
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
504a855
read engine metrics from PROMETHEUS_MULTIPROC_DIR in grpc mode
b33cf56
lint and format
2b96a90
fix(metrics): guard against empty prometheus multiproc metrics output
ConnorLi96 ad01bcf
fix(metrics): graceful metrics server bind failure
ConnorLi96 63eff15
feat(metrics): merge engine metrics into :29000/metrics endpoint
ConnorLi96 f4d1ecb
feat(observability): add worker selection and backend response INFO logs
ConnorLi96 0f93a4c
fix(metrics): merge sglang engine metrics via HTTP scrape of gRPC wor…
ConnorLi96 3f6eec0
fix(metrics): preserve colons in sglang metric names on merged /metri…
ConnorLi96 9a9b6e0
style: fix nightly rustfmt formatting in health.rs and manager.rs
ConnorLi96 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -142,6 +142,7 @@ class GrpcReqState: | |
| # Metrics (same as TokenizerManager's ReqState) | ||
| time_stats: APIServerReqTimeStats | ||
| last_completion_tokens: int = 1 | ||
| ttft_observed: bool = False | ||
|
|
||
| # Streaming state | ||
| stream_finished: bool = False | ||
|
|
@@ -205,6 +206,33 @@ def __init__( | |
|
|
||
| # Metrics | ||
| self.last_receive_tstamp = real_time() | ||
| self.metrics_collector = None | ||
| if server_args.enable_metrics: | ||
| try: | ||
| from sglang.srt.observability.metrics_collector import ( | ||
| TokenizerMetricsCollector, | ||
| ) | ||
|
|
||
| labels = { | ||
| "model_name": server_args.served_model_name, | ||
| } | ||
| self.metrics_collector = TokenizerMetricsCollector( | ||
| server_args=server_args, | ||
| labels=labels, | ||
| bucket_time_to_first_token=server_args.bucket_time_to_first_token, | ||
| bucket_e2e_request_latency=server_args.bucket_e2e_request_latency, | ||
| bucket_inter_token_latency=getattr( | ||
| server_args, "bucket_inter_token_latency", None | ||
| ), | ||
| ) | ||
| self._metrics_labels = labels | ||
| logger.info("TokenizerMetricsCollector initialized for gRPC request-level metrics") | ||
| except Exception: | ||
| logger.warning( | ||
| "Failed to initialize TokenizerMetricsCollector, " | ||
| "request-level metrics will be unavailable", | ||
| exc_info=True, | ||
| ) | ||
|
Comment on lines
+209
to
+235
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🧹 Nitpick | 🔵 Trivial Metrics init: minor hardening — Two small points:
Proposed diff self.metrics_collector = None
+ self._metrics_labels: dict[str, str] = {}
if server_args.enable_metrics:
try:
from sglang.srt.observability.metrics_collector import (
TokenizerMetricsCollector,
)🤖 Prompt for AI Agents |
||
|
|
||
| # Crash dump for debugging | ||
| self.crash_dump_request_list = [] | ||
|
|
@@ -576,12 +604,34 @@ async def _handle_batch_output(self, batch_out: BatchTokenIDOutput): | |
| logger.debug(f"Skipping output for aborted request {rid}") | ||
| continue | ||
|
|
||
| # Update metrics | ||
| # Update timing | ||
| if state.time_stats.first_token_time == 0.0: | ||
| state.time_stats.set_first_token_time() | ||
| else: | ||
| state.time_stats.set_last_time() | ||
|
|
||
| # Observe request-level Prometheus metrics | ||
| if self.metrics_collector is not None: | ||
| completion_tokens = ( | ||
| batch_out.completion_tokens[i] if batch_out.completion_tokens else 0 | ||
| ) | ||
| labels = dict(self._metrics_labels) | ||
| if not state.ttft_observed: | ||
| state.ttft_observed = True | ||
| state.last_completion_tokens = completion_tokens | ||
| self.metrics_collector.observe_time_to_first_token( | ||
| labels, state.time_stats.get_first_token_latency() | ||
| ) | ||
| else: | ||
| num_new_tokens = completion_tokens - state.last_completion_tokens | ||
| if num_new_tokens > 0: | ||
| self.metrics_collector.observe_inter_token_latency( | ||
| labels, | ||
| state.time_stats.get_interval(), | ||
| num_new_tokens, | ||
| ) | ||
| state.last_completion_tokens = completion_tokens | ||
|
|
||
| # Extract output for this request | ||
| output_data = { | ||
| "request_id": rid, | ||
|
|
@@ -671,6 +721,20 @@ def get_part(attr_name): | |
| state.stream_finished = True | ||
| state.event.set() | ||
|
|
||
| if self.metrics_collector is not None: | ||
| prompt_tokens = output_data["meta_info"].get("prompt_tokens", 0) | ||
| compl_tokens = output_data["meta_info"].get("completion_tokens", 0) | ||
| cached_tokens = output_data["meta_info"].get("cached_tokens", 0) | ||
| self.metrics_collector.observe_one_finished_request( | ||
| dict(self._metrics_labels), | ||
| prompt_tokens, | ||
| compl_tokens, | ||
| cached_tokens, | ||
| state.time_stats.get_e2e_latency(), | ||
| False, | ||
| 0, | ||
| ) | ||
|
|
||
| # Remove from tracking after a delay | ||
| async def cleanup(request_id): | ||
| await asyncio.sleep(5.0) | ||
|
|
||
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🟡 Nit: After removing the directory, the
PROMETHEUS_MULTIPROC_DIRenvironment variable still points to the now-deleted path. If any code (e.g., a straggling atexit handler or a library import) reads the env var after cleanup, it will reference a non-existent directory, which can cause confusing errors.Consider also clearing the env var: