Skip to content

Commit

Permalink
Use pants as the user-agent for report server (pantsbuild#8077)
Browse files Browse the repository at this point in the history
### Problem

When calling the report server, pants leveraging the [python requests](https://2.python-requests.org/) is using the default user agent (for example: `"python-requests/2.22.0"`)
This is not optimal, the user-agent should more actuarially reflect the software making the HTTP call 

### Solution

Use pants as the user-agent, adding the pants version as part of the user-agent header.

### Result

` "Pants/v1.18.0rc0"`
  • Loading branch information
asherf authored and Eric-Arellano committed Jul 20, 2019
1 parent 566c6b6 commit 929474c
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/python/pants/goal/run_tracker.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import uuid
from collections import OrderedDict
from contextlib import contextmanager
from typing import Dict

import requests

Expand All @@ -29,6 +30,7 @@
from pants.reporting.report import Report
from pants.subsystem.subsystem import Subsystem
from pants.util.dirutil import relative_symlink, safe_file_dump
from pants.version import VERSION


class RunTrackerOptionEncoder(CoercingOptionEncoder):
Expand Down Expand Up @@ -350,6 +352,10 @@ def log(self, level, *msg_elements):
"""Log a message against the current workunit."""
self.report.log(self._threadlocal.current_workunit, level, *msg_elements)

@classmethod
def _get_headers(cls) -> Dict[str, str]:
return {'User-Agent': f"pants/v{VERSION}"}

@classmethod
def post_stats(cls, stats_url, stats, timeout=2, auth_provider=None):
"""POST stats to the given url.
Expand All @@ -366,6 +372,7 @@ def error(msg):
# But this will first require changing the upload receiver at every shop that uses this.
params = {k: cls._json_dump_options(v) for (k, v) in stats.items()}
cookies = Cookies.global_instance()
headers = cls._get_headers()
auth_provider = auth_provider or '<provider>'

# We can't simply let requests handle redirects, as we only allow them for specific codes:
Expand All @@ -378,6 +385,7 @@ def do_post(url, num_redirects_allowed):
if num_redirects_allowed < 0:
return error('too many redirects.')
res = requests.post(url, data=params, timeout=timeout,
headers=headers,
cookies=cookies.get_cookie_jar(), allow_redirects=False)
if res.status_code in {307, 308}:
return do_post(res.headers['location'], num_redirects_allowed - 1)
Expand Down
2 changes: 2 additions & 0 deletions tests/python/pants_test/goal/test_run_tracker.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from pants.auth.cookies import Cookies
from pants.goal.run_tracker import RunTracker
from pants.util.contextutil import temporary_file_path
from pants.version import VERSION
from pants_test.test_base import TestBase


Expand All @@ -31,6 +32,7 @@ def do_POST(handler):
post_data = parse_qs(handler.rfile.read(length).decode())
decoded_post_data = {k: json.loads(v[0]) for k, v in post_data.items()}
self.assertEqual(stats, decoded_post_data)
self.assertEqual(handler.headers['User-Agent'], f"pants/v{VERSION}")
handler.send_response(200)
handler.end_headers()
except Exception:
Expand Down

0 comments on commit 929474c

Please sign in to comment.