From db1b376cb2f03b7e3664cfe98e99404e5dd3f1ec Mon Sep 17 00:00:00 2001 From: Caleb Courier Date: Thu, 29 Aug 2024 15:02:29 -0500 Subject: [PATCH 1/4] trace on server start --- guardrails_api/app.py | 3 +++ .../utils/has_internet_connection.py | 9 +++++++ .../utils/trace_server_start_if_enabled.py | 24 +++++++++++++++++++ pyproject.toml | 2 +- 4 files changed, 37 insertions(+), 1 deletion(-) create mode 100644 guardrails_api/utils/has_internet_connection.py create mode 100644 guardrails_api/utils/trace_server_start_if_enabled.py diff --git a/guardrails_api/app.py b/guardrails_api/app.py index 92e72d8..a33fbbc 100644 --- a/guardrails_api/app.py +++ b/guardrails_api/app.py @@ -9,10 +9,12 @@ from opentelemetry.instrumentation.flask import FlaskInstrumentor from guardrails_api.clients.postgres_client import postgres_is_enabled from guardrails_api.otel import otel_is_disabled, initialize +from guardrails_api.utils.trace_server_start_if_enabled import trace_server_start_if_enabled from guardrails_api.clients.cache_client import CacheClient from rich.console import Console from rich.rule import Rule + # TODO: Move this to a separate file class OverrideJsonProvider(DefaultJSONProvider): def default(self, o): @@ -50,6 +52,7 @@ def register_config(config: Optional[str] = None): def create_app( env: Optional[str] = None, config: Optional[str] = None, port: Optional[int] = None ): + trace_server_start_if_enabled() # used to print user-facing messages during server startup console = Console() diff --git a/guardrails_api/utils/has_internet_connection.py b/guardrails_api/utils/has_internet_connection.py new file mode 100644 index 0000000..1ebd5d8 --- /dev/null +++ b/guardrails_api/utils/has_internet_connection.py @@ -0,0 +1,9 @@ +import requests + + +def has_internet_connection() -> bool: + try: + requests.get("https://www.guardrailsai.com/") + return True + except requests.ConnectionError: + return False \ No newline at end of file diff --git a/guardrails_api/utils/trace_server_start_if_enabled.py b/guardrails_api/utils/trace_server_start_if_enabled.py new file mode 100644 index 0000000..467abd6 --- /dev/null +++ b/guardrails_api/utils/trace_server_start_if_enabled.py @@ -0,0 +1,24 @@ +import platform +from guardrails.classes.credentials import Credentials +from guardrails.version import GUARDRAILS_VERSION +from guardrails_api.utils.has_internet_connection import has_internet_connection + + +def trace_server_start_if_enabled(): + config = Credentials.from_rc_file() + if config.enable_metrics is True and has_internet_connection(): + from guardrails.utils.hub_telemetry_utils import HubTelemetry + HubTelemetry().create_new_span( + "guardrails-api/start", + [ + ("guardrails-version", GUARDRAILS_VERSION), + ("python-version", platform.python_version()), + ("system", platform.system()), + ("platform", platform.platform()), + ("arch", platform.architecture()[0]), + ("machine", platform.machine()), + ("processor", platform.processor()), + ], + True, + False, + ) \ No newline at end of file diff --git a/pyproject.toml b/pyproject.toml index b8078d2..a8a8937 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -10,7 +10,7 @@ readme = "README.md" keywords = ["Guardrails", "Guardrails AI", "Guardrails API", "Guardrails API"] requires-python = ">= 3.8.1" dependencies = [ - "guardrails-ai>=0.5.0a11", + "guardrails-ai>=0.5.6", "flask>=3.0.3,<4", "Flask-SQLAlchemy>=3.1.1,<4", "Flask-Caching>=2.3.0,<3", From df4c3fe44716ffdc5e65a10b62c7e2fba9b09d96 Mon Sep 17 00:00:00 2001 From: Caleb Courier Date: Thu, 29 Aug 2024 15:08:21 -0500 Subject: [PATCH 2/4] add requests to pyproject toml since it's now a direct dependency --- pyproject.toml | 1 + 1 file changed, 1 insertion(+) diff --git a/pyproject.toml b/pyproject.toml index a8a8937..8c5ae0c 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -27,6 +27,7 @@ dependencies = [ "opentelemetry-exporter-otlp-proto-grpc>=1.0.0,<2", "opentelemetry-exporter-otlp-proto-http>=1.0.0,<2", "opentelemetry-instrumentation-flask>=0.12b0,<1", + "requests>=2.32.3" ] [tool.setuptools.dynamic] From 9b900664c81a10323652c53934a0ab2a44607cf5 Mon Sep 17 00:00:00 2001 From: Caleb Courier Date: Thu, 29 Aug 2024 15:09:04 -0500 Subject: [PATCH 3/4] bump version in prep for release --- guardrails_api/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/guardrails_api/__init__.py b/guardrails_api/__init__.py index f102a9c..3b93d0b 100644 --- a/guardrails_api/__init__.py +++ b/guardrails_api/__init__.py @@ -1 +1 @@ -__version__ = "0.0.1" +__version__ = "0.0.2" From d31bdcc0fb82bc4e1e86a8e956f2d8fdc414eb6b Mon Sep 17 00:00:00 2001 From: Caleb Courier <13314870+CalebCourier@users.noreply.github.com> Date: Thu, 29 Aug 2024 15:23:57 -0500 Subject: [PATCH 4/4] raise on internet check Co-authored-by: Alejandro Esquivel --- guardrails_api/utils/has_internet_connection.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/guardrails_api/utils/has_internet_connection.py b/guardrails_api/utils/has_internet_connection.py index 1ebd5d8..8a7099c 100644 --- a/guardrails_api/utils/has_internet_connection.py +++ b/guardrails_api/utils/has_internet_connection.py @@ -3,7 +3,8 @@ def has_internet_connection() -> bool: try: - requests.get("https://www.guardrailsai.com/") + res = requests.get("https://www.guardrailsai.com/") + res.raise_for_status() return True except requests.ConnectionError: return False \ No newline at end of file