Skip to content

Commit c50c599

Browse files
authored
[Schematic-90] Use synapseclient user-agent string to track schematic library and CLI usage (#1569)
set synapseclient user strings to track schematic cli and library usage
1 parent 6a9601b commit c50c599

File tree

3 files changed

+53
-2
lines changed

3 files changed

+53
-2
lines changed

schematic/__init__.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
from opentelemetry.trace import Span, SpanContext, get_current_span
2222
from opentelemetry.sdk.trace.export import BatchSpanProcessor, Span
2323
from opentelemetry.sdk.trace.sampling import ALWAYS_OFF
24-
from synapseclient import Synapse
24+
from synapseclient import Synapse, USER_AGENT
2525
from werkzeug import Request
2626

2727
from schematic.configuration.configuration import CONFIG
@@ -35,6 +35,16 @@
3535
# Ensure environment variables are loaded
3636
load_dotenv()
3737

38+
USER_AGENT_LIBRARY = {
39+
"User-Agent": USER_AGENT["User-Agent"] + f" schematic/{__version__}"
40+
}
41+
42+
USER_AGENT_COMMAND_LINE = {
43+
"User-Agent": USER_AGENT["User-Agent"] + f" schematiccommandline/{__version__}"
44+
}
45+
46+
USER_AGENT |= USER_AGENT_LIBRARY
47+
3848

3949
class AttributePropagatingSpanProcessor(SpanProcessor):
4050
"""A custom span processor that propagates specific attributes from the parent span

schematic/__main__.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@
1414
from schematic.visualization.commands import (
1515
viz as viz_cli,
1616
) # viz generation commands
17-
from schematic import __version__
17+
from schematic import __version__, USER_AGENT_COMMAND_LINE
18+
1819

1920
logger = logging.getLogger()
2021
click_log.basic_config(logger)
@@ -31,6 +32,10 @@ def main():
3132
"""
3233
Command line interface to the `schematic` backend services.
3334
"""
35+
from synapseclient import USER_AGENT
36+
37+
USER_AGENT |= USER_AGENT_COMMAND_LINE
38+
3439
logger.info("Starting schematic...")
3540
logger.debug("Existing sub-commands need to be used with schematic.")
3641

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
from re import search
2+
from click.testing import CliRunner
3+
import pytest
4+
5+
6+
@pytest.fixture
7+
def command_line_user_agent_pattern():
8+
yield "schematiccommandline" + "/(\\S+)"
9+
10+
11+
@pytest.fixture
12+
def library_user_agent_pattern():
13+
yield "schematic" + "/(\\S+)"
14+
15+
16+
class TestUserAgentString:
17+
def test_user_agent_string(
18+
self,
19+
library_user_agent_pattern,
20+
command_line_user_agent_pattern,
21+
):
22+
# GIVEN the default USER_AGENT string from the synapse client
23+
from synapseclient import USER_AGENT
24+
25+
# WHEN schematic is imported to be used as a library
26+
from schematic.__main__ import main
27+
28+
# THEN the User-Agent string should be updated to include the schematic library client string
29+
assert search(library_user_agent_pattern, USER_AGENT["User-Agent"])
30+
31+
# AND when the command line is used to execute commands
32+
runner = CliRunner()
33+
result = runner.invoke(main)
34+
35+
# THEN the User-Agent string should be updated to include the schematic command line client string
36+
assert search(command_line_user_agent_pattern, USER_AGENT["User-Agent"])

0 commit comments

Comments
 (0)