Skip to content

Commit

Permalink
Add py-spy profiler support
Browse files Browse the repository at this point in the history
  • Loading branch information
eirsyl committed Jun 9, 2023
1 parent 4f5bc85 commit 62f3494
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 38 deletions.
26 changes: 19 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,19 @@ connect_troncos_logging_celery_signals()

## Profiling

### Enabling the profiler
### Enabling the continuous py-spy profiler

Start the profiler by running the `start_py_spy_profiler` method early in your application. This is
typically done in `settings.py` of you want to profile a Django application, or in `__init__.py`
in the root project package.

```python
from troncos.profiling import start_py_spy_profiler

start_py_spy_profiler(server_address="http://127.0.0.1:4100")
```

### Enabling the ddtrace profiler

Start the profiler by importing the profiler module early in your application. This is
typically done in `settings.py` of you want to profile a Django application, or in `__init__.py`
Expand All @@ -207,11 +219,11 @@ in the root project package.
import troncos.profiling.auto
```

### Setup profile endpoint
#### Setup profile endpoint

Use one of the methods bellow based on your selected framework.

#### Django
##### Django

Add the profile view to the url config.

Expand All @@ -225,7 +237,7 @@ urlpatterns = [
]
```

#### Starlette
##### Starlette

Add the profile view to your router.

Expand All @@ -239,7 +251,7 @@ routes = [
]
```

#### ASGI
##### ASGI

Mount the generic ASGI profiling application. There is no generic way to do this,
please check the relevant ASGI framework documentation.
Expand All @@ -255,15 +267,15 @@ app = FastAPI()
app.mount("/debug/pprof", profiling_asgi_app)
```

### Verify setup
#### Verify setup

You can verify that your setup works with the [pprof](https://github.com/google/pprof) cli:

```console
$ pprof -http :6060 "http://localhost:8080/debug/pprof"
```

### Enable scraping
#### Enable scraping

When you deploy your application, be sure to use the custom oda annotation for scraping:

Expand Down
49 changes: 18 additions & 31 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ django = "^4.2.1"
starlette = "^0.27.0"
structlog = "^23.1.0"
celery = "^5.2.7"
pytest-env = "^0.8.1"


[tool.black]
Expand Down Expand Up @@ -73,6 +74,9 @@ module = [
]
implicit_reexport = true

[tool.pytest]
env = "DD_SERVICE=troncos"

[tool.pytest.ini_options]
minversion = 6.0
python_files = "test_*.py"
Expand Down
3 changes: 3 additions & 0 deletions troncos/profiling/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from .pyroscope import start_py_spy_profiler

__all__ = ["start_py_spy_profiler"]
39 changes: 39 additions & 0 deletions troncos/profiling/pyroscope.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import pyroscope
from ddtrace import config


def start_py_spy_profiler(
*,
server_address: str,
tags: dict[str, str] | None = None,
enable_logging: bool = False,
) -> None:
"""Start the py-spy continuous profiler."""

app_name = config.service
profiler_tags = {
"app": app_name,
"env": config.env,
"version": config.version,
**config.tags,
}

if tags:
profiler_tags.update(tags)

if not app_name:
raise RuntimeError(
"Please set the DD_SERVICE env variable or convigure "
"the tracer service name manually."
)

pyroscope.configure(
app_name=config.service,
tags=profiler_tags,
server_address=server_address,
sample_rate=100,
detect_subprocesses=True,
oncpu=False,
gil_only=False,
enable_logging=enable_logging,
)

0 comments on commit 62f3494

Please sign in to comment.