From c297001997b066122623480fe64e58d07041633b Mon Sep 17 00:00:00 2001 From: smatzkel-newrelic <84398037+smatzkel-newrelic@users.noreply.github.com> Date: Wed, 8 Mar 2023 12:08:00 +0200 Subject: [PATCH] add event_client_host optional parameter to support eu users (#4) * add event_client_host optional parameter to support eu users * update README.md --- README.md | 28 +++++++++++++++++++++++-- src/nr_openai_observability/monitor.py | 29 +++++++++++++++++--------- 2 files changed, 45 insertions(+), 12 deletions(-) diff --git a/README.md b/README.md index 1fdb72a..c9d6b05 100644 --- a/README.md +++ b/README.md @@ -14,8 +14,8 @@ pip install nr-openai-observability ## Getting Started -#### STEP 1: Set Your Environment Variable -[Get your License key](https://one.newrelic.com/launcher/api-keys-ui.api-keys-launcher) (also referenced as `ingest - license`) and set it as environment variable: `NEW_RELIC_LICENSE_KEY`. +#### STEP 1: Set Your Environment Variables +* [Get your License key](https://one.newrelic.com/launcher/api-keys-ui.api-keys-launcher) (also referenced as `ingest - license`) and set it as environment variable: `NEW_RELIC_LICENSE_KEY`. [Click here](https://docs.newrelic.com/docs/apis/intro-apis/new-relic-api-keys/#license-key) for more details and instructions. **`Bash`** @@ -30,6 +30,10 @@ export NEW_RELIC_LICENSE_KEY= import os os.environ["NEW_RELIC_LICENSE_KEY"] = "" ``` +`NEW_RELIC_LICENSE_KEY` can also be sent as a parameter at the `monitor.initialization()` + call. + +* Are you reporting data to the New Relic EU region? click [here](#eu-account-users) for more instructions. #### STEP 2: Add the following two lines to your code @@ -60,6 +64,26 @@ openai.Completion.create( ) ``` +### EU Account Users: + +If you are using an EU region account, you should also set your `EVENT_CLIENT_HOST`: + +**`Bash`** + +```bash +export EVENT_CLIENT_HOST="insights-collector.eu01.nr-data.net" +``` + +**`Python`** + +```python +import os +os.environ["EVENT_CLIENT_HOST"] = "insights-collector.eu01.nr-data.net" +``` + +`EVENT_CLIENT_HOST` can also be sent as a parameter at the `monitor.initialization()` + call. + ## Support New Relic hosts and moderates an online forum where customers can interact with New Relic employees as well as other customers to get help and share best practices. Like all official New Relic open source projects, there's a related Community topic in the New Relic Explorers Hub. You can find this project's topic/threads here: diff --git a/src/nr_openai_observability/monitor.py b/src/nr_openai_observability/monitor.py index 1c025b1..b4ba7fc 100644 --- a/src/nr_openai_observability/monitor.py +++ b/src/nr_openai_observability/monitor.py @@ -26,16 +26,8 @@ class OpenAIMonitoring: # this class uses the telemetry SDK to record metrics to new relic, please see https://github.com/newrelic/newrelic-telemetry-sdk-python def __init__( self, - event_client_host: Optional[str] = None, use_logger: Optional[bool] = None, ): - if not isinstance(event_client_host, str) and event_client_host is not None: - raise TypeError("event_client_host instance type must be str or None") - - self.event_client_host = event_client_host or os.getenv( - "EVENT_CLIENT_HOST", EventClient.HOST - ) - self.use_logger = use_logger if use_logger else False def _set_license_key( @@ -53,6 +45,18 @@ def _set_license_key( ) or self.license_key is None: raise TypeError("license_key instance type must be str and not None") + def _set_client_host( + self, + event_client_host: Optional[str] = None, + ): + + if not isinstance(event_client_host, str) and event_client_host is not None: + raise TypeError("event_client_host instance type must be str or None") + + self.event_client_host = event_client_host or os.getenv( + "EVENT_CLIENT_HOST", EventClient.HOST + ) + def _log(self, msg: str): if self.use_logger: logger.info(msg) @@ -62,8 +66,10 @@ def _log(self, msg: str): def start( self, license_key: Optional[str] = None, + event_client_host: Optional[str] = None, ): self._set_license_key(license_key) + self._set_client_host(event_client_host) self._start() # initialize event thread @@ -142,8 +148,11 @@ def flatten_dict(dd, separator=".", prefix="", index=""): monitor = OpenAIMonitoring() -def initialization(license_key: Optional[str] = None): - monitor.start(license_key) +def initialization( + license_key: Optional[str] = None, + event_client_host: Optional[str] = None, +): + monitor.start(license_key, event_client_host) perform_patch()