Skip to content

Conversation

@lun-4
Copy link
Contributor

@lun-4 lun-4 commented Sep 29, 2025

Motivation

I was checking tracing for our setup and noticed that fastapi is not instrumented: #10804 (comment), this leads to being unable to use sglang in other services that do use specific conventions for tracing (such as w3c's trace context, also used by opentelemetry, see comment for references). this was tested manually in our go net/http + otelhttp.Transport calling the sglang deployment.

Modifications

  • add instrumentation for fastapi for optional tracing

Accuracy Tests

N/A

Benchmarking and Profiling

N/A

Checklist

@lun-4 lun-4 changed the title Luna.fastapi instrumentation [metrics] instrument fastapi for tracing Sep 29, 2025
@gemini-code-assist
Copy link
Contributor

Summary of Changes

Hello @lun-4, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request integrates OpenTelemetry tracing capabilities into the FastAPI server component of the sglang project. This enhancement ensures that requests processed by the FastAPI application are properly traced, enabling seamless integration with distributed tracing systems that adhere to standards like W3C Trace Context. The change addresses a critical gap in the existing tracing setup, allowing sglang to be used effectively in environments with comprehensive tracing requirements.

Highlights

  • FastAPI Instrumentation: Introduced OpenTelemetry instrumentation for the FastAPI server to enable proper distributed tracing for requests.
  • Dependency Update: Added opentelemetry-instrumentation-fastapi to the project's tracing dependencies and documentation.
  • Tracing Integration: Implemented a new instrument_fastapi function in trace.py and integrated it into http_server.py to conditionally apply instrumentation to the FastAPI application.
  • Motivation: This change addresses a previous issue where FastAPI was not instrumented, preventing the use of standard tracing conventions like W3C's trace context and OpenTelemetry in other services.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

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 adds optional FastAPI instrumentation for tracing by integrating opentelemetry-instrumentation-fastapi. The changes include updating dependencies, documentation, and adding the necessary code to instrument the FastAPI application.

My review has identified a critical issue in python/sglang/srt/entrypoints/http_server.py where the instrumentation is called at module import time, before tracing is initialized. This prevents the feature from working. I have provided a detailed comment on how to fix this by moving the instrumentation call into the server's lifespan context manager. Once this is addressed, the changes should correctly enable tracing for FastAPI requests.

@lun-4 lun-4 force-pushed the luna.fastapi-instrumentation branch from 59081d3 to 7a76146 Compare September 29, 2025 17:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants