Skip to content

Commit 7559aa2

Browse files
committed
fix: add unit tests
1 parent d860384 commit 7559aa2

15 files changed

+660
-60
lines changed

.github/workflows/tests.yml

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
name: tests-unittest
2+
3+
on:
4+
pull_request:
5+
6+
jobs:
7+
build:
8+
runs-on: ubuntu-latest
9+
strategy:
10+
matrix:
11+
python-version: ["3.8", "3.9", "3.10", "3.11"]
12+
poetry-version: ["1.2"]
13+
steps:
14+
- uses: actions/checkout@v2
15+
- name: Set up Python ${{ matrix.python-version }}
16+
uses: actions/setup-python@v2
17+
with:
18+
python-version: ${{ matrix.python-version }}
19+
- name: Run image
20+
uses: abatilo/[email protected]
21+
with:
22+
poetry-version: ${{ matrix.poetry-version }}
23+
- name: Install dependencies
24+
run: poetry install
25+
- name: Run tests with coverage
26+
run: |
27+
poetry run coverage run
28+
poetry run coverage xml
29+
- name: Upload Coverage to Codecov
30+
uses: codecov/codecov-action@v3
31+
with:
32+
verbose: true
33+
files: coverage.xml
34+
flags: unittests
35+
name: codecov-umbrella
36+
fail_ci_if_error: true

README.md

Lines changed: 43 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,60 @@
11
# Sentry Dynamic Sampling Controller
22

