From d47a9ae36fa74ad43cbf49dc6a961a3cf7c00358 Mon Sep 17 00:00:00 2001 From: David Brownman Date: Mon, 9 Mar 2026 18:08:57 -0700 Subject: [PATCH] tweak telemetry collection --- stripe/_api_requestor.py | 13 +++++++------ tests/test_api_requestor.py | 21 +++++++++++++++++++++ 2 files changed, 28 insertions(+), 6 deletions(-) diff --git a/stripe/_api_requestor.py b/stripe/_api_requestor.py index da73bbf3f..bb2ee24a6 100644 --- a/stripe/_api_requestor.py +++ b/stripe/_api_requestor.py @@ -5,6 +5,7 @@ from typing import ( Any, AsyncIterable, + Callable, Dict, List, Mapping, @@ -509,14 +510,14 @@ def request_headers( ua: Dict[str, Union[str, "AppInfo"]] = { "bindings_version": VERSION, "lang": "python", - "publisher": "stripe", "httplib": self._get_http_client().name, } - for attr, func in [ - ["lang_version", platform.python_version], - ["platform", platform.platform], - ["uname", lambda: " ".join(platform.uname())], - ]: + attr_funcs: List[Tuple[str, Callable[[], str]]] = [ + ("lang_version", platform.python_version), + ] + if stripe.enable_telemetry: + attr_funcs.append(("platform", platform.platform)) + for attr, func in attr_funcs: try: val = func() except Exception: diff --git a/tests/test_api_requestor.py b/tests/test_api_requestor.py index 235503782..bfd59926b 100644 --- a/tests/test_api_requestor.py +++ b/tests/test_api_requestor.py @@ -756,6 +756,7 @@ def fail(): mocker.patch("platform.platform", side_effect=fail) + stripe.enable_telemetry = True requestor.request("get", self.v1_path, {}, {}, base_address="api") last_call = http_client_mock.get_last_call() @@ -767,6 +768,26 @@ def fail(): == "(disabled)" ) + def test_platform_only_used_with_telemetry( + self, requestor, mocker, http_client_mock + ): + http_client_mock.stub_request( + "get", path=self.v1_path, rbody="{}", rcode=200 + ) + + def fail(): + raise RuntimeError + + mocker.patch("platform.platform", side_effect=fail) + + requestor.request("get", self.v1_path, {}, {}, base_address="api") + + last_call = http_client_mock.get_last_call() + last_call.assert_method("get") + assert "platform" not in json.loads( + last_call.get_raw_header("X-Stripe-Client-User-Agent") + ) + def test_uses_given_idempotency_key(self, requestor, http_client_mock): method = "post" http_client_mock.stub_request(