Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Schematic-90] Use synapseclient user-agent string to track schematic library and CLI usage #1569

Merged
merged 4 commits into from
Jan 28, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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"])
Loading