3-
This project aims to provide dynamic sampling without relying on Sentry Dynamic Sampling.
3+
[![PyPI](https://img.shields.io/pypi/v/sentry-dynamic-sampling-lib?color=blue)](https://pypi.org/project/sentry-dynamic-sampling-lib/)
4+
![Tests Status](https://github.com/SpikeeLabs/sentry-dynamic-sampling-lib/actions/workflows/.github/workflows/test.yml/badge.svg)
5+
[![codecov](https://codecov.io/gh/SpikeeLabs/sentry-dynamic-sampling-lib/branch/main/graph/badge.svg?token=NK5V6YMWW0)](https://codecov.io/gh/SpikeeLabs/sentry-dynamic-sampling-lib)
6+
![PyPI - Python Version](https://img.shields.io/pypi/pyversions/sentry-dynamic-sampling-lib)
7+
![PyPI - License](https://img.shields.io/pypi/l/sentry-dynamic-sampling-lib)
48

9+
---
10+
This project aims to provide dynamic sampling without relying on Sentry own's Dynamic Sampling.
11+
This libs works by adding a `traces_sampler` callback to sentry.
12+
In the background a thread fetch the data from the [controller](https://github.com/SpikeeLabs/sentry-dynamic-sampling-controller)
13+
It's also able to ignore WSGI route an Celery task set in controller.
514

6-
It work by installing the library [sentry-dynamic-sampling-lib](https://github.com/SpikeeLabs/sentry-dynamic-sampling-lib) on each project that use sentry. This lib hooks into the sentry callback to change the sampling rate. to get the rate the lib calls this service.
715

816

917

1018

11-
## Install
12-
```bash
13-
# install deps
14-
poetry install
19+
## Usage
20+
```python
21+
import sentry_sdk
22+
from sentry_dynamic_sampling_lib import init_wrapper
1523

16-
# pre-commit
17-
poetry run pre-commit install --install-hook
18-
poetry run pre-commit install --install-hooks --hook-type commit-msg
24+
# init sentry as usual
25+
# without traces_sampler and sample_rate param
26+
sentry_sdk.init( # pylint: disable=E0110
27+
dsn=SENTRY_DSN,
28+
integrations=[],
29+
environment=ENVIRONMENT,
30+
release=SENTRY_RELEASE,
31+
)
32+
33+
# hook sentry_dynamic_sampling_lib into sentry
34+
init_wrapper()
1935
```
2036

2137

22-
## Run
38+
## Configuration
39+
The following environment variables can be used to configure the lib
40+
2341
```bash
24-
poetry shell
42+
SENTRY_CONTROLLER_HOST=none # (required, no default)
43+
SENTRY_CONTROLLER_PATH="/sentry/apps/{}/" # (optional, default to example)
44+
SENTRY_CONTROLLER_METRIC_PATH="/sentry/apps/{}/metrics/{}/" # (optional, default to example)
45+
SENTRY_CONTROLLER_POLL_INTERVAL=60 # (optional, default to example)
46+
SENTRY_CONTROLLER_METRIC_INTERVAL=600 # (optional, default to example)
47+
```
2548

26-
# add user
27-
python manage.py createsuperuser
2849

29-
# run server
30-
# admin @ http://localhost:8000/admin/
31-
python manage.py runserver
3250

51+
52+
## Development
53+
```bash
54+
# install deps
55+
poetry install
56+
57+
# pre-commit
58+
poetry run pre-commit install --install-hook
59+
poetry run pre-commit install --install-hooks --hook-type commit-msg
3360
```
Binary file not shown.
-4.45 KB
Binary file not shown.

poetry.lock

Lines changed: 67 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pyproject.toml

Lines changed: 79 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,28 @@ name = "sentry-dynamic-sampling-lib"
33
version = "0.1.0"
44
description = ""
55
authors = ["jeanloup.monnier <[email protected]>"]
6+
maintainers = ["jeanloup.monnier <[email protected]>"]
67
readme = "README.md"
7-
packages = [{include = "sentry_dynamic_sampling_lib"}]
8+
packages = [{ include = "sentry_dynamic_sampling_lib" }]
9+
license = "BSD 2"
10+
repository = "https://github.com/SpikeeLabs/django-admin-action-tools"
11+
12+
classifiers = [
13+
"Programming Language :: Python :: 3.8",
14+
"Programming Language :: Python :: 3.9",
15+
"Programming Language :: Python :: 3.10",
16+
"Programming Language :: Python :: 3.11",
17+
"Programming Language :: Python :: 3 :: Only",
18+
]
19+
820

921
[tool.poetry.dependencies]
10-
python = "^3.9"
22+
python = "^3.8"
1123
schedule = "^1.1.0"
1224
wrapt = "^1.14.1"
1325
psutil = "^5.9.4"
1426
requests-cache = "^0.9.7"
27+
pytest = "^7.2.0"
1528

1629

1730
[tool.poetry.group.dev.dependencies]
@@ -27,3 +40,67 @@ sentry-sdk = "^1.12.1"
2740
[build-system]
2841
requires = ["poetry-core"]
2942
build-backend = "poetry.core.masonry.api"
43+
44+
45+
[tool.black]
46+
exclude = '''
47+
(
48+
/(
49+
\.git
50+
|\.tox
51+
|migrations
52+
)/
53+
)
54+
'''
55+
include = '\.pyi?$'
56+
line-length = 119
57+
58+
[tool.pylama]
59+
format = "pycodestyle"
60+
linters = "pycodestyle,pyflakes,pylint"
61+
max_line_length = 119
62+
skip = ".pytest_cache,.venv/*,*/migrations/*,tests/*,*/tests/*,docs/*"
63+
64+
[tool.pylama.linter.pycodestyle]
65+
max_line_length = 119
66+
67+
[tool.pylama.linter.pylint]
68+
disable = "W0108,W0511,W0602,W0603,W0703,C0206,C0209,C0114,C0115,C0116,R0903,R0913,R0914,R0901,E1101,E1130,E1136,W0212"
69+
# Ignored rules:
70+
# - W0108: Lambda may not be necessary
71+
# - W0511: fixme, todo
72+
# - W0602: global-variable-not-assigned
73+
# - W0603: global-statement
74+
# - W0703: Catching too general exception
75+
# - C0114: missing-module-docstring
76+
# - C0115: missing-class-docstring
77+
# - C0116: missing-function-docstring
78+
# - C0206: consider-using-dict-items
79+
# - C0209: consider-using-f-string
80+
# - R0903: too-few-public-methods
81+
# - R0913: too-many-arguments
82+
# - R0914: too-many-locals
83+
# - R0901: max-parents: Maximum number of parents for a class
84+
# - E1101: generated-members
85+
# List of members which are set dynamically and missed by pylint inference system,
86+
# and so shouldn't trigger E1101 when accessed.
87+
# - E1130: invalid-unary-operand-type
88+
# Emitted when a unary operand is used on an object which does not support this type of operation.
89+
# - E1136: unsubscriptable-object
90+
# Value '%s' is unsubscriptable Emitted when a subscripted value doesn't support subscription
91+
# - W0212: protected-access: Access to a protected member of a client class
92+
93+
94+
[tool.coverage.run]
95+
command_line = "-m pytest"
96+
relative_files = true
97+
omit = ["tests/*"]
98+
source = ["sentry_dynamic_sampling_lib"]
99+
branch = true
100+
101+
[tool.coverage.report]
102+
fail_under = 100
103+
104+
[tool.pytest.ini_options]
105+
testpaths = "tests/"
106+
# addopts = "--doctest-modules -ra -l --tb=short --show-capture=all --color=yes"

0 commit comments

Comments
 (0)