https://github.com/uswitch/service-standard-python
The RVU Service Standard Library for Python services.
https://github.com/uswitch/service-standard-python
Last synced: over 1 year ago
JSON representation
The RVU Service Standard Library for Python services.
- Host: GitHub
- URL: https://github.com/uswitch/service-standard-python
- Owner: uswitch
- License: apache-2.0
- Created: 2020-10-21T09:38:15.000Z (over 5 years ago)
- Default Branch: main
- Last Pushed: 2021-06-04T14:36:39.000Z (about 5 years ago)
- Last Synced: 2025-02-08T04:29:13.206Z (over 1 year ago)
- Language: Python
- Homepage:
- Size: 24.4 KB
- Stars: 0
- Watchers: 11
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# service-standard-python
The Service Standard Library for Python services.
This library covers implementations for various areas documented in the [Service Standard](https://github.com/uswitch/service-standard-docs)
## Setup
Any build environment will need access to this repo.
### Via pipenv
```
pipenv install -e "git+https://github.com/uswitch/service-standard-python.git@v0.3#egg=service-standard-python"
```
Your Pipfile should have a line like:
```
service-standard-python = {editable = true, git = "https://github.com/uswitch/service-standard-python.git", ref = "v0.3"}
```
### Via pip
```
pip install -e "git+https://github.com/uswitch/service-standard-python@v0.3#egg=service-standard-python"
```
Your requirements.txt should have a line specifying a link to the git repo.
## Usage
### HTTP requests & duration
To track Flask HTTP requests, use the flask wrapper for automatic timing and labelling:
```python
from service_standard_python.flask import service_standard_flask_wrapper
...
@app.route('/', methods=['GET'])
@service_standard_flask_wrapper
def myHandlerFunc():
```
### Errors
To count errors, use the exposed `ERRORS_TOTAL` counter:
```python
from service_standard_python.metrics import ERRORS_TOTAL
...
def myErrorHandler(e):
ERRORS_TOTAL.labels(type=type(e).__name__).inc()
handleError(e)
```
### Exposing the endpoint
For simple apps, `add_prometheus()` easily adds a `/metrics` endpoint:
```
from service_standard_python.flask import add_prometheus
app = Flask(__name__)
# Add prometheus wsgi middleware to route /metrics requests
add_prometheus(app)
```
Alternatively, if your application already has middlewares, you can instead use the [prometheus_client](https://pypi.org/project/prometheus-client/) package directly to create an endpoint with `make_wsgi_app()` like so:
```
app.wsgi_app = DispatcherMiddleware(app.wsgi_app, {
'/other-endpoints': other_wsgi_app()
'/metrics': make_wsgi_app()
})
```