grafana / grafana/pyroscope

Python package pyroscope-io not working with Python 3.12

Open
#2,979 3 comments 0 reactions 0 assignees View on GitHub
Dominant language
Go
Stars
11.7k
Forks
802
Avg merge
1d 19h
Merged PRs (30d)
80

Description

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

2. 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

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.