-
-
Notifications
You must be signed in to change notification settings - Fork 84
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
TODO: - [ ] support globalLabels - [ ] check if int / float is right - [ ] test multiple client instances - [ ] document
- Loading branch information
1 parent
d963f41
commit a166954
Showing
20 changed files
with
363 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import pytest | ||
|
||
from prisma import Prisma | ||
from prisma._compat import model_json | ||
|
||
|
||
@pytest.mark.asyncio | ||
async def test_prometheus(client: Prisma) -> None: | ||
"""Metrics can be returned in Prometheus format""" | ||
await client.user.create(data={'name': 'Robert'}) | ||
|
||
metrics = await client.get_metrics(format='prometheus') | ||
|
||
assert 'prisma_client_queries_total' in metrics | ||
assert 'prisma_datasource_queries_total' in metrics | ||
assert 'prisma_client_queries_active' in metrics | ||
assert 'prisma_client_queries_duration_histogram_ms_bucket' in metrics | ||
|
||
|
||
@pytest.mark.asyncio | ||
async def test_json_string(client: Prisma) -> None: | ||
"""Metrics can be serlialized to JSON""" | ||
await client.user.create(data={'name': 'Robert'}) | ||
|
||
metrics = await client.get_metrics() | ||
assert isinstance(model_json(metrics), str) | ||
|
||
|
||
@pytest.mark.asyncio | ||
async def test_json(client: Prisma) -> None: | ||
"""Metrics returned in the JSON format""" | ||
await client.user.create(data={'name': 'Robert'}) | ||
|
||
metrics = await client.get_metrics(format='json') | ||
|
||
assert len(metrics.counters) > 0 | ||
assert metrics.counters[0].value > 0 | ||
|
||
assert len(metrics.gauges) > 0 | ||
gauge = next(filter(lambda g: g.key == 'prisma_pool_connections_open', metrics.gauges)) | ||
assert gauge.value > 0 | ||
|
||
assert len(metrics.histograms) > 0 | ||
assert metrics.histograms[0].value.sum > 0 | ||
assert metrics.histograms[0].value.count > 0 | ||
|
||
assert len(metrics.histograms[0].value.buckets) > 0 | ||
|
||
for bucket in metrics.histograms[0].value.buckets: | ||
assert bucket.max_value >= 0 | ||
assert bucket.total_count >= 0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
# copied from https://github.com/prisma/prisma/blob/23d5ef0672372035a84552b6b457197ca19f486d/packages/client/src/runtime/core/engines/common/types/Metrics.ts | ||
from __future__ import annotations | ||
|
||
from typing import Generic, List, TypeVar, Dict, NamedTuple | ||
|
||
from pydantic import BaseModel | ||
|
||
from ._compat import GenericModel, model_rebuild | ||
|
||
|
||
__all__ = ( | ||
'Metrics', | ||
'Metric', | ||
'MetricHistogram', | ||
) | ||
|
||
|
||
_T = TypeVar('_T') | ||
|
||
|
||
# TODO: check if int / float is right | ||
|
||
|
||
class Metrics(BaseModel): | ||
counters: List[Metric[int]] # TODO | ||
gauges: List[Metric[float]] # TODO | ||
histograms: List[Metric[MetricHistogram]] | ||
|
||
|
||
class Metric(GenericModel, Generic[_T]): | ||
key: str | ||
value: _T | ||
labels: Dict[str, str] | ||
description: str | ||
|
||
|
||
class MetricHistogram(BaseModel): | ||
sum: float # TODO | ||
count: int # TODO | ||
buckets: List[HistogramBucket] | ||
|
||
|
||
class HistogramBucket(NamedTuple): | ||
max_value: float # TODO | ||
total_count: int # TODO | ||
|
||
|
||
model_rebuild(Metric) | ||
model_rebuild(Metrics) | ||
model_rebuild(MetricHistogram) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.