Skip to content

Commit

Permalink
Disable metrics integration by default
Browse files Browse the repository at this point in the history
  • Loading branch information
artsmolin committed Sep 9, 2022
1 parent fcc80c7 commit 47ec776
Show file tree
Hide file tree
Showing 12 changed files with 2,795 additions and 235 deletions.
2 changes: 2 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ format: install-pre-commit clean

clients-for-tests:
python pythogen/entrypoint.py tests/docs/openapi.yaml tests/clients/async_client.py
python pythogen/entrypoint.py tests/docs/openapi.yaml tests/clients/async_client_with_metrics.py --metrics
python pythogen/entrypoint.py tests/docs/openapi.yaml tests/clients/sync_client.py --sync
python pythogen/entrypoint.py tests/docs/openapi.yaml tests/clients/sync_client_with_metrics.py --sync --metrics

clients-for-examples:
python pythogen/entrypoint.py examples/petstore/openapi.yaml examples/petstore/client_async.py
Expand Down
10 changes: 9 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ Generator of python HTTP-clients from OpenApi specification based on `httpx` and
- [Discriminator](/docs/discriminator.md)
- Sync and async clients
- Tracing
- Metrics
- [Metrics](/docs/metrics.md)

## Examples
- [**Petstore OpenAPI**](/examples/petstore/openapi.yaml): [client_sync.py](/examples/petstore/client_sync.py) | [client_async.py](/examples/petstore/client_async.py)
Expand All @@ -40,10 +40,18 @@ pip install pythogen
```shell
pythogen path/to/input/openapi.yaml path/to/output/client.py
```
- Asynchronous client with integration for metrics
```shell
pythogen path/to/input/openapi.yaml path/to/output/client.py --metrics
```
- Synchronous client
```shell
pythogen path/to/input/openapi.yaml path/to/output/client.py --sync
```
- Synchronous client with integration for metrics
```shell
pythogen path/to/input/openapi.yaml path/to/output/client.py --sync --metrics
```
### Generate client as python-package
```shell
pythogen path/to/input/openapi.yaml path/to/package/output --package-version=0.0.1 --package-authors="Rick, Morty"
Expand Down
47 changes: 47 additions & 0 deletions docs/metrics.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
[← README.md](/README.md)

# Metrics
Pythogen is capable of generating a base class to integrate metrics into the client. To do this, use the `--metrics` flag when generating the client. The `BaseMetricsIntegration` and `DefaultMetricsIntegration` classes will be generated.

## Usage
```python
from prometheus_client import Counter
from prometheus_client import Histogram

from petstore.client_async import Client
from petstore.client_async import Pet
from petstore.client_async import EmptyBody
from petstore.client_async import DefaultMetricsIntegration


client_response_time = Histogram(
'http_client_duration',
'http_client_duration requests to external services duration in seconds',
labelnames=(
'client_name',
'http_method',
'http_target',
'http_status_code',
),
)

client_non_http_errors = Counter(
'http_client_other_errors_total',
'http_client_other_errors_total total count of non http errors occurred',
labelnames=(
'client_name',
'http_method',
'http_target',
'exception',
),
)


