Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

update lib to v3 #13

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
515 changes: 322 additions & 193 deletions poetry.lock

Large diffs are not rendered by default.

15 changes: 7 additions & 8 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,25 +14,24 @@ classifiers = [
"Intended Audience :: Developers",
"Development Status :: 3 - Alpha",
"License :: OSI Approved :: MIT License",

"Operating System :: OS Independent",
"Environment :: Web Environment",
"Topic :: System :: Monitoring",
"Topic :: Internet :: WWW/HTTP :: Dynamic Content",
"Topic :: Software Development :: Libraries :: Python Modules",

"Programming Language :: Python :: 3.6",
"Programming Language :: Python :: 3.7",
"Programming Language :: Python :: 3.8",
"Programming Language :: Python :: 3.9",
]

[tool.dephell.main]
from = {format = "poetry", path = "pyproject.toml"}
to = {format = "setuppy", path = "setup.py"}
from = { format = "poetry", path = "pyproject.toml" }
to = { format = "setuppy", path = "setup.py" }

[tool.poetry.dependencies]
python = "^3.6 || ^3.7"
sanic = ">=0.7.0"
prometheus-client = "^0.7.1"
python = ">=3.7,<4.0"
sanic = ">=21.6.2"
prometheus-client = "^0.11.0"

[tool.poetry.dev-dependencies]
flake8 = "^3.3.0"
Expand Down
2 changes: 2 additions & 0 deletions v3/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
from .main import Monitor
from .structs import MetricsStruct, Mode
6 changes: 6 additions & 0 deletions v3/exceptions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
class PromSanicException(Exception):
pass


class PromSanicInit(PromSanicException):
pass
50 changes: 50 additions & 0 deletions v3/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
from typing import List, Optional, Dict

from prometheus_client import CollectorRegistry, multiprocess, CONTENT_TYPE_LATEST
from prometheus_client.exposition import generate_latest
from prometheus_client.metrics import MetricWrapperBase
from sanic import Sanic
from sanic.response import raw

from .structs import Mode, MetricsStruct


class Monitor:
__registry = CollectorRegistry(auto_describe=True)

_metrics: Dict[str, MetricWrapperBase]

def __init__(self, app: Sanic, uri: Optional[str] = None, mode: Mode = None):
self.app: Sanic = app
self.uri: str = uri or '/metrics'

self.mode: Mode = mode or Mode.SINGLE
self._metrics = {}

self.sync_path = '.'

self.app.metrics = self
self.app.add_route(self._expose_metrics, self.uri, methods=('GET',))

def add_metric(self, metric: MetricsStruct):
self.__registry.register(metric.metric)
self._metrics[metric.key] = metric.metric

def add_metrics(self, metrics: List[MetricsStruct]):
for m in metrics:
self._metrics[m.key] = m.metric

async def _expose_metrics(self, _request):
return raw(self._get_metrics_data(),
content_type=CONTENT_TYPE_LATEST)

def _get_metrics_data(self):
if self.mode == Mode.SINGLE:
registry = self.__registry
elif self.mode.MULTIPROCESSING:
registry = CollectorRegistry()
multiprocess.MultiProcessCollector(registry, path=self.sync_path)
else:
raise
data = generate_latest(registry)
return data
19 changes: 19 additions & 0 deletions v3/structs.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
from dataclasses import dataclass
from enum import IntEnum
from typing import Type

from prometheus_client.metrics import MetricWrapperBase


@dataclass
class MetricsStruct:
key: str
metric: MetricWrapperBase


class Mode(IntEnum):
# use prometheus_client.registry.CollectorRegistry
# create tmp file in sync metrics in all workers
MULTIPROCESSING = 1
# use in one WORKER
SINGLE = 2
34 changes: 34 additions & 0 deletions v3_examples/base_app.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
from prometheus_client import Counter
from sanic import Sanic
from sanic.response import json

from v3 import Monitor, MetricsStruct

app = Sanic(name='tmp')


async def ping(_request):
return json({'success': 'you are home'})


async def hone(_request):
return json({'success': 'you are home'})


async def user(_request):
return json({'success': 'you are home'})


if __name__ == "__main__":
m = Monitor(app=app)
m.add_metric(MetricsStruct(
key='list',
metric=Counter(
name='list',
documentation='',
labelnames=['label_1', 'label_2'],
)
))
m._metrics['list'].labels('123', '123123').inc(1)

app.run(host="127.0.0.1", port=8000, workers=1)
27 changes: 27 additions & 0 deletions v3_examples/many_workers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
from sanic import Sanic
from sanic.response import json

from v3 import Monitor, Mode, MetricsStruct

app = Sanic(name='tmp')


async def ping(_request):
return json({'success': 'you are home'})


async def hone(_request):
return json({'success': 'you are home'})


async def user(_request):
return json({'success': 'you are home'})


if __name__ == "__main__":
Monitor(
app=app,
mode=Mode.MULTIPROCESSING,
)

app.run(host="127.0.0.1", port=8000, workers=2)