From 6add72a5d3346096e8828ec7c217f8dbf284c3bd Mon Sep 17 00:00:00 2001 From: skyflow-bharti Date: Wed, 17 May 2023 17:59:59 +0530 Subject: [PATCH] SK-678 add metrices in python-sdk --- .github/workflows/release.yml | 1 + ci-scripts/bump_version.sh | 3 +++ skyflow/_utils.py | 14 ++++++++++++++ skyflow/vault/_client.py | 6 ++++-- skyflow/vault/_detokenize.py | 6 ++++-- skyflow/vault/_get.py | 6 ++++-- skyflow/vault/_get_by_id.py | 5 +++-- skyflow/vault/_update.py | 5 +++-- tests/vault/test_url_encoder.py | 16 ++++++++++++++-- version.py | 1 + 10 files changed, 51 insertions(+), 12 deletions(-) create mode 100644 version.py diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index d1c47f8..9888770 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -39,6 +39,7 @@ jobs: git config user.name ${{ github.actor }} git config user.email ${{ github.actor }}@users.noreply.github.com git add setup.py + git add version.py git commit -m "[AUTOMATED] Public Release - ${{ steps.previoustag.outputs.tag }}" git push origin diff --git a/ci-scripts/bump_version.sh b/ci-scripts/bump_version.sh index 59f00df..5f02750 100755 --- a/ci-scripts/bump_version.sh +++ b/ci-scripts/bump_version.sh @@ -6,6 +6,8 @@ then echo "Bumping package version to $1" sed -E "s/current_version = .+/current_version = \'$SEMVER\'/g" setup.py > tempfile && cat tempfile > setup.py && rm -f tempfile + sed -E "s/SDK_VERSION = .+/SDK_VERSION = \'$SEMVER\'/g" version.py > tempfile && cat tempfile > version.py && rm -f tempfile + echo -------------------------- echo "Done, Package now at $1" @@ -13,6 +15,7 @@ else echo "Bumping package version to $1-dev.$2" sed -E "s/current_version = .+/current_version = \'$SEMVER-dev.$2\'/g" setup.py > tempfile && cat tempfile > setup.py && rm -f tempfile + sed -E "s/SDK_VERSION = .+/SDK_VERSION = \'$SEMVER-dev.$2\'/g" version.py > tempfile && cat tempfile > version.py && rm -f tempfile echo -------------------------- echo "Done, Package now at $1-dev.$2" diff --git a/skyflow/_utils.py b/skyflow/_utils.py index 5186dbe..1e165f4 100644 --- a/skyflow/_utils.py +++ b/skyflow/_utils.py @@ -4,6 +4,9 @@ import urllib.parse import logging from enum import Enum +import platform +import sys +from version import SDK_VERSION skyflowLog = logging.getLogger('skyflow') skyflowLog.setLevel(logging.ERROR) @@ -127,3 +130,14 @@ def render_key(parents): outStr += s % str(x) depth += 1 return outStr + +def getMetrics(): + ''' fetch metrics + ''' + 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, + } + return details_dic \ No newline at end of file diff --git a/skyflow/vault/_client.py b/skyflow/vault/_client.py index ff7aaa4..d8febed 100644 --- a/skyflow/vault/_client.py +++ b/skyflow/vault/_client.py @@ -1,6 +1,7 @@ ''' Copyright (c) 2022 Skyflow, Inc. ''' +import json import types import requests from ._insert import getInsertRequestBody, processResponse, convertResponse @@ -13,7 +14,7 @@ from ._get import sendGetRequests import asyncio from skyflow.errors._skyflow_errors import SkyflowError, SkyflowErrorCodes, SkyflowErrorMessages -from skyflow._utils import log_info, InfoMessages, InterfaceName +from skyflow._utils import log_info, InfoMessages, InterfaceName, getMetrics from ._token import tokenProviderWrapper @@ -52,7 +53,8 @@ def insert(self, records: dict, options: InsertOptions = InsertOptions()): self.storedToken = tokenProviderWrapper( self.storedToken, self.tokenProvider, interface) headers = { - "Authorization": "Bearer " + self.storedToken + "Authorization": "Bearer " + self.storedToken, + "sky-metadata": json.dumps(getMetrics()) } response = requests.post(requestURL, data=jsonBody, headers=headers) diff --git a/skyflow/vault/_detokenize.py b/skyflow/vault/_detokenize.py index de5d09f..8370de0 100644 --- a/skyflow/vault/_detokenize.py +++ b/skyflow/vault/_detokenize.py @@ -5,7 +5,7 @@ import asyncio from aiohttp import ClientSession, request import json -from skyflow._utils import InterfaceName +from skyflow._utils import InterfaceName, getMetrics interface = InterfaceName.DETOKENIZE.value @@ -48,7 +48,9 @@ async def sendDetokenizeRequests(data, url, token): async with ClientSession() as session: for record in validatedRecords: headers = { - "Authorization": "Bearer " + token + "Authorization": "Bearer " + token, + "sky-metadata": json.dumps(getMetrics()) + } task = asyncio.ensure_future(post(url, record, headers, session)) tasks.append(task) diff --git a/skyflow/vault/_get.py b/skyflow/vault/_get.py index 0670206..8099366 100644 --- a/skyflow/vault/_get.py +++ b/skyflow/vault/_get.py @@ -1,11 +1,12 @@ ''' Copyright (c) 2022 Skyflow, Inc. ''' +import json from skyflow.errors._skyflow_errors import SkyflowError, SkyflowErrorCodes, SkyflowErrorMessages import asyncio from aiohttp import ClientSession from ._config import RedactionType -from skyflow._utils import InterfaceName +from skyflow._utils import InterfaceName, getMetrics from ._get_by_id import get interface = InterfaceName.GET.value @@ -83,7 +84,8 @@ async def sendGetRequests(data, url, token): async with ClientSession() as session: for record in validatedRecords: headers = { - "Authorization": "Bearer " + token + "Authorization": "Bearer " + token, + "sky-metadata": json.dumps(getMetrics()) } params = {"redaction": redaction} if ids is not None: diff --git a/skyflow/vault/_get_by_id.py b/skyflow/vault/_get_by_id.py index 7cd3adc..1ac7a6f 100644 --- a/skyflow/vault/_get_by_id.py +++ b/skyflow/vault/_get_by_id.py @@ -6,7 +6,7 @@ from aiohttp import ClientSession import json from ._config import RedactionType -from skyflow._utils import InterfaceName +from skyflow._utils import InterfaceName, getMetrics interface = InterfaceName.GET_BY_ID.value @@ -66,7 +66,8 @@ async def sendGetByIdRequests(data, url, token): async with ClientSession() as session: for record in validatedRecords: headers = { - "Authorization": "Bearer " + token + "Authorization": "Bearer " + token, + "sky-metadata": json.dumps(getMetrics()) } params = {"skyflow_ids": record[0], "redaction": record[2]} task = asyncio.ensure_future( diff --git a/skyflow/vault/_update.py b/skyflow/vault/_update.py index ade08b2..c27a091 100644 --- a/skyflow/vault/_update.py +++ b/skyflow/vault/_update.py @@ -6,7 +6,7 @@ import asyncio from skyflow.errors._skyflow_errors import SkyflowError, SkyflowErrorCodes, SkyflowErrorMessages from ._insert import getTableAndFields -from skyflow._utils import InterfaceName +from skyflow._utils import InterfaceName, getMetrics from aiohttp import ClientSession from ._config import UpdateOptions @@ -40,7 +40,8 @@ async def sendUpdateRequests(data,options: UpdateOptions,url,token): } reqBody = json.dumps(reqBody) headers = { - "Authorization": "Bearer " + token + "Authorization": "Bearer " + token, + "sky-metadata": json.dumps(getMetrics()) } task = asyncio.ensure_future(put(recordUrl, reqBody, headers, session)) tasks.append(task) diff --git a/tests/vault/test_url_encoder.py b/tests/vault/test_url_encoder.py index c36a468..80d99db 100644 --- a/tests/vault/test_url_encoder.py +++ b/tests/vault/test_url_encoder.py @@ -1,9 +1,11 @@ ''' Copyright (c) 2022 Skyflow, Inc. ''' +import platform +import sys import unittest -from skyflow._utils import http_build_query - +from skyflow._utils import http_build_query, getMetrics +from version import SDK_VERSION class TestUrlEncoder(unittest.TestCase): def setUp(self) -> None: @@ -50,3 +52,13 @@ 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") + + 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, + } + actual = getMetrics() + self.assertEqual(actual, expected) \ No newline at end of file diff --git a/version.py b/version.py new file mode 100644 index 0000000..ccff82c --- /dev/null +++ b/version.py @@ -0,0 +1 @@ +SDK_VERSION = '1.8.1' \ No newline at end of file