client = Client(
base_url="http://your.base.url",
metrics_integration=DefaultMetricsIntegration(
client_response_time_histogram=client_response_time,
client_non_http_errors_counter=client_non_http_errors,
),
)
```
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "pythogen"
version = "0.0.28"
version = "0.0.29"
description = "Generator of python HTTP-clients from OpenApi specification."
homepage = "https://github.com/artsmolin/pythogen"
repository = "https://github.com/artsmolin/pythogen"
Expand Down
2 changes: 2 additions & 0 deletions pythogen/entrypoint.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ def main(
sync: bool = typer.Option(False, help="sync client"),
package_version: Optional[str] = typer.Option(None, help="package version"),
package_authors: Optional[str] = typer.Option(None, help="package authors"),
metrics: bool = typer.Option(False, help="include metrics integration"),
):
"""
Generate HTTP clients for python from OpenAPI
Expand All @@ -43,6 +44,7 @@ def main(
document=document,
name=name,
sync=sync,
metrics=metrics,
)


Expand Down
3 changes: 2 additions & 1 deletion pythogen/renderer.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
logger = logging.getLogger(__name__)


def render_client(*, output_path: str, document: models.Document, name, sync) -> None:
def render_client(*, output_path: str, document: models.Document, name: str, sync: bool, metrics: bool) -> None:
"""Отрисовывает сгенерированный клиент на основе j2-шаблонов
Arguments
Expand Down Expand Up @@ -61,6 +61,7 @@ def render_client(*, output_path: str, document: models.Document, name, sync) ->
put=prepared_operations.put,
delete_no_body=prepared_operations.delete_no_body,
sync=sync,
metrics=metrics,
discriminator_base_class_schemas=document.discriminator_base_class_schemas,
)
with open(output_path, 'w') as output_file:
Expand Down
4 changes: 4 additions & 0 deletions pythogen/templates/client/httpx-request-metrics.j2
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,18 @@
response = await self.client.{{ method }}(url, {%- if operation.request_body %} {%- if req_body.is_form_data or req_body.is_multipart_form_data %} data{%- else %} json{%- endif %}=json, {%- endif %} headers=headers_, params=params, auth=auth_{%- if operation.request_body and req_body.is_multipart_form_data %}, files=files{%- endif %})
{%- endif %}
except Exception as exc:
{%- if metrics %}
if self.metrics_integration:
self.metrics_integration.on_request_error(self.client_name, exc, "{{ method }}", "{{ path | replace('{', ':') | replace('}', '') }}")
{%- endif %}
raise exc
{#
https://www.python-httpx.org/api/#response
.elapsed - The amount of time elapsed between sending the request
and calling close() on the corresponding response received for that request.
total_seconds() to correctly get the total elapsed seconds.
#}
{%- if metrics %}
if self.metrics_integration:
self.metrics_integration.on_request_success(self.client_name, response, "{{ method }}", "{{ path | replace('{', ':') | replace('}', '') }}")
{%- endif %}
12 changes: 8 additions & 4 deletions pythogen/templates/httpx.j2
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,10 @@ from typing import cast
from jaeger_client import Tracer
from jaeger_client.span import Span
from opentracing.propagation import Format

{%- if metrics %}
from prometheus_client import Counter
from prometheus_client import Histogram

{%- endif %}
import httpx
from pydantic import BaseModel
from pydantic import Field
Expand Down Expand Up @@ -157,7 +157,7 @@ try:
except AttributeError:
DEFAULT_AUTH = None


{%- if metrics %}
class BaseMetricsIntegration(abc.ABC):
def __init__(
self,
Expand Down Expand Up @@ -191,7 +191,7 @@ class DefaultMetricsIntegration(BaseMetricsIntegration):
http_target=http_target,
http_status_code=response.status_code,
).observe(response.elapsed.total_seconds())

{%- endif %}

FileContent = Union[IO[str], IO[bytes], str, bytes]
FileTypes = Union[
Expand Down Expand Up @@ -301,7 +301,9 @@ class {{ name }}:
{%- endif %}
headers: Optional[Dict[str, str]] = None,
tracer_integration: Optional[BaseTracerIntegration] = None,
{%- if metrics %}
metrics_integration: Optional[BaseMetricsIntegration] = None,
{%- endif %}
):
{%- if sync %}
self.client = client or httpx.Client(timeout=Timeout(timeout))
Expand All @@ -311,7 +313,9 @@ class {{ name }}:
self.base_url = base_url
self.headers = headers or {}
self.tracer_integration = tracer_integration
{%- if metrics %}
self.metrics_integration=metrics_integration
{%- endif %}
self.client_name = client_name

{#- get items begin #}
Expand Down
Loading

0 comments on commit 47ec776

Please sign in to comment.