Buckets:

download
raw
13.7 kB
from __future__ import annotations
import inspect
from datetime import datetime, timezone
from typing import Any, cast
import pytest
from inline_snapshot import snapshot
from logfire._internal.config import LogfireConfigWarning
from logfire.query_client import AsyncLogfireQueryClient, LogfireQueryClient
# This file is intended to be updated by the Logfire developers, with the development platform running locally.
# To update, set the `CLIENT_BASE_URL` and `CLIENT_READ_TOKEN` values to match the local development environment,
# and run the tests with `--record-mode=rewrite --inline-snapshot=fix` to update the cassettes and snapshots.
#
# The snapshots expect 37 non-exception logs and 1 exception. The two rows surfaced by the
# `ORDER BY is_exception, message LIMIT 2` query are `about to raise an error` (no tags) and
# `aha 0` (tags `['tag1', 'tag2']`). To populate the local instance with matching data:
#
# ```python
# import logfire
#
# logfire.configure()
#
# logfire.info('about to raise an error')
#
# try:
# raise ValueError('oops')
# except Exception:
# logfire.exception('oh no')
#
# for i in range(36):
# logfire.with_tags('tag1', 'tag2').info('aha {n}', n=i)
# ```
CLIENT_BASE_URL = 'http://localhost:3000'
CLIENT_READ_TOKEN = 'pylf_v1_local_ZQHXp1vFjkR0dWxyQ8jCB4DPDlpd4752XWjpcNtdsPB6'
# Disable compression so cassettes record human-readable response bodies:
CLIENT_KWARGS: dict[str, Any] = {'headers': {'accept-encoding': 'identity'}}
MIN_TIMESTAMP = datetime(2020, 1, 1, tzinfo=timezone.utc)
pytestmark = pytest.mark.vcr()
@pytest.mark.parametrize('client_class', [AsyncLogfireQueryClient, LogfireQueryClient])
@pytest.mark.parametrize(
['token', 'expected'],
[
('pylf_v1_us_0kYhc414Ys2FNDRdt5vFB05xFx5NjVcbcBMy4Kp6PH0W', 'https://logfire-us.pydantic.dev'),
('pylf_v1_eu_0kYhc414Ys2FNDRdt5vFB05xFx5NjVcbcBMy4Kp6PH0W', 'https://logfire-eu.pydantic.dev'),
('0kYhc414Ys2FNDRdt5vFB05xFx5NjVcbcBMy4Kp6PH0W', 'https://logfire-us.pydantic.dev'),
],
)
def test_infers_base_url_from_token(
client_class: type[AsyncLogfireQueryClient | LogfireQueryClient], token: str, expected: str
):
client = client_class(read_token=token)
assert client.base_url == expected
@pytest.mark.parametrize('client_class', [AsyncLogfireQueryClient, LogfireQueryClient])
def test_infers_base_url_from_unknown_token_region(
client_class: type[AsyncLogfireQueryClient | LogfireQueryClient],
):
with pytest.warns(LogfireConfigWarning, match="Unknown region 'unknownregion'"):
client = client_class(read_token='pylf_v1_unknownregion_123456')
assert client.base_url == 'https://logfire-us.pydantic.dev'
def test_info_sync():
with LogfireQueryClient(read_token=CLIENT_READ_TOKEN, base_url=CLIENT_BASE_URL, **CLIENT_KWARGS) as client:
info = client.info()
assert info == snapshot(
{
'organization_name': 'viicos',
'project_name': 'logfire-sdk-cassettes',
}
)
@pytest.mark.anyio
async def test_info_async():
async with AsyncLogfireQueryClient(
read_token=CLIENT_READ_TOKEN, base_url=CLIENT_BASE_URL, **CLIENT_KWARGS
) as client:
info = await client.info()
assert info == snapshot(
{
'organization_name': 'viicos',
'project_name': 'logfire-sdk-cassettes',
}
)
def test_query_json_rows_read_sync():
with LogfireQueryClient(read_token=CLIENT_READ_TOKEN, base_url=CLIENT_BASE_URL, **CLIENT_KWARGS) as client:
sql = """
SELECT kind, message, is_exception, tags
FROM records
ORDER BY is_exception, message
LIMIT 2
"""
rows_result = client.query_json_rows(sql, min_timestamp=MIN_TIMESTAMP)
assert rows_result == snapshot(
{
'columns': [
{'name': 'kind', 'nullable': False, 'datatype': 'Utf8'},
{'name': 'message', 'nullable': False, 'datatype': 'Utf8'},
{'name': 'is_exception', 'nullable': True, 'datatype': 'Boolean'},
{
'name': 'tags',
'nullable': True,
'datatype': {'List': {'name': 'item', 'nullable': True, 'datatype': 'Utf8'}},
},
],
'rows': [
{
'kind': 'log',
'message': 'about to raise an error',
'is_exception': False,
'tags': [],
},
{
'kind': 'log',
'message': 'aha 0',
'is_exception': False,
'tags': ['tag1', 'tag2'],
},
],
}
)
def test_query_csv_read_sync():
with LogfireQueryClient(read_token=CLIENT_READ_TOKEN, base_url=CLIENT_BASE_URL, **CLIENT_KWARGS) as client:
sql = """
SELECT kind, message, is_exception, tags
FROM records
ORDER BY is_exception, message
LIMIT 2
"""
csv_result = client.query_csv(sql, min_timestamp=MIN_TIMESTAMP)
assert csv_result == snapshot("""\
kind,message,is_exception,tags
log,about to raise an error,false,[]
log,aha 0,false,"[""tag1"",""tag2""]"
""")
def test_query_arrow_read_sync():
with LogfireQueryClient(read_token=CLIENT_READ_TOKEN, base_url=CLIENT_BASE_URL, **CLIENT_KWARGS) as client:
sql = """
SELECT kind, message, is_exception, tags
FROM records
ORDER BY is_exception, message
LIMIT 2
"""
arrow_result = client.query_arrow(sql, min_timestamp=MIN_TIMESTAMP)
assert arrow_result.to_pylist() == snapshot(
[
{
'kind': 'log',
'message': 'about to raise an error',
'is_exception': False,
'tags': [],
},
{
'kind': 'log',
'message': 'aha 0',
'is_exception': False,
'tags': ['tag1', 'tag2'],
},
]
)
@pytest.mark.anyio
async def test_query_json_rows_read_async():
async with AsyncLogfireQueryClient(
read_token=CLIENT_READ_TOKEN, base_url=CLIENT_BASE_URL, **CLIENT_KWARGS
) as client:
sql = """
SELECT kind, message, is_exception, tags
FROM records
ORDER BY is_exception, message
LIMIT 2
"""
rows_result = await client.query_json_rows(sql, min_timestamp=MIN_TIMESTAMP)
assert rows_result == snapshot(
{
'columns': [
{'name': 'kind', 'nullable': False, 'datatype': 'Utf8'},
{'name': 'message', 'nullable': False, 'datatype': 'Utf8'},
{'name': 'is_exception', 'nullable': True, 'datatype': 'Boolean'},
{
'name': 'tags',
'nullable': True,
'datatype': {'List': {'name': 'item', 'nullable': True, 'datatype': 'Utf8'}},
},
],
'rows': [
{
'kind': 'log',
'message': 'about to raise an error',
'is_exception': False,
'tags': [],
},
{
'kind': 'log',
'message': 'aha 0',
'is_exception': False,
'tags': ['tag1', 'tag2'],
},
],
}
)
@pytest.mark.anyio
async def test_query_csv_read_async():
async with AsyncLogfireQueryClient(
read_token=CLIENT_READ_TOKEN, base_url=CLIENT_BASE_URL, **CLIENT_KWARGS
) as client:
sql = """
SELECT kind, message, is_exception, tags
FROM records
ORDER BY is_exception, message
LIMIT 2
"""
csv_result = await client.query_csv(sql, min_timestamp=MIN_TIMESTAMP)
assert csv_result == snapshot("""\
kind,message,is_exception,tags
log,about to raise an error,false,[]
log,aha 0,false,"[""tag1"",""tag2""]"
""")
@pytest.mark.anyio
async def test_query_arrow_read_async():
async with AsyncLogfireQueryClient(
read_token=CLIENT_READ_TOKEN, base_url=CLIENT_BASE_URL, **CLIENT_KWARGS
) as client:
sql = """
SELECT kind, message, is_exception, tags
FROM records
ORDER BY is_exception, message
LIMIT 2
"""
arrow_result = await client.query_arrow(sql, min_timestamp=MIN_TIMESTAMP)
assert arrow_result.to_pylist() == snapshot(
[
{
'kind': 'log',
'message': 'about to raise an error',
'is_exception': False,
'tags': [],
},
{
'kind': 'log',
'message': 'aha 0',
'is_exception': False,
'tags': ['tag1', 'tag2'],
},
]
)
def test_query_params_sync():
with LogfireQueryClient(read_token=CLIENT_READ_TOKEN, base_url=CLIENT_BASE_URL, **CLIENT_KWARGS) as client:
sql = """
SELECT is_exception, count(*)
FROM records
GROUP BY is_exception
ORDER BY is_exception
"""
assert client.query_csv(sql, min_timestamp=MIN_TIMESTAMP) == snapshot("""\
is_exception,count(*)
false,37
true,1
""")
assert client.query_csv(sql, min_timestamp=datetime(2030, 1, 1, tzinfo=timezone.utc)) == snapshot("""\
is_exception,count(*)
""")
assert client.query_csv(sql, min_timestamp=MIN_TIMESTAMP, max_timestamp=MIN_TIMESTAMP) == snapshot("""\
is_exception,count(*)
""")
assert client.query_csv(sql, min_timestamp=MIN_TIMESTAMP, limit=1) == snapshot("""\
is_exception,count(*)
false,37
""")
@pytest.mark.anyio
async def test_query_params_async():
async with AsyncLogfireQueryClient(
read_token=CLIENT_READ_TOKEN, base_url=CLIENT_BASE_URL, **CLIENT_KWARGS
) as client:
sql = """
SELECT is_exception, count(*)
FROM records
GROUP BY is_exception
ORDER BY is_exception
"""
assert await client.query_csv(sql, min_timestamp=MIN_TIMESTAMP) == snapshot("""\
is_exception,count(*)
false,37
true,1
""")
assert await client.query_csv(sql, min_timestamp=datetime(2030, 1, 1, tzinfo=timezone.utc)) == snapshot("""\
is_exception,count(*)
""")
assert await client.query_csv(sql, min_timestamp=MIN_TIMESTAMP, max_timestamp=MIN_TIMESTAMP) == snapshot("""\
is_exception,count(*)
""")
assert await client.query_csv(sql, min_timestamp=MIN_TIMESTAMP, limit=1) == snapshot("""\
is_exception,count(*)
false,37
""")
def test_query_body_params_sync():
"""Exercise every optional body parameter (naive timestamps, timezone, environment) on `/v2/query`."""
sql = 'SELECT count(*) AS n FROM records'
with LogfireQueryClient(read_token=CLIENT_READ_TOKEN, base_url=CLIENT_BASE_URL, **CLIENT_KWARGS) as client:
result = client.query_json_rows(
sql,
min_timestamp=datetime(2020, 1, 1),
max_timestamp=datetime(2099, 1, 1),
timezone='UTC',
environment='production',
)
assert result == snapshot(
{'columns': [{'name': 'n', 'datatype': 'Int64', 'nullable': False}], 'rows': [{'n': 0}]}
)
def test_query_methods_share_docstring():
# All query methods (on both clients) document the same arguments, so the `Args:` section of
# their docstrings should be identical. Use the first method as the reference.
query_methods = [
getattr(client_class, name)
for client_class in (LogfireQueryClient, AsyncLogfireQueryClient)
for name in ('query_arrow', 'query_csv', 'query_json_rows')
]
reference, *others = query_methods
reference_doc = inspect.cleandoc(reference.__doc__)
doc = reference_doc[reference_doc.index('Args:') :]
for method in others:
assert doc in inspect.cleandoc(method.__doc__)
@pytest.mark.parametrize('client_class', [AsyncLogfireQueryClient, LogfireQueryClient])
def test_query_method_signatures(client_class: type[AsyncLogfireQueryClient | LogfireQueryClient]):
assert not hasattr(client_class, 'query_json')
for method_name in ('query_arrow', 'query_csv', 'query_json_rows'):
parameters = inspect.signature(getattr(client_class, method_name)).parameters
assert parameters['sql'].kind is inspect.Parameter.POSITIONAL_OR_KEYWORD
assert parameters['min_timestamp'].default is inspect.Parameter.empty
assert all(
parameters[name].kind is inspect.Parameter.KEYWORD_ONLY
for name in ('min_timestamp', 'max_timestamp', 'limit', 'timezone', 'environment')
)
def test_query_json_rows_methods_share_returns_docstring():
# The `query_json_rows()` methods on both clients document the same return value, so the
# `Returns:` section of their docstrings should be identical. Use the sync method as the reference.
reference_doc = inspect.cleandoc(cast(str, LogfireQueryClient.query_json_rows.__doc__))
returns = reference_doc[reference_doc.index('Returns:') :]
assert returns in inspect.cleandoc(cast(str, AsyncLogfireQueryClient.query_json_rows.__doc__))

Xet Storage Details

Size:
13.7 kB
·
Xet hash:
618cfe30733c521b00142414677940cba8cdd09807b7cc908239e707aeb77918

Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.