Skip to content

Commit

Permalink
add event_client_host optional parameter to support eu users (#4)
Browse files Browse the repository at this point in the history
* add event_client_host optional parameter to support eu users

* update README.md
  • Loading branch information
smatzkel-newrelic committed Mar 8, 2023
1 parent e5422a9 commit c297001
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 12 deletions.
28 changes: 26 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`**
Expand All @@ -30,6 +30,10 @@ export NEW_RELIC_LICENSE_KEY=<license key>
import os
os.environ["NEW_RELIC_LICENSE_KEY"] = "<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

Expand Down Expand Up @@ -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:
Expand Down
29 changes: 19 additions & 10 deletions src/nr_openai_observability/monitor.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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)
Expand All @@ -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
Expand Down Expand Up @@ -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()


Expand Down

0 comments on commit c297001

Please sign in to comment.