Skip to content

Commit

Permalink
SK-678 Added metrics in invoke connection header
Browse files Browse the repository at this point in the history
  • Loading branch information
skyflow-bharti committed May 22, 2023
1 parent 6add72a commit a501618
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 8 deletions.
1 change: 1 addition & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ on:
- "setup.py"
- "*.yml"
- "*.md"
- "version.py"

jobs:
build-and-deploy:
Expand Down
25 changes: 21 additions & 4 deletions skyflow/_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
5 changes: 3 additions & 2 deletions skyflow/service_account/_token.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 *
Expand Down Expand Up @@ -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",
Expand Down
2 changes: 2 additions & 0 deletions skyflow/vault/_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
55 changes: 53 additions & 2 deletions tests/vault/test_url_encoder.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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)
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)

0 comments on commit a501618

Please sign in to comment.