pyroscope icon indicating copy to clipboard operation
pyroscope copied to clipboard

Python package pyroscope-io not working with Python 3.12

Open achainarong opened this issue 1 year ago • 3 comments

Hey guys i wanted to try out pyroscope to use as a tool to get performance metrics in my kubernetes cluster. We are using FastAPI with Python 3.12

Describe the bug

The pyroscope server seems to not receive any metrics from FastApi when i call my endpoints. When i downgrade to Python 3.11 it works like a charm.

To Reproduce

Steps to reproduce the behavior:

  1. Create FastApi project

Created a main.py with a middleware to get the metrics for all endpoints: poetry new fastapi-pyroscope-project cd fastapi-pyroscope-project poetry add fastapi uvicorn pyroscope poetry install poetry run uvicorn main:app --reload

You can switch between installed python version like this: poetry env use 3.11 or poetry env use 3.12 afterwards you have to install dependencies with poetry install

  1. Adding main.py and running my project

from fastapi import FastAPI, Request from starlette.middleware.base import BaseHTTPMiddleware from starlette.types import ASGIApp, Receive, Scope, Send import pyroscope import time import random

class PyroscopeMiddleware(BaseHTTPMiddleware): async def dispatch(self, request: Request, call_next): path = request.url.path with pyroscope.tag_wrapper({ "endpoint": path }): start_time = time.time() response = await call_next(request) process_time = time.time() - start_time response.headers["X-Process-Time"] = str(process_time) return response

app = FastAPI()

pyroscope.configure( app_name="app_lol_3.12", server_address="http://localhost:4040"

)

app.add_middleware(PyroscopeMiddleware)

@app.get("/") async def read_root(): return {"Hello": "World"}

@app.get("/random-sleep") async def random_sleep(): sleep_time = random.randint(1, 3) time.sleep(sleep_time) return {"sleep_time": sleep_time}

def fibonacci(n): if n <= 1: return n else: return fibonacci(n-1) + fibonacci(n-2)

@app.get("/cpu-intensive") async def cpu_intensive(): n = random.randint(30, 40)

start_time = time.time()

result = fibonacci(n)

end_time = time.time()
duration = end_time - start_time

return {
    "n": n,
    "Fibonacci(n)": result,
    "Duration": f"{duration} seconds"
}

Expected behavior

I expected to get the results in the dashboard like usually with python 3.11

Environment

  • macos 14.1.1 (23B81)
  • running grafana/pyroscope in docker with docker run -it -p 4040:4040 grafana/pyroscope

achainarong avatar Jan 31 '24 20:01 achainarong

Oh right i can only install the pyroscope-io 0.8.5 version from https://pypi.org/project/pyroscope-io/. Are there new version in another source?

achainarong avatar Jan 31 '24 20:01 achainarong

Oh right i can only install the pyroscope-io 0.8.5 version from https://pypi.org/project/pyroscope-io/. Are there new version in another source?

The source for the pyroscope-io package is here: https://github.com/grafana/pyroscope-rs/tree/main/pyroscope_ffi/python

s4v4g3 avatar Feb 07 '24 23:02 s4v4g3