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

Add methods to provide extra user agent and upstream user agent to sdk config #163

Merged
merged 3 commits into from
Jun 14, 2023
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
26 changes: 24 additions & 2 deletions databricks/sdk/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -499,6 +499,7 @@ def __init__(self,
product_version="0.0.0",
**kwargs):
self._inner = {}
self._user_agent_other_info = []
self._credentials_provider = credentials_provider if credentials_provider else DefaultCredentials()
try:
self._set_inner_config(kwargs)
Expand Down Expand Up @@ -602,8 +603,29 @@ def user_agent(self):
""" Returns User-Agent header used by this SDK """
py_version = platform.python_version()
os_name = platform.uname().system.lower()
return (f"{self._product}/{self._product_version} databricks-sdk-py/{__version__}"
f" python/{py_version} os/{os_name} auth/{self.auth_type}")

ua = [
f"{self._product}/{self._product_version}", f"databricks-sdk-py/{__version__}",
f"python/{py_version}", f"os/{os_name}", f"auth/{self.auth_type}",
]
if len(self._user_agent_other_info) > 0:
ua.append(' '.join(self._user_agent_other_info))
if len(self._upstream_user_agent) > 0:
ua.append(self._upstream_user_agent)

return ' '.join(ua)

@property
def _upstream_user_agent(self) -> str:
product = os.environ.get('DATABRICKS_SDK_UPSTREAM', None)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you also add the relevant Go SDK PR?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep. This was a bigger priority because I would like to get dbconnect to start using this asap.
Working on go sdk today.

product_version = os.environ.get('DATABRICKS_SDK_UPSTREAM_VERSION', None)
if product is not None and product_version is not None:
return f"upstream/{product} upstream-version/{product_version}"
return ""

def with_user_agent_extra(self, key: str, value: str) -> 'Config':
self._user_agent_other_info.append(f"{key}/{value}")
return self

@property
def oidc_endpoints(self) -> Optional[OidcEndpoints]:
Expand Down
24 changes: 24 additions & 0 deletions tests/test_core.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import os
import pathlib
import platform
import random
import string

import pytest

from databricks.sdk.core import (Config, DatabricksCliTokenSource,
databricks_cli)
from databricks.sdk.version import __version__


def test_parse_dsn():
Expand Down Expand Up @@ -123,3 +125,25 @@ def test_databricks_cli_credential_provider_installed_new(config, monkeypatch, t
write_large_dummy_executable(tmp_path)
monkeypatch.setenv('PATH', str(os.pathsep).join([tmp_path.as_posix(), os.environ['PATH']]))
assert databricks_cli(config) is not None


def test_extra_and_upstream_user_agent(monkeypatch):

class MockUname:

@property
def system(self):
return 'TestOS'

monkeypatch.setattr(platform, 'python_version', lambda: '3.0.0')
monkeypatch.setattr(platform, 'uname', MockUname)
monkeypatch.setenv('DATABRICKS_SDK_UPSTREAM', "upstream-product")
monkeypatch.setenv('DATABRICKS_SDK_UPSTREAM_VERSION', "0.0.1")

config = Config(host='http://localhost', username="something", password="something", product='test', product_version='0.0.0')\
.with_user_agent_extra('test-extra-1', '1')\
.with_user_agent_extra('test-extra-2', '2')

assert config.user_agent == (
f"test/0.0.0 databricks-sdk-py/{__version__} python/3.0.0 os/testos auth/basic"
f" test-extra-1/1 test-extra-2/2 upstream/upstream-product upstream-version/0.0.1")