Skip to content

Commit

Permalink
[Schematic-90] Use synapseclient user-agent string to track schematic…
Browse files Browse the repository at this point in the history
… library and CLI usage (#1569)

set synapseclient user strings to track schematic cli and library usage
  • Loading branch information
SageGJ authored Jan 28, 2025
1 parent 6a9601b commit c50c599
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 2 deletions.
12 changes: 11 additions & 1 deletion schematic/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
7 changes: 6 additions & 1 deletion schematic/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@
from schematic.visualization.commands import (
viz as viz_cli,
) # viz generation commands
from schematic import __version__
from schematic import __version__, USER_AGENT_COMMAND_LINE


logger = logging.getLogger()
click_log.basic_config(logger)
Expand All @@ -31,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.")

Expand Down
36 changes: 36 additions & 0 deletions tests/unit/test_metric_collection.py
Original file line number Diff line number Diff line change
@@ -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"])

0 comments on commit c50c599

Please sign in to comment.