Skip to content

Commit 46fb4ac

Browse files
authored
Allow proxy url in telemetry config to be optional to allow for local testing (#639)
1 parent 462fc45 commit 46fb4ac

File tree

2 files changed

+85
-13
lines changed

2 files changed

+85
-13
lines changed

payu/telemetry.py

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,13 @@
2626
# Required telemetry configuration fields
2727
CONFIG_FIELDS = {
2828
"URL": "telemetry_url",
29-
"PROXY_URL": "telemetry_proxy_url",
3029
"TOKEN": "telemetry_token",
3130
"SERVICE_NAME": "telemetry_service_name",
3231
"HOSTNAME": "hostname",
3332
}
33+
OPTIONAL_CONFIG_FIELDS = {
34+
"PROXY_URL": "telemetry_proxy_url",
35+
}
3436

3537
REQUEST_TIMEOUT = 10
3638

@@ -185,12 +187,12 @@ def write_error_log(
185187

186188

187189
def post_telemetry_data(url: str,
188-
proxy_url: str,
189190
token: str,
190191
data: dict[str, Any],
191192
service_name: str,
192193
archive_path: Path,
193194
job_file_path: Path,
195+
proxy_url: Optional[str] = None,
194196
request_timeout: int = REQUEST_TIMEOUT,
195197
) -> None:
196198
"""Posts telemetry data
@@ -199,8 +201,6 @@ def post_telemetry_data(url: str,
199201
----------
200202
url: str
201203
Endpoint for the telemetry
202-
proxy_url: str
203-
Proxy URL for the telemetry record request
204204
token: str
205205
Header token for the telemetry request
206206
data: dict[str, Any]
@@ -213,6 +213,8 @@ def post_telemetry_data(url: str,
213213
job_file_path: Path
214214
Path to the job file for the run. This is used for writing error logs
215215
if the telemetry request fails
216+
proxy_url: Optional[str]
217+
Proxy URL for the telemetry record request
216218
request_timeout: int, default REQUEST_TIMEOUT
217219
Timeout while waiting for request
218220
"""
@@ -228,13 +230,21 @@ def post_telemetry_data(url: str,
228230
}
229231

230232
try:
231-
response = requests.post(
232-
url,
233-
data=json.dumps(data),
234-
headers=headers,
235-
timeout=request_timeout,
236-
proxies={"https": proxy_url, "http": proxy_url},
237-
)
233+
if proxy_url is None:
234+
response = requests.post(
235+
url,
236+
data=json.dumps(data),
237+
headers=headers,
238+
timeout=request_timeout,
239+
)
240+
else:
241+
response = requests.post(
242+
url,
243+
data=json.dumps(data),
244+
headers=headers,
245+
timeout=request_timeout,
246+
proxies={"https": proxy_url, "http": proxy_url},
247+
)
238248
if response.status_code >= 400:
239249
error_message = (
240250
"Error posting telemetry request: "
@@ -278,12 +288,12 @@ def record_telemetry(run_info: dict[str, Any],
278288
target=post_telemetry_data,
279289
kwargs={
280290
"url": external_config[CONFIG_FIELDS["URL"]],
281-
"proxy_url": external_config[CONFIG_FIELDS["PROXY_URL"]],
282291
"token": external_config[CONFIG_FIELDS["TOKEN"]],
283292
"data": run_info,
284293
"service_name": external_config[CONFIG_FIELDS["SERVICE_NAME"]],
285294
"archive_path": archive_path,
286295
"job_file_path": job_file_path,
296+
"proxy_url": external_config.get(OPTIONAL_CONFIG_FIELDS["PROXY_URL"]),
287297
},
288298
)
289299
thread.start()

test/test_telemetry.py

Lines changed: 63 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,6 @@ def test_get_external_telemetry_config_no_file(
172172
"hostname",
173173
"telemetry_service_name",
174174
"telemetry_token",
175-
"telemetry_proxy_url"
176175
])
177176
def test_get_external_telemetry_config_missing_fields(
178177
tmp_path, setup_env, config_path, missing_field
@@ -196,6 +195,26 @@ def test_get_external_telemetry_config_missing_fields(
196195
check_invalid_get_external_config(tmp_path, expected_error)
197196

198197

198+
def test_get_external_telemetry_config_missing_proxy_url(
199+
tmp_path, setup_env, config_path
200+
):
201+
config_data = {
202+
"telemetry_url": "some/server/url",
203+
"hostname": "gadi",
204+
"telemetry_service_name": "payu",
205+
"telemetry_token": "some_token",
206+
}
207+
with open(config_path, 'w') as f:
208+
json.dump(config_data, f)
209+
210+
# Assert no error is raised when proxy URL is missing
211+
result = get_external_telemetry_config(
212+
archive_path=tmp_path / "archive",
213+
job_file_path=tmp_path / "job_file.json"
214+
)
215+
assert result == config_data
216+
217+
199218
def test_get_external_telemetry_config_invalid_json(
200219
tmp_path, setup_env, config_path
201220
):
@@ -536,3 +555,46 @@ def start_side_effect():
536555
with open(TELEMETRY_1_0_0_SCHEMA_PATH, "r") as f:
537556
schema = json.load(f)
538557
jsonschema.validate(sent_data, schema)
558+
559+
560+
@pytest.mark.parametrize("proxy_url,expected_proxies", [
561+
(None, None),
562+
("http://proxy.example.com:8080", {
563+
"http": "http://proxy.example.com:8080",
564+
"https": "http://proxy.example.com:8080"
565+
})
566+
])
567+
def test_post_telemetry_data(tmp_path, proxy_url, expected_proxies):
568+
"""Test the post_telemetry_data function with and without proxy URL"""
569+
570+
with patch('requests.post') as mock_post:
571+
mock_response = Mock()
572+
mock_response.status_code = 200
573+
mock_response.json.return_value = {"status": "success"}
574+
mock_post.return_value = mock_response
575+
576+
post_telemetry_data(
577+
url="http://example.com/telemetry",
578+
token="test-token",
579+
data={"key": "value"},
580+
service_name="test-service",
581+
proxy_url=proxy_url,
582+
archive_path=tmp_path / "archive",
583+
job_file_path=tmp_path / "job_file.json"
584+
)
585+
assert mock_post.called
586+
args, kwargs = mock_post.call_args
587+
assert args == ("http://example.com/telemetry",)
588+
assert kwargs.get('headers') == {
589+
'Content-type': 'application/json',
590+
'Authorization': 'Token test-token',
591+
}
592+
assert kwargs.get('timeout') == 10
593+
assert kwargs.get('proxies') == expected_proxies
594+
assert kwargs.get('data') == json.dumps(
595+
{
596+
"service": "test-service",
597+
"version": "1.0.0",
598+
"telemetry": {"key": "value"}
599+
}
600+
)

0 commit comments

Comments
 (0)