|
1 | 1 | from __future__ import annotations |
2 | 2 |
|
3 | 3 | import importlib |
| 4 | +import os |
4 | 5 | from contextlib import contextmanager |
5 | 6 | from typing import Any |
6 | 7 | from unittest import mock |
@@ -842,6 +843,74 @@ async def test_httpx_client_capture_all(exporter: TestExporter): |
842 | 843 | ) |
843 | 844 |
|
844 | 845 |
|
| 846 | +async def test_httpx_client_capture_all_environment_variable(exporter: TestExporter): |
| 847 | + with mock.patch.dict(os.environ, {'LOGFIRE_HTTPX_CAPTURE_ALL': 'true'}): |
| 848 | + with httpx.Client(transport=create_transport()) as client: |
| 849 | + logfire.instrument_httpx(client) |
| 850 | + response = client.get('https://example.org:8080/foo') |
| 851 | + assert response.json() == {'good': 'response'} |
| 852 | + assert await response.aread() == b'{"good": "response"}' |
| 853 | + |
| 854 | + assert without_metrics(exporter.exported_spans_as_dict()) == snapshot( |
| 855 | + [ |
| 856 | + { |
| 857 | + 'name': 'GET', |
| 858 | + 'context': {'trace_id': 1, 'span_id': 1, 'is_remote': False}, |
| 859 | + 'parent': None, |
| 860 | + 'start_time': 1000000000, |
| 861 | + 'end_time': 2000000000, |
| 862 | + 'attributes': { |
| 863 | + 'http.method': 'GET', |
| 864 | + 'http.request.method': 'GET', |
| 865 | + 'http.url': 'https://example.org:8080/foo', |
| 866 | + 'url.full': 'https://example.org:8080/foo', |
| 867 | + 'http.host': 'example.org', |
| 868 | + 'server.address': 'example.org', |
| 869 | + 'network.peer.address': 'example.org', |
| 870 | + 'net.peer.port': 8080, |
| 871 | + 'server.port': 8080, |
| 872 | + 'network.peer.port': 8080, |
| 873 | + 'logfire.span_type': 'span', |
| 874 | + 'logfire.msg': 'GET example.org/foo', |
| 875 | + 'http.request.header.host': ('example.org:8080',), |
| 876 | + 'http.request.header.accept': ('*/*',), |
| 877 | + 'http.request.header.accept-encoding': ('gzip, deflate, zstd',), |
| 878 | + 'http.request.header.connection': ('keep-alive',), |
| 879 | + 'http.request.header.user-agent': ('python-httpx/0.28.1',), |
| 880 | + 'http.status_code': 200, |
| 881 | + 'http.response.status_code': 200, |
| 882 | + 'http.flavor': '1.1', |
| 883 | + 'network.protocol.version': '1.1', |
| 884 | + 'http.response.header.host': ('example.org:8080',), |
| 885 | + 'http.response.header.accept': ('*/*',), |
| 886 | + 'http.response.header.accept-encoding': ('gzip, deflate, zstd',), |
| 887 | + 'http.response.header.connection': ('keep-alive',), |
| 888 | + 'http.response.header.user-agent': ('python-httpx/0.28.1',), |
| 889 | + 'http.response.header.traceparent': ('00-00000000000000000000000000000001-0000000000000001-01',), |
| 890 | + 'http.target': '/foo', |
| 891 | + }, |
| 892 | + }, |
| 893 | + { |
| 894 | + 'name': 'Reading response body', |
| 895 | + 'context': {'trace_id': 1, 'span_id': 3, 'is_remote': False}, |
| 896 | + 'parent': {'trace_id': 1, 'span_id': 1, 'is_remote': False}, |
| 897 | + 'start_time': 3000000000, |
| 898 | + 'end_time': 4000000000, |
| 899 | + 'attributes': { |
| 900 | + 'code.filepath': 'test_httpx.py', |
| 901 | + 'code.function': 'test_httpx_client_capture_all_environment_variable', |
| 902 | + 'code.lineno': 123, |
| 903 | + 'logfire.msg_template': 'Reading response body', |
| 904 | + 'logfire.msg': 'Reading response body', |
| 905 | + 'logfire.span_type': 'span', |
| 906 | + 'http.response.body.text': '{"good": "response"}', |
| 907 | + 'logfire.json_schema': '{"type":"object","properties":{"http.response.body.text":{"type":"object"}}}', |
| 908 | + }, |
| 909 | + }, |
| 910 | + ] |
| 911 | + ) |
| 912 | + |
| 913 | + |
845 | 914 | async def test_httpx_client_no_capture_empty_body(exporter: TestExporter): |
846 | 915 | async with httpx.AsyncClient(transport=create_transport()) as client: |
847 | 916 | logfire.instrument_httpx(client, capture_request_body=True) |
|
0 commit comments