Skip to content

Refactor: observability code cleanup#17862

Merged
merrymercy merged 1 commit intosgl-project:mainfrom
openanolis:sufeng-buaa/observability_integration
Feb 25, 2026
Merged

Refactor: observability code cleanup#17862
merrymercy merged 1 commit intosgl-project:mainfrom
openanolis:sufeng-buaa/observability_integration

Conversation

@sufeng-buaa
Copy link
Collaborator

Motivation

According to #17482, organize the observability-related code, remove redundant code, and unify the interfaces for time statistics, request latency metrics, and request tracing.

Modifications

  • Move observability-related code to python/sglang/srt/observability
  • Remove redundant code, refactor inappropriate code, and correct non-standard naming.
  • Timestamps Record
    • In python/sglang/srt/observability/req_time_stats.py, APIServerReqTimeStats, DPControllerReqTimeStats, and SchedulerReqTimeStats are defined to record timestamp information for the tokenizer/gRPC server, dp controller, and scheduler, respectively. A series of set_time methods are provided to set timestamps, along with get methods to calculate latency.
    • Uniformly use MONOTONIC TIME, and update the time difference between MONOTONIC TIME and REALTIME upon each incoming request, for converting to REALTIME when necessary.
  • Request latency metrics
    • Define the base class ReqTimeStatsBase for APIServerReqTimeStats, DPControllerReqTimeStats, and SchedulerReqTimeStats, integrating a metrics collector inside it. Export latency information to the metrics collector within each set_*_time method.
  • Request Tracing
    • Define the base class ReqTimeStatsBase for APIServerReqTimeStats, DPControllerReqTimeStats, and SchedulerReqTimeStats, integrating a trace context inside it. Export trace spans within each set_*_time method. Define getstate and setstate of ReqTimeStatsBase to propagate the trace context.
    • Refactor the tracing package and optimize the span structure.
    • Support trace levels, and dynamically adjust trace levels via HTTP API.
    • Support tracing for requests with parallel_sample_num > 1.
    • Support tracing for request retract

Accuracy Tests

Benchmarking and Profiling

Checklist

Review Process

  1. Ping Merge Oncalls to start the PR flow. See the PR Merge Process.
  2. Get approvals from CODEOWNERS and other reviewers.
  3. Trigger CI tests with comments or contact authorized users to do so.
    • /tag-run-ci-label, /rerun-failed-ci, /tag-and-rerun-ci
  4. After green CI and required approvals, ask Merge Oncalls to merge.

@gemini-code-assist
Copy link
Contributor

Warning

You have reached your daily quota limit. Please wait up to 24 hours and I will start processing your requests again!

@github-actions github-actions bot added the documentation Improvements or additions to documentation label Jan 28, 2026
@sufeng-buaa
Copy link
Collaborator Author

/tag-and-rerun-ci

@sufeng-buaa
Copy link
Collaborator Author

/gemini review

@gemini-code-assist
Copy link
Contributor

Warning

You have reached your daily quota limit. Please wait up to 24 hours and I will start processing your requests again!

@sufeng-buaa sufeng-buaa force-pushed the sufeng-buaa/observability_integration branch from fbb3f87 to c381672 Compare January 29, 2026 05:45
Copy link
Collaborator

@ShangmingCai ShangmingCai left a comment

Choose a reason for hiding this comment

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

The disaggregation part LGTM.

global_diff_realtime_monotonic = time.time() - time.perf_counter()


def calibrate_time_diff():
Copy link
Contributor

Choose a reason for hiding this comment

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

Using global_diff_realtime_monotonic is a good idea for converting perf_counter values to timestamps. However, when DP > 1, you may have a single tokenization manager but multiple schedulers running across different devices. In that case, you need to be careful: each process/device can have its own monotonic clock offset, so the conversion may be inconsistent across ranks unless those offsets are synchronized or computed per rank.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Monotonic time is typically obtained via the Linux kernel's vDSO (or system call), and the Linux kernel ensures timestamp consistency across different CPUs. Even in extremely rare cases, non-observability functionalities only rely on time retrieved within the same process, while observability features can fully tolerate such minimal timing discrepancies.

Copy link
Contributor

Choose a reason for hiding this comment

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

But if the tokenize manager and the scheduler are running on completely different machines, will the monotonic time they obtain still be consistent?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Fixed. Correct monotonic time during deserialization by propagating it with a diff.

@sufeng-buaa sufeng-buaa force-pushed the sufeng-buaa/observability_integration branch 2 times, most recently from 2c1c70f to 8092b3f Compare January 30, 2026 10:44
@sufeng-buaa
Copy link
Collaborator Author

/gemini review

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request introduces a significant and well-executed refactoring of the observability code. The changes centralize timing, metrics, and tracing logic into new ReqTimeStats and TraceContext objects, which greatly improves code organization and maintainability. The new API for tracing is much cleaner and more powerful, with features like dynamic trace levels. The use of monotonic time and careful handling of time across processes are also commendable. Overall, this is a high-quality refactoring that enhances the observability of the system. My review includes a few minor suggestions for improving the documentation.

