|
1 | 1 | from __future__ import annotations |
2 | 2 |
|
3 | 3 | import json |
4 | | -import warnings |
5 | 4 | from typing import Any |
6 | 5 | from unittest.mock import patch |
7 | 6 |
|
8 | | -import pytest |
9 | 7 | import requests |
10 | 8 | import requests_mock |
11 | | -from inline_snapshot import snapshot |
12 | 9 |
|
13 | 10 | import logfire |
14 | 11 | from logfire._internal.config import GLOBAL_CONFIG, LogfireCredentials |
15 | | -from logfire._internal.telemetry_header import ( |
16 | | - ERROR_HEADER_NAME, |
17 | | - TELEMETRY_HEADER_NAME, |
18 | | - WARNING_HEADER_NAME, |
19 | | - build_telemetry_header, |
20 | | - process_logfire_response_headers, |
21 | | -) |
22 | | -from logfire.exceptions import LogfireServerError, LogfireServerWarning |
| 12 | +from logfire._internal.telemetry_header import TELEMETRY_HEADER_NAME, build_telemetry_header |
23 | 13 | from logfire.version import VERSION |
24 | 14 |
|
25 | 15 |
|
@@ -112,71 +102,3 @@ def test_from_token_sends_telemetry_header(): |
112 | 102 | ) |
113 | 103 | [history] = m.request_history |
114 | 104 | assert history.headers[TELEMETRY_HEADER_NAME] == '{"sdk_version":"1.2.3"}' |
115 | | - |
116 | | - |
117 | | -def test_process_response_warning_header_emits_warning(): |
118 | | - response = requests.Response() |
119 | | - response.headers[WARNING_HEADER_NAME] = 'The /foo/bar endpoint is deprecated, please use /bar/baz' |
120 | | - with warnings.catch_warnings(record=True) as caught: |
121 | | - warnings.simplefilter('always') |
122 | | - process_logfire_response_headers(response) |
123 | | - assert [(w.category, str(w.message)) for w in caught] == snapshot( |
124 | | - [(LogfireServerWarning, 'The /foo/bar endpoint is deprecated, please use /bar/baz')] |
125 | | - ) |
126 | | - |
127 | | - |
128 | | -def test_process_response_warning_header_dedupes(): |
129 | | - """Python's default `warnings` filter should fold repeats of the same message into one entry.""" |
130 | | - response = requests.Response() |
131 | | - response.headers[WARNING_HEADER_NAME] = 'a duplicated warning' |
132 | | - with warnings.catch_warnings(record=True) as caught: |
133 | | - warnings.simplefilter('default') |
134 | | - for _ in range(5): |
135 | | - process_logfire_response_headers(response) |
136 | | - messages = [str(w.message) for w in caught] |
137 | | - assert messages == ['a duplicated warning'] |
138 | | - |
139 | | - |
140 | | -def test_process_response_error_header_raises(): |
141 | | - response = requests.Response() |
142 | | - response.headers[ERROR_HEADER_NAME] = 'something is wrong' |
143 | | - with pytest.raises(LogfireServerError, match='something is wrong'): |
144 | | - process_logfire_response_headers(response) |
145 | | - |
146 | | - |
147 | | -def test_response_hook_installed_on_logfire_client(): |
148 | | - from logfire._internal.auth import UserToken |
149 | | - from logfire._internal.client import LogfireClient |
150 | | - |
151 | | - token = UserToken( |
152 | | - token='pylf_v1_us_xxx', |
153 | | - base_url='https://logfire-us.pydantic.dev', |
154 | | - expiration='2099-12-31T23:59:59', |
155 | | - ) |
156 | | - client = LogfireClient(user_token=token) |
157 | | - |
158 | | - with requests_mock.Mocker() as m: |
159 | | - m.get( |
160 | | - 'https://logfire-us.pydantic.dev/v1/account/me', |
161 | | - json={'name': 'me'}, |
162 | | - headers={WARNING_HEADER_NAME: 'deprecated endpoint'}, |
163 | | - ) |
164 | | - with warnings.catch_warnings(record=True) as caught: |
165 | | - warnings.simplefilter('always') |
166 | | - client.get_user_information() |
167 | | - |
168 | | - assert any(isinstance(w.message, LogfireServerWarning) for w in caught) |
169 | | - |
170 | | - with requests_mock.Mocker() as m: |
171 | | - m.get( |
172 | | - 'https://logfire-us.pydantic.dev/v1/account/me', |
173 | | - json={'name': 'me'}, |
174 | | - headers={ERROR_HEADER_NAME: 'no longer supported'}, |
175 | | - ) |
176 | | - with pytest.raises(LogfireServerError, match='no longer supported'): |
177 | | - client.get_user_information() |
178 | | - |
179 | | - [history, *_] = m.request_history |
180 | | - assert TELEMETRY_HEADER_NAME in history.headers |
181 | | - pairs = _parse_header(history.headers[TELEMETRY_HEADER_NAME]) |
182 | | - assert pairs['sdk_version'] == VERSION |
0 commit comments