diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 9888770..e87b00f 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -7,6 +7,7 @@ on: - "setup.py" - "*.yml" - "*.md" + - "version.py" jobs: build-and-deploy: diff --git a/skyflow/_utils.py b/skyflow/_utils.py index 1e165f4..39d7e6d 100644 --- a/skyflow/_utils.py +++ b/skyflow/_utils.py @@ -134,10 +134,27 @@ def render_key(parents): def getMetrics(): ''' fetch metrics ''' + sdk_name_version = "skyflow-python@" + SDK_VERSION + + try: + sdk_client_device_model = platform.node() + except Exception: + sdk_client_device_model = "" + + try: + sdk_client_os_details = sys.platform + except Exception: + sdk_client_os_details = "" + + try: + sdk_runtime_details = sys.version + except Exception: + sdk_runtime_details = "" + details_dic = { - 'sdk_name_version': "skyflow-python@" + SDK_VERSION, - 'sdk_client_device_model': platform.node(), - 'sdk_client_os_details': sys.platform, - 'sdk_runtime_details': sys.version, + 'sdk_name_version': sdk_name_version, + 'sdk_client_device_model': sdk_client_device_model, + 'sdk_client_os_details': sdk_client_os_details, + 'sdk_runtime_details': "Python " + sdk_runtime_details, } return details_dic \ No newline at end of file diff --git a/skyflow/service_account/_token.py b/skyflow/service_account/_token.py index e459944..f73191b 100644 --- a/skyflow/service_account/_token.py +++ b/skyflow/service_account/_token.py @@ -7,7 +7,7 @@ import requests from warnings import warn from collections import namedtuple -from skyflow._utils import log_info, InterfaceName, InfoMessages +from skyflow._utils import log_info, InterfaceName, InfoMessages, getMetrics from skyflow.errors._skyflow_errors import * @@ -126,7 +126,8 @@ def getSignedJWT(clientID, keyID, tokenURI, privateKey): def sendRequestWithToken(url, token): headers = { - "content-type": "application/json" + "content-type": "application/json", + "sky-metadata": json.dumps(getMetrics()) } payload = { "grant_type": "urn:ietf:params:oauth:grant-type:jwt-bearer", diff --git a/skyflow/vault/_client.py b/skyflow/vault/_client.py index d8febed..a422b10 100644 --- a/skyflow/vault/_client.py +++ b/skyflow/vault/_client.py @@ -133,6 +133,8 @@ def invoke_connection(self, config: ConnectionConfig): if not 'X-Skyflow-Authorization'.lower() in request.headers: request.headers['x-skyflow-authorization'] = self.storedToken + request.headers['sky-metadata'] = json.dumps(getMetrics()) + response = session.send(request) session.close() return processResponse(response, interface=interface) diff --git a/tests/vault/test_url_encoder.py b/tests/vault/test_url_encoder.py index 80d99db..9afdf51 100644 --- a/tests/vault/test_url_encoder.py +++ b/tests/vault/test_url_encoder.py @@ -4,6 +4,7 @@ import platform import sys import unittest +from unittest import mock from skyflow._utils import http_build_query, getMetrics from version import SDK_VERSION @@ -53,12 +54,62 @@ def test_encoder_array(self): self.assertEqual( http_data, "key=value&nested%5Barray%5D%5B0%5D=one&nested%5Barray%5D%5B1%5D=two&nested%5Bkey%5D=value") + # Test Case 1: Success case def test_get_metrics(self): expected = { 'sdk_name_version': "skyflow-python@" + SDK_VERSION, 'sdk_client_device_model': platform.node(), 'sdk_client_os_details': sys.platform, - 'sdk_runtime_details': sys.version, + 'sdk_runtime_details': "Python " + sys.version, } actual = getMetrics() - self.assertEqual(actual, expected) \ No newline at end of file + self.assertEqual(actual, expected) + + @mock.patch('platform.node', return_value='') + def test_getMetrics_no_device_model(self, mock_node): + expected_output = { + 'sdk_name_version': 'skyflow-python@' + SDK_VERSION, + 'sdk_client_device_model': '', + 'sdk_client_os_details': sys.platform, + 'sdk_runtime_details': "Python " + sys.version + } + + actual_output = getMetrics() + expected_output['sdk_client_device_model'] = '' + self.assertEqual(actual_output, expected_output) + + @mock.patch('platform.node', return_value='Mocked Device Model') + def test_getMetrics_with_device_model(self, mock_node): + expected_output = { + 'sdk_name_version': 'skyflow-python@' + SDK_VERSION, + 'sdk_client_device_model': 'Mocked Device Model', + 'sdk_client_os_details': sys.platform, + 'sdk_runtime_details': "Python " + sys.version + } + + actual_output = getMetrics() + self.assertEqual(actual_output, expected_output) + + @mock.patch('sys.platform', return_value='mocked_os') + def test_getMetrics_with_os_details(self, mock_platform): + expected_output = { + 'sdk_name_version': 'skyflow-python@' + SDK_VERSION, + 'sdk_client_device_model': platform.node(), + 'sdk_client_os_details': sys.platform, + 'sdk_runtime_details': "Python " + sys.version + } + actual_output = getMetrics() + self.assertEqual(actual_output, expected_output) + + def test_getMetrics_with_runtime_details(self): + expected_output = { + 'sdk_name_version': 'skyflow-python@' + SDK_VERSION, + 'sdk_client_device_model': platform.node(), + 'sdk_client_os_details': sys.platform, + 'sdk_runtime_details': 'Python ' + 'mocked_version' + } + + with mock.patch('sys.version', 'mocked_version'), \ + mock.patch('sys.version_info', new=(3, 11, 2)): + actual_output = getMetrics() + self.assertEqual(actual_output, expected_output)