From 4648c1a9db579481bbbce17441fe18c41941e215 Mon Sep 17 00:00:00 2001 From: GiaJordan Date: Thu, 23 Jan 2025 10:15:56 -0700 Subject: [PATCH 1/4] add user agent strings to init file --- schematic/__init__.py | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/schematic/__init__.py b/schematic/__init__.py index 18b1216c0..6cc29fb5c 100644 --- a/schematic/__init__.py +++ b/schematic/__init__.py @@ -21,7 +21,7 @@ from opentelemetry.trace import Span, SpanContext, get_current_span from opentelemetry.sdk.trace.export import BatchSpanProcessor, Span from opentelemetry.sdk.trace.sampling import ALWAYS_OFF -from synapseclient import Synapse +from synapseclient import Synapse, USER_AGENT from werkzeug import Request from schematic.configuration.configuration import CONFIG @@ -35,6 +35,16 @@ # Ensure environment variables are loaded load_dotenv() +USER_AGENT_LIBRARY = { + "User-Agent": USER_AGENT["User-Agent"] + f" schematic/{__version__}" +} + +USER_AGENT_COMMAND_LINE = { + "User-Agent": USER_AGENT["User-Agent"] + f" schematiccommandline/{__version__}" +} + +USER_AGENT |= USER_AGENT_LIBRARY + class AttributePropagatingSpanProcessor(SpanProcessor): """A custom span processor that propagates specific attributes from the parent span From b09c97783b3acec0d65957ceff782aa39a716d83 Mon Sep 17 00:00:00 2001 From: GiaJordan Date: Thu, 23 Jan 2025 10:17:09 -0700 Subject: [PATCH 2/4] update user agent string for command line use --- schematic/__main__.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/schematic/__main__.py b/schematic/__main__.py index ab7a3fc76..9d213ca93 100644 --- a/schematic/__main__.py +++ b/schematic/__main__.py @@ -4,6 +4,8 @@ import click import click_log +from synapseclient import USER_AGENT + from schematic.manifest.commands import ( manifest as manifest_cli, ) # get manifest commands @@ -14,7 +16,9 @@ from schematic.visualization.commands import ( viz as viz_cli, ) # viz generation commands -from schematic import __version__ +from schematic import __version__, USER_AGENT_COMMAND_LINE + +USER_AGENT |= USER_AGENT_COMMAND_LINE logger = logging.getLogger() click_log.basic_config(logger) From 60030567c1f195a1e41471ea2ff16df837c9acb3 Mon Sep 17 00:00:00 2001 From: GiaJordan Date: Tue, 28 Jan 2025 09:58:54 -0700 Subject: [PATCH 3/4] move import and setting to main --- schematic/__main__.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/schematic/__main__.py b/schematic/__main__.py index 9d213ca93..973ede7c2 100644 --- a/schematic/__main__.py +++ b/schematic/__main__.py @@ -4,8 +4,6 @@ import click import click_log -from synapseclient import USER_AGENT - from schematic.manifest.commands import ( manifest as manifest_cli, ) # get manifest commands @@ -18,7 +16,6 @@ ) # viz generation commands from schematic import __version__, USER_AGENT_COMMAND_LINE -USER_AGENT |= USER_AGENT_COMMAND_LINE logger = logging.getLogger() click_log.basic_config(logger) @@ -35,6 +32,10 @@ def main(): """ Command line interface to the `schematic` backend services. """ + from synapseclient import USER_AGENT + + USER_AGENT |= USER_AGENT_COMMAND_LINE + logger.info("Starting schematic...") logger.debug("Existing sub-commands need to be used with schematic.") From aa346333f0529e0174c36128d97a3da074a412c7 Mon Sep 17 00:00:00 2001 From: GiaJordan Date: Tue, 28 Jan 2025 10:06:06 -0700 Subject: [PATCH 4/4] add tests for user agent string setting --- tests/unit/test_metric_collection.py | 36 ++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 tests/unit/test_metric_collection.py diff --git a/tests/unit/test_metric_collection.py b/tests/unit/test_metric_collection.py new file mode 100644 index 000000000..e995dba7f --- /dev/null +++ b/tests/unit/test_metric_collection.py @@ -0,0 +1,36 @@ +from re import search +from click.testing import CliRunner +import pytest + + +@pytest.fixture +def command_line_user_agent_pattern(): + yield "schematiccommandline" + "/(\\S+)" + + +@pytest.fixture +def library_user_agent_pattern(): + yield "schematic" + "/(\\S+)" + + +class TestUserAgentString: + def test_user_agent_string( + self, + library_user_agent_pattern, + command_line_user_agent_pattern, + ): + # GIVEN the default USER_AGENT string from the synapse client + from synapseclient import USER_AGENT + + # WHEN schematic is imported to be used as a library + from schematic.__main__ import main + + # THEN the User-Agent string should be updated to include the schematic library client string + assert search(library_user_agent_pattern, USER_AGENT["User-Agent"]) + + # AND when the command line is used to execute commands + runner = CliRunner() + result = runner.invoke(main) + + # THEN the User-Agent string should be updated to include the schematic command line client string + assert search(command_line_user_agent_pattern, USER_AGENT["User-Agent"])