diff --git a/docs/my-website/docs/proxy/config_settings.md b/docs/my-website/docs/proxy/config_settings.md index cbd56d7c091..ef3cc750f9f 100644 --- a/docs/my-website/docs/proxy/config_settings.md +++ b/docs/my-website/docs/proxy/config_settings.md @@ -775,6 +775,7 @@ router_settings: | LITELLM_ENABLE_PYROSCOPE | If true, enables Pyroscope CPU profiling. Profiles are sent to PYROSCOPE_SERVER_ADDRESS. Off by default. See [Pyroscope profiling](/proxy/pyroscope_profiling). | PYROSCOPE_APP_NAME | Application name reported to Pyroscope. Required when LITELLM_ENABLE_PYROSCOPE is true. No default. | PYROSCOPE_SERVER_ADDRESS | Pyroscope server URL to send profiles to. Required when LITELLM_ENABLE_PYROSCOPE is true. No default. +| PYROSCOPE_SAMPLE_RATE | Optional. Sample rate for Pyroscope profiling (integer). No default; when unset, the pyroscope-io library default is used. | LITELLM_MASTER_KEY | Master key for proxy authentication | LITELLM_MODE | Operating mode for LiteLLM (e.g., production, development) | LITELLM_NON_ROOT | Flag to run LiteLLM in non-root mode for enhanced security in Docker containers diff --git a/litellm/proxy/proxy_server.py b/litellm/proxy/proxy_server.py index 0bb8eb985c2..20381f8c46a 100644 --- a/litellm/proxy/proxy_server.py +++ b/litellm/proxy/proxy_server.py @@ -5561,9 +5561,10 @@ class ProxyStartupEvent: Off by default. Enable with LITELLM_ENABLE_PYROSCOPE=true. Requires: pip install pyroscope-io Sends profiles to PYROSCOPE_SERVER_ADDRESS (default http://localhost:4040). + Optional: PYROSCOPE_SAMPLE_RATE (float, no default) to set the sample rate. """ if not get_secret_bool("LITELLM_ENABLE_PYROSCOPE", False): - print( # noqa: T201 + verbose_proxy_logger.info( "LiteLLM: Pyroscope profiling is disabled (set LITELLM_ENABLE_PYROSCOPE=true to enable)." ) return @@ -5588,17 +5589,31 @@ class ProxyStartupEvent: ) if env_name: tags["environment"] = env_name - pyroscope.configure( - app_name=app_name, - server_address=server_address, - tags=tags if tags else None, - ) - print( # noqa: T201 + sample_rate_env = os.getenv("PYROSCOPE_SAMPLE_RATE") + configure_kwargs = { + "app_name": app_name, + "server_address": server_address, + "tags": tags if tags else None, + } + if sample_rate_env is not None: + try: + # pyroscope-io expects sample_rate as an integer + configure_kwargs["sample_rate"] = int(float(sample_rate_env)) + except (ValueError, TypeError): + raise ValueError( + "PYROSCOPE_SAMPLE_RATE must be a number, got: " + f"{sample_rate_env!r}" + ) + pyroscope.configure(**configure_kwargs) + msg = ( f"LiteLLM: Pyroscope profiling started (app_name={app_name}, server_address={server_address}). " f"View CPU profiles at the Pyroscope UI and select application '{app_name}'." ) + if "sample_rate" in configure_kwargs: + msg += f" sample_rate={configure_kwargs['sample_rate']}" + verbose_proxy_logger.info(msg) except ImportError: - print( # noqa: T201 + verbose_proxy_logger.warning( "LiteLLM: LITELLM_ENABLE_PYROSCOPE is set but the 'pyroscope-io' package is not installed. " "Pyroscope profiling will not run. Install with: pip install pyroscope-io" )