@sufeng-buaa
Copy link
Collaborator Author

/rerun-failed-ci

@sufeng-buaa sufeng-buaa force-pushed the sufeng-buaa/observability_integration branch 4 times, most recently from 584e0e6 to d42426b Compare February 18, 2026 01:15
Copy link
Contributor

@sherlockwu sherlockwu left a comment

Choose a reason for hiding this comment

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

Thanks for the cleanup of the metrics and tracing code! LGTM. A few small nits below.

# Detailed breakdown of cached tokens by source (device/host/storage)
cached_tokens_details: Optional[List[Optional[Dict[str, Any]]]] = None

# for observability
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: For observability

same as other places.

self.abort()

if attrs:
self.root_span.set_attributes(attrs)
Copy link
Contributor

Choose a reason for hiding this comment

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

Is it possible for root_span to be None (e.g. after deserialization)?

It looks impossible under current logic -- but probably safer to add an explicit guard / null check

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

ok, I have added it.

Copy link
Contributor

Choose a reason for hiding this comment

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

nice!

@sufeng-buaa sufeng-buaa force-pushed the sufeng-buaa/observability_integration branch 2 times, most recently from d76ad22 to d233882 Compare February 19, 2026 10:12
@sufeng-buaa
Copy link
Collaborator Author

/rerun-failed-ci

@sufeng-buaa sufeng-buaa force-pushed the sufeng-buaa/observability_integration branch 2 times, most recently from e558920 to 281156b Compare February 23, 2026 03:11
@sufeng-buaa
Copy link
Collaborator Author

/rerun-failed-ci

@sufeng-buaa
Copy link
Collaborator Author

/rerun-failed-ci

3 similar comments
@sufeng-buaa
Copy link
Collaborator Author

/rerun-failed-ci

@sufeng-buaa
Copy link
Collaborator Author

/rerun-failed-ci

@sufeng-buaa
Copy link
Collaborator Author

/rerun-failed-ci

@sufeng-buaa sufeng-buaa force-pushed the sufeng-buaa/observability_integration branch 2 times, most recently from 74c4f00 to 74112dd Compare February 24, 2026 00:19
Signed-off-by: Feng Su <sufeng@linux.alibaba.com>
@sufeng-buaa sufeng-buaa force-pushed the sufeng-buaa/observability_integration branch from 74112dd to 9f765ab Compare February 24, 2026 02:28
@sufeng-buaa
Copy link
Collaborator Author

/rerun-failed-ci

2 similar comments
@sufeng-buaa
Copy link
Collaborator Author

/rerun-failed-ci

@sherlockwu
Copy link
Contributor

/rerun-failed-ci

@merrymercy merrymercy merged commit 3b89302 into sgl-project:main Feb 25, 2026
256 of 271 checks passed
ishandhanani added a commit that referenced this pull request Feb 25, 2026
Adapt HiCache host-tier metrics to follow the canonical
SchedulerStats -> SchedulerMetricsCollector -> log_stats() pipeline
established in #17862. Mirrors the LoRA conditional-metrics pattern:

- Add hicache_host_used_tokens/hicache_host_total_tokens to SchedulerStats
- Add enable_hierarchical_cache flag to SchedulerMetricsCollector
- Populate stats in _log_hicache_stats() before log_stats() push
- Remove standalone HiCacheMetricsCollector class
@sufeng-buaa sufeng-buaa linked an issue Feb 27, 2026 that may be closed by this pull request
@feng-95
Copy link

feng-95 commented Feb 27, 2026

Hi @sufeng-buaa, Thanks for the great refactoring work!

While reviewing the observability code, I noticed that the service name is fixed as "sglang". Would it make sense to make it compatible with the standard OTEL_SERVICE_NAME environment variable? This could make it easier to integrate with existing OpenTelemetry setups.

If you are open to this enhancement, I'd be more than happy to implement it and submit a follow-up PR. Let me know what you think!

@sufeng-buaa
Copy link
Collaborator Author

Hi @sufeng-buaa, Thanks for the great refactoring work!

While reviewing the observability code, I noticed that the service name is fixed as "sglang". Would it make sense to make it compatible with the standard OTEL_SERVICE_NAME environment variable? This could make it easier to integrate with existing OpenTelemetry setups.

If you are open to this enhancement, I'd be more than happy to implement it and submit a follow-up PR. Let me know what you think!

The tracing feature is still pretty early. I haven't really thought hard about the naming yet. Feel free to send PRs and help standardize it.

magicYang1573 pushed a commit to magicYang1573/sglang that referenced this pull request Mar 9, 2026
Signed-off-by: Feng Su <sufeng@linux.alibaba.com>
Wangzheee pushed a commit to Wangzheee/sglang that referenced this pull request Mar 21, 2026
Signed-off-by: Feng Su <sufeng@linux.alibaba.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

documentation Improvements or additions to documentation high priority run-ci

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Refactor ideas] Per request time stats tracing

7 participants